react-native-camera的使用

1.首先安装react-native-camera

npm i react-native-camera --save

yarn add react-native-camera

2.在android/app/src/main/AndroidManifest.xml中添加

 <uses-permission android:name="android.permission.CAMERA" />

3.在android/app/build.gradle下添加

missingDimensionStrategy 'react-native-camera', 'general'

这样就完成了react-native-camera的配置

import {RNCamera} from 'react-native-camera';

import React, {useEffect, useRef} from 'react';

import {

  StyleSheet,

  Animated,

  PermissionsAndroid,

  default as Easing,

  ImageBackground,

  DeviceEventEmitter,

  View,

  Text,

  Image,

  TouchableOpacity,

} from 'react-native';

let camera;

const ScanQRCode = () => {

  const moveAnim = useRef(new Animated.Value(-2)).current;

  useEffect(() => {

    requestCameraPermission();

    startAnimation();

    // eslint-disable-next-line react-hooks/exhaustive-deps

  }, []);

  //请求权限的方法

  const requestCameraPermission = async () => {

    try {

      const granted = await PermissionsAndroid.request(

        PermissionsAndroid.PERMISSIONS.CAMERA,

        {

          title: '申请摄像头权限',

          message: '扫描二维码需要开启相机权限',

          buttonNeutral: '等会再问我',

          buttonNegative: '不行',

          buttonPositive: '好吧',

        },

      );

      if (granted === PermissionsAndroid.RESULTS.GRANTED) {

        console.log('现在你获得摄像头权限了');

      } else {

        console.log('用户没有允许相机权限');

      }

    } catch (err) {

      console.warn(err);

    }

  };

  /** 扫描框动画*/

  const startAnimation = () => {

    Animated.sequence([

      Animated.timing(moveAnim, {

        toValue: 200,

        duration: 1500,

        easing: Easing.linear,

        useNativeDriver: false,

      }),

      Animated.timing(moveAnim, {

        toValue: -1,

        duration: 1500,

        easing: Easing.linear,

        useNativeDriver: false,

      }),

    ]).start(() => startAnimation());

  };

  const onBarCodeRead = result => {

    const {data} = result; //只要拿到data就可以了

    DeviceEventEmitter.emit('camera', data);

    //扫码后的操作

    console.log(data);

    // alert(data);

  };

  return (

    <View style={styles.container}>

      <TouchableOpacity

        style={styles.test}

        onPress={() => {

          DeviceEventEmitter.emit('camerafalse', false);

        }}>

        <Image

          source={require('./../assets/imgs/goback.png')}

          style={styles.test}

        />

      </TouchableOpacity>

      <RNCamera

        ref={ref => {

          camera = ref;

        }}

        autoFocus={RNCamera.Constants.AutoFocus.on} /*自动对焦*/

        style={[styles.preview]}

        type={RNCamera.Constants.Type.back} /*切换前后摄像头 front前back后*/

        flashMode={RNCamera.Constants.FlashMode.off} /*相机闪光模式*/

        captureAudio={false}

        onBarCodeRead={onBarCodeRead}>

        <View

          style={{

            width: 500,

            height: 220,

            backgroundColor: 'rgba(0,0,0,0.5)',

          }}

        />

        <View style={[{flexDirection: 'row'}]}>

          <View

            style={{

              backgroundColor: 'rgba(0,0,0,0.5)',

              height: 200,

              width: 200,

            }}

          />

          <ImageBackground

            source={require('./../assets/imgs/saoma.png')}

            style={{width: 200, height: 200}}>

            <Animated.View

              style={[styles.border, {transform: [{translateY: moveAnim}]}]}

            />

          </ImageBackground>

          <View

            style={{

              backgroundColor: 'rgba(0,0,0,0.5)',

              height: 200,

              width: 200,

            }}

          />

        </View>

        <View

          style={{

            flex: 1,

            backgroundColor: 'rgba(0, 0, 0, 0.5)',

            width: 500,

            alignItems: 'center',

          }}>

          <Text style={styles.rectangleText}>

            将二维码放入框内,即可自动扫描

          </Text>

        </View>

      </RNCamera>

    </View>

  );

};

const styles = StyleSheet.create({

  test: {

    position: 'absolute',

    zIndex: 9999,

    width: 60,

    height: 60,

  },

  container: {

    flex: 1,

    flexDirection: 'row',

  },

  preview: {

    flex: 1,

    justifyContent: 'center',

    alignItems: 'center',

  },

  rectangleContainer: {

    flex: 1,

    justifyContent: 'flex-end',

    alignItems: 'center',

  },

  rectangle: {

    height: 200,

    width: 200,

    borderWidth: 1,

    borderColor: '#fcb602',

    backgroundColor: 'transparent',

    borderRadius: 10,

  },

  rectangleText: {

    flex: 0,

    color: '#fff',

    marginTop: 10,

  },

  border: {

    flex: 0,

    width: 196,

    height: 2,

    backgroundColor: '#fcb602',

    borderRadius: 50,

  },

});

export default ScanQRCode;

这是一个封装demo,可以在需要处直接引入该组件,扫二维码获取的信息会由 该事件发出DeviceEventEmitter.emit('camera', data);

 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值