1616import android .media .ExifInterface ;
1717import android .media .MediaActionSound ;
1818import android .net .Uri ;
19+ import android .os .Build ;
1920import android .os .Environment ;
2021import android .os .Handler ;
2122import android .os .HandlerThread ;
@@ -237,22 +238,24 @@ public void initOpenCv(Context context) {
237238
238239 Display display = mActivity .getWindowManager ().getDefaultDisplay ();
239240 android .graphics .Point size = new android .graphics .Point ();
240- display .getRealSize (size );
241+ if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .JELLY_BEAN_MR1 ) {
242+ display .getRealSize (size );
243+ }
241244
242245 BaseLoaderCallback mLoaderCallback = new BaseLoaderCallback (context ) {
243246 @ Override
244247 public void onManagerConnected (int status ) {
245248 switch (status ) {
246- case LoaderCallbackInterface .SUCCESS : {
247- Log .d (TAG , "SUCCESS init openCV: " + status );
248- // System.loadLibrary("ndklibrarysample");
249- enableCameraView ();
250- }
249+ case LoaderCallbackInterface .SUCCESS : {
250+ Log .d (TAG , "SUCCESS init openCV: " + status );
251+ // System.loadLibrary("ndklibrarysample");
252+ enableCameraView ();
253+ }
251254 break ;
252- default : {
253- Log .d (TAG , "ERROR init Opencv: " + status );
254- super .onManagerConnected (status );
255- }
255+ default : {
256+ Log .d (TAG , "ERROR init Opencv: " + status );
257+ super .onManagerConnected (status );
258+ }
256259 break ;
257260 }
258261 }
@@ -281,16 +284,11 @@ public HUDCanvasView getHUD() {
281284 }
282285
283286 private boolean imageProcessorBusy = true ;
284- private boolean attemptToFocus = false ;
285287
286288 public void setImageProcessorBusy (boolean imageProcessorBusy ) {
287289 this .imageProcessorBusy = imageProcessorBusy ;
288290 }
289291
290- public void setAttemptToFocus (boolean attemptToFocus ) {
291- this .attemptToFocus = attemptToFocus ;
292- }
293-
294292 public boolean isFocused () {
295293 return this .mFocused ;
296294 }
@@ -391,15 +389,17 @@ public void surfaceCreated(SurfaceHolder holder) {
391389 Camera .Parameters param ;
392390 param = mCamera .getParameters ();
393391
394- Camera . Size pSize = getMaxPreviewResolution ();
392+ Size pSize = getMaxPreviewResolution ();
395393 param .setPreviewSize (pSize .width , pSize .height );
396394 param .setWhiteBalance (Camera .Parameters .WHITE_BALANCE_AUTO );
397395 float previewRatio = (float ) pSize .width / pSize .height ;
398396
399397 Display display = mActivity .getWindowManager ().getDefaultDisplay ();
400398 // Display display =
401399 android .graphics .Point size = new android .graphics .Point ();
402- display .getRealSize (size );
400+ if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .JELLY_BEAN_MR1 ) {
401+ display .getRealSize (size );
402+ }
403403
404404 int displayWidth = Math .min (size .y , size .x );
405405 int displayHeight = Math .max (size .y , size .x );
@@ -420,7 +420,7 @@ public void surfaceCreated(SurfaceHolder holder) {
420420 int hotAreaWidth = displayWidth / 4 ;
421421 int hotAreaHeight = previewHeight / 2 - hotAreaWidth ;
422422
423- Camera . Size maxRes = getMaxPictureResolution (previewRatio );
423+ Size maxRes = getMaxPictureResolution (previewRatio );
424424 if (maxRes != null ) {
425425 param .setPictureSize (maxRes .width , maxRes .height );
426426 Log .d (TAG , "max supported picture resolution: " + maxRes .width + "x" + maxRes .height );
@@ -548,14 +548,14 @@ public void sendImageProcessorMessage(String messageText, Object obj) {
548548 public void blinkScreenAndShutterSound () {
549549 AudioManager audio = (AudioManager ) mActivity .getSystemService (Context .AUDIO_SERVICE );
550550 switch (audio .getRingerMode ()) {
551- case AudioManager .RINGER_MODE_NORMAL :
552- MediaActionSound sound = new MediaActionSound ();
553- sound .play (MediaActionSound .SHUTTER_CLICK );
554- break ;
555- case AudioManager .RINGER_MODE_SILENT :
556- break ;
557- case AudioManager .RINGER_MODE_VIBRATE :
558- break ;
551+ case AudioManager .RINGER_MODE_NORMAL :
552+ MediaActionSound sound = new MediaActionSound ();
553+ sound .play (MediaActionSound .SHUTTER_CLICK );
554+ break ;
555+ case AudioManager .RINGER_MODE_SILENT :
556+ break ;
557+ case AudioManager .RINGER_MODE_VIBRATE :
558+ break ;
559559 }
560560 }
561561
@@ -597,25 +597,12 @@ public void run() {
597597 });
598598 }
599599
600- public boolean requestManualPicture () {
601- this .blinkScreenAndShutterSound ();
600+ public void requestManualPicture () {
602601 this .waitSpinnerVisible ();
603-
604- if (safeToTakePicture ) {
605-
606- safeToTakePicture = false ;
607-
608- try {
609- mCamera .takePicture (null , null , pCallback );
610- } catch (Exception e ) {
611- this .waitSpinnerInvisible ();
612- }
613- return true ;
614- }
615- return false ;
602+ this .requestPicture ();
616603 }
617604
618- public boolean requestPicture () {
605+ public void requestPicture () {
619606 PackageManager pm = mActivity .getPackageManager ();
620607 if (safeToTakePicture ) {
621608
@@ -626,31 +613,27 @@ public boolean requestPicture() {
626613 mCamera .autoFocus (new Camera .AutoFocusCallback () {
627614 @ Override
628615 public void onAutoFocus (boolean success , Camera camera ) {
616+ Log .d (TAG , "onAutoFocusSuccess: " + success );
629617 if (success ) {
630- mCamera .takePicture (null , null , pCallback );
631- blinkScreen ();
632- blinkScreenAndShutterSound ();
633- }
634- if (attemptToFocus ) {
635- return ;
618+ takePicture ();
636619 } else {
637- attemptToFocus = true ;
620+ onPictureFailed () ;
638621 }
639622 }
640623 });
641624 } else {
642- mCamera .takePicture (null , null , pCallback );
643- blinkScreen ();
644- blinkScreenAndShutterSound ();
625+ takePicture ();
645626 }
646627 } catch (Exception e ) {
647- waitSpinnerInvisible ();
648- } finally {
649- waitSpinnerInvisible ();
628+ onPictureFailed ();
650629 }
651- return true ;
652630 }
653- return false ;
631+ }
632+
633+ private void takePicture () {
634+ mCamera .takePicture (null , null , pCallback );
635+ blinkScreen ();
636+ blinkScreenAndShutterSound ();
654637 }
655638
656639 public String saveToDirectory (Mat doc ) {
@@ -792,7 +775,9 @@ public void run() {
792775
793776 Display display = mActivity .getWindowManager ().getDefaultDisplay ();
794777 android .graphics .Point size = new android .graphics .Point ();
795- display .getRealSize (size );
778+ if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .JELLY_BEAN_MR1 ) {
779+ display .getRealSize (size );
780+ }
796781
797782 int width = Math .min (size .x , size .y );
798783 int height = Math .max (size .x , size .y );
@@ -864,7 +849,7 @@ public void onAnimationStart(Animation animation) {
864849 public void onAnimationEnd (Animation animation ) {
865850 imageView .setVisibility (View .INVISIBLE );
866851 imageView .setImageBitmap (null );
867- OpenNoteCameraView . AnimationRunnable .this .bitmap .recycle ();
852+ AnimationRunnable .this .bitmap .recycle ();
868853 }
869854
870855 @ Override
@@ -989,6 +974,14 @@ public void onPictureTaken(byte[] data, Camera camera) {
989974 sendImageProcessorMessage ("pictureTaken" , mat );
990975 camera .cancelAutoFocus ();
991976 safeToTakePicture = true ;
977+ waitSpinnerInvisible ();
978+
979+ }
980+
981+ public void onPictureFailed () {
982+ mCamera .cancelAutoFocus ();
983+ safeToTakePicture = true ;
984+ waitSpinnerInvisible ();
992985
993986 }
994987
0 commit comments