【问题标题】:Camera.getAvailableCameraTypesAsync() does not work on the web and nativeCamera.getAvailableCameraTypesAsync() 在网络和本机上不起作用
【发布时间】: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


    【解决方案1】:

    请记住,您必须检查网络浏览器是否可用。同样在expo docs 说它只适用于网络,错误消息告诉你。您可以使用:

    import { Camera } from 'expo-camera';
    
    if (await Camera.isAvailableAsync()) {
        const types = await Camera.getAvailableCameraTypesAsync();
        // save values into on types
    }
    

    您可以在组件的 useEffect 函数中使用它来计算。 抱歉英语不好:.

    【讨论】:

    • 你测试了吗?我有一个网络摄像头,我可以用它拍照,它是可用的,我只为网络调用这个方法,我不检查它是否异步可用,但我希望在我的情况下至少有前置摄像头,而方法返回网络上缺少功能的错误。我觉得你的回答无助于解决这个问题。
    猜你喜欢
    • 1970-01-01
    • 2020-03-21
    • 1970-01-01
    • 1970-01-01
    • 2022-01-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多