Skip to content

Commit 70d73bf

Browse files
refactor: move take photo and select function to scan page
1 parent 99e964b commit 70d73bf

File tree

4 files changed

+146
-158
lines changed

4 files changed

+146
-158
lines changed

BarcodeReaderSimpleSample/App.js

Lines changed: 3 additions & 151 deletions
Original file line numberDiff line numberDiff line change
@@ -1,75 +1,9 @@
1-
import React, {useState} from 'react';
2-
import {
3-
Button,
4-
Modal,
5-
PermissionsAndroid,
6-
Platform,
7-
StyleSheet,
8-
Text,
9-
TouchableOpacity,
10-
View,
11-
} from 'react-native';
1+
import React from 'react';
2+
import {Button, StyleSheet, View} from 'react-native';
123
import {NavigationContainer} from '@react-navigation/native';
134
import {createNativeStackNavigator} from '@react-navigation/native-stack';
145
import BarcodeScanner from './BarcodeScanner';
15-
import {
16-
BarcodeResult,
17-
DCVBarcodeReader,
18-
EnumDBRPresetTemplate,
19-
} from 'henry-capture-vision-react-native';
20-
import {launchCamera, launchImageLibrary} from 'react-native-image-picker';
21-
22-
const option = {
23-
mediaType: 'photo',
24-
maxWidth: 2000,
25-
maxHeight: 2000,
26-
quality: 0.8,
27-
};
28-
29-
const requestPermissions = async () => {
30-
try {
31-
if (Platform.OS === 'android') {
32-
await PermissionsAndroid.request(PermissionsAndroid.PERMISSIONS.CAMERA);
33-
}
34-
} catch (err) {
35-
console.log(err);
36-
}
37-
};
38-
39-
const decodeFile = async filePath => {
40-
return this.dbr.decodeFile(filePath);
41-
};
42-
43-
const mergeResultsText = results => {
44-
let str = '';
45-
if (results && results.length > 0) {
46-
for (let i = 0; i < results.length; i++) {
47-
str +=
48-
results[i].barcodeFormatString + ': ' + results[i].barcodeText + ' \n';
49-
}
50-
} else {
51-
str = 'No barcode detected.';
52-
}
53-
return str;
54-
};
55-
56-
const useImagePicker = (imagePickerLauncher, setModalState) => {
57-
imagePickerLauncher(option, res => {
58-
if (res.didCancel) {
59-
setModalState(modalInitState);
60-
return false;
61-
}
62-
setModalState({isVisible: true, text: 'decoding...'});
63-
decodeFile(res.assets[0].uri.split('file://')[1])
64-
.then(results => {
65-
let str = mergeResultsText(results);
66-
setModalState({isVisible: true, text: str});
67-
})
68-
.catch(err => {
69-
setModalState({isVisible: true, text: err.toString()});
70-
});
71-
});
72-
};
6+
import {DCVBarcodeReader} from 'henry-capture-vision-react-native';
737

748
(async () => {
759
// Initialize the license so that you can use full feature of the Barcode Reader module.
@@ -78,65 +12,11 @@ const useImagePicker = (imagePickerLauncher, setModalState) => {
7812
).catch(e => {
7913
console.log(e);
8014
});
81-
try {
82-
this.dbr = await DCVBarcodeReader.createInstance();
83-
await this.dbr.updateRuntimeSettings(
84-
EnumDBRPresetTemplate.IMAGE_READ_RATE_FIRST,
85-
);
86-
} catch (e) {
87-
console.log(e);
88-
}
8915
})();
9016

