如何在SDK 51.0.0中使用expo-camera启用手电筒?

问题描述 投票:0回答:1

我正在尝试使用带有

torch
expo-camera
包在我的 Expo 应用程序中启用
SDK 51.0.0

环境

世博SDK版本:51.0.0 平台: 安卓、iOS 依赖项: 世博相机 反应本机 @react-native-async-storage/异步存储 @react-navigation/native

import { CameraView, useCameraPermissions } from 'expo-camera';
import React, { useState, useContext, useCallback } from 'react';
import { Button, Text, TouchableOpacity, View } from 'react-native';
import { AppContext } from "../../context/AppContext";
import AsyncStorage from '@react-native-async-storage/async-storage';
import { useFocusEffect } from '@react-navigation/native';
import styles from './ScanEnterGTINScreenStyles';

const ScanEnterGTINScreen = ({ navigation }) => {
  const [permission, requestPermission] = useCameraPermissions();
  const [scannedData, setScannedData] = useState(null);
  const [isScanning, setIsScanning] = useState(true);
  const [isLoading, setIsLoading] = useState(false);
  const { t } = useContext(AppContext);
  const [isFlashOn, setIsFlashOn] = useState(false);

  useFocusEffect(
    useCallback(() => {
      setIsScanning(true);
      setScannedData(null);
      setIsLoading(false);
    }, [])
  );

  if (!permission) {
    return <View />;
  }

  if (!permission.granted) {
    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>
    );
  }

  const handleBarcodeScanned = ({ data }) => {
    // Processing scanned data
  };

  const toggleFlash = () => {
    setIsFlashOn(!isFlashOn);
  };

  return (
    <View style={styles.container}>
      <CameraView
        style={styles.camera}
        onBarcodeScanned={handleBarcodeScanned}
        flashMode={isFlashOn ? 'torch' : 'off'}
      />
      <View style={styles.overlay}>
        <TouchableOpacity
          style={styles.flashlightButton}
          onPress={toggleFlash}
        >
          <Text style={styles.flashlightText}>{isFlashOn ? 'Flash Off' : 'Flash On'}</Text>
        </TouchableOpacity>
      </View>
    </View>
  );
}

export default ScanEnterGTINScreen;
react-native expo
1个回答
0
投票

对于那些可能登陆这里的人。

Flashmode
已经不复存在了。使用
enableTorch

尝试:

    enableTorch= { cameraTorch }
     <CameraView
            style={styles.camera}
            onBarcodeScanned={handleBarcodeScanned}
            enableTorch={isFlashOn}
          />

这是一个简单的布尔值。

文档

这可能需要一秒钟才能打开闪光灯。

© www.soinside.com 2019 - 2024. All rights reserved.