dynamsoft-capture-vision-react-native 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/.gitattributes +1 -0
- package/LICENSE +5 -0
- package/Legal.txt +551 -0
- package/README.md +247 -0
- package/android/build.gradle +54 -0
- package/android/gradle/wrapper/gradle-wrapper.jar +0 -0
- package/android/gradle/wrapper/gradle-wrapper.properties +6 -0
- package/android/src/main/AndroidManifest.xml +6 -0
- package/android/src/main/java/com/dynamsoft/reactlibrary/RNDCECameraView.java +90 -0
- package/android/src/main/java/com/dynamsoft/reactlibrary/RNDCECameraViewManager.java +156 -0
- package/android/src/main/java/com/dynamsoft/reactlibrary/RNDynamsoftBarcodeReaderModule.java +253 -0
- package/android/src/main/java/com/dynamsoft/reactlibrary/RNDynamsoftCaptrueVisionPackage.java +28 -0
- package/dynamsoft-capture-vision-react-native.podspec +22 -0
- package/ios/RNDynamsoftCaptureVision/DYSCameraView.h +26 -0
- package/ios/RNDynamsoftCaptureVision/DYSCameraView.m +71 -0
- package/ios/RNDynamsoftCaptureVision/DYSCameraViewManager.h +17 -0
- package/ios/RNDynamsoftCaptureVision/DYSCameraViewManager.m +77 -0
- package/ios/RNDynamsoftCaptureVision/RCTDynamsoftBarcodeReader.h +14 -0
- package/ios/RNDynamsoftCaptureVision/RCTDynamsoftBarcodeReader.m +223 -0
- package/ios/RNDynamsoftCaptureVision/StaticClass.h +26 -0
- package/ios/RNDynamsoftCaptureVision/StaticClass.m +21 -0
- package/ios/RNDynamsoftCaptureVision.xcodeproj/project.pbxproj +337 -0
- package/js/BarcodeSettings.d.ts +112 -0
- package/js/BarcodeSettings.js +66 -0
- package/js/DynamsoftBarcodeReader.d.ts +14 -0
- package/js/DynamsoftBarcodeReader.js +75 -0
- package/js/DynamsoftCameraView.d.ts +10 -0
- package/js/DynamsoftCameraView.js +107 -0
- package/js/index.d.ts +3 -0
- package/js/index.js +3 -0
- package/package.json +31 -0
package/README.md
ADDED
|
@@ -0,0 +1,247 @@
|
|
|
1
|
+
# Dynamsoft Capture Vision React-Native Edition
|
|
2
|
+
|
|
3
|
+

|
|
4
|
+

|
|
5
|
+

|
|
6
|
+

