sweep_code_invoice.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. import React, {Component} from 'react';
  2. import {
  3. StyleSheet,
  4. Animated,
  5. default as Easing,
  6. ImageBackground,
  7. Text,
  8. View,
  9. } from 'react-native';
  10. import {RNCamera} from 'react-native-camera';
  11. export default class sweep_code_invoice extends Component {
  12. constructor(props) {
  13. super(props);
  14. this.props.navigation.dangerouslyGetParent().setOptions({
  15. tabBarVisible: false,
  16. });
  17. this.state = {
  18. //中间横线动画初始值
  19. moveAnim: new Animated.Value(-2),
  20. };
  21. }
  22. componentDidMount() {
  23. this.startAnimation();
  24. }
  25. /** 扫描框动画*/
  26. startAnimation = () => {
  27. this.state.moveAnim.setValue(-2);
  28. Animated.sequence([
  29. Animated.timing(this.state.moveAnim, {
  30. toValue: 200,
  31. duration: 1500,
  32. easing: Easing.linear,
  33. }),
  34. Animated.timing(this.state.moveAnim, {
  35. toValue: -1,
  36. duration: 1500,
  37. easing: Easing.linear,
  38. }),
  39. ]).start(() => this.startAnimation());
  40. };
  41. onBarCodeRead = result => {
  42. const {navigate} = this.props.navigation;
  43. const {data} = result; //只要拿到data就可以了
  44. //扫码后的操作
  45. console.log(data);
  46. };
  47. render() {
  48. return (
  49. <View style={styles.container}>
  50. <RNCamera
  51. ref={ref => {
  52. this.camera = ref;
  53. }}
  54. autoFocus={RNCamera.Constants.AutoFocus.on} /*自动对焦*/
  55. style={[styles.preview]}
  56. type={RNCamera.Constants.Type.back} /*切换前后摄像头 front前back后*/
  57. flashMode={RNCamera.Constants.FlashMode.off} /*相机闪光模式*/
  58. onBarCodeRead={this.onBarCodeRead}>
  59. <View
  60. style={{
  61. width: 500,
  62. height: 220,
  63. backgroundColor: 'rgba(0,0,0,0.5)',
  64. }}
  65. />
  66. <View style={[{flexDirection: 'row'}]}>
  67. <View
  68. style={{
  69. backgroundColor: 'rgba(0,0,0,0.5)',
  70. height: 200,
  71. width: 200,
  72. }}
  73. />
  74. <ImageBackground
  75. style={{width: 200, height: 200}}
  76. source={require('../../source/img/sweep_code/icon_scan_rect.png')}>
  77. <Animated.View
  78. style={[
  79. styles.border,
  80. {transform: [{translateY: this.state.moveAnim}]},
  81. ]}
  82. />
  83. </ImageBackground>
  84. <View
  85. style={{
  86. backgroundColor: 'rgba(0,0,0,0.5)',
  87. height: 200,
  88. width: 200,
  89. }}
  90. />
  91. </View>
  92. <View
  93. style={{
  94. flex: 1,
  95. backgroundColor: 'rgba(0, 0, 0, 0.5)',
  96. width: 500,
  97. alignItems: 'center',
  98. }}>
  99. <Text style={styles.rectangleText}>
  100. 将二维码放入框内,即可自动扫描
  101. </Text>
  102. </View>
  103. </RNCamera>
  104. </View>
  105. );
  106. }
  107. }
  108. const styles = StyleSheet.create({
  109. container: {
  110. flex: 1,
  111. flexDirection: 'row',
  112. },
  113. preview: {
  114. flex: 1,
  115. justifyContent: 'center',
  116. alignItems: 'center',
  117. },
  118. rectangleContainer: {
  119. flex: 1,
  120. justifyContent: 'flex-end',
  121. alignItems: 'center',
  122. },
  123. rectangle: {
  124. height: 200,
  125. width: 200,
  126. borderWidth: 1,
  127. borderColor: '#fcb602',
  128. backgroundColor: 'transparent',
  129. borderRadius: 10,
  130. },
  131. rectangleText: {
  132. flex: 0,
  133. color: '#fff',
  134. marginTop: 10,
  135. },
  136. border: {
  137. flex: 0,
  138. width: 196,
  139. height: 2,
  140. backgroundColor: '#76EE00',
  141. borderRadius: 50,
  142. },
  143. });