【发布时间】:2020-03-27 11:57:02
【问题描述】:
请提供以下信息: 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 在 web 上测试后置摄像头的存在?
曝光相机版本 8.0.0
编辑
我已尝试更新expo-camera@8.2.0,结果最糟糕:
【问题讨论】:
标签: javascript react-native web camera expo