react-native tesseract-ocr [错误:无法读取位图]

时间:2016-12-28 12:49:11

标签: android reactjs react-native tesseract react-native-android

我正在使用Android的OCR应用程序使用React-native使用react-native-tesseract-ocr的库, index.android.js的代码是

class ai extends Component{
  render(){
    return(
      <View style={styles.container}>
        <Camera
         ref = {(cam)=>{
           this.camera = cam
         }}
         style={styles.camera}>
        <Text style={styles.button} onPress={this.capture.bind(this)}>Read</Text>
        </Camera>
      </View>
    );
  }
  capture()
  {
    var path = "";
    this.camera.capture().done(function(data){
      path = data.path;
      console.log(data.path);
    });
    RNTesseractOcr.startOcr(path, "LANG_ENGLISH")
      .then((result) => {
        this.setState({ ocrResult: result });
        console.log("OCR Result: ", result);
      })
      .catch((err) => {
        console.log("OCR Error: ", err);
      })
      .done((data)=>{
        console.log(data);
      });
  }
}

并且日志数据是

12-28 18:14:18.187 25118 25152 I ReactNativeJS: file:///storage/emulated/0/DCIM/IMG_20161228_181418.jpg
12-28 18:14:28.159 25118 25152 I ReactNativeJS: 'OCR Error: ', { [Error: Failed to read bitmap] framesToPop: 1, code: 'An error occurred' }
12-28 18:14:28.404 25118 25152 I ReactNativeJS: file:///storage/emulated/0/DCIM/IMG_20161228_181428.jpg
12-28 18:15:04.940 25118 25152 I ReactNativeJS: Running application "ai" with appParams: {"initialProps":{},"rootTag":11}. __DEV__ === true, development-level warning are ON, performance optimizations are OFF

使用的权限是

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

2 个答案:

答案 0 :(得分:0)

对于未来的访客:

此问题无法读取位图是由于从相机获取的图像路径。只需将file:///替换为/即可解决问题。

之前:file:///storage/emulated/0/DCIM/IMG_20161228_181418.jpg

之后:/storage/emulated/0/DCIM/IMG_20161228_181418.jpg

参考:https://github.com/jonathanpalma/react-native-tesseract-ocr/issues/10

答案 1 :(得分:0)

下面的代码显示了如何从路径

替换file://
this.camera.capture().done(function(data){
  path = data.path.replace('file://', '');
  console.log('data path', path);
});