我想要有关 expo-camera 中的测试代码的帮助,我正在使用 expo go,一旦运行代码,它就会出现以下错误: TypeError: undefined is not an object (evaluating '_expoCamera.CameraType.back' ),这里是expo-camera的文档:https://docs.expo.dev/versions/latest/sdk/camera/,我目前正在使用expocamera 12.3.0并使用navigation.navigate导航到这个标签谢谢!这是我直接从网站复制的代码
import { Camera, CameraType } from 'expo-camera';
import { useState } from 'react';
import { Button, StyleSheet, Text, TouchableOpacity, View } from 'react-native';
export default function App() {
const [type, setType] = useState(CameraType.back);
const [permission, requestPermission] = Camera.useCameraPermissions();
if (!permission) {
// Camera permissions are still loading
return <View />;
}
if (!permission.granted) {
// Camera permissions are not granted yet
return (
<View style={styles.container}>
<Text style={{ textAlign: 'center' }}>
We need your permission to show the camera
</Text>
<Button onPress={requestPermission} title="grant permission" />
</View>
);
}
function toggleCameraType() {
setType((current) => (
current === CameraType.back ? CameraType.front : CameraType.back
));
}
return (
<View style={styles.container}>
<Camera style={styles.camera} type={type}>
<View style={styles.buttonContainer}>
<TouchableOpacity
style={styles.button}
onPress={toggleCameraType}>
<Text style={styles.text}>Flip Camera</Text>
</TouchableOpacity>
</View>
</Camera>
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
},
camera: {
flex: 1,
},
buttonContainer: {
flex: 1,
flexDirection: 'row',
backgroundColor: 'transparent',
margin: 64,
},
button: {
flex: 1,
alignSelf: 'flex-end',
alignItems: 'center',
},
text: {
fontSize: 24,
fontWeight: 'bold',
color: 'white',
},
});
更新:修复了,只需将CameraType.back更改为Camera.Constants.Type.back,解决了
我使用以下依赖项面临同样的问题:
"expo": "~51.0.26",
"expo-camera": "^15.0.14",
<Camera style={styles.camera} ref={cameraRef} type={Camera.Constant.Type.back}>
使用上面的方法返回错误:
Cannot read property 'Type' of undefined
。
尝试过:
有什么建议吗?谢谢!
代码
import { CameraView, Camera, useCameraPermissions } from 'expo-camera';
...
{showCamera ? (
<View style={styles.cameraContainer}>
<Camera style={styles.camera} ref={cameraRef} type={Camera.Constant.Type.back}>
<View style={styles.cameraButtonContainer}>
<TouchableOpacity style={styles.cameraButton} onPress={takePicture}>
<Text style={styles.buttonText}>Capture</Text>
</TouchableOpacity>
<TouchableOpacity style={styles.cameraButton} onPress={() => setShowCamera(false)}>
<Text style={styles.buttonText}>Cancel</Text>
</TouchableOpacity>
</View>
</Camera>
</View>