package pt.com.reverse.nothing; import java.util.List; import org.opencv.android.Utils; import org.opencv.core.Core; import org.opencv.core.Mat; import org.opencv.core.Size; import org.opencv.highgui.Highgui; import org.opencv.highgui.VideoCapture; import android.content.Context; import android.graphics.Bitmap; import android.graphics.Canvas; import android.os.Debug; import android.util.Log; import android.view.SurfaceHolder; import android.view.SurfaceView; public class SampleCvViewBase extends SurfaceView implements SurfaceHolder.Callback, Runnable { private static final String TAG = "Sample::SurfaceView"; private SurfaceHolder mHolder; private VideoCapture mCamera; private Mat mRgba; public SampleCvViewBase(Context context) { super(context); mHolder = getHolder(); mHolder.addCallback(this); } public void surfaceChanged(SurfaceHolder _holder, int format, int width, int height) { Log.i(TAG, "surfaceCreated"); if (mCamera != null && mCamera.isOpened()) { List sizes = mCamera.getSupportedPreviewSizes(); int mFrameWidth = width; int mFrameHeight = height; // selecting optimal camera preview size { double minDiff = Double.MAX_VALUE; for (Size size : sizes) { if (Math.abs(size.height - height) < minDiff) { mFrameWidth = (int) size.width; mFrameHeight = (int) size.height; minDiff = Math.abs(size.height - height); } } } mFrameWidth = 640; mFrameHeight = 480; mCamera.set(Highgui.CV_CAP_PROP_FRAME_WIDTH, mFrameWidth); mCamera.set(Highgui.CV_CAP_PROP_FRAME_HEIGHT, mFrameHeight); } } public void surfaceCreated(SurfaceHolder holder) { Log.i(TAG, "surfaceCreated"); mRgba = new Mat(); mCamera = new VideoCapture(Highgui.CV_CAP_ANDROID); if(mCamera.isOpened()){ //Debug.startMethodTracing("nothing3"); (new Thread(this)).start(); } else { mCamera.release(); mCamera = null; Log.e(TAG, "Failed to open native camera"); } } public void surfaceDestroyed(SurfaceHolder holder) { Log.i(TAG, "surfaceDestroyed"); // Debug.stopMethodTracing(); if (mCamera != null) { mCamera.release(); mCamera = null; } } //protected abstract Bitmap processFrame(VideoCapture capture); public double timet(String s, double l){ double d = ((Core.getTickCount() - l)/Core.getTickFrequency()) * 1000; return d; } public void run() { int i = 0; double total = 0; while (true) { double d = Core.getTickCount(); Bitmap bmp = null; if (mCamera == null) break; if (!mCamera.grab()) { Log.e(TAG, "mCamera.grab() failed"); break; } mCamera.retrieve(mRgba, Highgui.CV_CAP_ANDROID_COLOR_FRAME_RGBA); bmp = Bitmap.createBitmap(mRgba.cols(), mRgba.rows(), Bitmap.Config.ARGB_8888); Utils.matToBitmap(mRgba, bmp); if (bmp != null) { Canvas canvas = mHolder.lockCanvas(); if (canvas != null) { canvas.drawBitmap(bmp, (canvas.getWidth() - bmp.getWidth()) / 2, (canvas.getHeight() - bmp.getHeight()) / 2, null); mHolder.unlockCanvasAndPost(canvas); } bmp.recycle(); total += timet("Total frame time:", d);; i++; if(i % 100 == 0){ Log.w("TIME", "Average frame time: " + total/100 + "ms" + " ##Frame Rate:" + (100000/(total)) + "fps"); total = 0; } } } } }