17
17
18
18
import android .Manifest ;
19
19
import android .annotation .SuppressLint ;
20
+ import android .annotation .TargetApi ;
20
21
import android .content .Context ;
21
22
import android .graphics .ImageFormat ;
22
23
import android .graphics .SurfaceTexture ;
23
24
import android .hardware .Camera ;
24
25
import android .hardware .Camera .CameraInfo ;
25
26
import android .os .Build ;
26
27
import android .os .SystemClock ;
28
+ import android .support .annotation .Nullable ;
27
29
import android .support .annotation .RequiresPermission ;
28
30
import android .support .annotation .StringDef ;
29
31
import android .util .Log ;
37
39
import com .google .android .gms .vision .Frame ;
38
40
39
41
import java .io .IOException ;
42
+ import java .lang .Thread .State ;
40
43
import java .lang .annotation .Retention ;
41
44
import java .lang .annotation .RetentionPolicy ;
42
- import java .lang .Thread .State ;
43
45
import java .nio .ByteBuffer ;
44
46
import java .util .ArrayList ;
45
47
import java .util .HashMap ;
@@ -88,7 +90,7 @@ public class CameraSource {
88
90
* ratio is less than this tolerance, they are considered to be the same aspect ratio.
89
91
*/
90
92
private static final float ASPECT_RATIO_TOLERANCE = 0.01f ;
91
-
93
+
92
94
@ StringDef ({
93
95
Camera .Parameters .FOCUS_MODE_CONTINUOUS_PICTURE ,
94
96
Camera .Parameters .FOCUS_MODE_CONTINUOUS_VIDEO ,
@@ -100,7 +102,7 @@ public class CameraSource {
100
102
})
101
103
@ Retention (RetentionPolicy .SOURCE )
102
104
private @interface FocusMode {}
103
-
105
+
104
106
@ StringDef ({
105
107
Camera .Parameters .FLASH_MODE_ON ,
106
108
Camera .Parameters .FLASH_MODE_OFF ,
@@ -275,6 +277,41 @@ public interface PictureCallback {
275
277
void onPictureTaken (byte [] data );
276
278
}
277
279
280
+ /**
281
+ * Callback interface used to notify on completion of camera auto focus.
282
+ */
283
+ public interface AutoFocusCallback {
284
+ /**
285
+ * Called when the camera auto focus completes. If the camera
286
+ * does not support auto-focus and autoFocus is called,
287
+ * onAutoFocus will be called immediately with a fake value of
288
+ * <code>success</code> set to <code>true</code>.
289
+ * <p/>
290
+ * The auto-focus routine does not lock auto-exposure and auto-white
291
+ * balance after it completes.
292
+ *
293
+ * @param success true if focus was successful, false if otherwise
294
+ */
295
+ void onAutoFocus (boolean success );
296
+ }
297
+
298
+ /**
299
+ * Callback interface used to notify on auto focus start and stop.
300
+ * <p/>
301
+ * <p>This is only supported in continuous autofocus modes -- {@link
302
+ * Camera.Parameters#FOCUS_MODE_CONTINUOUS_VIDEO} and {@link
303
+ * Camera.Parameters#FOCUS_MODE_CONTINUOUS_PICTURE}. Applications can show
304
+ * autofocus animation based on this.</p>
305
+ */
306
+ public interface AutoFocusMoveCallback {
307
+ /**
308
+ * Called when the camera auto focus starts or stops.
309
+ *
310
+ * @param start true if focus starts to move, false if focus stops to move
311
+ */
312
+ void onAutoFocusMoving (boolean start );
313
+ }
314
+
278
315
//==============================================================================================
279
316
// Public
280
317
//==============================================================================================
@@ -464,6 +501,162 @@ public void takePicture(ShutterCallback shutter, PictureCallback jpeg) {
464
501
}
465
502
}
466
503
504
+ /**
505
+ * Gets the current focus mode setting.
506
+ *
507
+ * @return current focus mode. This value is null if the camera is not yet created. Applications should call {@link
508
+ * #autoFocus(AutoFocusCallback)} to start the focus if focus
509
+ * mode is FOCUS_MODE_AUTO or FOCUS_MODE_MACRO.
510
+ * @see Camera.Parameters#FOCUS_MODE_AUTO
511
+ * @see Camera.Parameters#FOCUS_MODE_INFINITY
512
+ * @see Camera.Parameters#FOCUS_MODE_MACRO
513
+ * @see Camera.Parameters#FOCUS_MODE_FIXED
514
+ * @see Camera.Parameters#FOCUS_MODE_EDOF
515
+ * @see Camera.Parameters#FOCUS_MODE_CONTINUOUS_VIDEO
516
+ * @see Camera.Parameters#FOCUS_MODE_CONTINUOUS_PICTURE
517
+ */
518
+ @ Nullable
519
+ @ FocusMode
520
+ public String getFocusMode () {
521
+ return mFocusMode ;
522
+ }
523
+
524
+ /**
525
+ * Sets the focus mode.
526
+ *
527
+ * @param mode the focus mode
528
+ * @return {@code true} if the focus mode is set, {@code false} otherwise
529
+ * @see #getFocusMode()
530
+ */
531
+ public boolean setFocusMode (@ FocusMode String mode ) {
532
+ synchronized (mCameraLock ) {
533
+ if (mCamera != null && mode != null ) {
534
+ Camera .Parameters parameters = mCamera .getParameters ();
535
+ if (parameters .getSupportedFocusModes ().contains (mode )) {
536
+ parameters .setFocusMode (mode );
537
+ mCamera .setParameters (parameters );
538
+ mFocusMode = mode ;
539
+ return true ;
540
+ }
541
+ }
542
+
543
+ return false ;
544
+ }
545
+ }
546
+
547
+ /**
548
+ * Gets the current flash mode setting.
549
+ *
550
+ * @return current flash mode. null if flash mode setting is not
551
+ * supported or the camera is not yet created.
552
+ * @see Camera.Parameters#FLASH_MODE_OFF
553
+ * @see Camera.Parameters#FLASH_MODE_AUTO
554
+ * @see Camera.Parameters#FLASH_MODE_ON
555
+ * @see Camera.Parameters#FLASH_MODE_RED_EYE
556
+ * @see Camera.Parameters#FLASH_MODE_TORCH
557
+ */
558
+ @ Nullable
559
+ @ FlashMode
560
+ public String getFlashMode () {
561
+ return mFlashMode ;
562
+ }
563
+
564
+ /**
565
+ * Sets the flash mode.
566
+ *
567
+ * @param mode flash mode.
568
+ * @return {@code true} if the flash mode is set, {@code false} otherwise
569
+ * @see #getFlashMode()
570
+ */
571
+ public boolean setFlashMode (@ FlashMode String mode ) {
572
+ synchronized (mCameraLock ) {
573
+ if (mCamera != null && mode != null ) {
574
+ Camera .Parameters parameters = mCamera .getParameters ();
575
+ if (parameters .getSupportedFlashModes ().contains (mode )) {
576
+ parameters .setFlashMode (mode );
577
+ mCamera .setParameters (parameters );
578
+ mFlashMode = mode ;
579
+ return true ;
580
+ }
581
+ }
582
+
583
+ return false ;
584
+ }
585
+ }
586
+
587
+ /**
588
+ * Starts camera auto-focus and registers a callback function to run when
589
+ * the camera is focused. This method is only valid when preview is active
590
+ * (between {@link #start()} or {@link #start(SurfaceHolder)} and before {@link #stop()} or {@link #release()}).
591
+ * <p/>
592
+ * <p>Callers should check
593
+ * {@link #getFocusMode()} to determine if
594
+ * this method should be called. If the camera does not support auto-focus,
595
+ * it is a no-op and {@link AutoFocusCallback#onAutoFocus(boolean)}
596
+ * callback will be called immediately.
597
+ * <p/>
598
+ * <p>If the current flash mode is not
599
+ * {@link Camera.Parameters#FLASH_MODE_OFF}, flash may be
600
+ * fired during auto-focus, depending on the driver and camera hardware.<p>
601
+ *
602
+ * @param cb the callback to run
603
+ * @see #cancelAutoFocus()
604
+ */
605
+ public void autoFocus (@ Nullable AutoFocusCallback cb ) {
606
+ synchronized (mCameraLock ) {
607
+ if (mCamera != null ) {
608
+ CameraAutoFocusCallback autoFocusCallback = null ;
609
+ if (cb != null ) {
610
+ autoFocusCallback = new CameraAutoFocusCallback ();
611
+ autoFocusCallback .mDelegate = cb ;
612
+ }
613
+ mCamera .autoFocus (autoFocusCallback );
614
+ }
615
+ }
616
+ }
617
+
618
+ /**
619
+ * Cancels any auto-focus function in progress.
620
+ * Whether or not auto-focus is currently in progress,
621
+ * this function will return the focus position to the default.
622
+ * If the camera does not support auto-focus, this is a no-op.
623
+ *
624
+ * @see #autoFocus(AutoFocusCallback)
625
+ */
626
+ public void cancelAutoFocus () {
627
+ synchronized (mCameraLock ) {
628
+ if (mCamera != null ) {
629
+ mCamera .cancelAutoFocus ();
630
+ }
631
+ }
632
+ }
633
+
634
+ /**
635
+ * Sets camera auto-focus move callback.
636
+ *
637
+ * @param cb the callback to run
638
+ * @return {@code true} if the operation is supported (i.e. from Jelly Bean), {@code false} otherwise
639
+ */
640
+ @ TargetApi (Build .VERSION_CODES .JELLY_BEAN )
641
+ public boolean setAutoFocusMoveCallback (@ Nullable AutoFocusMoveCallback cb ) {
642
+ if (Build .VERSION .SDK_INT < Build .VERSION_CODES .JELLY_BEAN ) {
643
+ return false ;
644
+ }
645
+
646
+ synchronized (mCameraLock ) {
647
+ if (mCamera != null ) {
648
+ CameraAutoFocusMoveCallback autoFocusMoveCallback = null ;
649
+ if (cb != null ) {
650
+ autoFocusMoveCallback = new CameraAutoFocusMoveCallback ();
651
+ autoFocusMoveCallback .mDelegate = cb ;
652
+ }
653
+ mCamera .setAutoFocusMoveCallback (autoFocusMoveCallback );
654
+ }
655
+ }
656
+
657
+ return true ;
658
+ }
659
+
467
660
//==============================================================================================
468
661
// Private
469
662
//==============================================================================================
@@ -508,6 +701,35 @@ public void onPictureTaken(byte[] data, Camera camera) {
508
701
}
509
702
}
510
703
704
+ /**
705
+ * Wraps the camera1 auto focus callback so that the deprecated API isn't exposed.
706
+ */
707
+ private class CameraAutoFocusCallback implements Camera .AutoFocusCallback {
708
+ private AutoFocusCallback mDelegate ;
709
+
710
+ @ Override
711
+ public void onAutoFocus (boolean success , Camera camera ) {
712
+ if (mDelegate != null ) {
713
+ mDelegate .onAutoFocus (success );
714
+ }
715
+ }
716
+ }
717
+
718
+ /**
719
+ * Wraps the camera1 auto focus move callback so that the deprecated API isn't exposed.
720
+ */
721
+ @ TargetApi (Build .VERSION_CODES .JELLY_BEAN )
722
+ private class CameraAutoFocusMoveCallback implements Camera .AutoFocusMoveCallback {
723
+ private AutoFocusMoveCallback mDelegate ;
724
+
725
+ @ Override
726
+ public void onAutoFocusMoving (boolean start , Camera camera ) {
727
+ if (mDelegate != null ) {
728
+ mDelegate .onAutoFocusMoving (start );
729
+ }
730
+ }
731
+ }
732
+
511
733
/**
512
734
* Opens the camera and applies the user settings.
513
735
*
@@ -553,6 +775,9 @@ private Camera createCamera() {
553
775
}
554
776
}
555
777
778
+ // setting mFocusMode to the one set in the params
779
+ mFocusMode = parameters .getFocusMode ();
780
+
556
781
if (mFlashMode != null ) {
557
782
if (parameters .getSupportedFlashModes ().contains (
558
783
mFlashMode )) {
@@ -562,6 +787,9 @@ private Camera createCamera() {
562
787
}
563
788
}
564
789
790
+ // setting mFlashMode to the one set in the params
791
+ mFlashMode = parameters .getFlashMode ();
792
+
565
793
camera .setParameters (parameters );
566
794
567
795
// Four frame buffers are needed for working with the camera:
0 commit comments