【问题标题】:Unhandled promise rejection: TypeError: undefined is not an object (evaluating 'camera.takePictureAsync') React-Native expo-camera未处理的承诺拒绝:TypeError:未定义不是对象(评估'camera.takePictureAsync')React-Native expo-camera
【发布时间】:2021-03-24 01:51:30
【问题描述】:

我正在尝试创建一个可以访问我设备的相机并允许我拍照的功能,但我收到了上述错误。我将此建模为类似于请求访问相机胶卷,它工作正常,但我无法让它为相机工作。

这可能是什么原因造成的?以下是我的一些代码:

import * as ImagePicker from 'expo-image-picker' //I am using expo
import {Camera} from 'expo-camera'  

export default function Photo(){

// Image Picker 函数启动

useEffect(() => {
(async ()=> {
   if (Platform.OS != 'web'){
      const ( status !== 'granted') {
      if(status !== 'granted) {
      alert('Camera roll required to upload photo from your library');
   }
 }
})();
},[]);

//Image Picker函数结束

const camera = useRef(null) //added this
const takePicture = async () => { // added this



useEffect(() => {
    (async () => {
        if (Platform.OS !== 'web'){
        const { status1 } = await Camera.requestPermissionsAsync();
            if (status1 !== 'granted'){
            alert('Camera required to take a photo');
            }
} //added this
        },
    })();
}, [])


}

<Camera //added this
  ref = { camera }
  onGoogleVisionBarcodesDetected = {({barcodes}) => {
       console.log(barcodes)
   }}
 /> //added this

<View style = {[ styles.button, {justifyContent: 'center',  borderRadius: 20, backgroundColor: '#fff', paddingTop: 10, width: width*0.5, alignItems: 'center' } ]}>                 
                <TouchableOpacity 
                    color='#fff'
                    onPress = { ()=> takePicture () }
                    >

                <Text style = {[ styles.button, {}]}>Take Photo </Text>
                </TouchableOpacity>


            </View>

【问题讨论】:

  • 我注意到的第一件事是您正在使用 camera.takePictureAsync()。在世博会上,我们有一个这样的例子 { this.camera = ref; }} />; // ... snap = async () => { if (this.camera) { let photo = await this.camera.takePictureAsync(); } }; docs.expo.io/versions/latest/sdk/camera 我认为您必须为相机创建一个参考并使用 this.camera。链接中的示例中提到的 takePictureAsync()。
  • 我以前看过这个模块。它访问我的相机,但它并没有完全完成我需要它做的事情。我有一个我要按下的按钮,我想提示我的相机,然后就可以拍照了。使用 expo-camera 中给出的模块,它只是不断地渲染一个相机,如果这有意义吗?
  • 当然有道理。其他人可能在这里问过类似的问题stackoverflow.com/questions/52707002/…
  • 他们使用类系统,我使用钩子。我相信他们的问题是关于能否拍照。我相信这个摄像头组件也只是在应用程序中创建了一个摄像头;我正在尝试导航到我设备的相机

标签: react-native async-await expo typeerror expo-camera


【解决方案1】:

这可能会有所帮助

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 () => {
        const result1 = await camera.takePictureAsync();
        ....
    };

    return (
        <View style={styles.container}>
            <RNCamera
                ref={camera}
                .....
                onGoogleVisionBarcodesDetected={({ barcodes }) => {
                    console.log(barcodes)
                }}
            />
            <View ... >                 
                <TouchableOpacity 
                    onPress={() => takePicture() } // change here
                >

                ......
                </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

【讨论】:

  • 我会在早上试试这个并回复你,谢谢
  • 这并没有访问设备的摄像头,而是在应用程序内部制作了一个摄像头。它也只有我做的那么大
  • @MK_Pierce 所以你想要的只是按下一个按钮来打开设备的相机并拍照并在你的应用程序中使用图片?
  • @MK_Pierce 我已经更新了我的答案 onPress 方法调用请检查
  • @Rigorousimplementation 是的,这正是我需要的!
猜你喜欢
  • 2018-06-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-03-14
  • 1970-01-01
  • 2021-07-24
  • 2021-01-26
  • 1970-01-01
相关资源
最近更新 更多