Merge pull request #3749 from alalek:fix_android_camera_master

This commit is contained in:
Vadim Pisarevsky 2015-03-02 10:55:16 +00:00
commit de0997ab32

View File

@ -43,11 +43,13 @@ public class JavaCameraView extends CameraBridgeViewBase implements PreviewCallb
public static class JavaCameraSizeAccessor implements ListItemAccessor { public static class JavaCameraSizeAccessor implements ListItemAccessor {
@Override
public int getWidth(Object obj) { public int getWidth(Object obj) {
Camera.Size size = (Camera.Size) obj; Camera.Size size = (Camera.Size) obj;
return size.width; return size.width;
} }
@Override
public int getHeight(Object obj) { public int getHeight(Object obj) {
Camera.Size size = (Camera.Size) obj; Camera.Size size = (Camera.Size) obj;
return size.height; return size.height;
@ -228,6 +230,8 @@ public class JavaCameraView extends CameraBridgeViewBase implements PreviewCallb
} }
} }
private boolean mCameraFrameReady = false;
@Override @Override
protected boolean connectCamera(int width, int height) { protected boolean connectCamera(int width, int height) {
@ -239,6 +243,8 @@ public class JavaCameraView extends CameraBridgeViewBase implements PreviewCallb
if (!initializeCamera(width, height)) if (!initializeCamera(width, height))
return false; return false;
mCameraFrameReady = false;
/* now we can start update thread */ /* now we can start update thread */
Log.d(TAG, "Starting processing thread"); Log.d(TAG, "Starting processing thread");
mStopThread = false; mStopThread = false;
@ -248,6 +254,7 @@ public class JavaCameraView extends CameraBridgeViewBase implements PreviewCallb
return true; return true;
} }
@Override
protected void disconnectCamera() { protected void disconnectCamera() {
/* 1. We need to stop thread which updating the frames /* 1. We need to stop thread which updating the frames
* 2. Stop camera and release it * 2. Stop camera and release it
@ -270,12 +277,16 @@ public class JavaCameraView extends CameraBridgeViewBase implements PreviewCallb
/* Now release camera */ /* Now release camera */
releaseCamera(); releaseCamera();
mCameraFrameReady = false;
} }
@Override
public void onPreviewFrame(byte[] frame, Camera arg1) { public void onPreviewFrame(byte[] frame, Camera arg1) {
Log.d(TAG, "Preview Frame received. Frame size: " + frame.length); Log.d(TAG, "Preview Frame received. Frame size: " + frame.length);
synchronized (this) { synchronized (this) {
mFrameChain[1 - mChainIdx].put(0, 0, frame); mFrameChain[mChainIdx].put(0, 0, frame);
mCameraFrameReady = true;
this.notify(); this.notify();
} }
if (mCamera != null) if (mCamera != null)
@ -283,10 +294,12 @@ public class JavaCameraView extends CameraBridgeViewBase implements PreviewCallb
} }
private class JavaCameraFrame implements CvCameraViewFrame { private class JavaCameraFrame implements CvCameraViewFrame {
@Override
public Mat gray() { public Mat gray() {
return mYuvFrameData.submat(0, mHeight, 0, mWidth); return mYuvFrameData.submat(0, mHeight, 0, mWidth);
} }
@Override
public Mat rgba() { public Mat rgba() {
Imgproc.cvtColor(mYuvFrameData, mRgba, Imgproc.COLOR_YUV2RGBA_NV21, 4); Imgproc.cvtColor(mYuvFrameData, mRgba, Imgproc.COLOR_YUV2RGBA_NV21, 4);
return mRgba; return mRgba;
@ -312,21 +325,25 @@ public class JavaCameraView extends CameraBridgeViewBase implements PreviewCallb
private class CameraWorker implements Runnable { private class CameraWorker implements Runnable {
@Override
public void run() { public void run() {
do { do {
synchronized (JavaCameraView.this) { synchronized (JavaCameraView.this) {
try { try {
JavaCameraView.this.wait(); while (!mCameraFrameReady && !mStopThread) {
JavaCameraView.this.wait();
}
} catch (InterruptedException e) { } catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace(); e.printStackTrace();
} }
if (mCameraFrameReady)
mChainIdx = 1 - mChainIdx;
} }
if (!mStopThread) { if (!mStopThread && mCameraFrameReady) {
if (!mFrameChain[mChainIdx].empty()) mCameraFrameReady = false;
deliverAndDrawFrame(mCameraFrame[mChainIdx]); if (!mFrameChain[1 - mChainIdx].empty())
mChainIdx = 1 - mChainIdx; deliverAndDrawFrame(mCameraFrame[1 - mChainIdx]);
} }
} while (!mStopThread); } while (!mStopThread);
Log.d(TAG, "Finish processing thread"); Log.d(TAG, "Finish processing thread");