Advertisement
Guest User

Untitled

a guest
Jan 27th, 2012
279
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.37 KB | None | 0 0
  1. package pt.com.reverse.nothing;
  2.  
  3. import java.util.List;
  4.  
  5. import org.opencv.android.Utils;
  6. import org.opencv.core.Core;
  7. import org.opencv.core.Mat;
  8. import org.opencv.core.Size;
  9. import org.opencv.highgui.Highgui;
  10. import org.opencv.highgui.VideoCapture;
  11.  
  12. import android.content.Context;
  13. import android.graphics.Bitmap;
  14. import android.graphics.Canvas;
  15. import android.os.Debug;
  16. import android.util.Log;
  17. import android.view.SurfaceHolder;
  18. import android.view.SurfaceView;
  19.  
  20. public class SampleCvViewBase extends SurfaceView implements SurfaceHolder.Callback, Runnable {
  21.     private static final String TAG = "Sample::SurfaceView";
  22.  
  23.     private SurfaceHolder       mHolder;
  24.     private VideoCapture        mCamera;
  25.     private Mat mRgba;
  26.  
  27.     public SampleCvViewBase(Context context) {
  28.         super(context);
  29.         mHolder = getHolder();
  30.         mHolder.addCallback(this);
  31.     }
  32.  
  33.     public void surfaceChanged(SurfaceHolder _holder, int format, int width, int height) {
  34.         Log.i(TAG, "surfaceCreated");
  35.  
  36.         if (mCamera != null && mCamera.isOpened()) {
  37.             List<Size> sizes = mCamera.getSupportedPreviewSizes();
  38.             int mFrameWidth = width;
  39.             int mFrameHeight = height;
  40.  
  41.             // selecting optimal camera preview size
  42.             {
  43.                 double minDiff = Double.MAX_VALUE;
  44.                 for (Size size : sizes) {
  45.                     if (Math.abs(size.height - height) < minDiff) {
  46.                         mFrameWidth = (int) size.width;
  47.                         mFrameHeight = (int) size.height;
  48.                         minDiff = Math.abs(size.height - height);
  49.                     }
  50.                 }
  51.             }
  52.  
  53.             mFrameWidth = 640;
  54.             mFrameHeight = 480;
  55.             mCamera.set(Highgui.CV_CAP_PROP_FRAME_WIDTH, mFrameWidth);
  56.             mCamera.set(Highgui.CV_CAP_PROP_FRAME_HEIGHT, mFrameHeight);
  57.         }
  58.     }
  59.  
  60.     public void surfaceCreated(SurfaceHolder holder) {
  61.         Log.i(TAG, "surfaceCreated");
  62.  
  63.         mRgba  = new Mat();
  64.         mCamera = new VideoCapture(Highgui.CV_CAP_ANDROID);
  65.  
  66.         if(mCamera.isOpened()){
  67.             //Debug.startMethodTracing("nothing3");
  68.             (new Thread(this)).start();
  69.         } else {
  70.  
  71.  
  72.             mCamera.release();
  73.             mCamera = null;
  74.             Log.e(TAG, "Failed to open native camera");
  75.         }
  76.  
  77.  
  78.     }
  79.  
  80.     public void surfaceDestroyed(SurfaceHolder holder) {
  81.         Log.i(TAG, "surfaceDestroyed");
  82.         //  Debug.stopMethodTracing();
  83.         if (mCamera != null) {
  84.             mCamera.release();
  85.             mCamera = null;
  86.  
  87.         }
  88.     }
  89.  
  90.     //protected abstract Bitmap processFrame(VideoCapture capture);
  91.  
  92.     public double timet(String s, double l){
  93.         double d = ((Core.getTickCount() - l)/Core.getTickFrequency()) * 1000;
  94.         return d;
  95.     }
  96.  
  97.     public void run() {
  98.         int i = 0;
  99.         double total = 0;
  100.         while (true) {
  101.             double d = Core.getTickCount();
  102.             Bitmap bmp = null;
  103.  
  104.             if (mCamera == null)
  105.                 break;
  106.             if (!mCamera.grab()) {
  107.                 Log.e(TAG, "mCamera.grab() failed");
  108.                 break;
  109.             }
  110.  
  111.             mCamera.retrieve(mRgba, Highgui.CV_CAP_ANDROID_COLOR_FRAME_RGBA);
  112.             bmp = Bitmap.createBitmap(mRgba.cols(), mRgba.rows(), Bitmap.Config.ARGB_8888);
  113.             Utils.matToBitmap(mRgba, bmp);
  114.  
  115.             if (bmp != null) {
  116.                 Canvas canvas = mHolder.lockCanvas();
  117.                 if (canvas != null) {
  118.                     canvas.drawBitmap(bmp, (canvas.getWidth() - bmp.getWidth()) / 2, (canvas.getHeight() - bmp.getHeight()) / 2, null);
  119.                     mHolder.unlockCanvasAndPost(canvas);
  120.                 }
  121.                 bmp.recycle();             
  122.                 total += timet("Total frame time:", d);;
  123.                 i++;
  124.                 if(i % 100 == 0){
  125.                     Log.w("TIME", "Average frame time: " + total/100 + "ms" + " ##Frame Rate:" + (100000/(total)) + "fps");
  126.                     total = 0;
  127.                 }
  128.  
  129.             }
  130.  
  131.         }
  132.     }
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement