-
Notifications
You must be signed in to change notification settings - Fork 591
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
+172
−30
Merged
Changes from 5 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
6345b96
added code format to see type of code scanned
android-imdad e1c3097
Update ios/ReactNativeCameraKit/SimulatorCamera.swift
android-imdad 72e6547
Update android/src/main/java/com/rncamerakit/CodeFormat.kt
android-imdad 4af9bac
Added CodeFormat types and fixed an indentation on a function to matc…
android-imdad 6579ce3
Replaced AVMetadataObject with CodeFormat in all files
android-imdad 369dbd7
Updated code format to case Iterable and changed supportedBarcodeType…
android-imdad 89420f9
Update src/Camera.d.ts
scarlac 4249b37
Merge branch 'master' into added-code-format
scarlac File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { | ||
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" | ||
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 { | ||
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 { | ||
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") | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,7 +31,7 @@ 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 onBarcodeRead: ((_ barcode: String,_ codeFormat : CodeFormat) -> Void)? | ||
private var scannerFrameSize: CGRect? = nil | ||
private var onOrientationChange: RCTDirectEventBlock? | ||
private var onZoomCallback: RCTDirectEventBlock? | ||
|
@@ -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 | ||
|
@@ -335,14 +335,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 = [] | ||
} | ||
|
@@ -388,8 +389,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 | ||
|
@@ -445,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 | ||
|
@@ -482,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() } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We are doing
|
||
} | ||
|
||
session.commitConfiguration() | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can use
CodeFormat
forsupportedBarcodeType
line 23, so no need to map it hereThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh yes I should have just done that :D
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@DavidBertet check now