如何使用 react-native-camera 捕获图片

Posted

技术标签:

【中文标题】如何使用 react-native-camera 捕获图片【英文标题】:How To Capture Picture using react-native-camera 【发布时间】:2020-10-26 08:19:57 【问题描述】:

我尝试使用 react-native-camera 捕获图像,但失败了, 租用我的代码。

【问题讨论】:

【参考方案1】:

您正在功能组件中访问“this”

类级组件:

takePicture = async () => 
    if (this.camera) 
      const options =  quality: 0.5, base64: true ;
      const data = await this.camera.takePictureAsync(options);
      console.log(data.uri);
    
  ;

<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);
          
        />

对于功能组件

const cameraRef = useRef();

takePicture = async () => 
      const options =  quality: 0.5, base64: true ;
      const data = await cameraRef.takePictureAsync(options);
      console.log(data.uri);
  ;

<RNCamera
          ref=cameraRef
          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);
          
        />

【讨论】:

它的工作 const data = await cameraRef.current.takePictureAsync(options);

以上是关于如何使用 react-native-camera 捕获图片的主要内容,如果未能解决你的问题,请参考以下文章

如何使用 react-router 重新加载页面?

如何使用 react-router-component 更新页面

如何在 react-router 中使用普通的锚链接

vue3 route和router的区别以及如何传参数接受参数,如何像vue2一样使用$route和$router详解

如何安装和导入 angular 2 '@angular/router'?

如何使用 vue-router 注册全局组件