请提供以下内容: 1. SDK版本:36 2.平台(Android / iOS / web / all):全部
我正在开发一个react-native
应用程序,我想在网络和本机上获取摄像机类型(前后)。
在documentation中写为:
Camera.getAvailableCameraTypesAsync(): string[]
返回摄像机类型列表['front', 'back']
。这对于仅具有前置摄像头的桌面浏览器很有用。import { Camera } from 'expo-camera'; const types = await 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';
现在,警报显示网络上的一个空数组。
如何使用Expo来测试网络上后置摄像头的存在?
expo-camera版本8.0.0
修改
我尝试更新expo-camera@8.2.0
,这很糟糕:
答案 0 :(得分:0)
请记住,您必须检查Web浏览器中是否可用。同样在expo docs上说它仅在网络上有效,错误消息告诉您。您可以使用:
import { Camera } from 'expo-camera';
if (await Camera.isAvailableAsync()) {
const types = await Camera.getAvailableCameraTypesAsync();
// save values into on types
}
您可以在组件的useEffect函数中使用此函数来进行查找。 对不起,英语不好:。