import React, { useState } from 'react';
import { StyleSheet, Text, View, Button } from 'react-native';
import RNCamera from 'react-native-camera';
import BaiduOCR, { RecognizeType } from 'react-native-baidu-ocr';
export default function App() {
const [cameraType, setCameraType] = useState(RNCamera.Constants.Type.back);
const onBarCodeRead = ({ data }) => console.log('Bar code: ', data);
const toggleCamera = () => {
setCameraType(cameraType === RNCamera.Constants.Type.back
? RNCamera.Constants.Type.front
: RNCamera.Constants.Type.back);
};
const recognizeText = async () => {
const result = await BaiduOCR.recognizeText({
image: {
data: {
path: 'path_to_image', // 替换为图片路径
mode: BaiduOCR.ImageMode.origin,
},
camera: {
type: cameraType,
},
},
recognizeType: RecognizeType.detect,
});
console.log('OCR Result: ', result);
};
return (
<View style={styles.container}>
<RNCamera
style={styles.preview}
type={cameraType}
onBarCodeRead={onBarCodeRead}
/>
<View style={styles.buttonContainer}>
<Button title="切换摄像头" onPress={toggleCamera} />
<Button title="识别文本" onPress={recognizeText} />
</View>
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
flexDirection: 'column',
backgroundColor: 'black',
},
preview: {
flex: 1,
justifyContent: 'flex-end',
alignItems: 'center',
},
buttonContainer: {
flex: 1,
backgroundColor: 'transparent',
flexDirection: 'row',
justifyContent: 'center',
alignItems: 'center',
},
});
这段代码展示了如何使用React Native和百度OCR服务来实现一个简单的文本识别应用。它包括了摄像头预览、切换摄像头和识别文本功能。需要注意的是,该代码只是一个示例,并且需要实现必要的依赖和API密钥配置。