|
|
|
@ -19,52 +19,52 @@ import android.util.Log; |
|
|
|
|
*/ |
|
|
|
|
public class OpenCvNativeCameraView extends OpenCvCameraBridgeViewBase { |
|
|
|
|
|
|
|
|
|
public static final String TAG = "OpenCvNativeCameraView"; |
|
|
|
|
private boolean mStopThread; |
|
|
|
|
private Thread mThread; |
|
|
|
|
private VideoCapture mCamera; |
|
|
|
|
public static final String TAG = "OpenCvNativeCameraView"; |
|
|
|
|
private boolean mStopThread; |
|
|
|
|
private Thread mThread; |
|
|
|
|
private VideoCapture mCamera; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public OpenCvNativeCameraView(Context context, AttributeSet attrs) { |
|
|
|
|
super(context, attrs); |
|
|
|
|
super(context, attrs); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
protected void connectCamera(int width, int height) { |
|
|
|
|
|
|
|
|
|
/* 1. We need to instantiate camera |
|
|
|
|
* 2. We need to start thread which will be getting frames |
|
|
|
|
*/ |
|
|
|
|
/* First step - initialize camera connection */ |
|
|
|
|
initializeCamera(getWidth(), getHeight()); |
|
|
|
|
|
|
|
|
|
/* now we can start update thread */ |
|
|
|
|
mThread = new Thread(new CameraWorker(getWidth(), getHeight())); |
|
|
|
|
mThread.start(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
protected void disconnectCamera() { |
|
|
|
|
/* 1. We need to stop thread which updating the frames |
|
|
|
|
* 2. Stop camera and release it |
|
|
|
|
*/ |
|
|
|
|
try { |
|
|
|
|
mStopThread = true; |
|
|
|
|
mThread.join(); |
|
|
|
|
} catch (InterruptedException e) { |
|
|
|
|
e.printStackTrace(); |
|
|
|
|
} finally { |
|
|
|
|
mThread = null; |
|
|
|
|
mStopThread = false; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/* Now release camera */ |
|
|
|
|
releaseCamera(); |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
protected void connectCamera(int width, int height) { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/* 1. We need to instantiate camera |
|
|
|
|
* 2. We need to start thread which will be getting frames |
|
|
|
|
*/ |
|
|
|
|
/* First step - initialize camera connection */ |
|
|
|
|
initializeCamera(getWidth(), getHeight()); |
|
|
|
|
|
|
|
|
|
/* now we can start update thread */ |
|
|
|
|
mThread = new Thread(new CameraWorker(getWidth(), getHeight())); |
|
|
|
|
mThread.start(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
protected void disconnectCamera() { |
|
|
|
|
/* 1. We need to stop thread which updating the frames |
|
|
|
|
* 2. Stop camera and release it |
|
|
|
|
*/ |
|
|
|
|
try { |
|
|
|
|
mStopThread = true; |
|
|
|
|
mThread.join(); |
|
|
|
|
} catch (InterruptedException e) { |
|
|
|
|
e.printStackTrace(); |
|
|
|
|
} finally { |
|
|
|
|
mThread = null; |
|
|
|
|
mStopThread = false; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/* Now release camera */ |
|
|
|
|
releaseCamera(); |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private void initializeCamera(int width, int height) { |
|
|
|
|
private void initializeCamera(int width, int height) { |
|
|
|
|
mCamera = new VideoCapture(Highgui.CV_CAP_ANDROID); |
|
|
|
|
//TODO: improve error handling
|
|
|
|
|
|
|
|
|
@ -85,42 +85,41 @@ public class OpenCvNativeCameraView extends OpenCvCameraBridgeViewBase { |
|
|
|
|
mFrameHeight = (int)frameHeight; |
|
|
|
|
|
|
|
|
|
Log.i(TAG, "Selected camera frame size = (" + mFrameWidth + ", " + mFrameHeight + ")"); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private void releaseCamera() { |
|
|
|
|
if (mCamera != null) { |
|
|
|
|
mCamera.release(); |
|
|
|
|
} |
|
|
|
|
private void releaseCamera() { |
|
|
|
|
if (mCamera != null) { |
|
|
|
|
mCamera.release(); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private class CameraWorker implements Runnable { |
|
|
|
|
private class CameraWorker implements Runnable { |
|
|
|
|
|
|
|
|
|
private Mat mRgba = new Mat(); |
|
|
|
|
private int mWidth; |
|
|
|
|
private int mHeight; |
|
|
|
|
private Mat mRgba = new Mat(); |
|
|
|
|
private int mWidth; |
|
|
|
|
private int mHeight; |
|
|
|
|
|
|
|
|
|
CameraWorker(int w, int h) { |
|
|
|
|
mWidth = w; |
|
|
|
|
mHeight = h; |
|
|
|
|
} |
|
|
|
|
CameraWorker(int w, int h) { |
|
|
|
|
mWidth = w; |
|
|
|
|
mHeight = h; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public void run() { |
|
|
|
|
Mat modified; |
|
|
|
|
public void run() { |
|
|
|
|
Mat modified; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
do { |
|
|
|
|
if (!mCamera.grab()) { |
|
|
|
|
Log.e(TAG, "Camera frame grab failed"); |
|
|
|
|
break; |
|
|
|
|
} |
|
|
|
|
mCamera.retrieve(mRgba, Highgui.CV_CAP_ANDROID_COLOR_FRAME_RGBA); |
|
|
|
|
do { |
|
|
|
|
if (!mCamera.grab()) { |
|
|
|
|
Log.e(TAG, "Camera frame grab failed"); |
|
|
|
|
break; |
|
|
|
|
} |
|
|
|
|
mCamera.retrieve(mRgba, Highgui.CV_CAP_ANDROID_COLOR_FRAME_RGBA); |
|
|
|
|
|
|
|
|
|
deliverAndDrawFrame(mRgba); |
|
|
|
|
deliverAndDrawFrame(mRgba); |
|
|
|
|
|
|
|
|
|
} while (!mStopThread); |
|
|
|
|
} while (!mStopThread); |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|