1 | package org.opencv.android;
|
2 |
|
3 | import java.util.List;
|
4 |
|
5 | import android.content.Context;
|
6 | import android.graphics.ImageFormat;
|
7 | import android.graphics.SurfaceTexture;
|
8 | import android.hardware.Camera;
|
9 | import android.hardware.Camera.PreviewCallback;
|
10 | import android.os.Build;
|
11 | import android.util.AttributeSet;
|
12 | import android.util.Log;
|
13 | import android.view.ViewGroup.LayoutParams;
|
14 |
|
15 | import org.opencv.core.CvType;
|
16 | import org.opencv.core.Mat;
|
17 | import org.opencv.core.Size;
|
18 | import org.opencv.imgproc.Imgproc;
|
19 |
|
20 | |
21 | * This class is an implementation of the Bridge View between OpenCV and Java Camera.
|
22 | * This class relays on the functionality available in base class and only implements
|
23 | * required functions:
|
24 | * connectCamera - opens Java camera and sets the PreviewCallback to be delivered.
|
25 | * disconnectCamera - closes the camera and stops preview.
|
26 | * When frame is delivered via callback from Camera - it processed via OpenCV to be
|
27 | * converted to RGBA32 and then passed to the external callback for modifications if required.
|
28 | */
|
29 | public class JavaCameraView extends CameraBridgeViewBase implements PreviewCallback {
|
30 |
|
31 | private static final int MAGIC_TEXTURE_ID = 10;
|
32 | private static final String TAG = "JavaCameraView";
|
33 |
|
34 | private byte mBuffer[];
|
35 | private Mat[] mFrameChain;
|
36 | private int mChainIdx = 0;
|
37 | private Thread mThread;
|
38 | private boolean mStopThread;
|
39 |
|
40 | protected Camera mCamera;
|
41 | protected JavaCameraFrame[] mCameraFrame;
|
42 | private SurfaceTexture mSurfaceTexture;
|
43 |
|
44 | public static class JavaCameraSizeAccessor implements ListItemAccessor {
|
45 |
|
46 | public int getWidth(Object obj) {
|
47 | Camera.Size size = (Camera.Size) obj;
|
48 | return size.width;
|
49 | }
|
50 |
|
51 | public int getHeight(Object obj) {
|
52 | Camera.Size size = (Camera.Size) obj;
|
53 | return size.height;
|
54 | }
|
55 | }
|
56 |
|
57 | public JavaCameraView(Context context, int cameraId) {
|
58 | super(context, cameraId);
|
59 | }
|
60 |
|
61 | public JavaCameraView(Context context, AttributeSet attrs) {
|
62 | super(context, attrs);
|
63 | }
|
64 |
|
65 | protected boolean initializeCamera(int width, int height) {
|
66 | Log.d(TAG, "Initialize java camera");
|
67 | boolean result = true;
|
68 | synchronized (this) {
|
69 | mCamera = null;
|
70 |
|
71 | if (mCameraIndex == CAMERA_ID_ANY) {
|
72 | Log.d(TAG, "Trying to open camera with old open()");
|
73 | try {
|
74 | mCamera = Camera.open();
|
75 | }
|
76 | catch (Exception e){
|
77 | Log.e(TAG, "Camera is not available (in use or does not exist): " + e.getLocalizedMessage());
|
78 | }
|
79 |
|
80 | if(mCamera == null && Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) {
|
81 | boolean connected = false;
|
82 | for (int camIdx = 0; camIdx < Camera.getNumberOfCameras(); ++camIdx) {
|
83 | Log.d(TAG, "Trying to open camera with new open(" + Integer.valueOf(camIdx) + ")");
|
84 | try {
|
85 | mCamera = Camera.open(camIdx);
|
86 | connected = true;
|
87 | } catch (RuntimeException e) {
|
88 | Log.e(TAG, "Camera #" + camIdx + "failed to open: " + e.getLocalizedMessage());
|
89 | }
|
90 | if (connected) break;
|
91 | }
|
92 | }
|
93 | } else {
|
94 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) {
|
95 | int localCameraIndex = mCameraIndex;
|
96 | if (mCameraIndex == CAMERA_ID_BACK) {
|
97 | Log.i(TAG, "Trying to open back camera");
|
98 | Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
|
99 | for (int camIdx = 0; camIdx < Camera.getNumberOfCameras(); ++camIdx) {
|
100 | Camera.getCameraInfo( camIdx, cameraInfo );
|
101 | if (cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_BACK) {
|
102 | localCameraIndex = camIdx;
|
103 | break;
|
104 | }
|
105 | }
|
106 | } else if (mCameraIndex == CAMERA_ID_FRONT) {
|
107 | Log.i(TAG, "Trying to open front camera");
|
108 | Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
|
109 | for (int camIdx = 0; camIdx < Camera.getNumberOfCameras(); ++camIdx) {
|
110 | Camera.getCameraInfo( camIdx, cameraInfo );
|
111 | if (cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
|
112 | localCameraIndex = camIdx;
|
113 | break;
|
114 | }
|
115 | }
|
116 | }
|
117 | if (localCameraIndex == CAMERA_ID_BACK) {
|
118 | Log.e(TAG, "Back camera not found!");
|
119 | } else if (localCameraIndex == CAMERA_ID_FRONT) {
|
120 | Log.e(TAG, "Front camera not found!");
|
121 | } else {
|
122 | Log.d(TAG, "Trying to open camera with new open(" + Integer.valueOf(localCameraIndex) + ")");
|
123 | try {
|
124 | mCamera = Camera.open(localCameraIndex);
|
125 | } catch (RuntimeException e) {
|
126 | Log.e(TAG, "Camera #" + localCameraIndex + "failed to open: " + e.getLocalizedMessage());
|
127 | }
|
128 | }
|
129 | }
|
130 | }
|
131 |
|
132 | if (mCamera == null)
|
133 | return false;
|
134 |
|
135 |
|
136 | try {
|
137 | Camera.Parameters params = mCamera.getParameters();
|
138 | Log.d(TAG, "getSupportedPreviewSizes()");
|
139 | List<android.hardware.Camera.Size> sizes = params.getSupportedPreviewSizes();
|
140 |
|
141 | if (sizes != null) {
|
142 |
|
143 | Size frameSize = calculateCameraFrameSize(sizes, new JavaCameraSizeAccessor(), width, height);
|
144 |
|
145 | params.setPreviewFormat(ImageFormat.NV21);
|
146 | Log.d(TAG, "Set preview size to " + Integer.valueOf((int)frameSize.width) + "x" + Integer.valueOf((int)frameSize.height));
|
147 | params.setPreviewSize((int)frameSize.width, (int)frameSize.height);
|
148 |
|
149 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH)
|
150 | params.setRecordingHint(false);
|
151 |
|
152 | List<String> FocusModes = params.getSupportedFocusModes();
|
153 | if (FocusModes != null && FocusModes.contains(Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO))
|
154 | {
|
155 | params.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO);
|
156 | }
|
157 |
|
158 | mCamera.setParameters(params);
|
159 | params = mCamera.getParameters();
|
160 |
|
161 | mFrameWidth = params.getPreviewSize().width;
|
162 | mFrameHeight = params.getPreviewSize().height;
|
163 |
|
164 | if ((getLayoutParams().width == LayoutParams.MATCH_PARENT) && (getLayoutParams().height == LayoutParams.MATCH_PARENT))
|
165 | mScale = Math.min(((float)height)/mFrameHeight, ((float)width)/mFrameWidth);
|
166 | else
|
167 | mScale = 0;
|
168 |
|
169 | if (mFpsMeter != null) {
|
170 | mFpsMeter.setResolution(mFrameWidth, mFrameHeight);
|
171 | }
|
172 |
|
173 | int size = mFrameWidth * mFrameHeight;
|
174 | size = size * ImageFormat.getBitsPerPixel(params.getPreviewFormat()) / 8;
|
175 | mBuffer = new byte[size];
|
176 |
|
177 | mCamera.addCallbackBuffer(mBuffer);
|
178 | mCamera.setPreviewCallbackWithBuffer(this);
|
179 |
|
180 | mFrameChain = new Mat[2];
|
181 | mFrameChain[0] = new Mat(mFrameHeight + (mFrameHeight/2), mFrameWidth, CvType.CV_8UC1);
|
182 | mFrameChain[1] = new Mat(mFrameHeight + (mFrameHeight/2), mFrameWidth, CvType.CV_8UC1);
|
183 |
|
184 | AllocateCache();
|
185 |
|
186 | mCameraFrame = new JavaCameraFrame[2];
|
187 | mCameraFrame[0] = new JavaCameraFrame(mFrameChain[0], mFrameWidth, mFrameHeight);
|
188 | mCameraFrame[1] = new JavaCameraFrame(mFrameChain[1], mFrameWidth, mFrameHeight);
|
189 |
|
190 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
|
191 | mSurfaceTexture = new SurfaceTexture(MAGIC_TEXTURE_ID);
|
192 | mCamera.setPreviewTexture(mSurfaceTexture);
|
193 | } else
|
194 | mCamera.setPreviewDisplay(null);
|
195 |
|
196 |
|
197 | Log.d(TAG, "startPreview");
|
198 | mCamera.startPreview();
|
199 | }
|
200 | else
|
201 | result = false;
|
202 | } catch (Exception e) {
|
203 | result = false;
|
204 | e.printStackTrace();
|
205 | }
|
206 | }
|
207 |
|
208 | return result;
|
209 | }
|
210 |
|
211 | protected void releaseCamera() {
|
212 | synchronized (this) {
|
213 | if (mCamera != null) {
|
214 | mCamera.stopPreview();
|
215 | mCamera.setPreviewCallback(null);
|
216 |
|
217 | mCamera.release();
|
218 | }
|
219 | mCamera = null;
|
220 | if (mFrameChain != null) {
|
221 | mFrameChain[0].release();
|
222 | mFrameChain[1].release();
|
223 | }
|
224 | if (mCameraFrame != null) {
|
225 | mCameraFrame[0].release();
|
226 | mCameraFrame[1].release();
|
227 | }
|
228 | }
|
229 | }
|
230 |
|
231 | @Override
|
232 | protected boolean connectCamera(int width, int height) {
|
233 |
|
234 | |
235 | * 2. We need to start thread which will be getting frames
|
236 | */
|
237 |
|
238 | Log.d(TAG, "Connecting to camera");
|
239 | if (!initializeCamera(width, height))
|
240 | return false;
|
241 |
|
242 |
|
243 | Log.d(TAG, "Starting processing thread");
|
244 | mStopThread = false;
|
245 | mThread = new Thread(new CameraWorker());
|
246 | mThread.start();
|
247 |
|
248 | return true;
|
249 | }
|
250 |
|
251 | protected void disconnectCamera() {
|
252 | |
253 | * 2. Stop camera and release it
|
254 | */
|
255 | Log.d(TAG, "Disconnecting from camera");
|
256 | try {
|
257 | mStopThread = true;
|
258 | Log.d(TAG, "Notify thread");
|
259 | synchronized (this) {
|
260 | this.notify();
|
261 | }
|
262 | Log.d(TAG, "Wating for thread");
|
263 | if (mThread != null)
|
264 | mThread.join();
|
265 | } catch (InterruptedException e) {
|
266 | e.printStackTrace();
|
267 | } finally {
|
268 | mThread = null;
|
269 | }
|
270 |
|
271 |
|
272 | releaseCamera();
|
273 | }
|
274 |
|
275 | public void onPreviewFrame(byte[] frame, Camera arg1) {
|
276 | Log.d(TAG, "Preview Frame received. Frame size: " + frame.length);
|
277 | synchronized (this) {
|
278 | mFrameChain[1 - mChainIdx].put(0, 0, frame);
|
279 | this.notify();
|
280 | }
|
281 | if (mCamera != null)
|
282 | mCamera.addCallbackBuffer(mBuffer);
|
283 | }
|
284 |
|
285 | private class JavaCameraFrame implements CvCameraViewFrame {
|
286 | public Mat gray() {
|
287 | return mYuvFrameData.submat(0, mHeight, 0, mWidth);
|
288 | }
|
289 |
|
290 | public Mat rgba() {
|
291 | Imgproc.cvtColor(mYuvFrameData, mRgba, Imgproc.COLOR_YUV2RGBA_NV21, 4);
|
292 | return mRgba;
|
293 | }
|
294 |
|
295 | public JavaCameraFrame(Mat Yuv420sp, int width, int height) {
|
296 | super();
|
297 | mWidth = width;
|
298 | mHeight = height;
|
299 | mYuvFrameData = Yuv420sp;
|
300 | mRgba = new Mat();
|
301 | }
|
302 |
|
303 | public void release() {
|
304 | mRgba.release();
|
305 | }
|
306 |
|
307 | private Mat mYuvFrameData;
|
308 | private Mat mRgba;
|
309 | private int mWidth;
|
310 | private int mHeight;
|
311 | };
|
312 |
|
313 | private class CameraWorker implements Runnable {
|
314 |
|
315 | public void run() {
|
316 | do {
|
317 | synchronized (JavaCameraView.this) {
|
318 | try {
|
319 | JavaCameraView.this.wait();
|
320 | } catch (InterruptedException e) {
|
321 |
|
322 | e.printStackTrace();
|
323 | }
|
324 | }
|
325 |
|
326 | if (!mStopThread) {
|
327 | if (!mFrameChain[mChainIdx].empty())
|
328 | deliverAndDrawFrame(mCameraFrame[mChainIdx]);
|
329 | mChainIdx = 1 - mChainIdx;
|
330 | }
|
331 | } while (!mStopThread);
|
332 | Log.d(TAG, "Finish processing thread");
|
333 | }
|
334 | }
|
335 | }
|