我不明白为什么无论我做什么都会收到此错误。我从 barrcodescanner 移动到 expo-camera 并收到此错误
import React, { useState, useEffect } from 'react';
import { Text, View, TouchableOpacity, StyleSheet, Image } from 'react-native';
import { Camera } from 'expo-camera';
export default function CameraComponent() {
const [hasPermission, setHasPermission] = useState(null);
const [cameraRef, setCameraRef] = useState(null);
const [type, setType] = useState(Camera.Constants.Type.back);
const [photo, setPhoto] = useState(null);
useEffect(() => {
(async () => {
const { status } = await Camera.requestCameraPermissionsAsync();
setHasPermission(status === 'granted');
})();
}, []);
if (hasPermission === null) {
return <View />;
}
if (hasPermission === false) {
return <Text>No access to camera</Text>;
}
const takePicture = async () => {
if (cameraRef) {
let photo = await cameraRef.takePictureAsync();
setPhoto(photo.uri);
}
};
return (
<View style={styles.container}>
<Camera style={styles.camera} type={type} ref={ref => setCameraRef(ref)}>
<View style={styles.buttonContainer}>
<TouchableOpacity
style={styles.button}
onPress={() => {
setType(
type === Camera.Constants.Type.back
? Camera.Constants.Type.front
: Camera.Constants.Type.back
);
}}>
<Text style={styles.text}> Flip </Text>
</TouchableOpacity>
<TouchableOpacity style={styles.button} onPress={takePicture}>
<Text style={styles.text}> Snap </Text>
</TouchableOpacity>
</View>
</Camera>
{photo && (
<View style={styles.previewContainer}>
<Text style={styles.previewText}>Preview:</Text>
<Image source={{ uri: photo }} style={styles.previewImage} />
</View>
)}
</View>
);
}
>const [type, setType] = useState(Camera.Constants.Type.back);
Unresolved variable Constants
Same error heres
>type === Camera.Constants.Type.back
? Camera.Constants.Type.front
: Camera.Constants.Type.back
我真的不明白哪里出了问题,也不知道从哪里开始。
或者我收到“无法读取‘类型’未定义的属性”错误
该错误是由相机切换“setType”设置引起的。使用 expo-camera 时,您需要使用此代码来更改相机切换:
function toggleCameraFacing() {
setFacing(current => (current === 'back' ? 'front' : 'back'));}
return (
<View style={styles.container}>
<CameraView style={styles.camera} facing={facing}>
<View style={styles.buttonContainer}>
<TouchableOpacity style={styles.button} onPress=
{toggleCameraFacing}>
<Text style={styles.text}>Flip Camera</Text>
</TouchableOpacity>
</View>
</CameraView>
</View>
);
来源: expo-camera