Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return height/width of capture on iOS #646

Merged
merged 1 commit into from
Apr 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion ios/ReactNativeCameraKit/CameraProtocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,6 @@ protocol CameraProtocol: AnyObject, FocusInterfaceViewDelegate {
func update(scannerFrameSize: CGRect?)

func capturePicture(onWillCapture: @escaping () -> Void,
onSuccess: @escaping (_ imageData: Data, _ thumbnailData: Data?) -> (),
onSuccess: @escaping (_ imageData: Data, _ thumbnailData: Data?, _ dimensions: CMVideoDimensions) -> (),
onError: @escaping (_ message: String) -> ())
}
13 changes: 10 additions & 3 deletions ios/ReactNativeCameraKit/CameraView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -242,9 +242,13 @@ class CameraView: UIView {
self?.camera.previewView.alpha = 1
})
}
}, onSuccess: { [weak self] imageData, thumbnailData in
}, onSuccess: { [weak self] imageData, thumbnailData, dimensions in
DispatchQueue.global(qos: .default).async {
self?.writeCaptured(imageData: imageData, thumbnailData: thumbnailData, onSuccess: onSuccess, onError: onError)
self?.writeCaptured(imageData: imageData,
thumbnailData: thumbnailData,
dimensions: dimensions,
onSuccess: onSuccess,
onError: onError)

self?.focusInterfaceView.resetFocus()
}
Expand Down Expand Up @@ -289,6 +293,7 @@ class CameraView: UIView {

private func writeCaptured(imageData: Data,
thumbnailData: Data?,
dimensions: CMVideoDimensions,
onSuccess: @escaping (_ imageObject: [String: Any]) -> (),
onError: @escaping (_ error: String) -> ()) {
do {
Expand All @@ -298,7 +303,9 @@ class CameraView: UIView {
"size": imageData.count,
"uri": temporaryImageFileURL.description,
"name": temporaryImageFileURL.lastPathComponent,
"thumb": ""
"thumb": "",
"height": dimensions.height,
"width": dimensions.width
])
} catch {
let errorMessage = "Error occurred while writing image data to a temporary file: \(error)"
Expand Down
6 changes: 3 additions & 3 deletions ios/ReactNativeCameraKit/PhotoCaptureDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ class PhotoCaptureDelegate: NSObject, AVCapturePhotoCaptureDelegate {
private(set) var requestedPhotoSettings: AVCapturePhotoSettings

private let onWillCapture: () -> Void
private let onCaptureSuccess: (_ uniqueID: Int64, _ imageData: Data, _ thumbnailData: Data?) -> Void
private let onCaptureSuccess: (_ uniqueID: Int64, _ imageData: Data, _ thumbnailData: Data?, _ dimensions: CMVideoDimensions) -> Void
private let onCaptureError: (_ uniqueID: Int64, _ message: String) -> Void

init(with requestedPhotoSettings: AVCapturePhotoSettings,
onWillCapture: @escaping () -> Void,
onCaptureSuccess: @escaping (_ uniqueID: Int64, _ imageData: Data, _ thumbnailData: Data?) -> Void,
onCaptureSuccess: @escaping (_ uniqueID: Int64, _ imageData: Data, _ thumbnailData: Data?, _ dimensions: CMVideoDimensions) -> Void,
onCaptureError: @escaping (_ uniqueID: Int64, _ errorMessage: String) -> Void) {
self.requestedPhotoSettings = requestedPhotoSettings
self.onWillCapture = onWillCapture
Expand Down Expand Up @@ -50,6 +50,6 @@ class PhotoCaptureDelegate: NSObject, AVCapturePhotoCaptureDelegate {
thumbnailData = uiImage.jpegData(compressionQuality: 0.7)
}

onCaptureSuccess(requestedPhotoSettings.uniqueID, imageData, thumbnailData)
onCaptureSuccess(requestedPhotoSettings.uniqueID, imageData, thumbnailData, photo.resolvedSettings.photoDimensions)
}
}
6 changes: 3 additions & 3 deletions ios/ReactNativeCameraKit/RealCamera.swift
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ class RealCamera: NSObject, CameraProtocol, AVCaptureMetadataOutputObjectsDelega
}

func capturePicture(onWillCapture: @escaping () -> Void,
onSuccess: @escaping (_ imageData: Data, _ thumbnailData: Data?) -> Void,
onSuccess: @escaping (_ imageData: Data, _ thumbnailData: Data?, _ dimensions: CMVideoDimensions) -> Void,
onError: @escaping (_ message: String) -> Void) {
/*
Retrieve the video preview layer's video orientation on the main queue before
Expand All @@ -317,10 +317,10 @@ class RealCamera: NSObject, CameraProtocol, AVCaptureMetadataOutputObjectsDelega
let photoCaptureDelegate = PhotoCaptureDelegate(
with: settings,
onWillCapture: onWillCapture,
onCaptureSuccess: { uniqueID, imageData, thumbnailData in
onCaptureSuccess: { uniqueID, imageData, thumbnailData, dimensions in
self.inProgressPhotoCaptureDelegates[uniqueID] = nil

onSuccess(imageData, thumbnailData)
onSuccess(imageData, thumbnailData, dimensions)
},
onCaptureError: { uniqueID, errorMessage in
self.inProgressPhotoCaptureDelegates[uniqueID] = nil
Expand Down
4 changes: 2 additions & 2 deletions ios/ReactNativeCameraKit/SimulatorCamera.swift
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ class SimulatorCamera: CameraProtocol {
func update(scannerFrameSize: CGRect?) {}

func capturePicture(onWillCapture: @escaping () -> Void,
onSuccess: @escaping (_ imageData: Data, _ thumbnailData: Data?) -> (),
onSuccess: @escaping (_ imageData: Data, _ thumbnailData: Data?, _ dimensions: CMVideoDimensions) -> (),
onError: @escaping (_ message: String) -> ()) {
onWillCapture()

Expand All @@ -180,7 +180,7 @@ class SimulatorCamera: CameraProtocol {
// Then switch to background thread
DispatchQueue.global(qos: .default).async {
if let imageData = previewSnapshot?.jpegData(compressionQuality: 0.85) {
onSuccess(imageData, nil)
onSuccess(imageData, nil, CMVideoDimensions(width: 480, height: 640))
} else {
onError("Failed to convert snapshot to JPEG data")
}
Expand Down
4 changes: 2 additions & 2 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ export type ZoomMode = 'on' | 'off';
export type CaptureData = {
uri: string;
name: string;
height: number;
width: number;
// Android only
id?: string;
path?: string;
height?: number;
width?: number;
// iOS only
size?: number;
};
Expand Down
Loading