【发布时间】: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