From 0804836c610eda560d5809bad30b787b71d5d68f Mon Sep 17 00:00:00 2001 From: Pedrum Golriz Date: Mon, 10 Jul 2023 13:33:00 -0400 Subject: [PATCH 1/3] CameraPreview Programatically Focus on point --- .../camera/preview/CameraPreview.java | 19 +++++++++++++++++++ ios/Plugin/CameraController.swift | 16 ++++++++++++++++ ios/Plugin/Plugin.swift | 18 ++++++++++++++++++ src/definitions.ts | 6 ++++++ 4 files changed, 59 insertions(+) diff --git a/android/src/main/java/com/ahm/capacitor/camera/preview/CameraPreview.java b/android/src/main/java/com/ahm/capacitor/camera/preview/CameraPreview.java index 784a15b6..650b043f 100644 --- a/android/src/main/java/com/ahm/capacitor/camera/preview/CameraPreview.java +++ b/android/src/main/java/com/ahm/capacitor/camera/preview/CameraPreview.java @@ -108,6 +108,25 @@ public void captureSample(PluginCall call) { fragment.takeSnapshot(quality); } + @PluginMethod + public void focusPoint(PluginCall call){ + if (this.hasCamera(call) == false) { + call.reject("Camera is not running"); + return; + } + Logger.debug("x:"+call.getInt("x")); + Logger.debug("y:"+call.getInt("y")); + fragment.setFocusArea(call.getInt("x"), call.getInt("y"), new Camera.AutoFocusCallback() { + public void onAutoFocus(boolean success, Camera camera) { + if (success) { + Logger.debug("onTouch:" + " setFocusArea() succeeded"); + } else { + Logger.debug("onTouch:" + " setFocusArea() did not suceed"); + } + } + }); + } + @PluginMethod public void stop(final PluginCall call) { bridge diff --git a/ios/Plugin/CameraController.swift b/ios/Plugin/CameraController.swift index a09e72a0..f2390538 100644 --- a/ios/Plugin/CameraController.swift +++ b/ios/Plugin/CameraController.swift @@ -279,6 +279,22 @@ extension CameraController { self.sampleBufferCaptureCompletionBlock = completion } + func focusPoint(x: Int, y: Int) throws{ + guard let device = self.currentCameraPosition == .rear ? rearCamera : frontCamera else { return } + do { + try device.lockForConfiguration() + let focusMode = AVCaptureDevice.FocusMode.continuousAutoFocus + if device.isFocusPointOfInterestSupported && device.isFocusModeSupported(focusMode) { + device.focusMode = AVCaptureDevice.FocusMode.locked; + device.focusPointOfInterest = CGPoint(x: CGFloat(x), y: CGFloat(y)) + device.focusMode = AVCaptureDevice.FocusMode.continuousAutoFocus; + } + device.unlockForConfiguration() + } catch { + debugPrint(error) + } + } + func getSupportedFlashModes() throws -> [String] { var currentCamera: AVCaptureDevice? switch currentCameraPosition { diff --git a/ios/Plugin/Plugin.swift b/ios/Plugin/Plugin.swift index 044a9e68..239f2762 100644 --- a/ios/Plugin/Plugin.swift +++ b/ios/Plugin/Plugin.swift @@ -288,4 +288,22 @@ public class CameraPreview: CAPPlugin { } } + @objc func focusPoint(_ call: CAPPluginCall){ + guard let x = call.getInt("x") else{ + call.reject("failed to set focuspoint, x is missing") + return + } + + guard let y = call.getInt("y") else{ + call.reject("failed to set focuspoint, x is missing") + return + } + do { + try self.cameraController.focusPoint(x: x, y: y) + call.resolve() + } catch { + call.reject("failed to set focus") + } + } + } diff --git a/src/definitions.ts b/src/definitions.ts index 1e4efe00..00f2bb86 100644 --- a/src/definitions.ts +++ b/src/definitions.ts @@ -71,4 +71,10 @@ export interface CameraPreviewPlugin { setFlashMode(options: { flashMode: CameraPreviewFlashMode | string }): Promise; flip(): Promise; setOpacity(options: CameraOpacityOptions): Promise<{}>; + focusPoint(options: CameraPreviewXY): void; } + +export interface CameraPreviewXY { + x?: number, + y?: number, +} \ No newline at end of file From 2e61d147378ac6943ff2547dfae5f82bf9d03d12 Mon Sep 17 00:00:00 2001 From: Phuc Nguyen Date: Tue, 23 Jan 2024 12:43:55 +0700 Subject: [PATCH 2/3] fix syntax error --- src/web.ts | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/web.ts b/src/web.ts index 5f9bf913..03d9f3e1 100644 --- a/src/web.ts +++ b/src/web.ts @@ -22,6 +22,9 @@ export class CameraPreviewWeb extends WebPlugin implements CameraPreviewPlugin { platforms: ['web'], }); } + focusPoint(): void { + throw new Error('Method not implemented.'); + } async start(options: CameraPreviewOptions): Promise<{}> { return new Promise(async (resolve, reject) => { From 61a5f91610e0ea8b608aaf019e3e1f47a07f0ed3 Mon Sep 17 00:00:00 2001 From: Phuc Nguyen Date: Tue, 23 Jan 2024 13:19:18 +0700 Subject: [PATCH 3/3] more syntax --- ios/Plugin/Plugin.m | 1 + 1 file changed, 1 insertion(+) diff --git a/ios/Plugin/Plugin.m b/ios/Plugin/Plugin.m index 06fb55e1..f33481e6 100644 --- a/ios/Plugin/Plugin.m +++ b/ios/Plugin/Plugin.m @@ -13,4 +13,5 @@ CAP_PLUGIN_METHOD(setFlashMode, CAPPluginReturnPromise); CAP_PLUGIN_METHOD(startRecordVideo, CAPPluginReturnPromise); CAP_PLUGIN_METHOD(stopRecordVideo, CAPPluginReturnPromise); + CAP_PLUGIN_METHOD(focusPoint, CAPPluginReturnPromise); )