|
|
7
|
+
|
|
8
|
+
<a href="https://www.dynamsoft.com/capture-vision/docs/introduction/">Dynamsoft Capture Vision (DCV) </a> is an aggregating SDK of a series of specific functional products including:
|
|
9
|
+
|
|
10
|
+
- Dynamsoft Camera Enhancer (DCE) which provides camera enhancements and UI configuration APIs.
|
|
11
|
+
- Dynamsoft Barcode Reader (DBR) which provides barcode decoding algorithm and APIs.
|
|
12
|
+
- Dynamsoft Label Recognizer (DLR) which provides label content recognizing algorithm and APIs.
|
|
13
|
+
- Dynamsoft Document Normalizer (DDN) which provides document scanning algorithms and APIs.
|
|
14
|
+
|
|
15
|
+
>Note: DCV React-Native edition currently only includes DCE and DBR modules. DLR and DDN modules are still under development and will be included in the future.
|
|
16
|
+
|
|
17
|
+
<span style="font-size:20px">Table of Contents</span>
|
|
18
|
+
|
|
19
|
+
- [System Requirements](#system-requirements)
|
|
20
|
+
- [Installation](#installation)
|
|
21
|
+
- [Build Your Barcode Scanner App](#build-your-barcode-scanner-app)
|
|
22
|
+
- [Initialize Project](#initialize-project)
|
|
23
|
+
- [Include the Library](#include-the-library)
|
|
24
|
+
- [Configure the Barcode Reader](#configure-the-barcode-reader)
|
|
25
|
+
- [Rendering the UI](#rendering-the-ui)
|
|
26
|
+
- [Configure Camera Permissions](#configure-camera-permissions)
|
|
27
|
+
- [Run the Project](#run-the-project)
|
|
28
|
+
- [Samples](#samples)
|
|
29
|
+
- [API References](#api-references)
|
|
30
|
+
- [License](#license)
|
|
31
|
+
- [Contact](#contact)
|
|
32
|
+
|
|
33
|
+
## System Requirements
|
|
34
|
+
|
|
35
|
+
### React Native
|
|
36
|
+
|
|
37
|
+
- Supported Version: 0.60 or higher
|
|
38
|
+
|
|
39
|
+
### Android
|
|
40
|
+
|
|
41
|
+
- Supported OS: Android 5.0 (API Level 21) or higher.
|
|
42
|
+
- Supported ABI: **armeabi-v7a**, **arm64-v8a**, **x86** and **x86_64**.
|
|
43
|
+
- Development Environment: Android Studio 3.4+ (Android Studio 4.2+ recommended).
|
|
44
|
+
- JDK: 1.8+
|
|
45
|
+
|
|
46
|
+
### iOS
|
|
47
|
+
|
|
48
|
+
- Supported OS: **iOS 10.0** or higher.
|
|
49
|
+
- Supported ABI: **arm64** and **x86_64**.
|
|
50
|
+
- Development Environment: Xcode 7.1 and above (Xcode 13.0+ recommended), CocoaPods 1.11.0+.
|
|
51
|
+
|
|
52
|
+
### Others
|
|
53
|
+
|
|
54
|
+
- Node: 16.15.1 recommended
|
|
55
|
+
|
|
56
|
+
## Installation
|
|
57
|
+
|
|
58
|
+
- **yarn**
|
|
59
|
+
|
|
60
|
+
```bash
|
|
61
|
+
yarn add dynamsoft-capture-vision-react-native
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
- **npm**
|
|
65
|
+
|
|
66
|
+
```bash
|
|
67
|
+
npm install dynamsoft-capture-vision-react-native
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
## Build Your Barcode Scanner App
|
|
71
|
+
|
|
72
|
+
Now you will learn how to create a simple barcode scanner using Dynamsoft Capture Vision SDK.
|
|
73
|
+
|
|
74
|
+
### Initialize the Project
|
|
75
|
+
|
|
76
|
+
Create a new React Native project.
|
|
77
|
+
|
|
78
|
+
```bash
|
|
79
|
+
npx react-native init SimpleBarcodeScanner
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
>Note: This sample uses react 17.0.2 and react-native 0.65.0.
|
|
83
|
+
|
|
84
|
+
### Include the Library
|
|
85
|
+
|
|
86
|
+
Add the SDK to your new project. Please read the [Installation](#installation) section for more details. Once the SDK is added, you will see a reference to it in the **package.json**.
|
|
87
|
+
|
|
88
|
+
For iOS, you must install the necessary native frameworks from cocoapods to run the application. In order to do this, the `pod install` command needs to be run as such:
|
|
89
|
+
|
|
90
|
+
```bash
|
|
91
|
+
cd ios
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
```bash
|
|
95
|
+
pod install
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
### Configure the Barcode Reader
|
|
99
|
+
|
|
100
|
+
In `App.js`, import the following components:
|
|
101
|
+
|
|
102
|
+
```js
|
|
103
|
+
import React from 'react';
|
|
104
|
+
import {Text} from 'react-native';
|
|
105
|
+
import {
|
|
106
|
+
DynamsoftBarcodeReader,
|
|
107
|
+
DynamsoftCameraView,
|
|
108
|
+
BarcodeResult,
|
|
109
|
+
EnumDBRPresetTemplate,
|
|
110
|
+
EnumBarcodeFormat,
|
|
111
|
+
DBRRuntimeSettings
|
|
112
|
+
} from 'dynamsoft-capture-vision-react-native';
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
Next in `App.js`, let's define the `state` to your component. In the `state`, add a `results` variable, initialized to null. In the following steps, we will store the newly decoded barcodes to `results`.
|
|
116
|
+
|
|
117
|
+
```js
|
|
118
|
+
class App extends React.Component {
|
|
119
|
+
state = {
|
|
120
|
+
results: null
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
export default App;
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
Next is the `componentDidMount` implementation. First up is adding the code to start barcode decoding:
|
|
127
|
+
|
|
128
|
+
```js
|
|
129
|
+
componentDidMount() {
|
|
130
|
+
(async () => {
|
|
131
|
+
// Initialize the license so that you can use full feature of the Barcode Reader module.
|
|
132
|
+
try {
|
|
133
|
+
await DynamsoftBarcodeReader.initLicense("DLS2eyJvcmdhbml6YXRpb25JRCI6IjIwMDAwMSJ9")
|
|
134
|
+
} catch (e) {
|
|
135
|
+
console.log(e);
|
|
136
|
+
}
|
|
137
|
+
// Create a barcode reader instance.
|
|
138
|
+
this.reader = await DynamsoftBarcodeReader.createInstance();
|
|
139
|
+
// Enable video barcode scanning.
|
|
140
|
+
// If the camera is opened, the barcode reader will start the barcode decoding thread when you triggered the startScanning.
|
|
141
|
+
// The barcode reader will scan the barcodes continuously before you trigger stopScanning.
|
|
142
|
+
await this.reader.startScanning();
|
|
143
|
+
// Add a result listener. The result listener will handle callback when barcode result is returned.
|
|
144
|
+
this.reader.addResultListener((results) => {
|
|
145
|
+
// Update the newly detected barcode results to the state.
|
|
146
|
+
this.setState({results});
|
|
147
|
+
});
|
|
148
|
+
})();
|
|
149
|
+
}
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
After implementing `componentDidMount`, `componentWillUnmount` will then include code to stop the barcode decoding and remove the result listener.
|
|
153
|
+
|
|
154
|
+
```js
|
|
155
|
+
async componentWillUnmount() {
|
|
156
|
+
// Stop the barcode decoding thread when your component is unmount.
|
|
157
|
+
await this.reader.stopScanning();
|
|
158
|
+
// Remove the result listener when your component is unmount.
|
|
159
|
+
this.reader.removeAllResultListeners();
|
|
160
|
+
}
|
|
161
|
+
```
|
|
162
|
+
|
|
163
|
+
### Rendering the UI
|
|
164
|
+
|
|
165
|
+
Lastly, let's create the `DynamsoftCameraView` UI component in the `render` function.
|
|
166
|
+
|
|
167
|
+
```js
|
|
168
|
+
render() {
|
|
169
|
+
// Add code to fetch barcode text and format from the BarcodeResult
|
|
170
|
+
let results = this.state.results;
|
|
171
|
+
let resultBoxText = "";
|
|
172
|
+
if (results && results.length>0){
|
|
173
|
+
for (let i=0;i<results.length;i++){
|
|
174
|
+
resultBoxText+=results[i].barcodeFormatString+"\n"+results[i].barcodeText+"\n";
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
// Render DynamsoftCameraView componment.
|
|
178
|
+
return (
|
|
179
|
+
<DynamsoftCameraView
|
|
180
|
+
style={{
|
|
181
|
+
flex: 1,
|
|
182
|
+
}}
|
|
183
|
+
ref = {(ref)=>{this.scanner = ref}}
|
|
184
|
+
isOverlayVisible={true}
|
|
185
|
+
>
|
|
186
|
+
// Add a text box to display the barcode result.
|
|
187
|
+
<Text style={{
|
|
188
|
+
flex: 0.9,
|
|
189
|
+
marginTop: 100,
|
|
190
|
+
textAlign: "center",
|
|
191
|
+
color: "white",
|
|
192
|
+
fontSize: 18,
|
|
193
|
+
}}>{results && results.length > 0 ? resultBoxText : "No Barcode Detected"}</Text>
|
|
194
|
+
</DynamsoftCameraView>
|
|
195
|
+
);
|
|
196
|
+
}
|
|
197
|
+
```
|
|
198
|
+
|
|
199
|
+
### Configure Camera Permissions
|
|
200
|
+
|
|
201
|
+
You need to set the "Privacy - Camera Usage Description" field in the `Info.plist` file for iOS. If this property is not set, the iOS application will fail at runtime. In order to set this property, you might need to use Xcode and open the corresponding `.xcworkspace` located in the `ios` folder. Once open, you can edit the `Info.plist` to include this property.
|
|
202
|
+
|
|
203
|
+
### Run the Project
|
|
204
|
+
|
|
205
|
+
#### Run Android on Windows
|
|
206
|
+
|
|
207
|
+
In the command line interface (we recommend using Powershell), go to your project folder and run the following command:
|
|
208
|
+
|
|
209
|
+
```bash
|
|
210
|
+
npx react-native run-android
|
|
211
|
+
```
|
|
212
|
+
|
|
213
|
+
#### Run iOS on macOS
|
|
214
|
+
|
|
215
|
+
In the terminal, go to the project folder in your project:
|
|
216
|
+
|
|
217
|
+
```bash
|
|
218
|
+
npx react-native run-ios
|
|
219
|
+
```
|
|
220
|
+
|
|
221
|
+
> Note:
|
|
222
|
+
>
|
|
223
|
+
>- The application needs to run on a physical device rather than a simulator as it requires the use of the camera. If you try running it on a simulator, you will most likely run into a number of errors/failures.
|
|
224
|
+
>- On iOS, in order to run the React Native app on a physical device you will need to install the [`ios-deploy`](https://www.npmjs.com/package/ios-deploy) library. Afterwards, you can run the react native app from the terminal as such `npx react-native run-ios --device` assuming it's the only device connected to the Mac.
|
|
225
|
+
>- Alternatively on iOS, you can simply open the `xcworkspace` of the project found in the `ios` folder using Xcode and run the sample on your connected iOS device from there. The advantage that this offers is that it is easier to deal with the developer signatures for deployment in there.
|
|
226
|
+
|
|
227
|
+
## Samples
|
|
228
|
+
|
|
229
|
+
You can view all the DCV React Native samples via the following links:
|
|
230
|
+
|
|
231
|
+
- <a href = "https://github.com/Dynamsoft/capture-vision-react-native-samples/BarcodeReaderSimpleSample" target = "_blank" >Barcode reader simple sample</a>
|
|
232
|
+
|
|
233
|
+
## API References
|
|
234
|
+
|
|
235
|
+
View the API reference of DCV React Native Edition to explore the full feature of DCV:
|
|
236
|
+
|
|
237
|
+
- <a href = "https://www.dynamsoft.com/capture-vision/docs/programming/react-native/api-reference/?ver=latest" target = "_blank" >DCV API Reference - React Native Edition</a>
|
|
238
|
+
- <a href = "https://www.dynamsoft.com/capture-vision/docs/programming/react-native/api-reference/barcode-reader.html?ver=latest" target = "_blank" >DynamsoftBarcodeReader Class</a>
|
|
239
|
+
- <a href = "https://www.dynamsoft.com/capture-vision/docs/programming/react-native/api-reference/camera-view.html?ver=latest" target = "_blank" >DynamsoftCameraEnhancer Class</a>
|
|
240
|
+
|
|
241
|
+
## License
|
|
242
|
+
|
|
243
|
+
- You can also request an extension for your trial license in the [customer portal](https://www.dynamsoft.com/customer/license/trialLicense?product=dbr&utm_source=github)
|
|
244
|
+
|
|
245
|
+
## Contact
|
|
246
|
+
|
|
247
|
+
https://www.dynamsoft.com/company/contact/
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
|
|
2
|
+
buildscript {
|
|
3
|
+
repositories {
|
|
4
|
+
google()
|
|
5
|
+
jcenter()
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
dependencies {
|
|
9
|
+
classpath 'com.android.tools.build:gradle:4.1.3'
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
apply plugin: 'com.android.library'
|
|
14
|
+
|
|
15
|
+
android {
|
|
16
|
+
compileSdkVersion 30
|
|
17
|
+
buildToolsVersion "30.0.3"
|
|
18
|
+
|
|
19
|
+
defaultConfig {
|
|
20
|
+
minSdkVersion 21
|
|
21
|
+
targetSdkVersion 27
|
|
22
|
+
versionCode 1
|
|
23
|
+
versionName "1.0"
|
|
24
|
+
}
|
|
25
|
+
lintOptions {
|
|
26
|
+
abortOnError false
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
repositories {
|
|
31
|
+
google()
|
|
32
|
+
jcenter()
|
|
33
|
+
mavenCentral()
|
|
34
|
+
maven {
|
|
35
|
+
// All of React Native (JS, Obj-C sources, Android binaries) is installed from npm
|
|
36
|
+
url "$rootDir/../node_modules/react-native/android"
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
rootProject.allprojects {
|
|
41
|
+
repositories {
|
|
42
|
+
maven {
|
|
43
|
+
url "https://download2.dynamsoft.com/maven/aar"
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
dependencies {
|
|
49
|
+
implementation 'com.dynamsoft:dynamsoftcameraenhancer:2.1.4@aar'
|
|
50
|
+
implementation 'com.dynamsoft:dynamsoftbarcodereader:9.0.2@aar'
|
|
51
|
+
|
|
52
|
+
implementation 'com.facebook.react:react-native:+'
|
|
53
|
+
}
|
|
54
|
+
|
|
Binary file
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
package com.dynamsoft.reactlibrary;
|
|
2
|
+
|
|
3
|
+
import com.dynamsoft.dce.CameraEnhancerException;
|
|
4
|
+
import com.dynamsoft.dce.DCECameraView;
|
|
5
|
+
import com.dynamsoft.dce.DCEFrame;
|
|
6
|
+
import com.dynamsoft.dce.DCEFrameListener;
|
|
7
|
+
import com.dynamsoft.dce.EnumResolution;
|
|
8
|
+
import com.facebook.react.bridge.Arguments;
|
|
9
|
+
import com.facebook.react.bridge.LifecycleEventListener;
|
|
10
|
+
import com.facebook.react.bridge.ReactApplicationContext;
|
|
11
|
+
import com.facebook.react.bridge.ReactContext;
|
|
12
|
+
import com.facebook.react.bridge.WritableMap;
|
|
13
|
+
import com.facebook.react.modules.core.DeviceEventManagerModule;
|
|
14
|
+
import com.facebook.react.uimanager.ThemedReactContext;
|
|
15
|
+
import static com.dynamsoft.reactlibrary.RNDCECameraViewManager.mCamera;
|
|
16
|
+
|
|
17
|
+
public class RNDCECameraView extends DCECameraView implements LifecycleEventListener {
|
|
18
|
+
|
|
19
|
+
public RNDCECameraView(ThemedReactContext context, ReactApplicationContext appContext) {
|
|
20
|
+
super(context);
|
|
21
|
+
context.addLifecycleEventListener(this);
|
|
22
|
+
mCamera.setCameraView(this);
|
|
23
|
+
// mCamera.addListener(new DCEFrameListener() {
|
|
24
|
+
// @Override
|
|
25
|
+
// public void frameOutputCallback(DCEFrame dceFrame, long l) {
|
|
26
|
+
// WritableMap frame = Arguments.createMap();
|
|
27
|
+
// frame.putInt("id",dceFrame.getFrameId());
|
|
28
|
+
// frame.putInt("width",dceFrame.getWidth());
|
|
29
|
+
// frame.putInt("height",dceFrame.getHeight());
|
|
30
|
+
// frame.putInt("stride",dceFrame.getStrides()[0]);
|
|
31
|
+
// frame.putInt("dataLength",dceFrame.getImageData().length);
|
|
32
|
+
// frame.putInt("format",dceFrame.getPixelFormat());
|
|
33
|
+
//
|
|
34
|
+
// ReactContext reactContext = (ReactContext)getContext();
|
|
35
|
+
// reactContext.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class).emit(
|
|
36
|
+
// "getFrameFromBuffer",
|
|
37
|
+
// frame);
|
|
38
|
+
// }
|
|
39
|
+
// });
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
@Override
|
|
43
|
+
public void requestLayout() {
|
|
44
|
+
super.requestLayout();
|
|
45
|
+
// The spinner relies on a measure + layout pass happening after it calls requestLayout().
|
|
46
|
+
// Without this, the widget never actually changes the selection and doesn't call the
|
|
47
|
+
// appropriate listeners. Since we override onLayout in our ViewGroups, a layout pass never
|
|
48
|
+
// happens after a call to requestLayout, so we simulate one here.
|
|
49
|
+
post(measureAndLayout);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
private final Runnable measureAndLayout = new Runnable() {
|
|
53
|
+
@Override
|
|
54
|
+
public void run() {
|
|
55
|
+
measure(MeasureSpec.makeMeasureSpec(getWidth(), MeasureSpec.EXACTLY),
|
|
56
|
+
MeasureSpec.makeMeasureSpec(getHeight(), MeasureSpec.EXACTLY));
|
|
57
|
+
|
|
58
|
+
layout(getLeft(), getTop(), getRight(), getBottom());
|
|
59
|
+
}
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
@Override
|
|
63
|
+
public void onHostResume() {
|
|
64
|
+
if(mCamera!=null){
|
|
65
|
+
try {
|
|
66
|
+
mCamera.open();
|
|
67
|
+
} catch (CameraEnhancerException e) {
|
|
68
|
+
e.printStackTrace();
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
@Override
|
|
74
|
+
public void onHostPause() {
|
|
75
|
+
if(mCamera != null) {
|
|
76
|
+
try {
|
|
77
|
+
mCamera.close();
|
|
78
|
+
} catch (CameraEnhancerException e) {
|
|
79
|
+
e.printStackTrace();
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
@Override
|
|
85
|
+
public void onHostDestroy() {
|
|
86
|
+
if(mCamera != null) {
|
|
87
|
+
mCamera = null;
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
}
|
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
package com.dynamsoft.reactlibrary;
|
|
2
|
+
|
|
3
|
+
import android.app.Activity;
|
|
4
|
+
import android.util.Log;
|
|
5
|
+
|
|
6
|
+
import com.dynamsoft.dce.CameraEnhancer;
|
|
7
|
+
import com.dynamsoft.dce.CameraEnhancerException;
|
|
8
|
+
import com.dynamsoft.dce.EnumResolution;
|
|
9
|
+
import com.dynamsoft.dce.RegionDefinition;
|
|
10
|
+
import com.facebook.react.bridge.ReactApplicationContext;
|
|
11
|
+
import com.facebook.react.bridge.ReadableArray;
|
|
12
|
+
import com.facebook.react.bridge.ReadableMap;
|
|
13
|
+
import com.facebook.react.bridge.ReadableType;
|
|
14
|
+
import com.facebook.react.uimanager.ThemedReactContext;
|
|
15
|
+
import com.facebook.react.uimanager.ViewGroupManager;
|
|
16
|
+
import com.facebook.react.uimanager.annotations.ReactProp;
|
|
17
|
+
|
|
18
|
+
import java.lang.reflect.Field;
|
|
19
|
+
import java.lang.reflect.InvocationTargetException;
|
|
20
|
+
import java.util.HashMap;
|
|
21
|
+
import java.util.Map;
|
|
22
|
+
import java.util.Objects;
|
|
23
|
+
|
|
24
|
+
import javax.annotation.Nullable;
|
|
25
|
+
|
|
26
|
+
public class RNDCECameraViewManager extends ViewGroupManager<RNDCECameraView> {
|
|
27
|
+
private static final String OPEN = "open";
|
|
28
|
+
private static final int OPEN_COMMAND = 1;
|
|
29
|
+
private static final String CLOSE = "close";
|
|
30
|
+
private static final int CLOSE_COMMAND = 2;
|
|
31
|
+
|
|
32
|
+
ReactApplicationContext mReactApplicationContext;
|
|
33
|
+
public static CameraEnhancer mCamera;
|
|
34
|
+
|
|
35
|
+
public RNDCECameraViewManager(ReactApplicationContext reactContext) {
|
|
36
|
+
mReactApplicationContext = reactContext;
|
|
37
|
+
mCamera = new CameraEnhancer(Objects.requireNonNull(getActivity()));
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
private static final String REACT_CLASS = "DYSCameraView";
|
|
41
|
+
|
|
42
|
+
@Override
|
|
43
|
+
public String getName() {
|
|
44
|
+
return REACT_CLASS;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
public static Activity getActivity() {
|
|
48
|
+
Class activityThreadClass = null;
|
|
49
|
+
try {
|
|
50
|
+
activityThreadClass = Class.forName("android.app.ActivityThread");
|
|
51
|
+
Object activityThread = activityThreadClass.getMethod("currentActivityThread").invoke(null);
|
|
52
|
+
Field activitiesField = activityThreadClass.getDeclaredField("mActivities");
|
|
53
|
+
activitiesField.setAccessible(true);
|
|
54
|
+
Map activities = (Map) activitiesField.get(activityThread);
|
|
55
|
+
for (Object activityRecord : activities.values()) {
|
|
56
|
+
Class activityRecordClass = activityRecord.getClass();
|
|
57
|
+
Field pausedField = activityRecordClass.getDeclaredField("paused");
|
|
58
|
+
pausedField.setAccessible(true);
|
|
59
|
+
if (!pausedField.getBoolean(activityRecord)) {
|
|
60
|
+
Field activityField = activityRecordClass.getDeclaredField("activity");
|
|
61
|
+
activityField.setAccessible(true);
|
|
62
|
+
Activity activity = (Activity) activityField.get(activityRecord);
|
|
63
|
+
return activity;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
} catch (ClassNotFoundException e) {
|
|
67
|
+
e.printStackTrace();
|
|
68
|
+
} catch (NoSuchMethodException e) {
|
|
69
|
+
e.printStackTrace();
|
|
70
|
+
} catch (IllegalAccessException e) {
|
|
71
|
+
e.printStackTrace();
|
|
72
|
+
} catch (InvocationTargetException e) {
|
|
73
|
+
e.printStackTrace();
|
|
74
|
+
} catch (NoSuchFieldException e) {
|
|
75
|
+
e.printStackTrace();
|
|
76
|
+
}
|
|
77
|
+
return null;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
@Override
|
|
81
|
+
protected RNDCECameraView createViewInstance(ThemedReactContext reactContext) {
|
|
82
|
+
return new RNDCECameraView(reactContext, mReactApplicationContext);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
@ReactProp(name = "overlayVisible")
|
|
86
|
+
public void setOverlayVisible(RNDCECameraView view, boolean isVisible) {
|
|
87
|
+
view.setOverlayVisible(isVisible);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
@ReactProp(name = "scanRegionVisible")
|
|
91
|
+
public void setScanRegionVisible(RNDCECameraView view, boolean isVisible) {
|
|
92
|
+
mCamera.setScanRegionVisible(isVisible);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
@ReactProp(name = "scanRegion")
|
|
96
|
+
public void setScanRegion(RNDCECameraView view, ReadableMap scanRegion) {
|
|
97
|
+
if (scanRegion != null) {
|
|
98
|
+
int left = scanRegion.getInt("regionLeft");
|
|
99
|
+
int top = scanRegion.getInt("regionTop");
|
|
100
|
+
int right = scanRegion.getInt("regionRight");
|
|
101
|
+
int bottom = scanRegion.getInt("regionBottom");
|
|
102
|
+
|
|
103
|
+
RegionDefinition regionDefinition = new RegionDefinition();
|
|
104
|
+
regionDefinition.regionLeft = left;
|
|
105
|
+
regionDefinition.regionRight = right;
|
|
106
|
+
regionDefinition.regionTop = top;
|
|
107
|
+
regionDefinition.regionBottom = bottom;
|
|
108
|
+
|
|
109
|
+
ReadableType byPercentageType = scanRegion.getType("regionMeasuredByPercentage");
|
|
110
|
+
if (byPercentageType == ReadableType.Boolean) {
|
|
111
|
+
regionDefinition.regionMeasuredByPercentage = scanRegion.getBoolean("regionMeasuredByPercentage") ? 1 : 0;
|
|
112
|
+
} else if (byPercentageType == ReadableType.Number) {
|
|
113
|
+
regionDefinition.regionMeasuredByPercentage = scanRegion.getInt("regionMeasuredByPercentage") > 0 ? 1 : 0;
|
|
114
|
+
} else {
|
|
115
|
+
regionDefinition.regionMeasuredByPercentage = 1;
|
|
116
|
+
}
|
|
117
|
+
try {
|
|
118
|
+
mCamera.setScanRegion(regionDefinition);
|
|
119
|
+
} catch (CameraEnhancerException e) {
|
|
120
|
+
e.printStackTrace();
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
@Nullable
|
|
126
|
+
@Override
|
|
127
|
+
public Map<String, Integer> getCommandsMap() {
|
|
128
|
+
Map<String, Integer> map = new HashMap<>();
|
|
129
|
+
map.put(OPEN, OPEN_COMMAND);
|
|
130
|
+
map.put(CLOSE, CLOSE_COMMAND);
|
|
131
|
+
return map;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
@Override
|
|
135
|
+
public void receiveCommand(RNDCECameraView root, int commandId, @Nullable ReadableArray args) {
|
|
136
|
+
super.receiveCommand(root, commandId, args);
|
|
137
|
+
switch (commandId) {
|
|
138
|
+
case OPEN_COMMAND:
|
|
139
|
+
try {
|
|
140
|
+
mCamera.open();
|
|
141
|
+
} catch (CameraEnhancerException e) {
|
|
142
|
+
e.printStackTrace();
|
|
143
|
+
}
|
|
144
|
+
break;
|
|
145
|
+
case CLOSE_COMMAND:
|
|
146
|
+
try {
|
|
147
|
+
mCamera.close();
|
|
148
|
+
} catch (CameraEnhancerException e) {
|
|
149
|
+
e.printStackTrace();
|
|
150
|
+
}
|
|
151
|
+
break;
|
|
152
|
+
default:
|
|
153
|
+
break;
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
}
|