-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathApp.tsx
More file actions
105 lines (91 loc) · 2.94 KB
/
Copy pathApp.tsx
File metadata and controls
105 lines (91 loc) · 2.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/**
* Sample React Native App
* https://github.com/facebook/react-native
*
* @format
*/
import {
CameraEnhancer,
CameraView,
CaptureVisionRouter,
EnumPresetTemplate,
LicenseManager,
} from 'dynamsoft-capture-vision-react-native';
import React, {useEffect, useRef} from 'react';
import {Alert, AppState, StyleSheet, View} from 'react-native';
// Initialize the license.
// The license string here is a trial license. Note that network connection is required for this license to work.
// You can request an extension via the following link: https://www.dynamsoft.com/customer/license/trialLicense?product=dbr&utm_source=samples&package=react-native
const License = 'DLS2eyJvcmdhbml6YXRpb25JRCI6IjIwMDAwMSJ9';
LicenseManager.initLicense(License).catch(e => {
Alert.alert('License error', e.message);
});
function App(): React.JSX.Element {
const cameraView = useRef<CameraView>(null!);
const cvr = CaptureVisionRouter.getInstance();
const camera = CameraEnhancer.getInstance();
useEffect(() => {
// Request camera permission once
CameraEnhancer.requestCameraPermission();
// Configure router, camera and view
cvr.setInput(camera);
camera.setCameraView(cameraView.current);
// Barcode result handler
const receiver = cvr.addResultReceiver({
onDecodedBarcodesReceived: ({items}) => {
if (items?.length) {
cvr.stopCapturing();
Alert.alert(
`Barcodes count: ${items.length}`,
items.map((barcode, i) => `${i + 1}. ${barcode.formatString}: ${barcode.text}`).join('\n\n'),
[
{
text: 'OK',
onPress: () => {
cvr.startCapturing(EnumPresetTemplate.PT_READ_BARCODES)
.catch(e => Alert.alert('Start error', e.message));
}
}
]
);
}
},
});
// Helper to start camera + scanning
const startScanning = () => {
console.log('Starting...');
camera.open();
cvr.startCapturing(EnumPresetTemplate.PT_READ_BARCODES)
.catch(e => Alert.alert('Start error', e.message));
};
// Helper to stop camera + scanning
const stopScanning = () => {
console.log('Stopping...');
cvr.stopCapturing();
camera.close();
};
// Initial start when component mounts
startScanning();
// Listen to AppState changes
const sub = AppState.addEventListener('change', nextState => {
if (nextState === 'active') {
startScanning();
} else if (nextState.match(/inactive|background/)) {
stopScanning();
}
});
// Cleanup on unmount
return () => {
sub.remove();
stopScanning();
cvr.removeResultReceiver(receiver);
camera.setCameraView(null);
};
}, [cvr, camera]);
return (
<View style={StyleSheet.absoluteFill}>
<CameraView style={StyleSheet.absoluteFill} ref={cameraView}/>
</View>
);
}
export default App;