camera.takePictureAsync(options) 不是 React Native 相机中的函数

camera.takePictureAsync(options) is not a function in react native camera

你好,我是 React Native 的新手,我只是在没有 expo 的情况下用相机构建我的第一个 React Native 项目。 我用 npm install react-native-camera 安装它,然后用 react-native link react-native-camera 链接它。 相机 运行 成功,但是当我触发快照按钮时出现这样的错误....

{ [TypeError: camera.takePictureAsync is not a function. (In 'camera.takePictureAsync(options)', 'camera.takePictureAsync' is undefined)] │ line: 131480, │ column: 72, └ sourceURL: 'http://localhost:8081/index.bundle?platform=android&dev=true&minify=false' }

这是我的代码……

import React, { useRef } from 'react'
import { View, Text, StyleSheet, TouchableOpacity } from 'react-native'
import { RNCamera } from 'react-native-camera'


function PlayWithCamera() {

    const camera = useRef(null)

    const takePicture = async () => {
        try {
            const options = { quality: 0.5, base64: true };
            const data = await camera.takePictureAsync(options);
            console.log(data.uri, '<<<<<<<<<<<<<<<<<<<<<');
        } catch (error) {
            console.log(error, "ERROR <<<<<<<<<<<<<")
        }
    };

    return (
        <View style={styles.container}>
            <RNCamera
                ref={camera}
                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)
                }}
            />
            <View style={{ flex: 1, width: '100%', flexDirection: 'row', justifyContent: 'center', position: 'absolute', bottom: 0 }}>
                <TouchableOpacity style={styles.capture} onPress={takePicture}>
                    <Text style={{ fontSize: 14 }}> SNAP </Text>
                </TouchableOpacity>
            </View>
        </View>
    )
}

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,
    },
})

export default PlayWithCamera

更新 (18.48): 我尝试像 react-native-camera 文档中那样使用 class 组件,它终于起作用了。但是我还是很好奇如何让它在函数组件中工作?

我遇到了同样的问题。解决方案是返回到 Class 组件而不是功能组件。

<Camera
   ref={ref => (this.cameraEl = ref)}
   style={{ flex: 1 }}
   type={Camera.Constants.Type.front}
/>

我得到了 react-native-camera 运行 功能组件。是这样的:

function CameraComponent(props){
  let camera;
  async function takePicture(){
    if( camera ) {
      const options = {quality: 0.5};
      const data = await camera.takePictureAsync(options);
      console.log(data.uri);
    }
  }

  return(
    <View>
      <RNCamera
        ref={ref => (camera = ref)}
       />
     </View>
  );
}

您应该使用 camera.current.takePictureAsync(options); 而不是 camera.takePictureAsync(options);

  const cameraEl = useRef(null);
  async function takePicture() {
    console.log('takePicture');
    if (cameraEl.current) {
      const options = { quality: 0.5, base64: true };
      const data = await cameraEl.current.takePictureAsync(options);
      console.log(data.uri);
    }
  }

    <RNCamera
      // ref={ref => {
      //   this.camera = ref;
      // }}
      ref={cameraEl}

你好,函数组件的正确方法应该是

const ref = React.createRef();

const takePicture = async () => {
    if (ref.current) {
      const options = { quality: 0.5, base64: true };
      const data = await ref.current.takePictureAsync(options);
    
      console.log(data.uri);
    }
  };


  return (
    <View style={styles.container}>
      <RNCamera
        ref={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);
        }}
      />
      <View style={{ flex: 0, flexDirection: 'row', justifyContent: 'center' }}>
        <TouchableOpacity onPress={ takePicture } style={styles.capture}>
          <Text style={{ fontSize: 14 }}> SNAP </Text>
        </TouchableOpacity>
      </View>
    </View>
  );