是否有人有如何在 React Native cli 中读取二维码的教程或代码,并且可以在当前的 React Native 版本中实际工作?我尝试了很多教程和文档,但没有任何效果。如果它能在 tsx 运行就好了。
使用React Native中的react-native-qrcode-scanner包来扫描二维码。以下是如何使用它的说明:
import QRCodeScanner from 'react-native-qrcode-scanner';
const MyQRCodeScanner = () => {
const onSuccess = (e) => {
console.log(e.data);
// e.data contains the QR code data
};
return (
<QRCodeScanner onRead={onSuccess} />
);
};
最好的解决方案是使用https://github.com/mrousavy/react-native-vision-camera,因为它基于ML Kit为该相机使用JSI和帧处理器 https://github.com/rodgomesc/vision-camera-code-scanner
import * as React from 'react';
import { StyleSheet, Text } from 'react-native';
import { Camera, useCameraDevices } from 'react-native-vision-camera';
import { useScanBarcodes, BarcodeFormat } from 'vision-camera-code-scanner';
export default function App() {
const [hasPermission, setHasPermission] = React.useState(false);
const devices = useCameraDevices();
const device = devices.back;
const [frameProcessor, barcodes] = useScanBarcodes([BarcodeFormat.QR_CODE], {
checkInverted: true,
});
// Alternatively you can use the underlying function:
//
// const frameProcessor = useFrameProcessor((frame) => {
// 'worklet';
// const detectedBarcodes = scanBarcodes(frame, [BarcodeFormat.QR_CODE], { checkInverted: true });
// runOnJS(setBarcodes)(detectedBarcodes);
// }, []);
React.useEffect(() => {
(async () => {
const status = await Camera.requestCameraPermission();
setHasPermission(status === 'authorized');
})();
}, []);
return (
device != null &&
hasPermission && (
<>
<Camera
style={StyleSheet.absoluteFill}
device={device}
isActive={true}
frameProcessor={frameProcessor}
frameProcessorFps={5}
/>
{barcodes.map((barcode, idx) => (
<Text key={idx} style={styles.barcodeTextURL}>
{barcode.displayValue}
</Text>
))}
</>
)
);
}
const styles = StyleSheet.create({
barcodeTextURL: {
fontSize: 20,
color: 'white',
fontWeight: 'bold',
},
});
您可以使用两个库进行二维码扫描 然后添加所有权限
react-native-qrcode-scanner
react-native-camera
代码:
const [barcode, setBarcode] = useState(null);
<View style={styles.screen}>
<View style={styles.caption}>
<Text style={styles.captionTitleText}>QR Code</Text>
</View>
{barcode ? (
<View style={{alignContent: 'center', alignItems: 'center'}}>
<TouchableOpacity
style={{
backgroundColor: 'navy',
borderRadius: 10,
paddingHorizontal: 15,
}}
onPress={() =>
navigation.navigate('Your next screen')
}>
<Text style={styles.rmCameraResultText2}>
Scan Successfully. Tap to fill the Audit.
</Text>
</TouchableOpacity>
</View>
) : (
<RNCamera style={styles.rnCamera} onBarCodeRead={setBarcode} />
)}
<View style={styles.cameraControl}>
<TouchableOpacity style={styles.btn} onPress={() => setBarcode(null)}>
<Text style={styles.btnText}>New QR Scan</Text>
</TouchableOpacity>
</View>
</View>
const styles = StyleSheet.create({
screen: {
flex: 1,
backgroundColor: '#F2F2FC',
},
saveArea: {
backgroundColor: '#62d1bc',
},
topBar: {
height: 50,
backgroundColor: '#62d1bc',
alignItems: 'center',
justifyContent: 'center',
},
topBarTitleText: {
color: '#ffffff',
fontSize: 20,
},
caption: {
height: 120,
justifyContent: 'center',
alignItems: 'center',
},
captionTitleText: {
color: '#121B0D',
fontSize: 16,
fontWeight: '600',
},
btn: {
width: 240,
borderRadius: 4,
backgroundColor: '#326A81',
paddingHorizontal: 20,
paddingVertical: 10,
marginVertical: 8,
},
btnText: {
fontSize: 18,
color: '#ffffff',
textAlign: 'center',
},
rnCamera: {
flex: 1,
width: '94%',
alignSelf: 'center',
},
rmCameraResult: {
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#eeeeee',
},
rmCameraResultText: {
fontSize: 15,
color: '#326A81',
},
rmCameraResultText2: {
fontSize: 20,
color: 'white',
},
cameraControl: {
height: 180,
justifyContent: 'center',
alignItems: 'center',
},
一种方法是使用 vision-camera-dynamsoft-barcode-reader。
使用react-native-vision-camera进行实时扫描(用作其帧处理器)。
import { decode } from 'vision-camera-dynamsoft-barcode-reader';
const frameProcessor = useFrameProcessor((frame) => {
'worklet';
const barcodes = decode(frame);
}, []);
从 Base64 编码的静态图像中读取条形码。
let results = await decodeBase64(base64);