1- import React from 'react' ;
2- import { Button , View } from 'react-native' ;
1+ import React , { useState } from 'react' ;
2+ import {
3+ Button ,
4+ Modal ,
5+ PermissionsAndroid ,
6+ StyleSheet ,
7+ Text ,
8+ TouchableOpacity ,
9+ View ,
10+ } from 'react-native' ;
311import { NavigationContainer } from '@react-navigation/native' ;
412import { createNativeStackNavigator } from '@react-navigation/native-stack' ;
513import BarcodeScanner from './BarcodeScanner' ;
6- import { DynamsoftBarcodeReader } from 'henry-capture-vision-react-native' ;
14+ import {
15+ BarcodeResult ,
16+ DCVBarcodeReader ,
17+ EnumDBRPresetTemplate ,
18+ } from 'henry-capture-vision-react-native' ;
19+ import { launchCamera , launchImageLibrary } from 'react-native-image-picker' ;
20+
21+ let mdbr ;
22+
23+ const option = {
24+ mediaType : 'photo' ,
25+ maxWidth : 2000 ,
26+ maxHeight : 2000 ,
27+ quality : 0.8 ,
28+ } ;
29+
30+ const requestPermissions = async ( ) => {
31+ try {
32+ if (
33+ ! ( await PermissionsAndroid . check ( PermissionsAndroid . PERMISSIONS . CAMERA ) )
34+ ) {
35+ await PermissionsAndroid . request ( PermissionsAndroid . PERMISSIONS . CAMERA ) ;
36+ }
37+ } catch ( err ) {
38+ console . log ( err ) ;
39+ }
40+ } ;
41+
42+ const decodeFile = async filePath => {
43+ try {
44+ return await mdbr . decodeFile ( filePath ) ;
45+ } catch ( err ) {
46+ return null ;
47+ }
48+ } ;
49+
50+ const mergeResultsText = ( results : BarcodeResult [ ] ) => {
51+ let str = '' ;
52+ if ( results && results . length > 0 ) {
53+ for ( let i = 0 ; i < results . length ; i ++ ) {
54+ str +=
55+ results [ i ] . barcodeFormatString + ': ' + results [ i ] . barcodeText + ' \n' ;
56+ }
57+ } else {
58+ str = 'No barcode detected.' ;
59+ }
60+ return str ;
61+ } ;
62+
63+ const useImagePicker = ( imagePickerLauncher , setModalVisible , setModalText ) => {
64+ imagePickerLauncher ( option ) . then ( res => {
65+ if ( res . didCancel ) {
66+ setModalVisible ( false ) ;
67+ return false ;
68+ }
69+ decodeFile ( res . assets [ 0 ] . uri . split ( 'file://' ) [ 1 ] ) . then ( results => {
70+ let str = mergeResultsText ( results ) ;
71+ setModalVisible ( true ) ;
72+ setModalText ( str ) ;
73+ } ) ;
74+ } ) ;
75+ setModalText ( 'Decoding...' ) ;
76+ setModalVisible ( true ) ;
77+ } ;
778
879function HomeScreen ( { navigation} ) {
980 ( async ( ) => {
1081 // Initialize the license so that you can use full feature of the Barcode Reader module.
1182 try {
12- await DynamsoftBarcodeReader . initLicense ( 'DLS2eyJvcmdhbml6YXRpb25JRCI6IjIwMDAwMSJ9' ) ;
83+ await DCVBarcodeReader . initLicense (
84+ 'DLS2eyJvcmdhbml6YXRpb25JRCI6IjIwMDAwMSJ9' ,
85+ ) ;
86+
87+ mdbr = await DCVBarcodeReader . createInstance ( ) ;
88+ await mdbr . updateRuntimeSettings (
89+ EnumDBRPresetTemplate . IMAGE_READ_RATE_FIRST ,
90+ ) ;
1391 } catch ( e ) {
1492 console . log ( e ) ;
1593 }
1694 } ) ( ) ;
1795
96+ const [ modalVisible , setModalVisible ] = useState ( false ) ;
97+ const [ modalText , setModalText ] = useState ( '' ) ;
98+
1899 return (
19- < View style = { { flex : 1 , alignItems : 'center' , justifyContent : 'center' } } >
100+ < View style = { styles . contentView } >
101+ < Modal
102+ animationType = "slide"
103+ transparent = { true }
104+ visible = { modalVisible }
105+ onRequestClose = { ( ) => {
106+ setModalVisible ( ! modalVisible ) ;
107+ } } >
108+ < TouchableOpacity
109+ activeOpacity = { 1 }
110+ onPress = { ( ) => {
111+ console . log ( 'false' ) ;
112+ setModalVisible ( false ) ;
113+ } }
114+ style = { styles . centeredView } >
115+ < View style = { styles . modalView } >
116+ < Text style = { styles . modalText } > { modalText } </ Text >
117+ </ View >
118+ </ TouchableOpacity >
119+ </ Modal >
120+
121+ < Button
122+ title = "Take Photo"
123+ onPress = { ( ) => {
124+ requestPermissions ( ) . then ( ( ) => {
125+ // eslint-disable-next-line react-hooks/rules-of-hooks
126+ useImagePicker ( launchCamera , setModalVisible , setModalText ) ;
127+ } ) ;
128+ } }
129+ />
130+ < View style = { styles . splitView } />
131+ < Button
132+ title = "Select Photo"
133+ onPress = { ( ) => {
134+ // eslint-disable-next-line react-hooks/rules-of-hooks
135+ useImagePicker ( launchImageLibrary , setModalVisible , setModalText ) ;
136+ } }
137+ />
138+ < View style = { styles . splitView } />
20139 < Button
21140 title = "Start Scanning"
22141 onPress = { ( ) => navigation . navigate ( 'Barcode' ) }
@@ -38,4 +157,41 @@ function App() {
38157 ) ;
39158}
40159
160+ const styles = StyleSheet . create ( {
161+ contentView : {
162+ flex : 1 ,
163+ alignItems : 'center' ,
164+ justifyContent : 'center' ,
165+ flexDirection : 'column' ,
166+ } ,
167+ centeredView : {
168+ flex : 1 ,
169+ backgroundColor : '#00000000' ,
170+ justifyContent : 'center' ,
171+ alignItems : 'center' ,
172+ marginTop : 20 ,
173+ } ,
174+ modalView : {
175+ margin : 20 ,
176+ backgroundColor : 'white' ,
177+ borderRadius : 20 ,
178+ padding : 35 ,
179+ alignItems : 'center' ,
180+ shadowColor : '#000' ,
181+ shadowOffset : {
182+ width : 0 ,
183+ height : 2 ,
184+ } ,
185+ shadowOpacity : 0.25 ,
186+ shadowRadius : 4 ,
187+ elevation : 5 ,
188+ } ,
189+ modalText : {
190+ textAlign : 'center' ,
191+ } ,
192+ splitView : {
193+ height : 100 ,
194+ } ,
195+ } ) ;
196+
41197export default App ;
0 commit comments