From eb217121f91bd6253cb8a34e3ad3d7afaf0c13f3 Mon Sep 17 00:00:00 2001 From: Marc Rousavy Date: Fri, 12 Jul 2024 19:09:07 +0200 Subject: [PATCH] fix: Fix `Frame.isMirrored` (#3078) * fix: Fix `Frame.isMirrored` * Update Frame.m * fix: Infer `isMirrored` from true connection value --- package/ios/Core/CameraSession.swift | 6 +++--- package/ios/Core/CameraSessionDelegate.swift | 2 +- package/ios/FrameProcessors/Frame.h | 2 +- package/ios/FrameProcessors/Frame.m | 17 ++++------------- package/ios/React/CameraView.swift | 6 ++++-- 5 files changed, 13 insertions(+), 20 deletions(-) diff --git a/package/ios/Core/CameraSession.swift b/package/ios/Core/CameraSession.swift index a8d866030e..10b0f3399c 100644 --- a/package/ios/Core/CameraSession.swift +++ b/package/ios/Core/CameraSession.swift @@ -268,7 +268,7 @@ final class CameraSession: NSObject, AVCaptureVideoDataOutputSampleBufferDelegat public final func captureOutput(_ captureOutput: AVCaptureOutput, didOutput sampleBuffer: CMSampleBuffer, from connection: AVCaptureConnection) { switch captureOutput { case is AVCaptureVideoDataOutput: - onVideoFrame(sampleBuffer: sampleBuffer, orientation: connection.orientation) + onVideoFrame(sampleBuffer: sampleBuffer, orientation: connection.orientation, isMirrored: connection.isVideoMirrored) case is AVCaptureAudioDataOutput: onAudioFrame(sampleBuffer: sampleBuffer) default: @@ -276,7 +276,7 @@ final class CameraSession: NSObject, AVCaptureVideoDataOutputSampleBufferDelegat } } - private final func onVideoFrame(sampleBuffer: CMSampleBuffer, orientation: Orientation) { + private final func onVideoFrame(sampleBuffer: CMSampleBuffer, orientation: Orientation, isMirrored: Bool) { if let recordingSession { do { // Write the Video Buffer to the .mov/.mp4 file @@ -290,7 +290,7 @@ final class CameraSession: NSObject, AVCaptureVideoDataOutputSampleBufferDelegat if let delegate { // Call Frame Processor (delegate) for every Video Frame - delegate.onFrame(sampleBuffer: sampleBuffer, orientation: orientation) + delegate.onFrame(sampleBuffer: sampleBuffer, orientation: orientation, isMirrored: isMirrored) } } diff --git a/package/ios/Core/CameraSessionDelegate.swift b/package/ios/Core/CameraSessionDelegate.swift index 735cb3d677..07ec0a7410 100644 --- a/package/ios/Core/CameraSessionDelegate.swift +++ b/package/ios/Core/CameraSessionDelegate.swift @@ -44,7 +44,7 @@ protocol CameraSessionDelegate: AnyObject { /** Called for every frame (if video or frameProcessor is enabled) */ - func onFrame(sampleBuffer: CMSampleBuffer, orientation: Orientation) + func onFrame(sampleBuffer: CMSampleBuffer, orientation: Orientation, isMirrored: Bool) /** Called whenever a QR/Barcode has been scanned. Only if the CodeScanner Output is enabled */ diff --git a/package/ios/FrameProcessors/Frame.h b/package/ios/FrameProcessors/Frame.h index bef7246ed9..a925b8813a 100644 --- a/package/ios/FrameProcessors/Frame.h +++ b/package/ios/FrameProcessors/Frame.h @@ -16,7 +16,7 @@ NS_ASSUME_NONNULL_BEGIN @interface Frame : NSObject -- (instancetype)initWithBuffer:(CMSampleBufferRef)buffer orientation:(UIImageOrientation)orientation; +- (instancetype)initWithBuffer:(CMSampleBufferRef)buffer orientation:(UIImageOrientation)orientation isMirrored:(BOOL)isMirrored; - (instancetype)init NS_UNAVAILABLE; - (void)incrementRefCount; diff --git a/package/ios/FrameProcessors/Frame.m b/package/ios/FrameProcessors/Frame.m index d1ce86515e..fa8ac7ab5e 100644 --- a/package/ios/FrameProcessors/Frame.m +++ b/package/ios/FrameProcessors/Frame.m @@ -13,13 +13,15 @@ @implementation Frame { CMSampleBufferRef _Nonnull _buffer; UIImageOrientation _orientation; + BOOL _isMirrored; } -- (instancetype)initWithBuffer:(CMSampleBufferRef)buffer orientation:(UIImageOrientation)orientation { +- (instancetype)initWithBuffer:(CMSampleBufferRef)buffer orientation:(UIImageOrientation)orientation isMirrored:(BOOL)isMirrored { self = [super init]; if (self) { _buffer = buffer; _orientation = orientation; + _isMirrored = isMirrored; } return self; } @@ -61,18 +63,7 @@ - (NSString*)pixelFormat { } - (BOOL)isMirrored { - switch (_orientation) { - case UIImageOrientationUp: - case UIImageOrientationDown: - case UIImageOrientationLeft: - case UIImageOrientationRight: - return false; - case UIImageOrientationDownMirrored: - case UIImageOrientationUpMirrored: - case UIImageOrientationLeftMirrored: - case UIImageOrientationRightMirrored: - return true; - } + return _isMirrored; } - (BOOL)isValid { diff --git a/package/ios/React/CameraView.swift b/package/ios/React/CameraView.swift index bbd05ce922..6b855381ba 100644 --- a/package/ios/React/CameraView.swift +++ b/package/ios/React/CameraView.swift @@ -360,7 +360,7 @@ public final class CameraView: UIView, CameraSessionDelegate, PreviewViewDelegat ]) } - func onFrame(sampleBuffer: CMSampleBuffer, orientation: Orientation) { + func onFrame(sampleBuffer: CMSampleBuffer, orientation: Orientation, isMirrored: Bool) { // Update latest frame that can be used for snapshot capture latestVideoFrame = Snapshot(imageBuffer: sampleBuffer, orientation: orientation) @@ -370,7 +370,9 @@ public final class CameraView: UIView, CameraSessionDelegate, PreviewViewDelegat #if VISION_CAMERA_ENABLE_FRAME_PROCESSORS if let frameProcessor = frameProcessor { // Call Frame Processor - let frame = Frame(buffer: sampleBuffer, orientation: orientation.imageOrientation) + let frame = Frame(buffer: sampleBuffer, + orientation: orientation.imageOrientation, + isMirrored: isMirrored) frameProcessor.call(frame) } #endif