QRScanner.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /**
  2. * QR码扫描器
  3. * @邠心vbe on 2026/04/21
  4. */
  5. import React, { useEffect, useState } from 'react';
  6. import { Pressable, StyleSheet, View } from 'react-native';
  7. import TextView from '../../components/TextView';
  8. import { Camera, useCameraDevice, useCodeScanner } from 'react-native-vision-camera';
  9. import { check, PERMISSIONS, RESULTS } from 'react-native-permissions';
  10. import utils from '../../utils/utils';
  11. const checkPermission = (back) => {
  12. if (isIOS) {
  13. back(true);
  14. } else {
  15. check(PERMISSIONS.ANDROID.CAMERA).then(res => {
  16. console.log('checkPermission', res);
  17. switch (res) {
  18. case RESULTS.DENIED://权限未被请求/被拒绝,但可以请求
  19. back(false);
  20. break;
  21. case RESULTS.LIMITED://权限是有限的:有些操作是可能的
  22. back(true);
  23. break;
  24. case RESULTS.GRANTED://许可被授予
  25. back(true);
  26. break;
  27. case RESULTS.BLOCKED://权限被拒绝,不再可请求
  28. back(false);
  29. break;
  30. default:
  31. back(false);
  32. break;
  33. }
  34. }).catch(err => {
  35. back(false);
  36. })
  37. }
  38. }
  39. const QRScanner = ({ onResult, isActive }) => {
  40. const [flashOn, switchFlash] = useState(false);
  41. const [hasPermission, setHasPermission] = useState(false);
  42. const device = useCameraDevice('back');
  43. useEffect(() => {
  44. console.log("相机设备", device);
  45. checkPermission(result => {
  46. setHasPermission(result);
  47. })
  48. Camera.requestCameraPermission().then(res => {
  49. utils.logEventTracking("scan_camera_permission", res)
  50. if (!hasPermission) {
  51. setHasPermission(res == 'granted');
  52. }
  53. }).catch(err => {
  54. console.warn("相机权限请求错误", err);
  55. utils.logEventTracking("scan_camera_permission_error", err)
  56. });
  57. }, []);
  58. const codeScanner = useCodeScanner({
  59. codeTypes: ['qr'],
  60. onCodeScanned: (codes) => {
  61. if (codes && codes.length > 0) {
  62. onResult(codes)
  63. }
  64. },
  65. });
  66. if (!device || !hasPermission) {
  67. return (
  68. <View style={styles.tipsScreen}>
  69. <TextView style={styles.tipsText}>
  70. {!hasPermission ? "Camera access has been denied. Please enable it in your device settings.(E0)" : "Can not find camera device.(E1)"}
  71. </TextView>
  72. </View>
  73. )
  74. }
  75. return (
  76. <>
  77. <Camera
  78. style={{width: $width, height: $vh(110)}}
  79. device={device}
  80. isActive={isActive}
  81. codeScanner={codeScanner}
  82. enableZoomGesture={true}
  83. photo={false}
  84. video={false}
  85. audio={false}
  86. resizeMode="cover"
  87. torch={flashOn ? 'on' : 'off'}
  88. />
  89. { isActive &&
  90. <Pressable
  91. style={styles.flashLight}
  92. onPress={() => switchFlash(!flashOn)}>
  93. <MaterialIcons
  94. name={flashOn ? "flashlight-on" : "flashlight-off"}
  95. size={36}
  96. color="#fff"/>
  97. </Pressable>
  98. }
  99. </>
  100. );
  101. };
  102. export default QRScanner;
  103. const styles = StyleSheet.create({
  104. tipsScreen: {
  105. top: 0,
  106. left: 0,
  107. right: 0,
  108. bottom: 0,
  109. position: 'absolute',
  110. alignItems: "center",
  111. justifyContent: "center",
  112. backgroundColor: '#111',
  113. },
  114. tipsText: {
  115. color: textLight,
  116. fontSize: 14,
  117. textAlign: "center"
  118. },
  119. flashLight: {
  120. bottom: 120,
  121. zIndex: 2,
  122. opacity: 0.7,
  123. padding: 8,
  124. position: 'absolute'
  125. }
  126. })