-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathReactNativeVisionCameraQRCODESCANNER.js
173 lines (157 loc) · 4.43 KB
/
ReactNativeVisionCameraQRCODESCANNER.js
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import React, { useState, useEffect, useCallback } from 'react';
import {
SafeAreaView,
Text,
View,
StyleSheet,
Alert,
TouchableOpacity,
Image,
} from 'react-native';
import {
Camera,
useCameraDevice,
useCodeScanner,
getCameraDevice,
} from 'react-native-vision-camera';
import Ionicons from 'react-native-vector-icons/Ionicons';
import { useFocusEffect } from '@react-navigation/native';
const App = ({ navigation }) => {
const [hasPermission, setHasPermission] = useState(false);
const [isScanning, setIsScanning] = useState(true);
const [flashMode, setFlashMode] = useState(false);
const [cameraPosition, setCameraPosition] = useState('back');
const [zoomLevel, setZoomLevel] = useState(1); // Initialize zoom level to 1 (no zoom)
const device = useCameraDevice(cameraPosition); // Use cameraPosition to dynamically select device
useFocusEffect(
useCallback(() => {
const getPermissions = async () => {
const cameraPermission = await Camera.requestCameraPermission();
setHasPermission(cameraPermission === 'authorized');
};
getPermissions();
}, [])
);
const codeScanner = useCodeScanner({
codeTypes: ['code-128'],
onCodeScanned: (codes) => {
for (const code of codes) {
setIsScanning(false);
Alert.alert('Scanned Code', `${code.value}`, [
{
text: 'OK',
onPress: () => setIsScanning(true), // Resume scanning after alert
},
]);
}
},
});
const toggleFlash = () => {
setFlashMode((prev) => !prev);
};
const toggleCamera = () =>
setCameraPosition((prev) => (prev === 'back' ? 'front' : 'back'));
const zoomIn = () => {
if (zoomLevel < 3) {
setZoomLevel((prevZoom) => prevZoom + 0.1); // Daha hassas zoom
}
};
const zoomOut = () => {
if (zoomLevel > 1) {
setZoomLevel((prevZoom) => prevZoom - 0.1);
}
};
if (!device) {
return <></>;
}
return (
<SafeAreaView style={styles.container}>
<Camera
style={StyleSheet.absoluteFill}
device={device} // Use the fetched camera device
isActive={true}
frameProcessorFps={2}
torch={flashMode ? 'on' : 'off'} // Use torch instead of flash
zoom={zoomLevel} // Use the updated zoomLevel
codeScanner={isScanning ? codeScanner : undefined}
/>
<Image source={require('./scanning.png')} style={styles.centerImage} />
{/* Back Icon */}
<TouchableOpacity style={styles.backButton} onPress={() => Alert.alert('Go Back')}>
<Ionicons name="arrow-back" size={30} color="#fff" />
</TouchableOpacity>
{/* Flashlight Toggle */}
<TouchableOpacity style={styles.flashButton} onPress={toggleFlash}>
<Ionicons name={flashMode ? 'flash' : 'flash-off'} size={30} color="#fff" />
</TouchableOpacity>
{/* Camera Switch */}
<TouchableOpacity style={styles.toggleButton} onPress={toggleCamera}>
<Ionicons name="camera-reverse" size={30} color="#fff" />
</TouchableOpacity>
{/* Zoom In */}
<TouchableOpacity style={styles.zoomInButton} onPress={zoomIn}>
<Ionicons name="add" size={30} color="#fff" />
</TouchableOpacity>
{/* Zoom Out */}
<TouchableOpacity style={styles.zoomOutButton} onPress={zoomOut}>
<Ionicons name="remove" size={30} color="#fff" />
</TouchableOpacity>
</SafeAreaView>
);
};
const styles = StyleSheet.create({
container: {
flex: 1,
},
centerImage: {
width: 200,
height: 200,
position: 'absolute',
left: '50%',
top: '50%',
marginLeft: -100,
marginTop: -100,
opacity: 0.6,
},
backButton: {
position: 'absolute',
top: 20,
left: 20,
padding: 10,
backgroundColor: 'rgba(0, 0, 0, 0.6)',
borderRadius: 50,
},
flashButton: {
position: 'absolute',
top: 20,
right: 20,
padding: 10,
backgroundColor: 'rgba(0, 0, 0, 0.6)',
borderRadius: 50,
},
toggleButton: {
position: 'absolute',
bottom: 20,
left: 20,
backgroundColor: 'rgba(0, 0, 0, 0.6)',
padding: 10,
borderRadius: 50,
},
zoomInButton: {
position: 'absolute',
bottom: 80,
right: 20,
backgroundColor: 'rgba(0, 0, 0, 0.6)',
padding: 10,
borderRadius: 50,
},
zoomOutButton: {
position: 'absolute',
bottom: 20,
right: 20,
backgroundColor: 'rgba(0, 0, 0, 0.6)',
padding: 10,
borderRadius: 50,
},
});
export default App;