@carviz/capacitor-camera-preview 7.2.3 → 7.2.4

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.
@@ -94,7 +94,7 @@ class CameraController: NSObject {
94
94
 
95
95
  // Configure video data output for sample capture
96
96
  self.videoDataOutput.setSampleBufferDelegate(self, queue: DispatchQueue(label: "videoQueue"))
97
- self.videoDataOutput.videoSettings = [kCVPixelBufferPixelFormatTypeKey as String: Int(kCVPixelFormatType_420YpCbCr8BiPlanarFullRange)]
97
+ self.videoDataOutput.videoSettings = [kCVPixelBufferPixelFormatTypeKey as String: Int(kCVPixelFormatType_32BGRA)]
98
98
  if captureSession.canAddOutput(self.videoDataOutput) {
99
99
  captureSession.addOutput(self.videoDataOutput)
100
100
 
@@ -311,41 +311,8 @@ class CameraController: NSObject {
311
311
  return
312
312
  }
313
313
 
314
- // Capture the preview layer content directly - this mimics what the user sees
315
- DispatchQueue.main.async {
316
- guard let image = self.capturePreviewLayerAsImage() else {
317
- completion(nil, CameraControllerError.unknown)
318
- return
319
- }
320
-
321
- // Apply the same orientation fix as the regular capture function
322
- let orientationFixedImage = image.fixedOrientation() ?? image
323
- completion(orientationFixedImage, nil)
324
- }
325
- }
326
-
327
- private func capturePreviewLayerAsImage() -> UIImage? {
328
- // Ensure we're on the main thread
329
- assert(Thread.isMainThread)
330
-
331
- // Get the preview layer bounds
332
- let bounds = previewLayer.bounds
333
-
334
- // Create a graphics context
335
- UIGraphicsBeginImageContextWithOptions(bounds.size, false, UIScreen.main.scale)
336
- guard let context = UIGraphicsGetCurrentContext() else {
337
- UIGraphicsEndImageContext()
338
- return nil
339
- }
340
-
341
- // Render the preview layer into the context
342
- previewLayer.render(in: context)
343
-
344
- // Get the image from the context
345
- let image = UIGraphicsGetImageFromCurrentImageContext()
346
- UIGraphicsEndImageContext()
347
-
348
- return image
314
+ // Store the completion block to be called when we receive the next video frame
315
+ self.sampleBufferCaptureCompletionBlock = completion
349
316
  }
350
317
 
351
318
  func getSupportedFlashModes() throws -> [String] {
@@ -571,8 +538,45 @@ extension CameraController: AVCapturePhotoCaptureDelegate {
571
538
 
572
539
  extension CameraController: AVCaptureVideoDataOutputSampleBufferDelegate {
573
540
  func captureOutput(_ output: AVCaptureOutput, didOutput sampleBuffer: CMSampleBuffer, from connection: AVCaptureConnection) {
574
- // We don't actually need to process video frames anymore since we're using preview layer capture
575
- // This method is kept for compatibility but does nothing
541
+ // Only process if we have a pending sample capture request
542
+ guard let completion = self.sampleBufferCaptureCompletionBlock else { return }
543
+
544
+ // Clear the completion block to avoid capturing multiple frames for one request
545
+ self.sampleBufferCaptureCompletionBlock = nil
546
+
547
+ // Convert sample buffer to UIImage using a simple, reliable approach
548
+ guard let image = self.imageFromSampleBuffer(sampleBuffer) else {
549
+ DispatchQueue.main.async {
550
+ completion(nil, CameraControllerError.unknown)
551
+ }
552
+ return
553
+ }
554
+
555
+ // Call completion on main queue
556
+ DispatchQueue.main.async {
557
+ completion(image, nil)
558
+ }
559
+ }
560
+
561
+ private func imageFromSampleBuffer(_ sampleBuffer: CMSampleBuffer) -> UIImage? {
562
+ guard let pixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer) else {
563
+ return nil
564
+ }
565
+
566
+ // Create CIImage from pixel buffer - this preserves the full frame
567
+ let ciImage = CIImage(cvPixelBuffer: pixelBuffer)
568
+
569
+ // Use CIContext to render the image
570
+ let context = CIContext(options: [.useSoftwareRenderer: false])
571
+
572
+ // Create CGImage from the FULL extent of the CIImage
573
+ guard let cgImage = context.createCGImage(ciImage, from: ciImage.extent) else {
574
+ return nil
575
+ }
576
+
577
+ // Create UIImage and apply orientation fix like the capture function does
578
+ let image = UIImage(cgImage: cgImage, scale: 1.0, orientation: .up)
579
+ return image.fixedOrientation()
576
580
  }
577
581
  }
578
582
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@carviz/capacitor-camera-preview",
3
- "version": "7.2.3",
3
+ "version": "7.2.4",
4
4
  "description": "Fork of the capacitor-community/camera-preview plugin focusing on high resolution photos without bloating up the code.",
5
5
  "type": "module",
6
6
  "main": "dist/plugin.cjs.js",