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

Add resize mode cover/contain #644

Merged
merged 7 commits into from
Apr 30, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions ios/ReactNativeCameraKit/CKCameraManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ @interface RCT_EXTERN_MODULE(CKCameraManager, RCTViewManager)
RCT_EXPORT_VIEW_PROPERTY(torchMode, CKTorchMode)
RCT_EXPORT_VIEW_PROPERTY(ratioOverlay, NSString)
RCT_EXPORT_VIEW_PROPERTY(ratioOverlayColor, UIColor)
RCT_EXPORT_VIEW_PROPERTY(resizeMode, CKResizeMode)

RCT_EXPORT_VIEW_PROPERTY(scanBarcode, BOOL)
RCT_EXPORT_VIEW_PROPERTY(onReadCode, RCTDirectEventBlock)
Expand Down
5 changes: 5 additions & 0 deletions ios/ReactNativeCameraKit/CKTypes+RCTConvert.m
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,9 @@ @implementation RCTConvert (CKTypes)
@"off": @(CKZoomModeOff)
}), CKZoomModeOn, integerValue)

RCT_ENUM_CONVERTER(CKResizeMode, (@{
@"cover": @(CKResizeModeCover),
@"contain": @(CKResizeModeContain)
}), CKResizeModeCover, integerValue)

@end
1 change: 1 addition & 0 deletions ios/ReactNativeCameraKit/CameraProtocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ protocol CameraProtocol: AnyObject, FocusInterfaceViewDelegate {
func update(onZoom: RCTDirectEventBlock?)
func update(zoom: Double?)
func update(maxZoom: Double?)
func update(resizeMode: ResizeMode)

func zoomPinchStart()
func zoomPinchChange(pinchScale: CGFloat)
Expand Down
12 changes: 12 additions & 0 deletions ios/ReactNativeCameraKit/RealCamera.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class RealCamera: NSObject, CameraProtocol, AVCaptureMetadataOutputObjectsDelega
private let photoOutput = AVCapturePhotoOutput()
private let metadataOutput = AVCaptureMetadataOutput()

private var resizeMode: ResizeMode = .contain
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we do .contain here, and .cover in CameraView? Seems like we should have contain for all default values to keep things consistent (and say so in the doc)

private var flashMode: FlashMode = .auto
private var torchMode: TorchMode = .off
private var resetFocus: (() -> Void)?
Expand Down Expand Up @@ -291,6 +292,17 @@ class RealCamera: NSObject, CameraProtocol, AVCaptureMetadataOutputObjectsDelega
}
}

func update(resizeMode: ResizeMode) {
DispatchQueue.main.async {
switch resizeMode {
case .cover:
self.cameraPreview.previewLayer.videoGravity = .resizeAspectFill
case .contain:
self.cameraPreview.previewLayer.videoGravity = .resizeAspect
}
}
}

func capturePicture(onWillCapture: @escaping () -> Void,
onSuccess: @escaping (_ imageData: Data, _ thumbnailData: Data?) -> Void,
onError: @escaping (_ message: String) -> Void) {
Expand Down
2 changes: 2 additions & 0 deletions ios/ReactNativeCameraKit/SimulatorCamera.swift
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,8 @@ class SimulatorCamera: CameraProtocol {
}
}
}

func update(resizeMode: ResizeMode) {}


func isBarcodeScannerEnabled(_ isEnabled: Bool,
Expand Down
13 changes: 13 additions & 0 deletions ios/ReactNativeCameraKit/Types.swift
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,19 @@ public enum ZoomMode: Int, CustomStringConvertible {
}
}

@objc(CKResizeMode)
public enum ResizeMode: Int {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we make it consistent with the other ones, like the one above?

No = 0, implements CustomStringConvertible, description instead of stringValue

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lichstam what about this one?

case cover = 0
case contain = 1

public var stringValue: String {
switch self {
case .cover: return "cover"
case .contain: return "contain"
}
}
}

@objc(CKSetupResult)
enum SetupResult: Int {
case notStarted
Expand Down
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { NativeModules } from 'react-native';

import Camera from './Camera';
import { CameraApi, CameraType, CaptureData, FlashMode, FocusMode, TorchMode, ZoomMode } from './types';
import { CameraApi, CameraType, CaptureData, FlashMode, FocusMode, TorchMode, ZoomMode, ResizeMode } from './types';

const { CameraKit } = NativeModules;

Expand All @@ -15,4 +15,4 @@ export const Orientation = {

export default CameraKit;

export { Camera, CameraType, TorchMode, FlashMode, FocusMode, ZoomMode, CameraApi, CaptureData };
export { Camera, CameraType, TorchMode, FlashMode, FocusMode, ZoomMode, CameraApi, CaptureData, ResizeMode };
2 changes: 2 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ export type FocusMode = 'on' | 'off';

export type ZoomMode = 'on' | 'off';

export type ResizeMode = "cover" | "contain";

export type CaptureData = {
uri: string;
name: string;
Expand Down