@@ -688,10 +688,18 @@ class Camera2ApiManager(context: Context) {
688688 val builderInputSurface = this .builderInputSurface ? : return false
689689 try {
690690 if (supportedFocusModes.isNotEmpty()) {
691- // cancel any existing AF trigger
691+ // cancel any existing AF trigger and clear the regions set by tapToFocus.
692+ builderInputSurface.setTag(" " )
693+ val maxRegionsAf = characteristics.secureGet(CameraCharacteristics .CONTROL_MAX_REGIONS_AF ) ? : 0
694+ if (maxRegionsAf > 0 ) {
695+ val clearRect = MeteringRectangle (0 , 0 , 0 , 0 , MeteringRectangle .METERING_WEIGHT_DONT_CARE )
696+ builderInputSurface.set(CaptureRequest .CONTROL_AF_REGIONS , arrayOf(clearRect))
697+ }
692698 builderInputSurface.set(CaptureRequest .CONTROL_AF_TRIGGER , CameraMetadata .CONTROL_AF_TRIGGER_CANCEL )
693699 builderInputSurface.set(CaptureRequest .CONTROL_AF_MODE , CaptureRequest .CONTROL_AF_MODE_OFF )
694- applyRequest(builderInputSurface)
700+ runCatching {
701+ cameraCaptureSession?.capture(builderInputSurface.build(), null , null )
702+ }.onFailure { Log .e(TAG , " Error" , it) }
695703 // Prefer CONTINUOUS_VIDEO: same smooth AF as CONTINUOUS_PICTURE but defers
696704 // fine adjustments to not interrupt frame delivery (no dropped frames on focus hunt).
697705 if (supportedFocusModes.contains(CaptureRequest .CONTROL_AF_MODE_CONTINUOUS_VIDEO )) {
@@ -707,6 +715,7 @@ class Camera2ApiManager(context: Context) {
707715 builderInputSurface.set(CaptureRequest .CONTROL_AF_MODE , supportedFocusModes[0 ])
708716 isAutoFocusEnabled = false
709717 }
718+ builderInputSurface.set(CaptureRequest .CONTROL_AF_TRIGGER , CameraMetadata .CONTROL_AF_TRIGGER_IDLE )
710719 applyRequest(builderInputSurface)
711720 }
712721 result = isAutoFocusEnabled
@@ -718,23 +727,20 @@ class Camera2ApiManager(context: Context) {
718727 }
719728
720729 fun disableAutoFocus (): Boolean {
721- val result = false
722730 val characteristics = cameraCharacteristics ? : return false
723731 val builderInputSurface = this .builderInputSurface ? : return false
724732 val supportedFocusModes = characteristics.secureGet(CameraCharacteristics .CONTROL_AF_AVAILABLE_MODES ) ? : return false
725733 for (mode in supportedFocusModes) {
726- try {
734+ runCatching {
727735 if (mode == CaptureRequest .CONTROL_AF_MODE_OFF ) {
728736 builderInputSurface.set(CaptureRequest .CONTROL_AF_MODE , CaptureRequest .CONTROL_AF_MODE_OFF )
729737 applyRequest(builderInputSurface)
730738 isAutoFocusEnabled = false
731739 return true
732740 }
733- } catch (e: Exception ) {
734- Log .e(TAG , " Error" , e)
735- }
741+ }.onFailure { Log .e(TAG , " Error" , it) }
736742 }
737- return result
743+ return false
738744 }
739745
740746 fun enableFaceDetection (faceDetectorCallback : FaceDetectorCallback ? ): Boolean {
0 commit comments