请提供以下内容: 1. SDK版本: 36 2.平台(Android/iOS/web/ all ):all
我正在做一个react-native应用程序,我想让相机类型(前和后)在网上和本机。
在文档中,它被写成:
Camera.getAvailableCameraTypesAsync(): string[]返回相机类型['front', 'back']的列表。这对于只有正面摄像头的桌面浏览器很有用。 从‘世博相机’导入{ Camera };const type=等待Camera.getAvailableCameraTypesAsync();
这是我的Camera.js
import React, { useState, useEffect } from 'react';
import { Platform, View, TouchableOpacity } from 'react-native';
import { FAB, Text } from 'react-native-paper';
import { Camera } from 'expo-camera';
export default function CoreCamera() {
const [hasPermission, setHasPermission] = useState(null);
const [type, setType] = useState(Camera.Constants.Type.back);
const [types, setTypes] = useState(null);
useEffect(() => {
(async () => {
const types = await Camera.getAvailableCameraTypesAsync();
alert(JSON.stringify(types));
setTypes(types);
if (Platform.OS === 'web') {
setHasPermission(true);
} else {
const { status } = await Camera.requestPermissionsAsync();
setHasPermission(status === 'granted');
}
})();
}, []);
if (hasPermission === null) {
return <View />;
}
if (hasPermission === false) {
return <Text>No access to camera</Text>;
}
return (
<View style={{ flex: 1 }}>
<Camera style={{ flex: 1 }} type={type}>
<View
style={{
flex: 1,
backgroundColor: 'transparent',
flexDirection: 'row',
}}
>
<TouchableOpacity
style={{
flex: 0.1,
alignSelf: 'flex-end',
alignItems: 'center',
}}
onPress={() => {
setType(
type === Camera.Constants.Type.back
? Camera.Constants.Type.front
: Camera.Constants.Type.back,
);
}}
>
<FAB
style={{ marginBottom: 20 }}
icon="camera-switch"
/>
</TouchableOpacity>
</View>
</Camera>
</View>
);
}在Android上,它给出了以下错误:
[Unhandled promise rejection: Error: The method or property expo-camera.getAvailableCameraTypesAsync is not available on android, are you sure you've linked all the native dependencies properly?]在iOS上,它给出了以下错误:
[Unhandled promise rejection: Error: The method or property expo-camera.getAvailableCameraTypesAsync is not available on ios, are you sure you've linked all the native dependencies properly?]在网络上,它给了我以下错误:
bundle.js:73612 Uncaught (in promise) TypeError: Cannot read property 'getAvailableCameraTypesAsync' of undefined
at getAvailableCameraTypesAsync$ (bundle.js:73612)
at tryCatch (bundle.js:185959)我试图取代:
-import { Camera } from 'expo-camera';
+import Camera from 'expo-camera/build/Camera';现在,警报在网络上显示一个空数组。
我怎样才能用世博会来测试网络上摄像头的存在呢?
世博会-摄像机版本8.0.0
编辑
我试图更新expo-camera@8.2.0,这是最糟糕的:
发布于 2020-04-02 21:42:07
请记住,您必须检查网页浏览器是否可用。同样在世博会文件,说它只在web上工作,错误信息告诉你。您可以使用:
import { Camera } from 'expo-camera';
if (await Camera.isAvailableAsync()) {
const types = await Camera.getAvailableCameraTypesAsync();
// save values into on types
}您可以在组件的useEffect中的一个函数中使用这个函数来计算。对不起英语不好:.
https://stackoverflow.com/questions/60885525
复制相似问题