A BarCodeScanner telepítése:
npx expo install expo-barcode-scanner
A következő példa egy másolat a következő oldalról, magyar nyelvre átírva:
import { BarCodeScanner } from 'expo-barcode-scanner'; import { StatusBar } from 'expo-status-bar'; import { useEffect, useState } from 'react'; import { Button, StyleSheet, Text, View } from 'react-native'; export default function App() { const [hasPermission, setHasPermission] = useState(null); const [scanned, setScanned] = useState(false); useEffect(() => { const getBarCodeScannerPermissions = async () => { const { status } = await BarCodeScanner.requestPermissionsAsync(); setHasPermission(status === 'granted'); }; getBarCodeScannerPermissions(); }, []); const handleBarCodeScanned = ({ type, data }) => { setScanned(true); alert(`A vonalkód típusa ${type}\nAz olvasott adat: ${data}`); }; if (hasPermission === null) { return <Text>Szükséges a kamerához hozzáférés</Text>; } if (hasPermission === false) { return <Text>A kamera nem elérhető</Text>; } return ( <View style={styles.container}> <BarCodeScanner onBarCodeScanned={scanned ? undefined : handleBarCodeScanned} style={StyleSheet.absoluteFillObject} /> {scanned && <Button title={'Koppints az újabb szkenneléshez'} onPress={() => setScanned(false)} />} <StatusBar style="auto" /> </View> ); } const styles = StyleSheet.create({ container: { flex: 1, backgroundColor: '#fff', alignItems: 'center', justifyContent: 'center', }, });
A kamera-olvasó nem jelenik meg azonnal. Gombnyomásra indul.
import { BarCodeScanner } from 'expo-barcode-scanner'; import { StatusBar } from 'expo-status-bar'; import { useEffect, useState } from 'react'; import { Button, StyleSheet, Text, View } from 'react-native'; export default function App() { const [hasPermission, setHasPermission] = useState(null); const [scanned, setScanned] = useState(false); const [scanning, setScanning] = useState(false); useEffect(() => { const getBarCodeScannerPermissions = async () => { const { status } = await BarCodeScanner.requestPermissionsAsync(); setHasPermission(status === 'granted'); }; getBarCodeScannerPermissions(); }, []); const handleBarCodeScanned = ({ type, data }) => { setScanned(true); alert(`A vonalkód típusa ${type}\nAz olvasott adat: ${data}`); }; if (hasPermission === null) { return <Text>Szükséges a kamerához hozzáférés</Text>; } if (hasPermission === false) { return <Text>A kamera nem elérhető</Text>; } return ( <View style={styles.container}> <Text>A "Szkennelés" gombra kattintva elindual a QR-kód szkenner.</Text> <Button title={'Szkennelés'} onPress={() => setScanning(true)}/> { scanning && <BarCodeScanner onBarCodeScanned={scanned ? undefined : handleBarCodeScanned} style={StyleSheet.absoluteFillObject} /> } {(scanned && scanning) && <Button title={'Tap to Scan Again'} onPress={() => setScanned(false)} />} {scanning && <Button title={'Bezár'} onPress={() => setScanning(false)} />} <StatusBar style="auto" /> </View> ); } const styles = StyleSheet.create({ container: { flex: 1, backgroundColor: '#fff', alignItems: 'center', justifyContent: 'center', }, });