1+ import React from 'react' ;
2+ import { Text } from 'react-native' ;
3+ import {
4+ DynamsoftBarcodeReader ,
5+ DynamsoftCameraView ,
6+ BarcodeResult ,
7+ EnumDBRPresetTemplate ,
8+ Region ,
9+ EnumBarcodeFormat ,
10+ DBRRuntimeSettings
11+ } from 'dynamsoft-capture-vision-react-native' ;
12+
13+
14+ class App extends React . Component {
15+ state = {
16+ results : null
17+ } ;
18+
19+ componentDidMount ( ) {
20+ ( async ( ) => {
21+ try {
22+ await DynamsoftBarcodeReader . initLicense ( "DLS2eyJvcmdhbml6YXRpb25JRCI6IjIwMDAwMSJ9" )
23+ } catch ( e ) {
24+ console . log ( e )
25+ }
26+
27+
28+ this . reader = await DynamsoftBarcodeReader . createInstance ( ) ;
29+ await this . reader . updateDBRRuntimeSettings ( EnumDBRPresetTemplate . DEFAULT ) ;
30+ let settings : DBRRuntimeSettings = await this . reader . getDBRRuntimeSettings ( ) ;
31+ // Set the expected barcode count to 0 when you are not sure how many barcodes you are scanning.
32+ // Set the expected barcode count to 1 can maximize the barcode decoding speed.
33+ settings . expectedBarcodesCount = 0 ;
34+ settings . barcodeFormatIds = EnumBarcodeFormat . BF_ONED | EnumBarcodeFormat . BF_QR_CODE ;
35+ await this . reader . updateDBRRuntimeSettings ( settings )
36+
37+ await this . reader . startScanning ( ) ;
38+ this . reader . addResultListener ( ( results : BarcodeResult [ ] ) => {
39+ this . setState ( { results : results } )
40+ } )
41+ } ) ( ) ;
42+
43+
44+ }
45+
46+ async componentWillUnmount ( ) {
47+ await this . reader . stopScanning ( )
48+ this . reader . removeResultListener ( )
49+ }
50+
51+
52+ render ( ) {
53+ let region : Region ;
54+ let barcode_text = "" ;
55+ region = {
56+ regionTop : 30 ,
57+ regionLeft : 15 ,
58+ regionBottom : 70 ,
59+ regionRight : 85 ,
60+ regionMeasuredByPercentage : true
61+ }
62+ let results : BarcodeResult [ ] = this . state . results ;
63+ if ( results && results . length > 0 ) {
64+ for ( var i = 0 ; i < results . length ; i ++ ) {
65+ barcode_text += results [ i ] . barcodeFormatString + ":" + results [ i ] . barcodeText + "\n"
66+ }
67+ }
68+
69+ return (
70+ < DynamsoftCameraView
71+ style = { {
72+ flex : 1 ,
73+ } }
74+ ref = { ( ref ) => { this . scanner = ref } }
75+ isOverlayVisible = { true }
76+ isScanRegionVisible = { true }
77+ scanRegion = { region }
78+ >
79+ < Text style = { {
80+ flex : 0.9 ,
81+ textAlignVertical : "bottom" ,
82+ textAlign : "center" ,
83+ color : "white" ,
84+ fontSize : 18 ,
85+ } } > { results && results . length > 0 ? barcode_text : "null" } </ Text >
86+ </ DynamsoftCameraView >
87+
88+
89+ ) ;
90+ }
91+ }
92+
93+ export default App ;
0 commit comments