Skip to content

Commit

Permalink
fix: Fix Frame.isMirrored (#3078)
Browse files Browse the repository at this point in the history
* fix: Fix `Frame.isMirrored`

* Update Frame.m

* fix: Infer `isMirrored` from true connection value
  • Loading branch information
mrousavy authored Jul 12, 2024
1 parent f39ca07 commit eb21712
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 20 deletions.
6 changes: 3 additions & 3 deletions package/ios/Core/CameraSession.swift
Original file line number Diff line number Diff line change
Expand Up @@ -268,15 +268,15 @@ 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:
break
}
}

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
Expand All @@ -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)
}
}

Expand Down
2 changes: 1 addition & 1 deletion package/ios/Core/CameraSessionDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
2 changes: 1 addition & 1 deletion package/ios/FrameProcessors/Frame.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
17 changes: 4 additions & 13 deletions package/ios/FrameProcessors/Frame.m
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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 {
Expand Down
6 changes: 4 additions & 2 deletions package/ios/React/CameraView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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
Expand Down

0 comments on commit eb21712

Please sign in to comment.