简体   繁体   English

camera.takePictureAsync(options) 不是反应原生相机中的 function

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

Hello im new in react native and i just building my first react native project with camera without expo.你好,我是 react native 的新手,我只是用没有 expo 的相机构建我的第一个 react native 项目。 I installed it with npm install react-native-camera and then linked it with react-native link react-native-camera .我使用npm install react-native-camera安装它,然后将其与react-native link react-native-camera The camera run successfuly, but when i triggered the snap button it got error like this....相机运行成功,但是当我触发快照按钮时,出现这样的错误......

{ [TypeError: camera.takePictureAsync is not a function. { [TypeError: camera.takePictureAsync 不是 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 ' } (在 'camera.takePictureAsync(options)' 中,'camera.takePictureAsync' 未定义)] │ line: 131480, │ column: 72, └ sourceURL: ' http://localhost:8081/index.bundle?platform=android&dev=真&缩小=假' }

Here is my code looks like...这是我的代码看起来像......

 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

UPDATE (18.48): I tried using class component like in react-native-camera documentation did, and it finally works.更新(18.48):我尝试使用 class 组件,就像在react-native-camera文档中所做的那样,它终于可以工作了。 But i still curious how to make it works in function component?但我仍然很好奇如何使它在 function 组件中工作?

You should use camera.current.takePictureAsync(options);你应该使用camera.current.takePictureAsync(options); rather than camera.takePictureAsync(options);而不是camera.takePictureAsync(options); . .

I was having the same problem.我遇到了同样的问题。 The solution was to go back to a Class component instead of a function one.解决方案是将 go 返回到 Class 组件,而不是 function 组件。

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

I got react-native-camera running with Functional Components.我让 react-native-camera 使用功能组件运行。 This is how:这是如何:

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

hi the correct way in function component should be您好 function 组件中的正确方法应该是

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

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

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