Skip to content

Commit

Permalink
Replaced AVMetadataObject with CodeFormat in all files
Browse files Browse the repository at this point in the history
  • Loading branch information
android-imdad committed Feb 22, 2024
1 parent 4af9bac commit 6579ce3
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 11 deletions.
4 changes: 2 additions & 2 deletions ios/ReactNativeCameraKit/CameraProtocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import AVFoundation
protocol CameraProtocol: AnyObject, FocusInterfaceViewDelegate {
var previewView: UIView { get }

func setup(cameraType: CameraType, supportedBarcodeType: [AVMetadataObject.ObjectType])
func setup(cameraType: CameraType, supportedBarcodeType: [CodeFormat])
func cameraRemovedFromSuperview()

func update(torchMode: TorchMode)
Expand All @@ -23,7 +23,7 @@ protocol CameraProtocol: AnyObject, FocusInterfaceViewDelegate {
func zoomPinchChange(pinchScale: CGFloat)

func isBarcodeScannerEnabled(_ isEnabled: Bool,
supportedBarcodeTypes: [AVMetadataObject.ObjectType],
supportedBarcodeTypes: [CodeFormat],
onBarcodeRead: ((_ barcode: String, _ codeFormat: CodeFormat) -> Void)?)

func update(scannerFrameSize: CGRect?)
Expand Down
8 changes: 6 additions & 2 deletions ios/ReactNativeCameraKit/CameraView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,12 @@ class CameraView: UIView {
private func setupCamera() {
if (hasPropBeenSetup && hasPermissionBeenGranted && !hasCameraBeenSetup) {
hasCameraBeenSetup = true
camera.setup(cameraType: cameraType, supportedBarcodeType: scanBarcode && onReadCode != nil ? supportedBarcodeType : [])
let supportedFormats = supportedBarcodeType.map { CodeFormat.fromAVMetadataObjectType($0) }
camera.setup(cameraType: cameraType, supportedBarcodeType: scanBarcode && onReadCode != nil ? supportedFormats : [])
}
}


// MARK: Lifecycle

@available(*, unavailable)
Expand Down Expand Up @@ -184,14 +186,16 @@ class CameraView: UIView {

// Scanner
if changedProps.contains("scanBarcode") || changedProps.contains("onReadCode") {
let supportedFormats = supportedBarcodeType.map { CodeFormat.fromAVMetadataObjectType($0) }
camera.isBarcodeScannerEnabled(scanBarcode,
supportedBarcodeTypes: supportedBarcodeType,
supportedBarcodeTypes: supportedFormats,
onBarcodeRead: { [weak self] (barcode, codeFormat) in
self?.onBarcodeRead(barcode: barcode, codeFormat: codeFormat)
})
}



if changedProps.contains("showFrame") || changedProps.contains("scanBarcode") {
DispatchQueue.main.async {
self.scannerInterfaceView.isHidden = !self.showFrame
Expand Down
14 changes: 8 additions & 6 deletions ios/ReactNativeCameraKit/RealCamera.swift
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ class RealCamera: NSObject, CameraProtocol, AVCaptureMetadataOutputObjectsDelega

// MARK: - Public

func setup(cameraType: CameraType, supportedBarcodeType: [AVMetadataObject.ObjectType]) {
func setup(cameraType: CameraType, supportedBarcodeType: [CodeFormat]) {
DispatchQueue.main.async {
self.cameraPreview.session = self.session
self.cameraPreview.previewLayer.videoGravity = .resizeAspect
Expand Down Expand Up @@ -335,14 +335,15 @@ class RealCamera: NSObject, CameraProtocol, AVCaptureMetadataOutputObjectsDelega
}

func isBarcodeScannerEnabled(_ isEnabled: Bool,
supportedBarcodeTypes supportedBarcodeType: [AVMetadataObject.ObjectType],
supportedBarcodeTypes supportedBarcodeType: [CodeFormat],
onBarcodeRead: ((_ barcode: String,_ codeFormat:CodeFormat) -> Void)?) {
sessionQueue.async {
self.onBarcodeRead = onBarcodeRead
let newTypes: [AVMetadataObject.ObjectType]
if isEnabled && onBarcodeRead != nil {
let availableTypes = self.metadataOutput.availableMetadataObjectTypes
newTypes = supportedBarcodeType.filter { type in availableTypes.contains(type) }
newTypes = supportedBarcodeType.map { $0.toAVMetadataObjectType() }
.filter { availableTypes.contains($0) }
} else {
newTypes = []
}
Expand Down Expand Up @@ -447,7 +448,7 @@ class RealCamera: NSObject, CameraProtocol, AVCaptureMetadataOutputObjectsDelega
}

private func setupCaptureSession(cameraType: CameraType,
supportedBarcodeType: [AVMetadataObject.ObjectType]) -> SetupResult {
supportedBarcodeType: [CodeFormat]) -> SetupResult {
guard let videoDevice = self.getBestDevice(for: cameraType),
let videoDeviceInput = try? AVCaptureDeviceInput(device: videoDevice) else {
return .sessionConfigurationFailed
Expand Down Expand Up @@ -484,8 +485,9 @@ class RealCamera: NSObject, CameraProtocol, AVCaptureMetadataOutputObjectsDelega
metadataOutput.setMetadataObjectsDelegate(self, queue: DispatchQueue.main)

let availableTypes = self.metadataOutput.availableMetadataObjectTypes
let filteredTypes = supportedBarcodeType.filter { type in availableTypes.contains(type) }
metadataOutput.metadataObjectTypes = filteredTypes
let filteredTypes = supportedBarcodeType.filter { type in availableTypes.contains(type.toAVMetadataObjectType()) }

metadataOutput.metadataObjectTypes = filteredTypes.map { $0.toAVMetadataObjectType() }
}

session.commitConfiguration()
Expand Down
2 changes: 1 addition & 1 deletion ios/ReactNativeCameraKit/SimulatorCamera.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class SimulatorCamera: CameraProtocol {

// MARK: - Public

func setup(cameraType: CameraType, supportedBarcodeType: [AVMetadataObject.ObjectType]) {
func setup(cameraType: CameraType, supportedBarcodeType: [CodeFormat]) {
DispatchQueue.main.async {
self.mockPreview.cameraTypeLabel.text = "Camera type: \(cameraType)"
}
Expand Down

0 comments on commit 6579ce3

Please sign in to comment.