Skip to content

Commit

Permalink
resolved conflicts
Browse files Browse the repository at this point in the history
  • Loading branch information
gev2002 committed Aug 28, 2024
1 parent 1d4d52a commit ee637e2
Show file tree
Hide file tree
Showing 15 changed files with 365 additions and 209 deletions.
37 changes: 29 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ A plugin to scan barcodes using ML Kit Barcode Scanning

# 🚨 Required Modules

react-native-vision-camera => 4.0.0 <br />
react-native-worklets-core = 1.3.0
react-native-vision-camera => 4.5.2 <br />
react-native-worklets-core = 1.3.3

## 💻 Installation

Expand All @@ -17,7 +17,7 @@ yarn add react-native-vision-camera-barcodes-scanner
Works With React Native Vision Camera.
Works for Both Cameras.
Works Fast.
Works With Android 🤖 and IOS.📱
Works With Android 🤖 and iOS .
Writen With Kotlin and Swift.

## 💡 Usage
Expand All @@ -39,7 +39,7 @@ function App (){
device={device}
isActive
// optional
options={["qr"]}
options={["qr","data_matrix"]}
callback={(d) => setData(d)}
/>
)}
Expand All @@ -62,7 +62,7 @@ import { useBarcodeScanner } from "react-native-vision-camera-barcodes-scanner";

