capacitor-camera-view 1.2.0 → 1.2.2

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/Package.swift CHANGED
@@ -10,7 +10,7 @@ let package = Package(
10
10
  targets: ["CameraViewPlugin"])
11
11
  ],
12
12
  dependencies: [
13
- .package(url: "https://github.com/ionic-team/capacitor-swift-pm.git", from: "7.2.0")
13
+ .package(url: "https://github.com/ionic-team/capacitor-swift-pm.git", from: "7.4.2")
14
14
  ],
15
15
  targets: [
16
16
  .target(
package/README.md CHANGED
@@ -150,6 +150,9 @@ This plugin supports real-time barcode detection directly from the live camera f
150
150
  * **Android:** Utilizes Google's [**ML Kit Barcode Scanning**](https://developers.google.com/ml-kit/vision/barcode-scanning).
151
151
  * **Web:** Uses the [**Barcode Detection API**](https://developer.mozilla.org/en-US/docs/Web/API/Barcode_Detection_API) where available in the browser.
152
152
 
153
+ > [!NOTE]
154
+ > **Web Support:** The Barcode Detection API is not supported in all browsers (e.g., Windows browsers). For full browser compatibility, consider using a polyfill such as [`@undecaf/barcode-detector-polyfill`](https://www.npmjs.com/package/@undecaf/barcode-detector-polyfill) or [`barcode-detector`](https://www.npmjs.com/package/barcode-detector).
155
+
153
156
  **Enabling Barcode Detection:**
154
157
  To enable this feature, set the `enableBarcodeDetection` option to `true` when calling the `start()` method:
155
158
 
@@ -19,6 +19,7 @@ import androidx.camera.core.ImageAnalysis
19
19
  import androidx.camera.core.ImageCapture
20
20
  import androidx.camera.core.ImageCaptureException
21
21
  import androidx.camera.core.ImageProxy
22
+ import androidx.camera.core.TorchState
22
23
  import androidx.camera.core.resolutionselector.AspectRatioStrategy
23
24
  import androidx.camera.core.resolutionselector.ResolutionSelector
24
25
  import androidx.camera.mlkit.vision.MlKitAnalyzer
@@ -55,7 +56,6 @@ class CameraView(plugin: Plugin) {
55
56
  // Camera state
56
57
  private var currentCameraSelector = CameraSelector.DEFAULT_BACK_CAMERA
57
58
  private var currentFlashMode: Int = ImageCapture.FLASH_MODE_OFF
58
- private var isTorchEnabled: Boolean = false
59
59
 
60
60
  // Plugin context
61
61
  private var lifecycleOwner: LifecycleOwner? = null
@@ -416,8 +416,16 @@ class CameraView(plugin: Plugin) {
416
416
  }
417
417
 
418
418
  /** Get the current torch mode */
419
- fun getTorchMode(): Boolean {
420
- return isTorchEnabled
419
+ fun getTorchMode(callback: (Boolean) -> Unit) {
420
+ mainHandler.post {
421
+ val cameraInfo = cameraController?.cameraInfo
422
+ ?: run {
423
+ callback(false)
424
+ return@post
425
+ }
426
+
427
+ callback(cameraInfo.torchState.value == TorchState.ON)
428
+ }
421
429
  }
422
430
 
423
431
  /** Set the torch mode */
@@ -437,7 +445,6 @@ class CameraView(plugin: Plugin) {
437
445
  }
438
446
 
439
447
  controller.cameraControl?.enableTorch(enabled)
440
- isTorchEnabled = enabled
441
448
  callback?.invoke(null)
442
449
  } catch (e: Exception) {
443
450
  callback?.invoke(e)
@@ -220,8 +220,7 @@ class CameraViewPlugin : Plugin() {
220
220
 
221
221
  @PluginMethod
222
222
  fun getTorchMode(call: PluginCall) {
223
- try {
224
- val enabled = implementation.getTorchMode()
223
+ implementation.getTorchMode { enabled ->
225
224
  call.resolve(JSObject().apply {
226
225
  put("enabled", enabled)
227
226
  put(
@@ -230,8 +229,6 @@ class CameraViewPlugin : Plugin() {
230
229
  if (enabled) 1.0f else 0.0f
231
230
  )
232
231
  })
233
- } catch (e: Exception) {
234
- call.reject("Failed to get torch mode: ${e.localizedMessage}", e)
235
232
  }
236
233
  }
237
234
 
@@ -29,10 +29,6 @@ internal let SUPPORTED_CAMERA_DEVICE_TYPES: [AVCaptureDevice.DeviceType] = [
29
29
  /// Currently selected flash mode.
30
30
  private var flashMode: AVCaptureDevice.FlashMode = .auto
31
31
 
32
- /// Currently selected torch mode and level.
33
- private var torchEnabled: Bool = false
34
- private var torchLevel: Float = 1.0
35
-
36
32
  /// Reference to the blur overlay view that is shown when switching to the triple camera in order to have a smooth transition
37
33
  private var blurOverlayView: UIVisualEffectView?
38
34
 
@@ -94,10 +90,20 @@ internal let SUPPORTED_CAMERA_DEVICE_TYPES: [AVCaptureDevice.DeviceType] = [
94
90
  completion: { error in
95
91
  if error != nil { completion(error) }
96
92
 
93
+ // Handle barcode detection after session is running
94
+ if configuration.enableBarcodeDetection {
95
+ do {
96
+ try self.enableBarcodeDetection()
97
+ } catch {
98
+ completion(error)
99
+ return
100
+ }
101
+ }
102
+
97
103
  // Complete already because the camera is ready to be used
98
- // We might asynchronously upgrade to a triple camera in the background if available and configured
99
104
  completion(nil)
100
105
 
106
+ // We might asynchronously upgrade to a triple camera in the background if available and configured
101
107
  if configuration.useTripleCameraIfAvailable {
102
108
  Task {
103
109
  await self.upgradeToTripleCameraIfAvailable()
@@ -248,7 +254,12 @@ internal let SUPPORTED_CAMERA_DEVICE_TYPES: [AVCaptureDevice.DeviceType] = [
248
254
  ///
249
255
  /// - Returns: A tuple containing the torch enabled state and level
250
256
  public func getTorchMode() -> (enabled: Bool, level: Float) {
251
- return (torchEnabled, torchLevel)
257
+ guard let camera = currentCameraDevice else { return (false, 0.0) }
258
+
259
+ let isEnabled = camera.torchMode == .on
260
+ let level = camera.torchLevel
261
+
262
+ return (isEnabled, level)
252
263
  }
253
264
 
254
265
  /// Sets the torch mode and level for the currently active camera device.
@@ -270,9 +281,6 @@ internal let SUPPORTED_CAMERA_DEVICE_TYPES: [AVCaptureDevice.DeviceType] = [
270
281
  } else {
271
282
  camera.torchMode = .off
272
283
  }
273
-
274
- torchEnabled = enabled && level > 0.0
275
- torchLevel = level
276
284
  } catch {
277
285
  throw CameraError.configurationFailed(error)
278
286
  }
@@ -361,10 +369,7 @@ internal let SUPPORTED_CAMERA_DEVICE_TYPES: [AVCaptureDevice.DeviceType] = [
361
369
  // Set up the video data output for snapshots
362
370
  try setupVideoDataOutput()
363
371
 
364
- // Setup metadata output for QR code scanning if enabled
365
- if configuration.enableBarcodeDetection {
366
- try setupMetadataOutput()
367
- } else {
372
+ if !configuration.enableBarcodeDetection {
368
373
  // Remove the metadata output in case it already existed for the
369
374
  // capture session
370
375
  removeMetadataOutput()
@@ -407,6 +412,26 @@ internal let SUPPORTED_CAMERA_DEVICE_TYPES: [AVCaptureDevice.DeviceType] = [
407
412
  }
408
413
  }
409
414
 
415
+
416
+ /// Enables barcode detection by adding metadata output to the running session.
417
+ /// Somehow adding the metadata output with the session not being started yet
418
+ /// caused issues on some devices (iPad 7th Gen) where the session would just
419
+ /// not start at all without an error being thrown. I haven't been able to
420
+ /// figure out the root cause of this but it might generally be a good idea
421
+ /// to only add the metadata output when the session is already running.
422
+ ///
423
+ /// - Throws: An error if the metadata output cannot be added.
424
+ private func enableBarcodeDetection() throws {
425
+ guard captureSession.isRunning else {
426
+ throw CameraError.sessionNotRunning
427
+ }
428
+
429
+ captureSession.beginConfiguration()
430
+ defer { captureSession.commitConfiguration() }
431
+
432
+ try setupMetadataOutput()
433
+ }
434
+
410
435
  /// Retrieve a list of a available camera devices
411
436
  ///
412
437
  /// - Returns: a list of all available camera devices.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "capacitor-camera-view",
3
- "version": "1.2.0",
3
+ "version": "1.2.2",
4
4
  "description": "A Capacitor plugin for embedding a live camera feed directly into your app.",
5
5
  "main": "dist/plugin.cjs.js",
6
6
  "module": "dist/esm/index.js",