capacitor-plugin-camera-forked 3.0.8 → 3.0.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/ios/Plugin/CameraPreviewPlugin.swift +218 -20
- package/package.json +1 -1
|
@@ -28,6 +28,9 @@ public class CameraPreviewPlugin: CAPPlugin, AVCaptureVideoDataOutputSampleBuffe
|
|
|
28
28
|
var lastValidOrientation = "portrait"
|
|
29
29
|
var focusView: UIView?
|
|
30
30
|
var isFocusAnimating = false
|
|
31
|
+
var focusCompletionTimer: Timer?
|
|
32
|
+
var lastFocusTime: Date = Date()
|
|
33
|
+
private let focusThrottleInterval: TimeInterval = 0.5
|
|
31
34
|
@objc func initialize(_ call: CAPPluginCall) {
|
|
32
35
|
// Initialize a camera view for previewing video.
|
|
33
36
|
DispatchQueue.main.sync {
|
|
@@ -152,6 +155,10 @@ public class CameraPreviewPlugin: CAPPlugin, AVCaptureVideoDataOutputSampleBuffe
|
|
|
152
155
|
// If the input can be added, add it to the session.
|
|
153
156
|
if self.captureSession.canAddInput(videoInput) {
|
|
154
157
|
self.captureSession.addInput(videoInput)
|
|
158
|
+
|
|
159
|
+
// Configure camera device for optimal focus performance
|
|
160
|
+
try self.configureCameraForOptimalFocus(device: videoDevice)
|
|
161
|
+
|
|
155
162
|
self.previewView.videoPreviewLayer.session = self.captureSession
|
|
156
163
|
self.previewView.videoPreviewLayer.videoGravity = AVLayerVideoGravity.resizeAspectFill
|
|
157
164
|
|
|
@@ -169,7 +176,12 @@ public class CameraPreviewPlugin: CAPPlugin, AVCaptureVideoDataOutputSampleBuffe
|
|
|
169
176
|
|
|
170
177
|
self.photoOutput = AVCapturePhotoOutput()
|
|
171
178
|
self.photoOutput.isHighResolutionCaptureEnabled = true
|
|
172
|
-
|
|
179
|
+
|
|
180
|
+
// Configure photo output for better focus
|
|
181
|
+
if #available(iOS 13.0, *) {
|
|
182
|
+
self.photoOutput.maxPhotoQualityPrioritization = .quality
|
|
183
|
+
}
|
|
184
|
+
|
|
173
185
|
if self.captureSession.canAddOutput(self.photoOutput) {
|
|
174
186
|
self.captureSession.addOutput(photoOutput)
|
|
175
187
|
}
|
|
@@ -195,13 +207,100 @@ public class CameraPreviewPlugin: CAPPlugin, AVCaptureVideoDataOutputSampleBuffe
|
|
|
195
207
|
print(error)
|
|
196
208
|
}
|
|
197
209
|
}
|
|
210
|
+
|
|
211
|
+
private func configureCameraForOptimalFocus(device: AVCaptureDevice) throws {
|
|
212
|
+
try device.lockForConfiguration()
|
|
213
|
+
|
|
214
|
+
// Set optimal focus mode for continuous operation
|
|
215
|
+
if device.isFocusModeSupported(.continuousAutoFocus) {
|
|
216
|
+
device.focusMode = .continuousAutoFocus
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
// Set optimal exposure mode
|
|
220
|
+
if device.isExposureModeSupported(.continuousAutoExposure) {
|
|
221
|
+
device.exposureMode = .continuousAutoExposure
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
// Enable subject area change monitoring for responsive focus
|
|
225
|
+
device.isSubjectAreaChangeMonitoringEnabled = true
|
|
226
|
+
|
|
227
|
+
// Configure white balance for better color accuracy
|
|
228
|
+
if device.isWhiteBalanceModeSupported(.continuousAutoWhiteBalance) {
|
|
229
|
+
device.whiteBalanceMode = .continuousAutoWhiteBalance
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
// Set focus range restriction if supported (helps with faster focus)
|
|
233
|
+
if device.isAutoFocusRangeRestrictionSupported {
|
|
234
|
+
device.autoFocusRangeRestriction = .none // Allow full range for versatility
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
// Enable smooth auto focus if available (iOS 7+)
|
|
238
|
+
if device.isSmoothAutoFocusSupported {
|
|
239
|
+
device.isSmoothAutoFocusEnabled = true
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
device.unlockForConfiguration()
|
|
243
|
+
}
|
|
244
|
+
|
|
198
245
|
func takePhotoWithAVFoundation(){
|
|
199
246
|
//self.captureSession.sessionPreset = AVCaptureSession.Preset.hd4K3840x2160
|
|
200
247
|
let photoSettings: AVCapturePhotoSettings
|
|
201
|
-
|
|
248
|
+
|
|
249
|
+
// Use HEIF format if available for better quality
|
|
250
|
+
if #available(iOS 11.0, *), self.photoOutput.availablePhotoCodecTypes.contains(.hevc) {
|
|
251
|
+
photoSettings = AVCapturePhotoSettings(format: [AVVideoCodecKey: AVVideoCodecType.hevc])
|
|
252
|
+
} else {
|
|
253
|
+
photoSettings = AVCapturePhotoSettings()
|
|
254
|
+
}
|
|
255
|
+
|
|
202
256
|
photoSettings.isHighResolutionPhotoEnabled = true
|
|
203
257
|
|
|
204
|
-
|
|
258
|
+
// Enable auto-focus before capture for better focus accuracy
|
|
259
|
+
if #available(iOS 11.0, *) {
|
|
260
|
+
photoSettings.photoQualityPrioritization = .quality
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
// Enable auto-focus and auto-exposure for optimal capture
|
|
264
|
+
if #available(iOS 14.1, *) {
|
|
265
|
+
photoSettings.isAutoContentAwareDistortionCorrectionEnabled = true
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
// Trigger focus before capture if the device supports it
|
|
269
|
+
let device = self.videoInput.device
|
|
270
|
+
if device.isFocusModeSupported(.autoFocus) {
|
|
271
|
+
do {
|
|
272
|
+
try device.lockForConfiguration()
|
|
273
|
+
|
|
274
|
+
// Temporarily switch to autoFocus for the photo capture
|
|
275
|
+
let previousFocusMode = device.focusMode
|
|
276
|
+
device.focusMode = .autoFocus
|
|
277
|
+
|
|
278
|
+
device.unlockForConfiguration()
|
|
279
|
+
|
|
280
|
+
// Wait a brief moment for focus to settle, then capture
|
|
281
|
+
DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
|
|
282
|
+
self.photoOutput.capturePhoto(with: photoSettings, delegate: self)
|
|
283
|
+
|
|
284
|
+
// Restore previous focus mode after a short delay
|
|
285
|
+
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
|
|
286
|
+
do {
|
|
287
|
+
try device.lockForConfiguration()
|
|
288
|
+
device.focusMode = previousFocusMode
|
|
289
|
+
device.unlockForConfiguration()
|
|
290
|
+
} catch {
|
|
291
|
+
print("Could not restore focus mode: \(error)")
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
} catch {
|
|
296
|
+
// If focus configuration fails, capture anyway
|
|
297
|
+
print("Could not configure focus for capture: \(error)")
|
|
298
|
+
self.photoOutput.capturePhoto(with: photoSettings, delegate: self)
|
|
299
|
+
}
|
|
300
|
+
} else {
|
|
301
|
+
// Capture immediately if auto focus isn't supported
|
|
302
|
+
self.photoOutput.capturePhoto(with: photoSettings, delegate: self)
|
|
303
|
+
}
|
|
205
304
|
}
|
|
206
305
|
|
|
207
306
|
public func photoOutput(_ output: AVCapturePhotoOutput, didFinishProcessingPhoto photo: AVCapturePhoto, error: Error?) {
|
|
@@ -545,6 +644,12 @@ public class CameraPreviewPlugin: CAPPlugin, AVCaptureVideoDataOutputSampleBuffe
|
|
|
545
644
|
if let x = call.getFloat("x"), let y = call.getFloat("y"),
|
|
546
645
|
x >= 0.0 && x <= 1.0, y >= 0.0 && y <= 1.0 {
|
|
547
646
|
let point = CGPoint(x: CGFloat(x), y: CGFloat(y))
|
|
647
|
+
|
|
648
|
+
// Check if focus is currently animating and reset if stuck
|
|
649
|
+
if isFocusAnimating {
|
|
650
|
+
resetFocusIfStuck()
|
|
651
|
+
}
|
|
652
|
+
|
|
548
653
|
focusWithPoint(point: point)
|
|
549
654
|
|
|
550
655
|
// Calculate the point in the preview layer's coordinate space
|
|
@@ -557,6 +662,35 @@ public class CameraPreviewPlugin: CAPPlugin, AVCaptureVideoDataOutputSampleBuffe
|
|
|
557
662
|
}
|
|
558
663
|
}
|
|
559
664
|
|
|
665
|
+
private func resetFocusIfStuck() {
|
|
666
|
+
// Remove any existing focus indicator
|
|
667
|
+
focusView?.removeFromSuperview()
|
|
668
|
+
focusCompletionTimer?.invalidate()
|
|
669
|
+
isFocusAnimating = false
|
|
670
|
+
|
|
671
|
+
// Reset focus to continuous mode
|
|
672
|
+
let device = self.videoInput.device
|
|
673
|
+
do {
|
|
674
|
+
try device.lockForConfiguration()
|
|
675
|
+
if device.isFocusModeSupported(.continuousAutoFocus) {
|
|
676
|
+
device.focusMode = .continuousAutoFocus
|
|
677
|
+
}
|
|
678
|
+
device.unlockForConfiguration()
|
|
679
|
+
} catch {
|
|
680
|
+
print("Could not reset focus: \(error)")
|
|
681
|
+
}
|
|
682
|
+
}
|
|
683
|
+
|
|
684
|
+
@objc func resetFocus(_ call: CAPPluginCall) {
|
|
685
|
+
resetFocusIfStuck()
|
|
686
|
+
|
|
687
|
+
// Reset to center focus
|
|
688
|
+
let centerPoint = CGPoint(x: 0.5, y: 0.5)
|
|
689
|
+
focusWithPoint(point: centerPoint)
|
|
690
|
+
|
|
691
|
+
call.resolve()
|
|
692
|
+
}
|
|
693
|
+
|
|
560
694
|
@objc func handleTapToFocus(_ gesture: UITapGestureRecognizer) {
|
|
561
695
|
let location = gesture.location(in: self.previewView)
|
|
562
696
|
let convertedPoint = self.previewView.videoPreviewLayer.captureDevicePointConverted(fromLayerPoint: location)
|
|
@@ -567,55 +701,114 @@ public class CameraPreviewPlugin: CAPPlugin, AVCaptureVideoDataOutputSampleBuffe
|
|
|
567
701
|
|
|
568
702
|
func focusWithPoint(point: CGPoint) {
|
|
569
703
|
let device = self.videoInput.device
|
|
704
|
+
|
|
705
|
+
let now = Date()
|
|
706
|
+
if now.timeIntervalSince(lastFocusTime) < focusThrottleInterval {
|
|
707
|
+
return
|
|
708
|
+
}
|
|
709
|
+
lastFocusTime = now
|
|
710
|
+
|
|
570
711
|
do {
|
|
571
712
|
try device.lockForConfiguration()
|
|
572
713
|
|
|
714
|
+
focusCompletionTimer?.invalidate()
|
|
715
|
+
|
|
573
716
|
if device.isFocusPointOfInterestSupported {
|
|
574
717
|
device.focusPointOfInterest = point
|
|
575
|
-
|
|
718
|
+
|
|
719
|
+
if device.isFocusModeSupported(.continuousAutoFocus) {
|
|
720
|
+
device.focusMode = .continuousAutoFocus
|
|
721
|
+
} else if device.isFocusModeSupported(.autoFocus) {
|
|
722
|
+
device.focusMode = .autoFocus
|
|
723
|
+
} else {
|
|
724
|
+
device.focusMode = .locked
|
|
725
|
+
}
|
|
726
|
+
|
|
727
|
+
if device.focusMode == .autoFocus {
|
|
728
|
+
NotificationCenter.default.addObserver(
|
|
729
|
+
self,
|
|
730
|
+
selector: #selector(subjectAreaDidChange),
|
|
731
|
+
name: .AVCaptureDeviceSubjectAreaDidChange,
|
|
732
|
+
object: device
|
|
733
|
+
)
|
|
734
|
+
}
|
|
576
735
|
}
|
|
577
736
|
|
|
578
737
|
if device.isExposurePointOfInterestSupported {
|
|
579
738
|
device.exposurePointOfInterest = point
|
|
580
|
-
|
|
739
|
+
|
|
740
|
+
if device.isExposureModeSupported(.continuousAutoExposure) {
|
|
741
|
+
device.exposureMode = .continuousAutoExposure
|
|
742
|
+
} else if device.isExposureModeSupported(.autoExpose) {
|
|
743
|
+
device.exposureMode = .autoExpose
|
|
744
|
+
}
|
|
581
745
|
}
|
|
582
746
|
|
|
747
|
+
device.isSubjectAreaChangeMonitoringEnabled = true
|
|
748
|
+
|
|
583
749
|
device.unlockForConfiguration()
|
|
584
750
|
|
|
751
|
+
focusCompletionTimer = Timer.scheduledTimer(withTimeInterval: 2.0, repeats: false) { [weak self] _ in
|
|
752
|
+
DispatchQueue.main.async {
|
|
753
|
+
self?.hideFocusIndicatorWithCompletion()
|
|
754
|
+
}
|
|
755
|
+
}
|
|
756
|
+
|
|
585
757
|
} catch {
|
|
586
758
|
print("Could not focus: \(error.localizedDescription)")
|
|
587
759
|
}
|
|
588
760
|
}
|
|
589
761
|
|
|
762
|
+
@objc private func subjectAreaDidChange(notification: NSNotification) {
|
|
763
|
+
DispatchQueue.main.async { [weak self] in
|
|
764
|
+
self?.hideFocusIndicatorWithCompletion()
|
|
765
|
+
}
|
|
766
|
+
|
|
767
|
+
NotificationCenter.default.removeObserver(self, name: .AVCaptureDeviceSubjectAreaDidChange, object: notification.object)
|
|
768
|
+
}
|
|
769
|
+
|
|
770
|
+
private func hideFocusIndicatorWithCompletion() {
|
|
771
|
+
guard let focusView = self.focusView else { return }
|
|
772
|
+
|
|
773
|
+
UIView.animate(withDuration: 0.2, animations: {
|
|
774
|
+
focusView.alpha = 0.0
|
|
775
|
+
focusView.transform = CGAffineTransform(scaleX: 0.8, y: 0.8)
|
|
776
|
+
}) { _ in
|
|
777
|
+
focusView.removeFromSuperview()
|
|
778
|
+
focusView.transform = CGAffineTransform.identity
|
|
779
|
+
self.isFocusAnimating = false
|
|
780
|
+
}
|
|
781
|
+
}
|
|
782
|
+
|
|
590
783
|
func showFocusView(at point: CGPoint) {
|
|
591
784
|
if isFocusAnimating {
|
|
592
785
|
self.focusView?.removeFromSuperview()
|
|
786
|
+
focusCompletionTimer?.invalidate()
|
|
593
787
|
}
|
|
594
788
|
|
|
595
|
-
// Create focus view if needed
|
|
789
|
+
// Create focus view if needed - but make it invisible
|
|
596
790
|
if self.focusView == nil {
|
|
597
|
-
self.focusView = UIView(frame: CGRect(x: 0, y: 0, width:
|
|
598
|
-
|
|
599
|
-
self.focusView?.layer.
|
|
791
|
+
self.focusView = UIView(frame: CGRect(x: 0, y: 0, width: 80, height: 80))
|
|
792
|
+
// Make the focus view completely transparent
|
|
793
|
+
self.focusView?.layer.borderColor = UIColor.clear.cgColor
|
|
794
|
+
self.focusView?.layer.borderWidth = 0.0
|
|
795
|
+
self.focusView?.layer.cornerRadius = 40
|
|
600
796
|
self.focusView?.backgroundColor = .clear
|
|
797
|
+
self.focusView?.alpha = 0.0
|
|
798
|
+
|
|
799
|
+
// Remove the inner circle to make it completely invisible
|
|
800
|
+
// No inner circle added
|
|
601
801
|
}
|
|
602
802
|
|
|
603
803
|
self.focusView?.center = point
|
|
604
|
-
self.focusView?.alpha = 0.0
|
|
804
|
+
self.focusView?.alpha = 0.0 // Keep invisible
|
|
805
|
+
self.focusView?.transform = CGAffineTransform.identity
|
|
605
806
|
self.previewView.addSubview(self.focusView!)
|
|
606
807
|
|
|
607
808
|
self.isFocusAnimating = true
|
|
608
809
|
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
}, completion: { _ in
|
|
612
|
-
UIView.animate(withDuration: 0.3, delay: 0.5, options: .curveEaseInOut, animations: {
|
|
613
|
-
self.focusView?.alpha = 0.0
|
|
614
|
-
}, completion: { _ in
|
|
615
|
-
self.focusView?.removeFromSuperview()
|
|
616
|
-
self.isFocusAnimating = false
|
|
617
|
-
})
|
|
618
|
-
})
|
|
810
|
+
// Skip the animation since the view is invisible
|
|
811
|
+
// Focus functionality still works, just no visual feedback
|
|
619
812
|
}
|
|
620
813
|
|
|
621
814
|
@objc func requestCameraPermission(_ call: CAPPluginCall) {
|
|
@@ -718,4 +911,9 @@ public class CameraPreviewPlugin: CAPPlugin, AVCaptureVideoDataOutputSampleBuffe
|
|
|
718
911
|
takePhotoWithAVFoundation()
|
|
719
912
|
}
|
|
720
913
|
|
|
914
|
+
deinit {
|
|
915
|
+
NotificationCenter.default.removeObserver(self)
|
|
916
|
+
focusCompletionTimer?.invalidate()
|
|
917
|
+
}
|
|
918
|
+
|
|
721
919
|
}
|
package/package.json
CHANGED