react-native-fabric-barcode-scanner 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +203 -0
- package/android/build.gradle +68 -0
- package/android/src/main/AndroidManifest.xml +4 -0
- package/android/src/main/java/com/rncustomscanner/BarcodeAnalyzer.kt +56 -0
- package/android/src/main/java/com/rncustomscanner/BarcodeFormatMapper.kt +59 -0
- package/android/src/main/java/com/rncustomscanner/CameraPermissionHelper.kt +140 -0
- package/android/src/main/java/com/rncustomscanner/RNCustomScannerPackage.kt +44 -0
- package/android/src/main/java/com/rncustomscanner/ScannerView.kt +245 -0
- package/android/src/main/java/com/rncustomscanner/events/BarcodeScannedEvent.kt +29 -0
- package/android/src/newarch/java/com/rncustomscanner/ScannerModule.kt +103 -0
- package/android/src/newarch/java/com/rncustomscanner/ScannerViewManager.kt +61 -0
- package/android/src/oldarch/java/com/rncustomscanner/ScannerModule.kt +109 -0
- package/android/src/oldarch/java/com/rncustomscanner/ScannerViewManager.kt +54 -0
- package/ios/RNCustomScanner-Bridging-Header.h +3 -0
- package/ios/RNScannerView.swift +132 -0
- package/ios/ScannerModule.mm +65 -0
- package/ios/ScannerModule.swift +105 -0
- package/ios/ScannerViewManager.mm +67 -0
- package/package.json +71 -0
- package/react-native-fabric-barcode-scanner.podspec +28 -0
- package/react-native.config.js +8 -0
- package/src/NativeScannerModule.ts +21 -0
- package/src/ScannerViewNativeComponent.ts +28 -0
- package/src/index.tsx +66 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Rajubarde12
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,203 @@
|
|
|
1
|
+
# react-native-fabric-barcode-scanner
|
|
2
|
+
|
|
3
|
+
Native barcode scanner for React Native built with **TurboModules** and **Fabric** (New Architecture). Uses AVFoundation on iOS and CameraX + ML Kit on Android.
|
|
4
|
+
|
|
5
|
+
[](https://www.npmjs.com/package/react-native-fabric-barcode-scanner)
|
|
6
|
+
[](https://github.com/Rajubarde12/react-native-fabric-barcode-scanner/blob/main/LICENSE)
|
|
7
|
+
|
|
8
|
+
## Features
|
|
9
|
+
|
|
10
|
+
- Fabric `ScannerView` component with continuous barcode scanning
|
|
11
|
+
- TurboModule `ScannerModule` for camera permission and torch control
|
|
12
|
+
- New Architecture support on iOS and Android
|
|
13
|
+
- Configurable barcode formats (`qr`, `ean13`, `code128`, and more)
|
|
14
|
+
- No permission dialogs from the view — permission is handled in JS via the module
|
|
15
|
+
|
|
16
|
+
## Requirements
|
|
17
|
+
|
|
18
|
+
- React Native **0.74+**
|
|
19
|
+
- New Architecture enabled (`newArchEnabled=true` recommended)
|
|
20
|
+
- iOS 13.4+
|
|
21
|
+
- Android API 24+
|
|
22
|
+
|
|
23
|
+
## Installation
|
|
24
|
+
|
|
25
|
+
```bash
|
|
26
|
+
npm install react-native-fabric-barcode-scanner
|
|
27
|
+
# or
|
|
28
|
+
yarn add react-native-fabric-barcode-scanner
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
### iOS
|
|
32
|
+
|
|
33
|
+
```bash
|
|
34
|
+
cd ios && pod install
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
Add camera usage description to `Info.plist`:
|
|
38
|
+
|
|
39
|
+
```xml
|
|
40
|
+
<key>NSCameraUsageDescription</key>
|
|
41
|
+
<string>Camera access is required to scan barcodes</string>
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
### Android
|
|
45
|
+
|
|
46
|
+
Camera permission is declared by the library. Ensure your app manifest includes camera access if not merged automatically:
|
|
47
|
+
|
|
48
|
+
```xml
|
|
49
|
+
<uses-permission android:name="android.permission.CAMERA" />
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
Enable New Architecture in `android/gradle.properties`:
|
|
53
|
+
|
|
54
|
+
```properties
|
|
55
|
+
newArchEnabled=true
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
## Usage
|
|
59
|
+
|
|
60
|
+
```tsx
|
|
61
|
+
import { useEffect, useState } from 'react';
|
|
62
|
+
import { Button, Text, View } from 'react-native';
|
|
63
|
+
import {
|
|
64
|
+
ScannerView,
|
|
65
|
+
getCameraPermissionState,
|
|
66
|
+
requestCameraPermission,
|
|
67
|
+
openCameraSettings,
|
|
68
|
+
type CameraPermissionStatus,
|
|
69
|
+
} from 'react-native-fabric-barcode-scanner';
|
|
70
|
+
|
|
71
|
+
function ScannerScreen() {
|
|
72
|
+
const [status, setStatus] = useState<CameraPermissionStatus>('not-determined');
|
|
73
|
+
|
|
74
|
+
const refreshPermission = async () => {
|
|
75
|
+
const state = await getCameraPermissionState();
|
|
76
|
+
setStatus(state.status);
|
|
77
|
+
return state;
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
useEffect(() => {
|
|
81
|
+
refreshPermission();
|
|
82
|
+
}, []);
|
|
83
|
+
|
|
84
|
+
if (status === 'granted') {
|
|
85
|
+
return (
|
|
86
|
+
<ScannerView
|
|
87
|
+
style={{ flex: 1 }}
|
|
88
|
+
isActive
|
|
89
|
+
torchOn={false}
|
|
90
|
+
scanFormats={['qr', 'ean13', 'code128']}
|
|
91
|
+
onBarcodeScanned={(event) => {
|
|
92
|
+
console.log(event.nativeEvent.value, event.nativeEvent.type);
|
|
93
|
+
}}
|
|
94
|
+
/>
|
|
95
|
+
);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
return (
|
|
99
|
+
<View style={{ flex: 1, justifyContent: 'center', alignItems: 'center', padding: 24 }}>
|
|
100
|
+
{status === 'not-determined' || status === 'denied' ? (
|
|
101
|
+
<Button
|
|
102
|
+
title="Allow camera"
|
|
103
|
+
onPress={async () => {
|
|
104
|
+
const next = await requestCameraPermission();
|
|
105
|
+
setStatus(next);
|
|
106
|
+
}}
|
|
107
|
+
/>
|
|
108
|
+
) : null}
|
|
109
|
+
|
|
110
|
+
{status === 'blocked' || status === 'restricted' ? (
|
|
111
|
+
<>
|
|
112
|
+
<Text style={{ marginBottom: 12, textAlign: 'center' }}>
|
|
113
|
+
Camera access is {status}. Open settings to enable it.
|
|
114
|
+
</Text>
|
|
115
|
+
<Button title="Open settings" onPress={openCameraSettings} />
|
|
116
|
+
</>
|
|
117
|
+
) : null}
|
|
118
|
+
</View>
|
|
119
|
+
);
|
|
120
|
+
}
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
## API
|
|
124
|
+
|
|
125
|
+
### `ScannerView` props
|
|
126
|
+
|
|
127
|
+
| Prop | Type | Default | Description |
|
|
128
|
+
|------|------|---------|-------------|
|
|
129
|
+
| `isActive` | `boolean` | `true` | Start/stop the camera session |
|
|
130
|
+
| `torchOn` | `boolean` | `false` | Enable device torch |
|
|
131
|
+
| `scanFormats` | `string[]` | all supported | Barcode formats to decode |
|
|
132
|
+
| `onBarcodeScanned` | `(event) => void` | — | Fired continuously while a code is visible |
|
|
133
|
+
|
|
134
|
+
### Camera permission (same API on iOS & Android)
|
|
135
|
+
|
|
136
|
+
Use **one flow** for both platforms:
|
|
137
|
+
|
|
138
|
+
```tsx
|
|
139
|
+
if (status === 'not-determined' || status === 'denied') {
|
|
140
|
+
// Show "Allow camera" → requestCameraPermission()
|
|
141
|
+
} else if (status === 'blocked' || status === 'restricted') {
|
|
142
|
+
// Show "Open settings" → openCameraSettings()
|
|
143
|
+
} else {
|
|
144
|
+
// granted → show ScannerView
|
|
145
|
+
}
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
| Status | Meaning | Developer action |
|
|
149
|
+
|--------|---------|------------------|
|
|
150
|
+
| `granted` | Camera allowed | Show `ScannerView` |
|
|
151
|
+
| `not-determined` | Never asked yet | `requestCameraPermission()` |
|
|
152
|
+
| `denied` | Denied, system dialog can still appear | `requestCameraPermission()` |
|
|
153
|
+
| `blocked` | System will not ask again | `openCameraSettings()` |
|
|
154
|
+
| `restricted` | Restricted by device policy (iOS) | `openCameraSettings()` |
|
|
155
|
+
|
|
156
|
+
**How each platform maps to the same status:**
|
|
157
|
+
|
|
158
|
+
| Scenario | iOS returns | Android returns |
|
|
159
|
+
|----------|-------------|-----------------|
|
|
160
|
+
| Never asked | `not-determined` | `not-determined` |
|
|
161
|
+
| User allowed | `granted` | `granted` |
|
|
162
|
+
| Denied, can ask again | — (iOS does not re-prompt) | `denied` |
|
|
163
|
+
| Denied permanently | `blocked` (after 1 deny) | `blocked` (after 2+ denies or "Don't ask again") |
|
|
164
|
+
| Device restricted | `restricted` | — |
|
|
165
|
+
|
|
166
|
+
iOS blocks after **one** deny. Android allows **2–3** prompts, then returns `blocked` — but your JS code stays the same on both platforms.
|
|
167
|
+
|
|
168
|
+
### `ScannerModule` methods
|
|
169
|
+
|
|
170
|
+
| Method | Returns | Description |
|
|
171
|
+
|--------|---------|-------------|
|
|
172
|
+
| `getCameraPermissionState()` | `Promise<{ status, canRequest, canOpenSettings }>` | Full permission state for UI |
|
|
173
|
+
| `checkCameraPermission()` | `Promise<CameraPermissionStatus>` | Current camera permission status |
|
|
174
|
+
| `requestCameraPermission()` | `Promise<CameraPermissionStatus>` | Request permission (only when allowed) |
|
|
175
|
+
| `openCameraSettings()` | `void` | Open app settings so user can enable camera |
|
|
176
|
+
| `canRequestCameraPermission(status)` | `boolean` | `true` for `not-determined` or `denied` |
|
|
177
|
+
| `shouldOpenCameraSettings(status)` | `boolean` | `true` for `blocked` or `restricted` |
|
|
178
|
+
| `isCameraPermissionGranted(status)` | `boolean` | `true` when `granted` |
|
|
179
|
+
| `setTorchEnabled(enabled)` | `void` | Toggle torch outside the view |
|
|
180
|
+
|
|
181
|
+
### Supported formats
|
|
182
|
+
|
|
183
|
+
`qr`, `ean13`, `ean8`, `upce`, `code128`, `code39`, `code93`, `pdf417`, `aztec`, `datamatrix`, `itf14`, `interleaved2of5`
|
|
184
|
+
|
|
185
|
+
## New Architecture
|
|
186
|
+
|
|
187
|
+
This library is built for React Native's New Architecture:
|
|
188
|
+
|
|
189
|
+
- **TurboModule**: `ScannerModule`
|
|
190
|
+
- **Fabric component**: `ScannerView`
|
|
191
|
+
- Codegen spec: `RNCustomScannerSpec`
|
|
192
|
+
|
|
193
|
+
Legacy architecture is supported via paper fallbacks on Android.
|
|
194
|
+
|
|
195
|
+
## License
|
|
196
|
+
|
|
197
|
+
MIT — see [LICENSE](LICENSE).
|
|
198
|
+
|
|
199
|
+
## Repository
|
|
200
|
+
|
|
201
|
+
- **GitHub**: [Rajubarde12/react-native-fabric-barcode-scanner](https://github.com/Rajubarde12/react-native-fabric-barcode-scanner)
|
|
202
|
+
- **npm**: [react-native-fabric-barcode-scanner](https://www.npmjs.com/package/react-native-fabric-barcode-scanner)
|
|
203
|
+
- **Issues**: [Report a bug](https://github.com/Rajubarde12/react-native-fabric-barcode-scanner/issues)
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
buildscript {
|
|
2
|
+
ext.safeExtGet = { prop, fallback ->
|
|
3
|
+
rootProject.ext.has(prop) ? rootProject.ext.get(prop) : fallback
|
|
4
|
+
}
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
def isNewArchitectureEnabled() {
|
|
8
|
+
return project.hasProperty("newArchEnabled") && project.newArchEnabled == "true"
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
apply plugin: "com.android.library"
|
|
12
|
+
apply plugin: "org.jetbrains.kotlin.android"
|
|
13
|
+
|
|
14
|
+
if (isNewArchitectureEnabled()) {
|
|
15
|
+
apply plugin: "com.facebook.react"
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
android {
|
|
19
|
+
namespace "com.rncustomscanner"
|
|
20
|
+
compileSdk safeExtGet("compileSdkVersion", 36)
|
|
21
|
+
|
|
22
|
+
defaultConfig {
|
|
23
|
+
minSdkVersion safeExtGet("minSdkVersion", 24)
|
|
24
|
+
targetSdkVersion safeExtGet("targetSdkVersion", 36)
|
|
25
|
+
buildConfigField "boolean", "IS_NEW_ARCHITECTURE_ENABLED", isNewArchitectureEnabled().toString()
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
buildFeatures {
|
|
29
|
+
buildConfig true
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
sourceSets {
|
|
33
|
+
main {
|
|
34
|
+
java {
|
|
35
|
+
if (isNewArchitectureEnabled()) {
|
|
36
|
+
srcDirs += [
|
|
37
|
+
"src/newarch/java",
|
|
38
|
+
"${project.buildDir}/generated/source/codegen/java",
|
|
39
|
+
]
|
|
40
|
+
} else {
|
|
41
|
+
srcDirs += ["src/oldarch/java"]
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
repositories {
|
|
49
|
+
google()
|
|
50
|
+
mavenCentral()
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
def kotlinVersion = safeExtGet("kotlinVersion", "2.1.20")
|
|
54
|
+
|
|
55
|
+
dependencies {
|
|
56
|
+
implementation "com.facebook.react:react-android"
|
|
57
|
+
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlinVersion"
|
|
58
|
+
|
|
59
|
+
def cameraxVersion = "1.4.2"
|
|
60
|
+
implementation "androidx.camera:camera-core:$cameraxVersion"
|
|
61
|
+
implementation "androidx.camera:camera-camera2:$cameraxVersion"
|
|
62
|
+
implementation "androidx.camera:camera-lifecycle:$cameraxVersion"
|
|
63
|
+
implementation "androidx.camera:camera-view:$cameraxVersion"
|
|
64
|
+
implementation "androidx.appcompat:appcompat:1.7.0"
|
|
65
|
+
implementation "androidx.lifecycle:lifecycle-runtime-ktx:2.8.7"
|
|
66
|
+
|
|
67
|
+
implementation "com.google.mlkit:barcode-scanning:17.3.0"
|
|
68
|
+
}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
package com.rncustomscanner
|
|
2
|
+
|
|
3
|
+
import android.annotation.SuppressLint
|
|
4
|
+
import androidx.camera.core.ExperimentalGetImage
|
|
5
|
+
import androidx.camera.core.ImageAnalysis
|
|
6
|
+
import androidx.camera.core.ImageProxy
|
|
7
|
+
import com.google.mlkit.vision.barcode.BarcodeScanner
|
|
8
|
+
import com.google.mlkit.vision.barcode.BarcodeScannerOptions
|
|
9
|
+
import com.google.mlkit.vision.barcode.BarcodeScanning
|
|
10
|
+
import com.google.mlkit.vision.barcode.common.Barcode
|
|
11
|
+
import com.google.mlkit.vision.common.InputImage
|
|
12
|
+
|
|
13
|
+
class BarcodeAnalyzer(
|
|
14
|
+
private var onBarcodesDetected: (List<Barcode>) -> Unit,
|
|
15
|
+
) : ImageAnalysis.Analyzer {
|
|
16
|
+
|
|
17
|
+
private var scanner: BarcodeScanner = createScanner(intArrayOf())
|
|
18
|
+
|
|
19
|
+
fun updateFormats(formats: IntArray) {
|
|
20
|
+
scanner.close()
|
|
21
|
+
scanner = createScanner(formats)
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
private fun createScanner(formats: IntArray): BarcodeScanner {
|
|
25
|
+
val builder = BarcodeScannerOptions.Builder()
|
|
26
|
+
if (formats.isNotEmpty()) {
|
|
27
|
+
builder.setBarcodeFormats(formats.first(), *formats.drop(1).toIntArray())
|
|
28
|
+
}
|
|
29
|
+
return BarcodeScanning.getClient(builder.build())
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
@SuppressLint("UnsafeOptInUsageError")
|
|
33
|
+
@ExperimentalGetImage
|
|
34
|
+
override fun analyze(image: ImageProxy) {
|
|
35
|
+
val mediaImage = image.image
|
|
36
|
+
if (mediaImage == null) {
|
|
37
|
+
image.close()
|
|
38
|
+
return
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
val inputImage = InputImage.fromMediaImage(mediaImage, image.imageInfo.rotationDegrees)
|
|
42
|
+
scanner.process(inputImage)
|
|
43
|
+
.addOnSuccessListener { barcodes ->
|
|
44
|
+
if (barcodes.isNotEmpty()) {
|
|
45
|
+
onBarcodesDetected(barcodes)
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
.addOnCompleteListener {
|
|
49
|
+
image.close()
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
fun close() {
|
|
54
|
+
scanner.close()
|
|
55
|
+
}
|
|
56
|
+
}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
package com.rncustomscanner
|
|
2
|
+
|
|
3
|
+
import com.google.mlkit.vision.barcode.common.Barcode
|
|
4
|
+
|
|
5
|
+
object BarcodeFormatMapper {
|
|
6
|
+
val supportedFormats: List<String> = listOf(
|
|
7
|
+
"qr",
|
|
8
|
+
"ean13",
|
|
9
|
+
"ean8",
|
|
10
|
+
"upce",
|
|
11
|
+
"code128",
|
|
12
|
+
"code39",
|
|
13
|
+
"code93",
|
|
14
|
+
"pdf417",
|
|
15
|
+
"aztec",
|
|
16
|
+
"datamatrix",
|
|
17
|
+
"itf14",
|
|
18
|
+
"interleaved2of5",
|
|
19
|
+
)
|
|
20
|
+
|
|
21
|
+
fun toMlKitFormat(name: String): Int? {
|
|
22
|
+
return when (name) {
|
|
23
|
+
"qr" -> Barcode.FORMAT_QR_CODE
|
|
24
|
+
"ean13" -> Barcode.FORMAT_EAN_13
|
|
25
|
+
"ean8" -> Barcode.FORMAT_EAN_8
|
|
26
|
+
"upce" -> Barcode.FORMAT_UPC_E
|
|
27
|
+
"code128" -> Barcode.FORMAT_CODE_128
|
|
28
|
+
"code39" -> Barcode.FORMAT_CODE_39
|
|
29
|
+
"code93" -> Barcode.FORMAT_CODE_93
|
|
30
|
+
"pdf417" -> Barcode.FORMAT_PDF417
|
|
31
|
+
"aztec" -> Barcode.FORMAT_AZTEC
|
|
32
|
+
"datamatrix" -> Barcode.FORMAT_DATA_MATRIX
|
|
33
|
+
"itf14", "interleaved2of5" -> Barcode.FORMAT_ITF
|
|
34
|
+
else -> null
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
fun fromMlKitFormat(@Barcode.BarcodeFormat format: Int): String {
|
|
39
|
+
return when (format) {
|
|
40
|
+
Barcode.FORMAT_QR_CODE -> "qr"
|
|
41
|
+
Barcode.FORMAT_EAN_13 -> "ean13"
|
|
42
|
+
Barcode.FORMAT_EAN_8 -> "ean8"
|
|
43
|
+
Barcode.FORMAT_UPC_E -> "upce"
|
|
44
|
+
Barcode.FORMAT_UPC_A -> "upce"
|
|
45
|
+
Barcode.FORMAT_CODE_128 -> "code128"
|
|
46
|
+
Barcode.FORMAT_CODE_39 -> "code39"
|
|
47
|
+
Barcode.FORMAT_CODE_93 -> "code93"
|
|
48
|
+
Barcode.FORMAT_PDF417 -> "pdf417"
|
|
49
|
+
Barcode.FORMAT_AZTEC -> "aztec"
|
|
50
|
+
Barcode.FORMAT_DATA_MATRIX -> "datamatrix"
|
|
51
|
+
Barcode.FORMAT_ITF -> "itf14"
|
|
52
|
+
else -> "unknown"
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
fun toMlKitFormats(names: List<String>): IntArray {
|
|
57
|
+
return names.mapNotNull { toMlKitFormat(it) }.distinct().toIntArray()
|
|
58
|
+
}
|
|
59
|
+
}
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
package com.rncustomscanner
|
|
2
|
+
|
|
3
|
+
import android.Manifest
|
|
4
|
+
import android.content.Context
|
|
5
|
+
import android.content.Intent
|
|
6
|
+
import android.content.pm.PackageManager
|
|
7
|
+
import android.net.Uri
|
|
8
|
+
import android.provider.Settings
|
|
9
|
+
import androidx.core.app.ActivityCompat
|
|
10
|
+
import androidx.core.content.ContextCompat
|
|
11
|
+
import com.facebook.react.bridge.ReactApplicationContext
|
|
12
|
+
|
|
13
|
+
object CameraPermissionHelper {
|
|
14
|
+
|
|
15
|
+
const val STATUS_GRANTED = "granted"
|
|
16
|
+
const val STATUS_NOT_DETERMINED = "not-determined"
|
|
17
|
+
const val STATUS_DENIED = "denied"
|
|
18
|
+
const val STATUS_BLOCKED = "blocked"
|
|
19
|
+
const val STATUS_RESTRICTED = "restricted"
|
|
20
|
+
|
|
21
|
+
private const val PREFS_NAME = "rncustomscanner_permissions"
|
|
22
|
+
private const val KEY_CAMERA_REQUESTED = "camera_requested"
|
|
23
|
+
private const val KEY_CAMERA_DENY_COUNT = "camera_deny_count"
|
|
24
|
+
|
|
25
|
+
fun checkPermission(context: ReactApplicationContext): String {
|
|
26
|
+
if (isGranted(context)) {
|
|
27
|
+
return STATUS_GRANTED
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
if (!wasPermissionRequested(context)) {
|
|
31
|
+
return STATUS_NOT_DETERMINED
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
if (shouldShowRationale(context)) {
|
|
35
|
+
return STATUS_DENIED
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
// Unified with iOS: only blocked when the system will not show the dialog again.
|
|
39
|
+
// Android may deny 1-2 times before reaching this state.
|
|
40
|
+
return if (getDenyCount(context) >= 2) {
|
|
41
|
+
STATUS_BLOCKED
|
|
42
|
+
} else {
|
|
43
|
+
STATUS_DENIED
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
fun resolveRequestResult(
|
|
48
|
+
context: ReactApplicationContext,
|
|
49
|
+
grantResults: IntArray,
|
|
50
|
+
): String {
|
|
51
|
+
markPermissionRequested(context)
|
|
52
|
+
|
|
53
|
+
val granted = grantResults.isNotEmpty() &&
|
|
54
|
+
grantResults[0] == PackageManager.PERMISSION_GRANTED
|
|
55
|
+
|
|
56
|
+
if (granted) {
|
|
57
|
+
clearDenyCount(context)
|
|
58
|
+
return STATUS_GRANTED
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
incrementDenyCount(context)
|
|
62
|
+
return resolveDeniedStatus(context)
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
fun canRequestPermission(status: String): Boolean {
|
|
66
|
+
return status == STATUS_NOT_DETERMINED || status == STATUS_DENIED
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
fun markPermissionRequested(context: ReactApplicationContext) {
|
|
70
|
+
getPrefs(context)
|
|
71
|
+
.edit()
|
|
72
|
+
.putBoolean(KEY_CAMERA_REQUESTED, true)
|
|
73
|
+
.apply()
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
fun openAppSettings(context: ReactApplicationContext) {
|
|
77
|
+
val intent = Intent(
|
|
78
|
+
Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
|
|
79
|
+
Uri.fromParts("package", context.packageName, null),
|
|
80
|
+
).apply {
|
|
81
|
+
addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
|
|
82
|
+
}
|
|
83
|
+
context.startActivity(intent)
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
private fun resolveDeniedStatus(context: ReactApplicationContext): String {
|
|
87
|
+
if (shouldShowRationale(context)) {
|
|
88
|
+
return STATUS_DENIED
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
// First denial: always "denied" (Android can still show the dialog again).
|
|
92
|
+
// Second+ denial without rationale: "blocked" (same as iOS after one deny).
|
|
93
|
+
return if (getDenyCount(context) >= 2) {
|
|
94
|
+
STATUS_BLOCKED
|
|
95
|
+
} else {
|
|
96
|
+
STATUS_DENIED
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
private fun isGranted(context: ReactApplicationContext): Boolean {
|
|
101
|
+
val activity = context.currentActivity
|
|
102
|
+
val checkContext = activity ?: context
|
|
103
|
+
return ContextCompat.checkSelfPermission(checkContext, Manifest.permission.CAMERA) ==
|
|
104
|
+
PackageManager.PERMISSION_GRANTED
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
private fun shouldShowRationale(context: ReactApplicationContext): Boolean {
|
|
108
|
+
val activity = context.currentActivity ?: return false
|
|
109
|
+
return ActivityCompat.shouldShowRequestPermissionRationale(
|
|
110
|
+
activity,
|
|
111
|
+
Manifest.permission.CAMERA,
|
|
112
|
+
)
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
private fun wasPermissionRequested(context: ReactApplicationContext): Boolean {
|
|
116
|
+
return getPrefs(context).getBoolean(KEY_CAMERA_REQUESTED, false)
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
private fun getDenyCount(context: ReactApplicationContext): Int {
|
|
120
|
+
return getPrefs(context).getInt(KEY_CAMERA_DENY_COUNT, 0)
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
private fun incrementDenyCount(context: ReactApplicationContext) {
|
|
124
|
+
val count = getDenyCount(context)
|
|
125
|
+
getPrefs(context)
|
|
126
|
+
.edit()
|
|
127
|
+
.putInt(KEY_CAMERA_DENY_COUNT, count + 1)
|
|
128
|
+
.apply()
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
private fun clearDenyCount(context: ReactApplicationContext) {
|
|
132
|
+
getPrefs(context)
|
|
133
|
+
.edit()
|
|
134
|
+
.putInt(KEY_CAMERA_DENY_COUNT, 0)
|
|
135
|
+
.apply()
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
private fun getPrefs(context: ReactApplicationContext) =
|
|
139
|
+
context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE)
|
|
140
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
package com.rncustomscanner
|
|
2
|
+
|
|
3
|
+
import com.facebook.react.TurboReactPackage
|
|
4
|
+
import com.facebook.react.bridge.NativeModule
|
|
5
|
+
import com.facebook.react.bridge.ReactApplicationContext
|
|
6
|
+
import com.facebook.react.module.model.ReactModuleInfo
|
|
7
|
+
import com.facebook.react.module.model.ReactModuleInfoProvider
|
|
8
|
+
import com.facebook.react.uimanager.ViewManager
|
|
9
|
+
|
|
10
|
+
class RNCustomScannerPackage : TurboReactPackage() {
|
|
11
|
+
|
|
12
|
+
override fun createViewManagers(
|
|
13
|
+
reactContext: ReactApplicationContext,
|
|
14
|
+
): List<ViewManager<*, *>> {
|
|
15
|
+
return listOf(ScannerViewManager(reactContext))
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
override fun getModule(
|
|
19
|
+
name: String,
|
|
20
|
+
reactContext: ReactApplicationContext,
|
|
21
|
+
): NativeModule? {
|
|
22
|
+
return if (name == ScannerModule.NAME) {
|
|
23
|
+
ScannerModule(reactContext)
|
|
24
|
+
} else {
|
|
25
|
+
null
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
override fun getReactModuleInfoProvider(): ReactModuleInfoProvider {
|
|
30
|
+
val isTurboModule = BuildConfig.IS_NEW_ARCHITECTURE_ENABLED
|
|
31
|
+
return ReactModuleInfoProvider {
|
|
32
|
+
mapOf(
|
|
33
|
+
ScannerModule.NAME to ReactModuleInfo(
|
|
34
|
+
ScannerModule.NAME,
|
|
35
|
+
ScannerModule.NAME,
|
|
36
|
+
false,
|
|
37
|
+
false,
|
|
38
|
+
false,
|
|
39
|
+
isTurboModule,
|
|
40
|
+
),
|
|
41
|
+
)
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
}
|