function App() {
const device = useCameraDevice('back');
const options = ["qr"]
const options = ["qr", "data_matrix"]
const {scanBarcodes} = useBarcodeScanner(options)
const frameProcessor = useFrameProcessor((frame) => {
'worklet'
Expand All @@ -84,13 +84,34 @@ function App() {
}
export default App;
```
## Scan By Image 📸

```js
import { ImageScanner } from "react-native-vision-camera-barcodes-scanner";

const result = await ImageScanner({
uri:assets.uri
})
console.log(result);

```

![Static Badge](https://img.shields.io/badge/-%25?style=&logo=typescript&color=rgba(0%2C0%2C0%2C0)) \
Type **`Options`** = aztec, code_128, code_39, code_93, codabar, ean_13,
ean_8, pdf_417, qr, upc_e, upc_a, itf, data_matrix, all

| Name | Type | Values | Required | Default |
|:-------:|:--------:|:-------:|:--------:|:-------:|
| uri | string | String | yes | - |
| options | string[] | Options | no | all |

---

## ⚙️ Options

| Name | Type | Values | Default |
| :---: | :---: | :---: | :---: |
| codeType | String | all, code-39, code-93, codabar, ean-13, ean-8, itf, upc-e, upc-a, qr, pdf-417, aztec, data-matrix, code-128 | all |
| Name | Type | Values | Default |
|:-------:|:--------:|:-------:|:-------:|
| options | string[] | Options | all |



Expand Down
3 changes: 2 additions & 1 deletion android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
implementation project(':react-native-vision-camera')
implementation "androidx.camera:camera-core:1.1.0"
implementation 'com.google.mlkit:barcode-scanning:17.2.0'
implementation 'com.google.mlkit:barcode-scanning:17.3.0'
implementation 'com.google.android.gms:play-services-mlkit-barcode-scanning:18.3.1'
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package com.visioncamerabarcodesscanner

import android.net.Uri
import com.facebook.react.bridge.Promise
import com.facebook.react.bridge.ReactApplicationContext
import com.facebook.react.bridge.ReactContextBaseJavaModule
import com.facebook.react.bridge.ReactMethod
import com.facebook.react.bridge.ReadableArray
import com.facebook.react.bridge.WritableNativeArray
import com.google.android.gms.tasks.Task
import com.google.android.gms.tasks.Tasks
import com.google.mlkit.vision.barcode.BarcodeScannerOptions
import com.google.mlkit.vision.barcode.BarcodeScanning
import com.google.mlkit.vision.barcode.common.Barcode
import com.google.mlkit.vision.barcode.common.Barcode.FORMAT_ALL_FORMATS
import com.google.mlkit.vision.barcode.common.Barcode.FORMAT_AZTEC
import com.google.mlkit.vision.barcode.common.Barcode.FORMAT_CODABAR
import com.google.mlkit.vision.barcode.common.Barcode.FORMAT_CODE_128
import com.google.mlkit.vision.barcode.common.Barcode.FORMAT_CODE_39
import com.google.mlkit.vision.barcode.common.Barcode.FORMAT_CODE_93
import com.google.mlkit.vision.barcode.common.Barcode.FORMAT_DATA_MATRIX
import com.google.mlkit.vision.barcode.common.Barcode.FORMAT_EAN_13
import com.google.mlkit.vision.barcode.common.Barcode.FORMAT_EAN_8
import com.google.mlkit.vision.barcode.common.Barcode.FORMAT_ITF
import com.google.mlkit.vision.barcode.common.Barcode.FORMAT_PDF417
import com.google.mlkit.vision.barcode.common.Barcode.FORMAT_QR_CODE
import com.google.mlkit.vision.barcode.common.Barcode.FORMAT_UPC_A
import com.google.mlkit.vision.barcode.common.Barcode.FORMAT_UPC_E
import com.google.mlkit.vision.common.InputImage

class ImageScanner(reactContext: ReactApplicationContext) :
ReactContextBaseJavaModule(reactContext) {

private var barcodeOptions = BarcodeScannerOptions.Builder()


@ReactMethod
fun process(uri: String, options: ReadableArray?, promise: Promise) {
options?.toArrayList()?.forEach {
when (it) {
"code_128" -> barcodeOptions.setBarcodeFormats(FORMAT_CODE_128)
"code_39" -> barcodeOptions.setBarcodeFormats(FORMAT_CODE_39)
"code_93" -> barcodeOptions.setBarcodeFormats(FORMAT_CODE_93)
"codabar" -> barcodeOptions.setBarcodeFormats(FORMAT_CODABAR)
"ean_13" -> barcodeOptions.setBarcodeFormats(FORMAT_EAN_13)
"ean_8" -> barcodeOptions.setBarcodeFormats(FORMAT_EAN_8)
"itf" -> barcodeOptions.setBarcodeFormats(FORMAT_ITF)
"upc_e" -> barcodeOptions.setBarcodeFormats(FORMAT_UPC_E)
"upc_a" -> barcodeOptions.setBarcodeFormats(FORMAT_UPC_A)
"qr" -> barcodeOptions.setBarcodeFormats(FORMAT_QR_CODE)
"pdf_417" -> barcodeOptions.setBarcodeFormats(FORMAT_PDF417)
"aztec" -> barcodeOptions.setBarcodeFormats(FORMAT_AZTEC)
"data-matrix" -> barcodeOptions.setBarcodeFormats(FORMAT_DATA_MATRIX)
"all" -> barcodeOptions.setBarcodeFormats(FORMAT_ALL_FORMATS)
}
}
val scanner = BarcodeScanning.getClient(barcodeOptions.build())
val parsedUri = Uri.parse(uri)
val data = WritableNativeArray()
val image = InputImage.fromFilePath(this.reactApplicationContext, parsedUri)
val task: Task<List<Barcode>> = scanner.process(image)
val barcodes: List<Barcode> = Tasks.await(task)
for (barcode in barcodes) {
val map = VisionCameraBarcodesScannerModule.processData(barcode)
data.pushMap(map)
}
promise.resolve(data)
}


override fun getName() = NAME

companion object {
const val NAME = "ImageScanner"
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.visioncamerabarcodesscanner

import android.media.Image
import com.facebook.react.bridge.ReadableNativeMap
import com.facebook.react.bridge.WritableNativeArray
import com.facebook.react.bridge.WritableNativeMap
import com.google.mlkit.vision.barcode.BarcodeScannerOptions
Expand Down Expand Up @@ -28,38 +29,33 @@ import com.google.mlkit.vision.barcode.common.Barcode.FORMAT_UPC_A
import com.google.mlkit.vision.barcode.common.Barcode.FORMAT_UPC_E

class VisionCameraBarcodesScannerModule(
proxy: VisionCameraProxy,
options: MutableMap<String, Any>?
proxy: VisionCameraProxy, options: MutableMap<String, Any>?
) : FrameProcessorPlugin() {
private val optionsBuilder: BarcodeScannerOptions.Builder =
BarcodeScannerOptions.Builder()
private val optionsBuilder: BarcodeScannerOptions.Builder = BarcodeScannerOptions.Builder()
private val barcodeOptions = options?.values?.firstOrNull() as? List<*> ?: listOf("all")

init {
options?.values?.forEach { value ->
if (value is List<*>) {
value.forEach { format ->
when (format) {
"code_128" -> optionsBuilder.setBarcodeFormats(FORMAT_CODE_128)
"code_39" -> optionsBuilder.setBarcodeFormats(FORMAT_CODE_39)
"code_93" -> optionsBuilder.setBarcodeFormats(FORMAT_CODE_93)
"codabar" -> optionsBuilder.setBarcodeFormats(FORMAT_CODABAR)
"ean_13" -> optionsBuilder.setBarcodeFormats(FORMAT_EAN_13)
"ean_8" -> optionsBuilder.setBarcodeFormats(FORMAT_EAN_8)
"itf" -> optionsBuilder.setBarcodeFormats(FORMAT_ITF)
"upc_e" -> optionsBuilder.setBarcodeFormats(FORMAT_UPC_E)
"upc_a" -> optionsBuilder.setBarcodeFormats(FORMAT_UPC_A)
"qr" -> optionsBuilder.setBarcodeFormats(FORMAT_QR_CODE)
"pdf_417" -> optionsBuilder.setBarcodeFormats(FORMAT_PDF417)
"aztec" -> optionsBuilder.setBarcodeFormats(FORMAT_AZTEC)
"data-matrix" -> optionsBuilder.setBarcodeFormats(FORMAT_DATA_MATRIX)
"all" -> optionsBuilder.setBarcodeFormats(FORMAT_ALL_FORMATS)
else -> optionsBuilder.setBarcodeFormats(FORMAT_ALL_FORMATS)
}
}
barcodeOptions.forEach { format ->
when (format) {
"code_128" -> optionsBuilder.setBarcodeFormats(FORMAT_CODE_128)
"code_39" -> optionsBuilder.setBarcodeFormats(FORMAT_CODE_39)
"code_93" -> optionsBuilder.setBarcodeFormats(FORMAT_CODE_93)
"codabar" -> optionsBuilder.setBarcodeFormats(FORMAT_CODABAR)
"ean_13" -> optionsBuilder.setBarcodeFormats(FORMAT_EAN_13)
"ean_8" -> optionsBuilder.setBarcodeFormats(FORMAT_EAN_8)
"itf" -> optionsBuilder.setBarcodeFormats(FORMAT_ITF)
"upc_e" -> optionsBuilder.setBarcodeFormats(FORMAT_UPC_E)
"upc_a" -> optionsBuilder.setBarcodeFormats(FORMAT_UPC_A)
"qr" -> optionsBuilder.setBarcodeFormats(FORMAT_QR_CODE)
"pdf_417" -> optionsBuilder.setBarcodeFormats(FORMAT_PDF417)
"aztec" -> optionsBuilder.setBarcodeFormats(FORMAT_AZTEC)
"data-matrix" -> optionsBuilder.setBarcodeFormats(FORMAT_DATA_MATRIX)
"all" -> optionsBuilder.setBarcodeFormats(FORMAT_ALL_FORMATS)
else -> optionsBuilder.setBarcodeFormats(FORMAT_ALL_FORMATS)
}
}
}


override fun callback(frame: Frame, arguments: Map<String, Any>?): Any {
try {
val scanner = BarcodeScanning.getClient(optionsBuilder.build())
Expand All @@ -70,39 +66,7 @@ class VisionCameraBarcodesScannerModule(
val barcodes: List<Barcode> = Tasks.await(task)
val array = WritableNativeArray()
for (barcode in barcodes) {
val map = WritableNativeMap()
val bounds = barcode.boundingBox
if (bounds != null) {
map.putInt("width", bounds.width())
map.putInt("height", bounds.height())
map.putInt("top", bounds.top)
map.putInt("bottom", bounds.bottom)
map.putInt("left", bounds.left)
map.putInt("right", bounds.right)
}
val rawValue = barcode.rawValue
map.putString("rawValue", rawValue)
val displayValue = barcode.displayValue;
map.putString("displayValue", displayValue);

val valueType = barcode.valueType
when (valueType) {
Barcode.TYPE_WIFI -> {
val ssid = barcode.wifi!!.ssid
map.putString("ssid", ssid)
val password = barcode.wifi!!.password
map.putString("password", password)
val encryptionType = barcode.wifi!!.encryptionType
map.putInt("encryptionType", encryptionType)
}

Barcode.TYPE_URL -> {
val title = barcode.url!!.title
map.putString("title", title)
val url = barcode.url!!.url
map.putString("url", url)
}
}
val map = processData(barcode)
array.pushMap(map)
}
return array.toArrayList()
Expand All @@ -111,6 +75,45 @@ class VisionCameraBarcodesScannerModule(
}
}


companion object {
fun processData(barcode: Barcode): ReadableNativeMap {
val map = WritableNativeMap()
val bounds = barcode.boundingBox
if (bounds != null) {
map.putInt("width", bounds.width())
map.putInt("height", bounds.height())
map.putInt("top", bounds.top)
map.putInt("bottom", bounds.bottom)
map.putInt("left", bounds.left)
map.putInt("right", bounds.right)
}
val rawValue = barcode.rawValue
map.putString("rawValue", rawValue)
val displayValue = barcode.displayValue
map.putString("displayValue", displayValue)
val valueType = barcode.valueType

when (valueType) {
Barcode.TYPE_WIFI -> {
val ssid = barcode.wifi!!.ssid
map.putString("ssid", ssid)
val password = barcode.wifi!!.password
map.putString("password", password)
val encryptionType = barcode.wifi!!.encryptionType
map.putInt("encryptionType", encryptionType)
}

Barcode.TYPE_URL -> {
val title = barcode.url!!.title
map.putString("title", title)
val url = barcode.url!!.url
map.putString("url", url)
}
}
return map
}
}
}


Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,19 @@ import com.mrousavy.camera.frameprocessors.FrameProcessorPluginRegistry


class VisionCameraBarcodesScannerPackage : ReactPackage {
companion object {
init {
FrameProcessorPluginRegistry.addFrameProcessorPlugin("scanBarcodes") {proxy,options ->
VisionCameraBarcodesScannerModule(proxy,options)
}
companion object {
init {
FrameProcessorPluginRegistry.addFrameProcessorPlugin("scanBarcodes") { proxy, options ->
VisionCameraBarcodesScannerModule(proxy, options)
}
}
}
}
override fun createNativeModules(reactContext: ReactApplicationContext): List<NativeModule> {
return emptyList()
}

override fun createViewManagers(reactContext: ReactApplicationContext): List<ViewManager<*, *>> {
return emptyList()
}
override fun createNativeModules(reactContext: ReactApplicationContext): List<NativeModule> {
return listOf(ImageScanner(reactContext))
}

override fun createViewManagers(reactContext: ReactApplicationContext): List<ViewManager<*, *>> {
return emptyList()
}
}
Loading

0 comments on commit ee637e2

Please sign in to comment.