OSDN Git Service

ライブビューデータを拾うために検討中。その4。
[gokigen/FujiCam.git] / opencv41 / src / main / java / org / opencv / android / CameraBridgeViewBase.java
1 package org.opencv.android;
2
3 import java.util.List;
4
5 import org.opencv.BuildConfig;
6 import org.opencv.R;
7 import org.opencv.core.Mat;
8 import org.opencv.core.Size;
9
10 import android.app.Activity;
11 import android.app.AlertDialog;
12 import android.content.Context;
13 import android.content.DialogInterface;
14 import android.content.res.TypedArray;
15 import android.graphics.Bitmap;
16 import android.graphics.Canvas;
17 import android.graphics.Rect;
18 import android.util.AttributeSet;
19 import android.util.Log;
20 import android.view.SurfaceHolder;
21 import android.view.SurfaceView;
22
23 /**
24  * This is a basic class, implementing the interaction with Camera and OpenCV library.
25  * The main responsibility of it - is to control when camera can be enabled, process the frame,
26  * call external listener to make any adjustments to the frame and then draw the resulting
27  * frame to the screen.
28  * The clients shall implement CvCameraViewListener.
29  */
30 public abstract class CameraBridgeViewBase extends SurfaceView implements SurfaceHolder.Callback {
31
32     private static final String TAG = "CameraBridge";
33     private static final int MAX_UNSPECIFIED = -1;
34     private static final int STOPPED = 0;
35     private static final int STARTED = 1;
36
37     private int mState = STOPPED;
38     private Bitmap mCacheBitmap;
39     private CvCameraViewListener2 mListener;
40     private boolean mSurfaceExist;
41     private final Object mSyncObject = new Object();
42
43     protected int mFrameWidth;
44     protected int mFrameHeight;
45     protected int mMaxHeight;
46     protected int mMaxWidth;
47     protected float mScale = 0;
48     protected int mPreviewFormat = RGBA;
49     protected int mCameraIndex = CAMERA_ID_ANY;
50     protected boolean mEnabled;
51     protected FpsMeter mFpsMeter = null;
52
53     public static final int CAMERA_ID_ANY   = -1;
54     public static final int CAMERA_ID_BACK  = 99;
55     public static final int CAMERA_ID_FRONT = 98;
56     public static final int RGBA = 1;
57     public static final int GRAY = 2;
58
59     public CameraBridgeViewBase(Context context, int cameraId) {
60         super(context);
61         mCameraIndex = cameraId;
62         getHolder().addCallback(this);
63         mMaxWidth = MAX_UNSPECIFIED;
64         mMaxHeight = MAX_UNSPECIFIED;
65     }
66
67     public CameraBridgeViewBase(Context context, AttributeSet attrs) {
68         super(context, attrs);
69
70         int count = attrs.getAttributeCount();
71         Log.d(TAG, "Attr count: " + Integer.valueOf(count));
72
73         TypedArray styledAttrs = getContext().obtainStyledAttributes(attrs, R.styleable.CameraBridgeViewBase);
74         if (styledAttrs.getBoolean(R.styleable.CameraBridgeViewBase_show_fps, false))
75             enableFpsMeter();
76
77         mCameraIndex = styledAttrs.getInt(R.styleable.CameraBridgeViewBase_camera_id, -1);
78
79         getHolder().addCallback(this);
80         mMaxWidth = MAX_UNSPECIFIED;
81         mMaxHeight = MAX_UNSPECIFIED;
82         styledAttrs.recycle();
83     }
84
85     /**
86      * Sets the camera index
87      * @param cameraIndex new camera index
88      */
89     public void setCameraIndex(int cameraIndex) {
90         this.mCameraIndex = cameraIndex;
91     }
92
93     public interface CvCameraViewListener {
94         /**
95          * This method is invoked when camera preview has started. After this method is invoked
96          * the frames will start to be delivered to client via the onCameraFrame() callback.
97          * @param width -  the width of the frames that will be delivered
98          * @param height - the height of the frames that will be delivered
99          */
100         public void onCameraViewStarted(int width, int height);
101
102         /**
103          * This method is invoked when camera preview has been stopped for some reason.
104          * No frames will be delivered via onCameraFrame() callback after this method is called.
105          */
106         public void onCameraViewStopped();
107
108         /**
109          * This method is invoked when delivery of the frame needs to be done.
110          * The returned values - is a modified frame which needs to be displayed on the screen.
111          * TODO: pass the parameters specifying the format of the frame (BPP, YUV or RGB and etc)
112          */
113         public Mat onCameraFrame(Mat inputFrame);
114     }
115
116     public interface CvCameraViewListener2 {
117         /**
118          * This method is invoked when camera preview has started. After this method is invoked
119          * the frames will start to be delivered to client via the onCameraFrame() callback.
120          * @param width -  the width of the frames that will be delivered
121          * @param height - the height of the frames that will be delivered
122          */
123         public void onCameraViewStarted(int width, int height);
124
125         /**
126          * This method is invoked when camera preview has been stopped for some reason.
127          * No frames will be delivered via onCameraFrame() callback after this method is called.
128          */
129         public void onCameraViewStopped();
130
131         /**
132          * This method is invoked when delivery of the frame needs to be done.
133          * The returned values - is a modified frame which needs to be displayed on the screen.
134          * TODO: pass the parameters specifying the format of the frame (BPP, YUV or RGB and etc)
135          */
136         public Mat onCameraFrame(CvCameraViewFrame inputFrame);
137     };
138
139     protected class CvCameraViewListenerAdapter implements CvCameraViewListener2  {
140         public CvCameraViewListenerAdapter(CvCameraViewListener oldStypeListener) {
141             mOldStyleListener = oldStypeListener;
142         }
143
144         public void onCameraViewStarted(int width, int height) {
145             mOldStyleListener.onCameraViewStarted(width, height);
146         }
147
148         public void onCameraViewStopped() {
149             mOldStyleListener.onCameraViewStopped();
150         }
151
152         public Mat onCameraFrame(CvCameraViewFrame inputFrame) {
153              Mat result = null;
154              switch (mPreviewFormat) {
155                 case RGBA:
156                     result = mOldStyleListener.onCameraFrame(inputFrame.rgba());
157                     break;
158                 case GRAY:
159                     result = mOldStyleListener.onCameraFrame(inputFrame.gray());
160                     break;
161                 default:
162                     Log.e(TAG, "Invalid frame format! Only RGBA and Gray Scale are supported!");
163             };
164
165             return result;
166         }
167
168         public void setFrameFormat(int format) {
169             mPreviewFormat = format;
170         }
171
172         private int mPreviewFormat = RGBA;
173         private CvCameraViewListener mOldStyleListener;
174     };
175
176     /**
177      * This class interface is abstract representation of single frame from camera for onCameraFrame callback
178      * Attention: Do not use objects, that represents this interface out of onCameraFrame callback!
179      */
180     public interface CvCameraViewFrame {
181
182         /**
183          * This method returns RGBA Mat with frame
184          */
185         public Mat rgba();
186
187         /**
188          * This method returns single channel gray scale Mat with frame
189          */
190         public Mat gray();
191     };
192
193     public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
194         Log.d(TAG, "call surfaceChanged event");
195         synchronized(mSyncObject) {
196             if (!mSurfaceExist) {
197                 mSurfaceExist = true;
198                 checkCurrentState();
199             } else {
200                 /** Surface changed. We need to stop camera and restart with new parameters */
201                 /* Pretend that old surface has been destroyed */
202                 mSurfaceExist = false;
203                 checkCurrentState();
204                 /* Now use new surface. Say we have it now */
205                 mSurfaceExist = true;
206                 checkCurrentState();
207             }
208         }
209     }
210
211     public void surfaceCreated(SurfaceHolder holder) {
212         /* Do nothing. Wait until surfaceChanged delivered */
213     }
214
215     public void surfaceDestroyed(SurfaceHolder holder) {
216         synchronized(mSyncObject) {
217             mSurfaceExist = false;
218             checkCurrentState();
219         }
220     }
221
222     /**
223      * This method is provided for clients, so they can enable the camera connection.
224      * The actual onCameraViewStarted callback will be delivered only after both this method is called and surface is available
225      */
226     public void enableView() {
227         synchronized(mSyncObject) {
228             mEnabled = true;
229             checkCurrentState();
230         }
231     }
232
233     /**
234      * This method is provided for clients, so they can disable camera connection and stop
235      * the delivery of frames even though the surface view itself is not destroyed and still stays on the scren
236      */
237     public void disableView() {
238         synchronized(mSyncObject) {
239             mEnabled = false;
240             checkCurrentState();
241         }
242     }
243
244     /**
245      * This method enables label with fps value on the screen
246      */
247     public void enableFpsMeter() {
248         if (mFpsMeter == null) {
249             mFpsMeter = new FpsMeter();
250             mFpsMeter.setResolution(mFrameWidth, mFrameHeight);
251         }
252     }
253
254     public void disableFpsMeter() {
255             mFpsMeter = null;
256     }
257
258     /**
259      *
260      * @param listener
261      */
262
263     public void setCvCameraViewListener(CvCameraViewListener2 listener) {
264         mListener = listener;
265     }
266
267     public void setCvCameraViewListener(CvCameraViewListener listener) {
268         CvCameraViewListenerAdapter adapter = new CvCameraViewListenerAdapter(listener);
269         adapter.setFrameFormat(mPreviewFormat);
270         mListener = adapter;
271     }
272
273     /**
274      * This method sets the maximum size that camera frame is allowed to be. When selecting
275      * size - the biggest size which less or equal the size set will be selected.
276      * As an example - we set setMaxFrameSize(200,200) and we have 176x152 and 320x240 sizes. The
277      * preview frame will be selected with 176x152 size.
278      * This method is useful when need to restrict the size of preview frame for some reason (for example for video recording)
279      * @param maxWidth - the maximum width allowed for camera frame.
280      * @param maxHeight - the maximum height allowed for camera frame
281      */
282     public void setMaxFrameSize(int maxWidth, int maxHeight) {
283         mMaxWidth = maxWidth;
284         mMaxHeight = maxHeight;
285     }
286
287     public void SetCaptureFormat(int format)
288     {
289         mPreviewFormat = format;
290         if (mListener instanceof CvCameraViewListenerAdapter) {
291             CvCameraViewListenerAdapter adapter = (CvCameraViewListenerAdapter) mListener;
292             adapter.setFrameFormat(mPreviewFormat);
293         }
294     }
295
296     /**
297      * Called when mSyncObject lock is held
298      */
299     private void checkCurrentState() {
300         Log.d(TAG, "call checkCurrentState");
301         int targetState;
302
303         if (mEnabled && mSurfaceExist && getVisibility() == VISIBLE) {
304             targetState = STARTED;
305         } else {
306             targetState = STOPPED;
307         }
308
309         if (targetState != mState) {
310             /* The state change detected. Need to exit the current state and enter target state */
311             processExitState(mState);
312             mState = targetState;
313             processEnterState(mState);
314         }
315     }
316
317     private void processEnterState(int state) {
318         Log.d(TAG, "call processEnterState: " + state);
319         switch(state) {
320         case STARTED:
321             onEnterStartedState();
322             if (mListener != null) {
323                 mListener.onCameraViewStarted(mFrameWidth, mFrameHeight);
324             }
325             break;
326         case STOPPED:
327             onEnterStoppedState();
328             if (mListener != null) {
329                 mListener.onCameraViewStopped();
330             }
331             break;
332         };
333     }
334
335     private void processExitState(int state) {
336         Log.d(TAG, "call processExitState: " + state);
337         switch(state) {
338         case STARTED:
339             onExitStartedState();
340             break;
341         case STOPPED:
342             onExitStoppedState();
343             break;
344         };
345     }
346
347     private void onEnterStoppedState() {
348         /* nothing to do */
349     }
350
351     private void onExitStoppedState() {
352         /* nothing to do */
353     }
354
355     // NOTE: The order of bitmap constructor and camera connection is important for android 4.1.x
356     // Bitmap must be constructed before surface
357     private void onEnterStartedState() {
358         Log.d(TAG, "call onEnterStartedState");
359         /* Connect camera */
360         if (!connectCamera(getWidth(), getHeight())) {
361             AlertDialog ad = new AlertDialog.Builder(getContext()).create();
362             ad.setCancelable(false); // This blocks the 'BACK' button
363             ad.setMessage("It seems that you device does not support camera (or it is locked). Application will be closed.");
364             ad.setButton(DialogInterface.BUTTON_NEUTRAL,  "OK", new DialogInterface.OnClickListener() {
365                 public void onClick(DialogInterface dialog, int which) {
366                     dialog.dismiss();
367                     ((Activity) getContext()).finish();
368                 }
369             });
370             ad.show();
371
372         }
373     }
374
375     private void onExitStartedState() {
376         disconnectCamera();
377         if (mCacheBitmap != null) {
378             mCacheBitmap.recycle();
379         }
380     }
381
382     /**
383      * This method shall be called by the subclasses when they have valid
384      * object and want it to be delivered to external client (via callback) and
385      * then displayed on the screen.
386      * @param frame - the current frame to be delivered
387      */
388     protected void deliverAndDrawFrame(CvCameraViewFrame frame) {
389         Mat modified;
390
391         if (mListener != null) {
392             modified = mListener.onCameraFrame(frame);
393         } else {
394             modified = frame.rgba();
395         }
396
397         boolean bmpValid = true;
398         if (modified != null) {
399             try {
400                 Utils.matToBitmap(modified, mCacheBitmap);
401             } catch(Exception e) {
402                 Log.e(TAG, "Mat type: " + modified);
403                 Log.e(TAG, "Bitmap type: " + mCacheBitmap.getWidth() + "*" + mCacheBitmap.getHeight());
404                 Log.e(TAG, "Utils.matToBitmap() throws an exception: " + e.getMessage());
405                 bmpValid = false;
406             }
407         }
408
409         if (bmpValid && mCacheBitmap != null) {
410             Canvas canvas = getHolder().lockCanvas();
411             if (canvas != null) {
412                 canvas.drawColor(0, android.graphics.PorterDuff.Mode.CLEAR);
413                 if (BuildConfig.DEBUG)
414                     Log.d(TAG, "mStretch value: " + mScale);
415
416                 if (mScale != 0) {
417                     canvas.drawBitmap(mCacheBitmap, new Rect(0,0,mCacheBitmap.getWidth(), mCacheBitmap.getHeight()),
418                          new Rect((int)((canvas.getWidth() - mScale*mCacheBitmap.getWidth()) / 2),
419                          (int)((canvas.getHeight() - mScale*mCacheBitmap.getHeight()) / 2),
420                          (int)((canvas.getWidth() - mScale*mCacheBitmap.getWidth()) / 2 + mScale*mCacheBitmap.getWidth()),
421                          (int)((canvas.getHeight() - mScale*mCacheBitmap.getHeight()) / 2 + mScale*mCacheBitmap.getHeight())), null);
422                 } else {
423                      canvas.drawBitmap(mCacheBitmap, new Rect(0,0,mCacheBitmap.getWidth(), mCacheBitmap.getHeight()),
424                          new Rect((canvas.getWidth() - mCacheBitmap.getWidth()) / 2,
425                          (canvas.getHeight() - mCacheBitmap.getHeight()) / 2,
426                          (canvas.getWidth() - mCacheBitmap.getWidth()) / 2 + mCacheBitmap.getWidth(),
427                          (canvas.getHeight() - mCacheBitmap.getHeight()) / 2 + mCacheBitmap.getHeight()), null);
428                 }
429
430                 if (mFpsMeter != null) {
431                     mFpsMeter.measure();
432                     mFpsMeter.draw(canvas, 20, 30);
433                 }
434                 getHolder().unlockCanvasAndPost(canvas);
435             }
436         }
437     }
438
439     /**
440      * This method is invoked shall perform concrete operation to initialize the camera.
441      * CONTRACT: as a result of this method variables mFrameWidth and mFrameHeight MUST be
442      * initialized with the size of the Camera frames that will be delivered to external processor.
443      * @param width - the width of this SurfaceView
444      * @param height - the height of this SurfaceView
445      */
446     protected abstract boolean connectCamera(int width, int height);
447
448     /**
449      * Disconnects and release the particular camera object being connected to this surface view.
450      * Called when syncObject lock is held
451      */
452     protected abstract void disconnectCamera();
453
454     // NOTE: On Android 4.1.x the function must be called before SurfaceTexture constructor!
455     protected void AllocateCache()
456     {
457         mCacheBitmap = Bitmap.createBitmap(mFrameWidth, mFrameHeight, Bitmap.Config.ARGB_8888);
458     }
459
460     public interface ListItemAccessor {
461         public int getWidth(Object obj);
462         public int getHeight(Object obj);
463     };
464
465     /**
466      * This helper method can be called by subclasses to select camera preview size.
467      * It goes over the list of the supported preview sizes and selects the maximum one which
468      * fits both values set via setMaxFrameSize() and surface frame allocated for this view
469      * @param supportedSizes
470      * @param surfaceWidth
471      * @param surfaceHeight
472      * @return optimal frame size
473      */
474     protected Size calculateCameraFrameSize(List<?> supportedSizes, ListItemAccessor accessor, int surfaceWidth, int surfaceHeight) {
475         int calcWidth = 0;
476         int calcHeight = 0;
477
478         int maxAllowedWidth = (mMaxWidth != MAX_UNSPECIFIED && mMaxWidth < surfaceWidth)? mMaxWidth : surfaceWidth;
479         int maxAllowedHeight = (mMaxHeight != MAX_UNSPECIFIED && mMaxHeight < surfaceHeight)? mMaxHeight : surfaceHeight;
480
481         for (Object size : supportedSizes) {
482             int width = accessor.getWidth(size);
483             int height = accessor.getHeight(size);
484
485             if (width <= maxAllowedWidth && height <= maxAllowedHeight) {
486                 if (width >= calcWidth && height >= calcHeight) {
487                     calcWidth = (int) width;
488                     calcHeight = (int) height;
489                 }
490             }
491         }
492
493         return new Size(calcWidth, calcHeight);
494     }
495 }