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

added code format to see type of code scanned #633

Merged
merged 8 commits into from
Apr 30, 2024
7 changes: 5 additions & 2 deletions android/src/main/java/com/rncamerakit/CKCamera.kt
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import kotlin.math.min
import android.graphics.Canvas
import android.graphics.Paint
import android.graphics.RectF
import com.google.mlkit.vision.barcode.common.Barcode

class RectOverlay constructor(context: Context) :
View(context) {
Expand Down Expand Up @@ -457,9 +458,11 @@ class CKCamera(context: ThemedReactContext) : FrameLayout(context), LifecycleObs
rectOverlay.drawRectBounds(focusRects)
}

private fun onBarcodeRead(barcodes: List<String>) {
private fun onBarcodeRead(barcodes: List<Barcode>) {
val event: WritableMap = Arguments.createMap()
event.putString("codeStringValue", barcodes.first())
event.putString("codeStringValue", barcodes.first().rawValue)
val codeFormat = CodeFormat.fromBarcodeType(barcodes.first().format);
event.putString("codeFormat",codeFormat.code );
currentContext.getJSModule(RCTEventEmitter::class.java).receiveEvent(
id,
"onReadCode",
Expand Down
56 changes: 56 additions & 0 deletions android/src/main/java/com/rncamerakit/CodeFormat.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package com.rncamerakit

import com.google.mlkit.vision.barcode.common.Barcode

enum class CodeFormat(val code: String) {
CODE_128("code-128"),
CODE_39("code-39"),
CODE_93("code-93"),
CODABAR("codabar"),
EAN_13("ean-13"),
EAN_8("ean-8"),
ITF("itf"),
UPC_E("upc-e"),
QR("qr"),
PDF_417("pdf-417"),
AZTEC("aztec"),
DATA_MATRIX("data-matrix"),
UNKNOWN("unknown");

fun toBarcodeType(): Int {
return when (this) {
CODE_128 -> Barcode.FORMAT_CODE_128
CODE_39 -> Barcode.FORMAT_CODE_39
CODE_93 -> Barcode.FORMAT_CODE_93
CODABAR -> Barcode.FORMAT_CODABAR
EAN_13 -> Barcode.FORMAT_EAN_13
EAN_8 -> Barcode.FORMAT_EAN_8
ITF -> Barcode.FORMAT_ITF
UPC_E -> Barcode.FORMAT_UPC_E
QR -> Barcode.FORMAT_QR_CODE
PDF_417 -> Barcode.FORMAT_PDF417
AZTEC -> Barcode.FORMAT_AZTEC
DATA_MATRIX -> Barcode.FORMAT_DATA_MATRIX
UNKNOWN -> -1 // Or any other default value you prefer
}
}

companion object {
fun fromBarcodeType(@Barcode.BarcodeFormat barcodeType: Int): CodeFormat =
when (barcodeType) {
Barcode.FORMAT_CODE_128 -> CODE_128
Barcode.FORMAT_CODE_39 -> CODE_39
Barcode.FORMAT_CODE_93 -> CODE_93
Barcode.FORMAT_CODABAR -> CODABAR
Barcode.FORMAT_EAN_13 -> EAN_13
Barcode.FORMAT_EAN_8 -> EAN_8
Barcode.FORMAT_ITF -> ITF
Barcode.FORMAT_UPC_E -> UPC_E
Barcode.FORMAT_QR_CODE -> QR
Barcode.FORMAT_PDF417 -> PDF_417
Barcode.FORMAT_AZTEC -> AZTEC
Barcode.FORMAT_DATA_MATRIX -> DATA_MATRIX
else -> UNKNOWN
}
}
}
7 changes: 4 additions & 3 deletions android/src/main/java/com/rncamerakit/QRCodeAnalyzer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ import androidx.camera.core.ExperimentalGetImage
import androidx.camera.core.ImageAnalysis
import androidx.camera.core.ImageProxy
import com.google.mlkit.vision.barcode.BarcodeScanning
import com.google.mlkit.vision.barcode.common.Barcode
import com.google.mlkit.vision.common.InputImage

class QRCodeAnalyzer (
private val onQRCodesDetected: (qrCodes: List<String>) -> Unit
private val onQRCodesDetected: (qrCodes: List<Barcode>) -> Unit
) : ImageAnalysis.Analyzer {
@SuppressLint("UnsafeExperimentalUsageError")
@ExperimentalGetImage
Expand All @@ -18,9 +19,9 @@ class QRCodeAnalyzer (
val scanner = BarcodeScanning.getClient()
scanner.process(inputImage)
.addOnSuccessListener { barcodes ->
val strBarcodes = mutableListOf<String>()
val strBarcodes = mutableListOf<Barcode>()
barcodes.forEach { barcode ->
strBarcodes.add(barcode.rawValue ?: return@forEach)
strBarcodes.add(barcode ?: return@forEach)
}
onQRCodesDetected(strBarcodes)
}
Expand Down
2 changes: 2 additions & 0 deletions example/src/BarcodeScreenExample.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,8 @@ const BarcodeExample = ({ onBack }: { onBack: () => void }) => {
Vibration.vibrate(100);
setBarcode(event.nativeEvent.codeStringValue);
console.log('barcode', event.nativeEvent.codeStringValue);
console.log('codeFormat', event.nativeEvent.codeFormat);

}}
/>
</View>
Expand Down
4 changes: 4 additions & 0 deletions ios/ReactNativeCameraKit.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
46C558CF2A4AAD7300C68BA0 /* FocusInterfaceView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 46C558CE2A4AAD7300C68BA0 /* FocusInterfaceView.swift */; };
46F30C012A3A859B000597F6 /* ScannerFrameView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 46F30C002A3A859B000597F6 /* ScannerFrameView.swift */; };
46F30C032A3ABB9D000597F6 /* ScannerInterfaceView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 46F30C022A3ABB9D000597F6 /* ScannerInterfaceView.swift */; };
B5C747452B35924D00C95030 /* CodeFormat.swift in Sources */ = {isa = PBXBuildFile; fileRef = B5C747442B35924D00C95030 /* CodeFormat.swift */; };
/* End PBXBuildFile section */

/* Begin PBXCopyFilesBuildPhase section */
Expand Down Expand Up @@ -55,6 +56,7 @@
46C558CE2A4AAD7300C68BA0 /* FocusInterfaceView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FocusInterfaceView.swift; sourceTree = "<group>"; };
46F30C002A3A859B000597F6 /* ScannerFrameView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ScannerFrameView.swift; sourceTree = "<group>"; };
46F30C022A3ABB9D000597F6 /* ScannerInterfaceView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ScannerInterfaceView.swift; sourceTree = "<group>"; };
B5C747442B35924D00C95030 /* CodeFormat.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CodeFormat.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -104,6 +106,7 @@
46C558CE2A4AAD7300C68BA0 /* FocusInterfaceView.swift */,
4620AA682A2BFDBC00BC8929 /* ReactNativeCameraKit-Bridging-Header.h */,
463096892A2C7D89002ABA1A /* ReactNativeCameraKit.h */,
B5C747442B35924D00C95030 /* CodeFormat.swift */,
);
path = ReactNativeCameraKit;
sourceTree = "<group>";
Expand Down Expand Up @@ -181,6 +184,7 @@
26550AF61CFC7086007FF2DF /* CKCameraManager.m in Sources */,
46C558CB2A4AAB3400C68BA0 /* CameraProtocol.swift in Sources */,
4620AA722A2C4FA500BC8929 /* CameraManager.swift in Sources */,
B5C747452B35924D00C95030 /* CodeFormat.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
7 changes: 4 additions & 3 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,8 +23,9 @@ protocol CameraProtocol: AnyObject, FocusInterfaceViewDelegate {
func zoomPinchChange(pinchScale: CGFloat)

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

func update(scannerFrameSize: CGRect?)

func capturePicture(onWillCapture: @escaping () -> Void,
Expand Down
22 changes: 13 additions & 9 deletions ios/ReactNativeCameraKit/CameraView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@
// scanner
private var lastBarcodeDetectedTime: TimeInterval = 0
private var scannerInterfaceView: ScannerInterfaceView
private var supportedBarcodeType: [AVMetadataObject.ObjectType] = [.upce, .code39, .code39Mod43,
.ean13, .ean8, .code93,
.code128, .pdf417, .qr,
.aztec, .dataMatrix, .interleaved2of5]
private var supportedBarcodeType: [CodeFormat] = {
return CodeFormat.allCases
}()

Check warning on line 26 in ios/ReactNativeCameraKit/CameraView.swift

View workflow job for this annotation

GitHub Actions / Lint iOS

Lines should not have trailing whitespace (trailing_whitespace)
// camera
private var ratioOverlayView: RatioOverlayView?

Expand Down Expand Up @@ -69,14 +69,14 @@
setupCamera()
}
}

private func setupCamera() {
if hasPropBeenSetup && hasPermissionBeenGranted && !hasCameraBeenSetup {
hasCameraBeenSetup = true
camera.setup(cameraType: cameraType, supportedBarcodeType: scanBarcode && onReadCode != nil ? supportedBarcodeType : [])
}
}


Check warning on line 79 in ios/ReactNativeCameraKit/CameraView.swift

View workflow job for this annotation

GitHub Actions / Lint iOS

Limit vertical whitespace to a single empty line; currently 2 (vertical_whitespace)
// MARK: Lifecycle

@available(*, unavailable)
Expand Down Expand Up @@ -186,10 +186,14 @@
// Scanner
if changedProps.contains("scanBarcode") || changedProps.contains("onReadCode") {
camera.isBarcodeScannerEnabled(scanBarcode,
supportedBarcodeType: supportedBarcodeType,
onBarcodeRead: { [weak self] barcode in self?.onBarcodeRead(barcode: barcode) })
supportedBarcodeTypes: supportedBarcodeType,
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 Expand Up @@ -330,7 +334,7 @@
return temporaryFileURL
}

private func onBarcodeRead(barcode: String) {
private func onBarcodeRead(barcode: String, codeFormat:CodeFormat) {

Check warning on line 337 in ios/ReactNativeCameraKit/CameraView.swift

View workflow job for this annotation

GitHub Actions / Lint iOS

Colons should be next to the identifier when specifying a type and next to the key in dictionary literals (colon)
// Throttle barcode detection
let now = Date.timeIntervalSinceReferenceDate
guard lastBarcodeDetectedTime + Double(scanThrottleDelay) / 1000 < now else {
Expand All @@ -339,7 +343,7 @@

lastBarcodeDetectedTime = now

onReadCode?(["codeStringValue": barcode])
onReadCode?(["codeStringValue": barcode,"codeFormat":codeFormat.rawValue])

Check warning on line 346 in ios/ReactNativeCameraKit/CameraView.swift

View workflow job for this annotation

GitHub Actions / Lint iOS

Colons should be next to the identifier when specifying a type and next to the key in dictionary literals (colon)

Check warning on line 346 in ios/ReactNativeCameraKit/CameraView.swift

View workflow job for this annotation

GitHub Actions / Lint iOS

There should be no space before and one after any comma (comma)
}

// MARK: - Gesture selectors
Expand Down
60 changes: 60 additions & 0 deletions ios/ReactNativeCameraKit/CodeFormat.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
//
// CodeFormat.swift
// ReactNativeCameraKit
//
// Created by Imdad on 2023-12-22.
//

import Foundation
import AVFoundation

enum CodeFormat: String, CaseIterable {
case code128 = "code-128"
case code39 = "code-39"
case code93 = "code-93"
case ean13 = "ean-13"
case ean8 = "ean-8"
case itf14 = "itf-14"
case upce = "upc-e"
case qr = "qr"

Check warning on line 19 in ios/ReactNativeCameraKit/CodeFormat.swift

View workflow job for this annotation

GitHub Actions / Lint iOS

Enum element name 'qr' should be between 3 and 40 characters long (identifier_name)
case pdf417 = "pdf-417"
case aztec = "aztec"
case dataMatrix = "data-matrix"
case unknown = "unknown"

// Convert from AVMetadataObject.ObjectType to CodeFormat
static func fromAVMetadataObjectType(_ type: AVMetadataObject.ObjectType) -> CodeFormat {

Check warning on line 26 in ios/ReactNativeCameraKit/CodeFormat.swift

View workflow job for this annotation

GitHub Actions / Lint iOS

Function should have complexity 10 or less; currently complexity is 12 (cyclomatic_complexity)
switch type {
case .code128: return .code128
case .code39: return .code39
case .code93: return .code93
case .ean13: return .ean13
case .ean8: return .ean8
case .itf14: return .itf14
case .upce: return .upce
case .qr: return .qr
case .pdf417: return .pdf417
case .aztec: return .aztec
case .dataMatrix: return .dataMatrix
default: return .unknown
}
}

// Convert from CodeFormat to AVMetadataObject.ObjectType
func toAVMetadataObjectType() -> AVMetadataObject.ObjectType {

Check warning on line 44 in ios/ReactNativeCameraKit/CodeFormat.swift

View workflow job for this annotation

GitHub Actions / Lint iOS

Function should have complexity 10 or less; currently complexity is 12 (cyclomatic_complexity)
switch self {
case .code128: return .code128
case .code39: return .code39
case .code93: return .code93
case .ean13: return .ean13
case .ean8: return .ean8
case .itf14: return .itf14
case .upce: return .upce
case .qr: return .qr
case .pdf417: return .pdf417
case .aztec: return .aztec
case .dataMatrix: return .dataMatrix
case .unknown: return .init(rawValue: "unknown")
}
}
}
24 changes: 15 additions & 9 deletions ios/ReactNativeCameraKit/RealCamera.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ class RealCamera: NSObject, CameraProtocol, AVCaptureMetadataOutputObjectsDelega
private var torchMode: TorchMode = .off
private var resetFocus: (() -> Void)?
private var focusFinished: (() -> Void)?
private var onBarcodeRead: ((_ barcode: String) -> Void)?
private var scannerFrameSize: CGRect?
private var onBarcodeRead: ((_ barcode: String,_ codeFormat : CodeFormat) -> Void)?
private var scannerFrameSize: CGRect? = nil
private var onOrientationChange: RCTDirectEventBlock?
private var onZoomCallback: RCTDirectEventBlock?
private var lastOnZoom: Double?
Expand Down Expand Up @@ -93,7 +93,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 @@ -340,14 +340,15 @@ class RealCamera: NSObject, CameraProtocol, AVCaptureMetadataOutputObjectsDelega
}

func isBarcodeScannerEnabled(_ isEnabled: Bool,
supportedBarcodeType: [AVMetadataObject.ObjectType],
onBarcodeRead: ((_ barcode: String) -> Void)?) {
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 @@ -396,8 +397,10 @@ class RealCamera: NSObject, CameraProtocol, AVCaptureMetadataOutputObjectsDelega
let codeStringValue = machineReadableCodeObject.stringValue else {
return
}
// Determine the barcode type and convert it to CodeFormat
let barcodeType = CodeFormat.fromAVMetadataObjectType(machineReadableCodeObject.type)

onBarcodeRead?(codeStringValue)
onBarcodeRead?(codeStringValue,barcodeType)
}

// MARK: - Private
Expand Down Expand Up @@ -453,7 +456,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 @@ -489,7 +492,10 @@ 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) }
let filteredTypes = supportedBarcodeType
.map { $0.toAVMetadataObjectType() }
.filter { availableTypes.contains($0) }

metadataOutput.metadataObjectTypes = filteredTypes
}

Expand Down
6 changes: 3 additions & 3 deletions ios/ReactNativeCameraKit/SimulatorCamera.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@

// 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 Expand Up @@ -162,8 +162,8 @@
}

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

Check warning on line 166 in ios/ReactNativeCameraKit/SimulatorCamera.swift

View workflow job for this annotation

GitHub Actions / Lint iOS

Colons should be next to the identifier when specifying a type and next to the key in dictionary literals (colon)

Check warning on line 166 in ios/ReactNativeCameraKit/SimulatorCamera.swift

View workflow job for this annotation

GitHub Actions / Lint iOS

There should be no space before and one after any comma (comma)
func update(scannerFrameSize: CGRect?) {}

func capturePicture(onWillCapture: @escaping () -> Void,
Expand Down
3 changes: 2 additions & 1 deletion src/Camera.d.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { CameraApi, FlashMode, FocusMode, ZoomMode, TorchMode, CameraType } from './types';
import { CameraApi, FlashMode, FocusMode, ZoomMode, TorchMode, CameraType, CodeFormat } from './types';
import { Orientation } from './index';

export type OnReadCodeData = {
nativeEvent: {
codeStringValue: string;
codeFormat: CodeFormat;
};
};

Expand Down
Loading
Loading