capacitor-community-multilens-camerapreview 7.1.0 → 7.1.1

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.
@@ -222,14 +222,22 @@ extension CameraController {
222
222
  }
223
223
 
224
224
  func switchCameras() throws {
225
- guard let currentCameraPosition = currentCameraPosition, let captureSession = self.captureSession, captureSession.isRunning else { throw CameraControllerError.captureSessionIsMissing }
225
+ guard let currentCameraPosition = currentCameraPosition, let captureSession = self.captureSession, captureSession.isRunning else {
226
+ throw CameraControllerError.captureSessionIsMissing
227
+ }
228
+
229
+ // Stop the capture session
230
+ captureSession.stopRunning()
226
231
 
227
232
  captureSession.beginConfiguration()
228
233
 
229
234
  func switchToFrontCamera() throws {
230
235
 
231
236
  guard let rearCameraInput = self.rearCameraInput, captureSession.inputs.contains(rearCameraInput),
232
- let frontCamera = self.frontCamera else { throw CameraControllerError.invalidOperation }
237
+ let frontCamera = self.frontCamera else {
238
+ captureSession.startRunning() // Restart before throwing
239
+ throw CameraControllerError.invalidOperation
240
+ }
233
241
 
234
242
  self.frontCameraInput = try AVCaptureDeviceInput(device: frontCamera)
235
243
 
@@ -240,6 +248,7 @@ extension CameraController {
240
248
 
241
249
  self.currentCameraPosition = .front
242
250
  } else {
251
+ captureSession.startRunning() // Restart before throwing
243
252
  throw CameraControllerError.invalidOperation
244
253
  }
245
254
  }
@@ -247,7 +256,10 @@ extension CameraController {
247
256
  func switchToRearCamera() throws {
248
257
 
249
258
  guard let frontCameraInput = self.frontCameraInput, captureSession.inputs.contains(frontCameraInput),
250
- let rearCamera = self.rearCamera else { throw CameraControllerError.invalidOperation }
259
+ let rearCamera = self.rearCamera else {
260
+ captureSession.startRunning() // Restart before throwing
261
+ throw CameraControllerError.invalidOperation
262
+ }
251
263
 
252
264
  self.rearCameraInput = try AVCaptureDeviceInput(device: rearCamera)
253
265
 
@@ -257,7 +269,10 @@ extension CameraController {
257
269
  captureSession.addInput(self.rearCameraInput!)
258
270
 
259
271
  self.currentCameraPosition = .rear
260
- } else { throw CameraControllerError.invalidOperation }
272
+ } else {
273
+ captureSession.startRunning() // Restart before throwing
274
+ throw CameraControllerError.invalidOperation
275
+ }
261
276
  }
262
277
 
263
278
  switch currentCameraPosition {
@@ -269,41 +284,73 @@ extension CameraController {
269
284
  }
270
285
 
271
286
  captureSession.commitConfiguration()
287
+
288
+ // Restart the capture session
289
+ captureSession.startRunning()
272
290
  }
273
291
  func setZoom(lens: String) throws {
274
292
  print(lens)
275
- guard let captureSession = self.captureSession, captureSession.isRunning else { throw CameraControllerError.captureSessionIsMissing }
293
+ guard let captureSession = self.captureSession, captureSession.isRunning else {
294
+ throw CameraControllerError.captureSessionIsMissing
295
+ }
296
+
297
+ // Stop the capture session
298
+ captureSession.stopRunning()
276
299
 
277
300
  captureSession.beginConfiguration()
278
301
 
279
- guard let rearCameraInput = self.rearCameraInput, captureSession.inputs.contains(rearCameraInput) else { throw CameraControllerError.invalidOperation }
302
+ guard let rearCameraInput = self.rearCameraInput, captureSession.inputs.contains(rearCameraInput) else {
303
+ captureSession.startRunning() // Restart before throwing
304
+ throw CameraControllerError.invalidOperation
305
+ }
280
306
 
281
- var session = AVCaptureDevice.DiscoverySession(deviceTypes: [.builtInWideAngleCamera,.builtInUltraWideCamera,.builtInTelephotoCamera], mediaType: AVMediaType.video, position: .unspecified)
282
- if(lens == "ultra"){
283
- session = AVCaptureDevice.DiscoverySession(deviceTypes: [.builtInWideAngleCamera,.builtInUltraWideCamera], mediaType: AVMediaType.video, position: .unspecified)
284
- } else if(lens == "wide"){
285
- session = AVCaptureDevice.DiscoverySession(deviceTypes: [.builtInWideAngleCamera], mediaType: AVMediaType.video, position: .unspecified)
286
- } else if(lens == "tele"){
287
- session = AVCaptureDevice.DiscoverySession(deviceTypes: [.builtInWideAngleCamera,.builtInTelephotoCamera], mediaType: AVMediaType.video, position: .unspecified)
288
- }
289
- let cameras = session.devices.compactMap { $0 }
290
- guard !cameras.isEmpty else { throw CameraControllerError.noCamerasAvailable }
307
+ var session = AVCaptureDevice.DiscoverySession(
308
+ deviceTypes: [.builtInWideAngleCamera, .builtInUltraWideCamera, .builtInTelephotoCamera],
309
+ mediaType: AVMediaType.video,
310
+ position: .unspecified
311
+ )
312
+
313
+ if lens == "ultra" {
314
+ session = AVCaptureDevice.DiscoverySession(
315
+ deviceTypes: [.builtInWideAngleCamera, .builtInUltraWideCamera],
316
+ mediaType: AVMediaType.video,
317
+ position: .unspecified
318
+ )
319
+ } else if lens == "wide" {
320
+ session = AVCaptureDevice.DiscoverySession(
321
+ deviceTypes: [.builtInWideAngleCamera],
322
+ mediaType: AVMediaType.video,
323
+ position: .unspecified
324
+ )
325
+ } else if lens == "tele" {
326
+ session = AVCaptureDevice.DiscoverySession(
327
+ deviceTypes: [.builtInWideAngleCamera, .builtInTelephotoCamera],
328
+ mediaType: AVMediaType.video,
329
+ position: .unspecified
330
+ )
331
+ }
332
+
333
+ let cameras = session.devices.compactMap { $0 }
334
+ guard !cameras.isEmpty else {
335
+ captureSession.startRunning() // Restart before throwing
336
+ throw CameraControllerError.noCamerasAvailable
337
+ }
291
338
 
292
- for camera in cameras {
293
- if camera.position == .front {
294
- self.frontCamera = camera
295
- }
339
+ for camera in cameras {
340
+ if camera.position == .front {
341
+ self.frontCamera = camera
342
+ }
296
343
 
297
- if camera.position == .back {
298
- self.rearCamera = camera
344
+ if camera.position == .back {
345
+ self.rearCamera = camera
299
346
 
300
- try camera.lockForConfiguration()
301
- if camera.isFocusModeSupported(.continuousAutoFocus){
302
- camera.focusMode = .continuousAutoFocus
303
- }
304
- camera.unlockForConfiguration()
347
+ try camera.lockForConfiguration()
348
+ if camera.isFocusModeSupported(.continuousAutoFocus) {
349
+ camera.focusMode = .continuousAutoFocus
305
350
  }
351
+ camera.unlockForConfiguration()
306
352
  }
353
+ }
307
354
 
308
355
  if let inputs = captureSession.inputs as? [AVCaptureDeviceInput] {
309
356
  for input in inputs {
@@ -314,25 +361,29 @@ extension CameraController {
314
361
 
315
362
  self.rearCameraInput = try AVCaptureDeviceInput(device: rearCamera!)
316
363
 
317
- //captureSession.removeInput(frontCameraInput)
318
-
319
364
  if captureSession.canAddInput(self.rearCameraInput!) {
320
365
  captureSession.addInput(self.rearCameraInput!)
321
366
  self.currentCameraPosition = .rear
322
367
  }
323
368
 
324
369
  do {
325
- guard let device = self.currentCameraPosition == .rear ? rearCamera : frontCamera else { return }
370
+ guard let device = self.currentCameraPosition == .rear ? rearCamera : frontCamera else {
371
+ captureSession.startRunning() // Restart before returning
372
+ return
373
+ }
326
374
 
327
375
  try device.lockForConfiguration()
328
376
  defer { device.unlockForConfiguration() }
329
377
 
330
- device.videoZoomFactor = 1.0
378
+ device.videoZoomFactor = 1.0
331
379
  zoomFactor = device.videoZoomFactor
332
380
 
333
381
  } catch {
334
382
  debugPrint(error)
335
383
  }
384
+
385
+ // Restart the capture session
386
+ captureSession.startRunning()
336
387
  }
337
388
 
338
389
  func captureImage(completion: @escaping (UIImage?, Error?) -> Void) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "capacitor-community-multilens-camerapreview",
3
- "version": "7.1.0",
3
+ "version": "7.1.1",
4
4
  "description": "fork of capacitor community camera preview with support for switchting lenses",
5
5
  "main": "dist/plugin.cjs.js",
6
6
  "module": "dist/esm/index.js",