import React, { Component } from 'react';
import { View, Text, StyleSheet, Image, Platform, Vibration, TouchableOpacity, Animated, Easing, Dimensions, StatusBar } from 'react-native'; import Icon from 'react-native-vector-icons/Feather'; const { width, height } = Dimensions.get('window'); import { Modal } from '@ant-design/react-native';
import {RNCamera} from 'react-native-camera'; import ViewFinder from './ViewFinder';
import scanLine from '../../assets/images/scan_line.png'; import { scaleSizeW, scaleSizeH, setSpText } from '../../utils/scale';
export default class MyScan extends Component { static navigationOptions = ({ navigation, navigationOptions }) => { return({ header: null }) }; constructor(props) { super(props); this.camera = null; this.state = { openFlash: false, active: true, flag: true, fadeInOpacity: new Animated.Value(0), isEndAnimation: false, key: '' }
} componentDidMount() { const {navigation} = this.props; this._startAnimation(false); } componentWillReceiveProps(nProps) { const {key } = this.state; const {navigation} = nProps;
let newKey = navigation.getParam('key');
if(key!=newKey){ this.transCode = ''; this.setState({ key: newKey }) } console.log('scan进来了') }
_startAnimation = (isEnd) => { Animated.timing(this.state.fadeInOpacity, { toValue: 1, duration: 3000, easing: Easing.linear }).start( () => { if (isEnd) { this.setState({ isEndAnimation: true }) return; } if (!this.state.isEndAnimation) { this.state.fadeInOpacity.setValue(0); this._startAnimation(false) } } ); } barcodeReceived (e){ const {navigation:{navigate}} = this.props; if (e.data !== this.transCode) { Vibration.vibrate([0, 500, 200, 500]); this.transCode = e.data; if (this.state.flag) { this.changeState(false); } console.log("transCode=" + this.transCode); Modal.alert('温馨提示', `确定绑定: ${this.transCode} 吗?`, [ { text: '取消', onPress: () => console.log('cancel'), style: 'cancel', }, { text: '确定', onPress: () => { navigate('Record') } }, ]); } } _goBack = () => { this.setState({ isEndAnimation: true, }); this.props.navigation.goBack(); } _changeFlash = () => { this.setState({ openFlash: !this.state.openFlash, }); } changeState = (status) => { this.setState({ flag: status }); console.log('status=' + status); }
render() { const { openFlash, active, } = this.state; return ( <View style={styles.allContainer}> <RNCamera barCodeTypes={[RNCamera.Constants.BarCodeType.qr]} onBarCodeRead={this.barcodeReceived.bind(this)} onCameraReady={() => { console.log('ready') }} permissionDialogTitle={'Permission to use camera'} permissionDialogMessage={'We need your permission to use your camera phone'} style={styles.cameraStyle} > <View style={styles.container}> <View style={styles.titleContainer}> <View style={styles.leftContainer}> <TouchableOpacity activeOpacity={1} onPress={this._goBack}> <View> {} <Icon name="chevron-left" style={styles.backIcon}/> </View> </TouchableOpacity> </View> </View> </View> <View style={styles.centerContainer} /> <View style={{ flexDirection: 'row' }}> <View style={styles.fillView} /> <View style={styles.scan}> <ViewFinder /> <Animated.View style={[styles.scanLine, { opacity: 1, transform: [{ translateY: this.state.fadeInOpacity.interpolate({ inputRange: [0, 1], outputRange: [0, scaleSizeH(300)] }) }] }]}> <Image source={scanLine} /> </Animated.View> </View> <View style={styles.fillView} /> </View> <View style={styles.bottomContainer}> <Text style={[ styles.text, { textAlign: 'center', width: 220, marginTop: active ? 25 : 245, }, ]} numberOfLines={2} > 请将手表二维码放入扫码框内 </Text> {
} </View> </RNCamera> </View> ) } }
const styles = StyleSheet.create({ allContainer: { flex: 1, }, container: { ...Platform.select({ ios: { height: 64 + StatusBar.currentHeight, }, android: { height: 50 + StatusBar.currentHeight, } }), backgroundColor: '#000', opacity: 0.5 }, titleContainer: { flex: 1, ...Platform.select({ ios: { paddingTop: 15, }, android: { paddingTop: 0, } }), flexDirection: 'row', }, leftContainer: { flex: 0, justifyContent: 'center', }, backIcon: { marginLeft: 10, fontSize: 30, color: '#fff' }, cameraStyle: { alignSelf: 'center', width: width, height: height, }, flash: { flexDirection: 'column', alignItems: 'center', justifyContent: 'flex-start', marginTop: 60, }, flashIcon: { fontSize: 1, color: '#fff', }, text: { fontSize: 14, color: '#fff', marginTop: 5 }, icon: { color: '#fff', fontSize: 20, fontFamily: 'iconfont' }, scanLine: { alignSelf: 'center', }, centerContainer: { ...Platform.select({ ios: { height: 80, }, android: { height: 60, } }), width: width, backgroundColor: '#000', opacity: 0.5 }, bottomContainer: { alignItems: 'center', backgroundColor: '#000', alignSelf: 'center', opacity: 0.5, flex: 1, width: width }, fillView: { width: (width - scaleSizeH(300)) / 2, height: scaleSizeH(300), backgroundColor: '#000', opacity: 0.5 }, scan: { width: scaleSizeH(300), height: scaleSizeH(300), alignSelf: 'center' }, info:{ color: '#fff' }
})
|