91-
const modalInitState = {
92-
isVisible: false,
93-
text: '',
94-
};
95-
9617
function HomeScreen({navigation}) {
97-
// const [modalVisible, setModalVisible] = useState(false);
98-
// const [modalText, setModalText] = useState('');
99-
const [modalState, setModalState] = useState(modalInitState);
100-
10118
return (
10219
<View style={styles.contentView}>
103-
<Modal
104-
animationType="slide"
105-
transparent={true}
106-
visible={modalState.isVisible}
107-
onRequestClose={() => {
108-
setModalState(modalInitState);
109-
}}>
110-
<TouchableOpacity
111-
activeOpacity={1}
112-
onPress={() => {
113-
setModalState(modalInitState);
114-
}}
115-
style={styles.centeredView}>
116-
<View style={styles.modalView}>
117-
<Text style={styles.modalText}>{modalState.text}</Text>
118-
</View>
119-
</TouchableOpacity>
120-
</Modal>
121-
122-
<Button
123-
title="Take Photo"
124-
onPress={() => {
125-
requestPermissions().then(() => {
126-
// eslint-disable-next-line react-hooks/rules-of-hooks
127-
useImagePicker(launchCamera, setModalState);
128-
});
129-
}}
130-
/>
131-
<View style={styles.splitView} />
132-
<Button
133-
title="Select Photo"
134-
onPress={() => {
135-
// eslint-disable-next-line react-hooks/rules-of-hooks
136-
useImagePicker(launchImageLibrary, setModalState);
137-
}}
138-
/>
139-
<View style={styles.splitView} />
14020
<Button
14121
title="Start Scanning"
14222
onPress={() => navigation.navigate('Barcode')}
@@ -165,34 +45,6 @@ const styles = StyleSheet.create({
16545
justifyContent: 'center',
16646
flexDirection: 'column',
16747
},
168-
centeredView: {
169-
flex: 1,
170-
backgroundColor: '#00000000',
171-
justifyContent: 'center',
172-
alignItems: 'center',
173-
marginTop: 20,
174-
},
175-
modalView: {
176-
margin: 20,
177-
backgroundColor: 'white',
178-
borderRadius: 20,
179-
padding: 35,
180-
alignItems: 'center',
181-
shadowColor: '#000',
182-
shadowOffset: {
183-
width: 0,
184-
height: 2,
185-
},
186-
shadowOpacity: 0.25,
187-
shadowRadius: 4,
188-
elevation: 5,
189-
},
190-
modalText: {
191-
textAlign: 'center',
192-
},
193-
splitView: {
194-
height: 100,
195-
},
19648
});
19749

19850
export default App;

BarcodeReaderSimpleSample/BarcodeScanner.js

Lines changed: 136 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,77 @@
11
import React from 'react';
2-
import {Text} from 'react-native';
2+
import {Modal, StyleSheet, Text, TouchableOpacity, View} from 'react-native';
33
import {
4-
DynamsoftBarcodeReader,
5-
DynamsoftCameraView,
4+
DCVBarcodeReader,
5+
DCVCameraView,
66
EnumBarcodeFormat,
77
EnumTorchState,
88
} from 'henry-capture-vision-react-native';
9+
import Entypo from 'react-native-vector-icons/Entypo';
10+
import {launchCamera, launchImageLibrary} from 'react-native-image-picker';
11+
12+
const option = {
13+
mediaType: 'photo',
14+
maxWidth: 2000,
15+
maxHeight: 2000,
16+
quality: 0.7,
17+
};
18+
19+
const mergeResultsText = results => {
20+
let str = '';
21+
if (results && results.length > 0) {
22+
for (let i = 0; i < results.length; i++) {
23+
str +=
24+
results[i].barcodeFormatString + ': ' + results[i].barcodeText + ' \n';
25+
}
26+
} else {
27+
str = 'No barcode detected.';
28+
}
29+
return str;
30+
};
31+
32+
const modalInitState = {
33+
isVisible: false,
34+
modalText: '',
35+
};
936

1037
class BarcodeScanner extends React.Component {
1138
state = {
1239
results: null,
40+
isVisible: false,
41+
modalText: '',
42+
};
43+
44+
decodeFile = async filePath => {
45+
return this.reader.decodeFile(filePath);
46+
};
47+
48+
useImagePicker = imagePickerLauncher => {
49+
console.log(this.reader);
50+
imagePickerLauncher(option, res => {
51+
if (res.didCancel) {
52+
// this.setState(modalInitState);
53+
return false;
54+
}
55+
56+
// setModalState({isVisible: true, modalText: 'decoding...'});
57+
console.log(res.assets[0].uri.split('file://')[1]);
58+
this.decodeFile(res.assets[0].uri.split('file://')[1])
59+
.then(results => {
60+
let str = mergeResultsText(results);
61+
console.log(str);
62+
this.setState({isVisible: true, modalText: str});
63+
})
64+
.catch(err => {
65+
console.log(err);
66+
this.setState({isVisible: true, modalText: err.toString()});
67+
});
68+
});
1369
};
1470

1571
async componentDidMount() {
72+
console.log('start');
1673
// Create a barcode reader instance.
17-
this.reader = await DynamsoftBarcodeReader.createInstance();
74+
this.reader = await DCVBarcodeReader.createInstance();
1875

1976
await this.reader.resetRuntimeSettings();
2077

@@ -45,6 +102,29 @@ class BarcodeScanner extends React.Component {
45102
// If the camera is opened, the barcode reader will start the barcode decoding thread when you triggered the startScanning.
46103
// The barcode reader will scan the barcodes continuously before you trigger stopScanning.
47104
this.reader.startScanning();
105+
106+
this.props.navigation.setOptions({
107+
headerRight: () => (
108+
<View style={styles.headerRight}>
109+
<Entypo
110+
style={{paddingRight: 15}}
111+
name={'camera'}
112+
size={35}
113+
onPress={() => {
114+
this.useImagePicker(launchCamera);
115+
}}
116+
/>
117+
<Entypo
118+
style={{paddingLeft: 15}}
119+
name={'folder-images'}
120+
size={35}
121+
onPress={() => {
122+
this.useImagePicker(launchImageLibrary);
123+
}}
124+
/>
125+
</View>
126+
),
127+
});
48128
}
49129

50130
async componentWillUnmount() {
@@ -72,7 +152,7 @@ class BarcodeScanner extends React.Component {
72152
}
73153

74154
return (
75-
<DynamsoftCameraView
155+
<DCVCameraView
76156
style={{
77157
flex: 1,
78158
}}
@@ -97,9 +177,59 @@ class BarcodeScanner extends React.Component {
97177
}}>
98178
{results && results.length > 0 ? barcode_text : ''}
99179
</Text>
100-
</DynamsoftCameraView>
180+
<Modal
181+
animationType="slide"
182+
transparent={true}
183+
visible={this.state.isVisible}
184+
onRequestClose={() => {
185+
this.setState(modalInitState);
186+
}}>
187+
<TouchableOpacity
188+
activeOpacity={1}
189+
onPress={() => {
190+
this.setState(modalInitState);
191+
}}
192+
style={styles.centeredView}>
193+
<View style={styles.modalView}>
194+
<Text style={styles.modalText}>{this.state.modalText}</Text>
195+
</View>
196+
</TouchableOpacity>
197+
</Modal>
198+
</DCVCameraView>
101199
);
102200
}
103201
}
104202

203+
const styles = StyleSheet.create({
204+
centeredView: {
205+
flex: 1,
206+
backgroundColor: '#00000000',
207+
justifyContent: 'center',
208+
alignItems: 'center',
209+
marginTop: 20,
210+
},
211+
modalView: {
212+
margin: 20,
213+
backgroundColor: 'white',
214+
borderRadius: 20,
215+
padding: 35,
216+
alignItems: 'center',
217+
shadowColor: '#000',
218+
shadowOffset: {
219+
width: 0,
220+
height: 2,
221+
},
222+
shadowOpacity: 0.25,
223+
shadowRadius: 4,
224+
elevation: 5,
225+
},
226+
modalText: {
227+
textAlign: 'center',
228+
},
229+
headerRight: {
230+
flexDirection: 'row',
231+
justifyContent: 'space-between',
232+
},
233+
});
234+
105235
export default BarcodeScanner;

BarcodeReaderSimpleSample/android/app/build.gradle

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,4 +214,9 @@ task copyDownloadableDepsToLibs(type: Copy) {
214214
into 'libs'
215215
}
216216

217+
project.ext.vectoricons = [
218+
iconFontNames: [ 'Entypo.ttf']
219+
]
220+
apply from: "../../node_modules/react-native-vector-icons/fonts.gradle"
221+
217222
apply from: file("../../node_modules/@react-native-community/cli-platform-android/native_modules.gradle"); applyNativeModulesAppBuildGradle(project)

BarcodeReaderSimpleSample/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
"react-native": "0.67.2",
1818
"react-native-image-picker": "^4.10.0",
1919
"react-native-safe-area-context": "^4.2.5",
20-
"react-native-screens": "^3.13.1"
20+
"react-native-screens": "^3.13.1",
21+
"react-native-vector-icons": "^9.2.0"
2122
},
2223
"devDependencies": {
2324
"@babel/core": "^7.12.9",

0 commit comments

Comments
 (0)