简体   繁体   English

react-native-camera (android): takePictureAsync() 抛出错误

[英]react-native-camera (android): takePictureAsync() throws error

After calling takePictureAsync() from react-native-camera, i'm getting this error:从 react-native-camera 调用 takePictureAsync() 后,我收到此错误:

{
  "framesToPop": 1,
  "nativeStackAndroid": [],
  "userInfo": null,
  "message": "Preview is paused - resume it before taking a picture.",
  "code": "E_TAKE_PICTURE_FAILED",
  "line": 2131,
  "column": 45,
  "sourceURL": "http://10.0.2.2:8081/index.bundle?platform=android&dev=true&minify=false"
}

So I tried using resumePreview() method before calling takePictureAsync() and now I'm getting a different error message:所以我在调用 takePictureAsync() 之前尝试使用 resumePreview() 方法,现在我收到了不同的错误消息:

{
  "framesToPop": 1,
  "nativeStackAndroid": [],
  "userInfo": null,
  "message": "takePicture failed",
  "code": "E_TAKE_PICTURE_FAILED",
  "line": 2131,
  "column": 45,
  "sourceURL": "http://10.0.2.2:8081/index.bundle?platform=android&dev=true&minify=false"
}

My component and usage i identical to that of https://react-native-community.github.io/react-native-camera/docs/rncamera我的组件和用法与https://react-native-community.github.io/react-native-camera/docs/rncamera相同

    <RNCamera
      ref={ref => {
        this.camera = ref;
      }}
      style={styles.preview}
      type={RNCamera.Constants.Type.back}
      flashMode={RNCamera.Constants.FlashMode.on}
      androidCameraPermissionOptions={{
        title: 'Permission to use camera',
        message: 'We need your permission to use your camera',
        buttonPositive: 'Ok',
        buttonNegative: 'Cancel',
      }}
      androidRecordAudioPermissionOptions={{
        title: 'Permission to use audio recording',
        message: 'We need your permission to use your audio',
        buttonPositive: 'Ok',
        buttonNegative: 'Cancel',
      }}
      onGoogleVisionBarcodesDetected={({ barcodes }) => {
        console.log(barcodes);
      }}
    />




takePicture = async () => {
    if (this.camera) {
      const options = { quality: 0.5, base64: true };
      try {
        this.camera.resumePreview();
        const data = await this.camera.takePictureAsync(options);
        console.log(data.uri);
      } catch (error) {
        console.log(JSON.stringify(error, null, 2));
      }
    }
  };

versions:版本:

"react-native": "0.61.2",
"react-native-camera": "git+https://git@github.com/react-native-community/react-native-camera.git",

Works fine on iOS.在 iOS 上运行良好。 Is this an issue with the library or my implementation?这是图书馆的问题还是我的实现?

Try to use component as FaCC (Function as Child Components)!尝试使用组件作为 FaCC (Function as Child Components)! This way worked for me.这种方式对我有用。

const PendingView = () => (
  <View
    style={{
      flex: 1,
      backgroundColor: 'lightgreen',
      justifyContent: 'center',
      alignItems: 'center',
    }}
  >
    <Text>Waiting</Text>
  </View>
);

class ExampleApp extends PureComponent {
  render() {
    return (
      <View style={styles.container}>
        <RNCamera
          style={styles.preview}
          type={RNCamera.Constants.Type.back}
        >
          {({ camera, status, recordAudioPermissionStatus }) => {
            if (status !== 'READY') return <PendingView />;
            return (
              <View style={{ flex: 0, flexDirection: 'row', justifyContent: 'center' }}>
                <TouchableOpacity onPress={() => this.takePicture(camera)} style={styles.capture}>
                  <Text style={{ fontSize: 14 }}> SNAP </Text>
                </TouchableOpacity>
              </View>
            );
          }}
        </RNCamera>
      </View>
    );
  }

  takePicture = async function(camera) {
    const options = { quality: 0.5, base64: true };
    const data = await camera.takePictureAsync(options);
    //  eslint-disable-next-line
    console.log(data.uri);
  };
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    flexDirection: 'column',
    backgroundColor: 'black',
  },
  preview: {
    flex: 1,
    justifyContent: 'flex-end',
    alignItems: 'center',
  },
  capture: {
    flex: 0,
    backgroundColor: '#fff',
    borderRadius: 5,
    padding: 15,
    paddingHorizontal: 20,
    alignSelf: 'center',
    margin: 20,
  },
});

Is this an issue with the library or my implementation?这是图书馆的问题还是我的实现?

It appears to be a problem with the example code.示例代码似乎有问题。 I noticed the same thing and my solution is similar to FredVieira but doesn't use FaCC.我注意到了同样的事情,我的解决方案类似于FredVieira,但不使用 FaCC。 It appears that the camera will not resume on Android unless the RNCamera component has children.除非 RNCamera 组件有子组件,否则相机似乎不会在 Android 上恢复。 So if you change the example from因此,如果您将示例从

<RNCamera ... /> 
<View ...>
          <TouchableOpacity ...>
            <Text style={{ fontSize: 14 }}> SNAP </Text>
          </TouchableOpacity>
 </View>

to

<RNCamera ...>
<View ...>
          <TouchableOpacity ...>
            <Text style={{ fontSize: 14 }}> SNAP </Text>
          </TouchableOpacity>
 </View>
</RNCamera>

it should work.它应该工作。 So you can use a function or a component, just as long as the RNCamera has a child.所以你可以使用一个函数或一个组件,只要 RNCamera 有一个孩子。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM