Advertisement
Guest User

Android Camera

a guest
Jan 30th, 2012
366
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.19 KB | None | 0 0
  1. import java.io.IOException;
  2. import org.opencv.android.Utils;
  3. import org.opencv.core.Core;
  4. import org.opencv.core.CvType;
  5. import org.opencv.core.Mat;
  6. import org.opencv.imgproc.Imgproc;
  7.  
  8. import android.content.Context;
  9. import android.graphics.Bitmap;
  10. import android.graphics.Canvas;
  11. import android.hardware.Camera;
  12. import android.hardware.Camera.PreviewCallback;
  13. import android.util.Log;
  14. import android.view.SurfaceHolder;
  15. import android.view.SurfaceView;
  16.  
  17. public class CameraViewAndroid extends SurfaceView implements SurfaceHolder.Callback, Runnable {
  18.     private static final String TAG = "Sample::SurfaceView";
  19.  
  20.     private Camera              mCamera;
  21.     private SurfaceHolder       mHolder;
  22.     private byte[]              mFrame;
  23.     private Mat mYuv;
  24.     private Mat mRgba;
  25.  
  26.     public CameraViewAndroid(Context context) {
  27.         super(context);
  28.         mHolder = getHolder();
  29.         mHolder.addCallback(this);
  30.         Log.i(TAG, "Instantiated new " + this.getClass());
  31.     }
  32.  
  33.     public void surfaceChanged(SurfaceHolder _holder, int format, int width, int height) {
  34.         Log.i(TAG, "surfaceCreated");
  35.         if (mCamera != null) {
  36.             Camera.Parameters params = mCamera.getParameters();
  37.             params.setPreviewSize(640,480);
  38.             mCamera.setParameters(params);
  39.             try {
  40.                 mCamera.setPreviewDisplay(null);
  41.             } catch (IOException e) {
  42.                 Log.e(TAG, "mCamera.setPreviewDisplay fails: " + e);
  43.             }
  44.             mCamera.startPreview();
  45.         }
  46.     }
  47.  
  48.     public void surfaceCreated(SurfaceHolder holder) {
  49.         Log.i(TAG, "surfaceCreated");
  50.  
  51.         mYuv = new Mat(480 + 480 / 2, 640, CvType.CV_8UC1);
  52.         mRgba = new Mat();
  53.  
  54.         mCamera = Camera.open();
  55.         mCamera.setPreviewCallback(new PreviewCallback() {
  56.             public void onPreviewFrame(byte[] data, Camera camera) {
  57.                 synchronized (CameraViewAndroid.this) {
  58.                     mFrame = data;
  59.                     CameraViewAndroid.this.notify();
  60.                 }
  61.             }
  62.         });
  63.         (new Thread(this)).start();
  64.     }
  65.  
  66.     public void surfaceDestroyed(SurfaceHolder holder) {
  67.         Log.i(TAG, "surfaceDestroyed");
  68.         if (mCamera != null) {
  69.             synchronized (this) {
  70.                 mCamera.stopPreview();
  71.                 mCamera.setPreviewCallback(null);
  72.                 mCamera.release();
  73.                 mCamera = null;
  74.             }
  75.         }
  76.     }
  77.  
  78.     public double timet(double l){
  79.         return ((Core.getTickCount() - l)/Core.getTickFrequency()) * 1000;
  80.     }
  81.  
  82.     public void run() {
  83.         int i = 0 ;
  84.         double d, total = 0;
  85.            
  86.         while (true) {
  87.             d = Core.getTickCount();
  88.             Bitmap bmp = null;
  89.  
  90.             synchronized (this) {
  91.                 try {
  92.                     this.wait();
  93.  
  94.                     mYuv.put(0, 0, mFrame);
  95.                     Imgproc.cvtColor(mYuv, mRgba, Imgproc.COLOR_YUV420sp2RGB, 4);
  96.                     bmp = Bitmap.createBitmap(640,480, Bitmap.Config.ARGB_8888);
  97.                     Utils.matToBitmap(mRgba, bmp);                  
  98.  
  99.                 } catch (InterruptedException e) {
  100.                     e.printStackTrace();
  101.                 }
  102.             }
  103.  
  104.             if (bmp != null) {
  105.                 Canvas canvas = mHolder.lockCanvas();
  106.                 if (canvas != null) {
  107.                     canvas.drawBitmap(bmp, (canvas.getWidth() - bmp.getWidth()) / 2, (canvas.getHeight() - bmp.getHeight()) / 2, null);
  108.                     mHolder.unlockCanvasAndPost(canvas);
  109.                 }
  110.                 bmp.recycle();
  111.             }
  112.             total += timet(d);
  113.             if((i++ % 100) == 0){
  114.                 Log.w("Android Camera", "Average frame time: " + total/100 + "ms" + " ##Frame Rate:" + (100000/(total)) + "fps");
  115.                 total = 0;
  116.             }
  117.         }
  118.     }
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement