Advertisement
Guest User

Opencv Camera

a guest
Jan 30th, 2012
502
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 5 2.61 KB | None | 0 0
  1.  
  2. import org.opencv.android.Utils;
  3. import org.opencv.core.Core;
  4. import org.opencv.core.Mat;
  5. import org.opencv.highgui.Highgui;
  6. import org.opencv.highgui.VideoCapture;
  7.  
  8. import android.content.Context;
  9. import android.graphics.Bitmap;
  10. import android.graphics.Canvas;
  11. import android.util.Log;
  12. import android.view.SurfaceHolder;
  13. import android.view.SurfaceView;
  14.  
  15. public class CameraViewOpenCV extends SurfaceView implements SurfaceHolder.Callback, Runnable {
  16.     private static final String TAG = "Sample::SurfaceView";
  17.  
  18.     private SurfaceHolder       mHolder;
  19.     private VideoCapture        mCamera;
  20.     private Mat mRgba;
  21.  
  22.     public CameraViewOpenCV(Context context) {
  23.         super(context);
  24.         mHolder = getHolder();
  25.         mHolder.addCallback(this);
  26.     }
  27.  
  28.     public void surfaceChanged(SurfaceHolder _holder, int format, int width, int height) {
  29.         Log.i(TAG, "surfaceCreated");
  30.  
  31.         if (mCamera != null && mCamera.isOpened()) {
  32.             mCamera.set(Highgui.CV_CAP_PROP_FRAME_WIDTH, 640);
  33.             mCamera.set(Highgui.CV_CAP_PROP_FRAME_HEIGHT, 480);
  34.         }
  35.     }
  36.  
  37.     public void surfaceCreated(SurfaceHolder holder) {
  38.         Log.i(TAG, "surfaceCreated");
  39.  
  40.         mRgba  = new Mat();
  41.         mCamera = new VideoCapture(Highgui.CV_CAP_ANDROID);
  42.         if(mCamera.isOpened()){
  43.             (new Thread(this)).start();
  44.         } else {
  45.             mRgba.release();
  46.             mCamera.release();
  47.             mCamera = null;
  48.             Log.e(TAG, "Failed to open native camera");
  49.         }
  50.  
  51.  
  52.     }
  53.  
  54.     public void surfaceDestroyed(SurfaceHolder holder) {
  55.         Log.i(TAG, "surfaceDestroyed");
  56.         if (mCamera != null) {
  57.             mCamera.release();
  58.             mCamera = null;
  59.         }
  60.     }
  61.  
  62.     public double timet(double l){
  63.         return ((Core.getTickCount() - l)/Core.getTickFrequency()) * 1000;
  64.     }
  65.  
  66.     public void run() {
  67.         int i = 0;
  68.         double d, total = 0;
  69.         while (true) {
  70.             d = Core.getTickCount();
  71.             Bitmap bmp = null;
  72.  
  73.             if (mCamera == null)
  74.                 break;
  75.             if (!mCamera.grab()) {
  76.                 Log.e(TAG, "mCamera.grab() failed");
  77.                 break;
  78.             }
  79.  
  80.             mCamera.retrieve(mRgba, Highgui.CV_CAP_ANDROID_COLOR_FRAME_RGBA);
  81.             bmp = Bitmap.createBitmap(mRgba.cols(), mRgba.rows(), Bitmap.Config.ARGB_8888);
  82.             Utils.matToBitmap(mRgba, bmp);
  83.  
  84.             if (bmp != null) {
  85.                 Canvas canvas = mHolder.lockCanvas();
  86.                 if (canvas != null) {
  87.                     canvas.drawBitmap(bmp, (canvas.getWidth() - bmp.getWidth()) / 2, (canvas.getHeight() - bmp.getHeight()) / 2, null);
  88.                     mHolder.unlockCanvasAndPost(canvas);
  89.                 }
  90.                 bmp.recycle(); 
  91.                
  92.                 // measure frame rate
  93.                 total += timet(d);
  94.                 if((i++ % 100) == 0){
  95.                     Log.w("Opencv Camera", "Average frame time: " + total/100 + "ms" + " ##Frame Rate:" + (100000/(total)) + "fps");
  96.                     total = 0;
  97.                 }
  98.             }
  99.         }
  100.     }
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement