QRScanner.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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 [permissionStr, setPermissionStr] = useState("");
  43. const device = useCameraDevice('back');
  44. useEffect(() => {
  45. console.log("相机设备", device);
  46. checkPermission(result => {
  47. setHasPermission(result);
  48. })
  49. Camera.requestCameraPermission().then(res => {
  50. setPermissionStr(res);
  51. utils.logEventTracking("scan_camera_permission", res)
  52. if (!hasPermission) {
  53. setHasPermission(res == 'granted');
  54. }
  55. }).catch(err => {
  56. console.warn("相机权限请求错误", err);
  57. utils.logEventTracking("scan_camera_permission_error", err)
  58. });
  59. }, []);
  60. const codeScanner = useCodeScanner({
  61. codeTypes: ['qr'],
  62. onCodeScanned: (codes) => {
  63. if (codes && codes.length > 0) {
  64. onResult(codes)
  65. }
  66. },
  67. });
  68. if (!device || !hasPermission) {
  69. return (
  70. <View style={styles.tipsScreen}>
  71. <TextView style={styles.tipsText}>
  72. {!hasPermission ? "Camera access has been denied. Please enable it in your device settings.(E0)" : "Can not find camera device.(E1)"}
  73. </TextView>
  74. </View>
  75. )
  76. }
  77. return (
  78. <>
  79. <Camera
  80. style={{width: $width, height: $vh(110)}}
  81. device={device}
  82. isActive={isActive}
  83. codeScanner={codeScanner}
  84. enableZoomGesture={true}
  85. photo={false}
  86. video={false}
  87. audio={false}
  88. resizeMode="cover"
  89. torch={flashOn ? 'on' : 'off'}
  90. />
  91. { isActive &&
  92. <Pressable
  93. style={styles.flashLight}
  94. onPress={() => switchFlash(!flashOn)}>
  95. <MaterialIcons
  96. name={flashOn ? "flashlight-on" : "flashlight-off"}
  97. size={36}
  98. color="#fff"/>
  99. <TextView style={{color: "#fff", fontSize: 12}}>{permissionStr}</TextView>
  100. </Pressable>
  101. }
  102. </>
  103. );
  104. };
  105. export default QRScanner;
  106. const styles = StyleSheet.create({
  107. tipsScreen: {
  108. top: 0,
  109. left: 0,
  110. right: 0,
  111. bottom: 0,
  112. position: 'absolute',
  113. alignItems: "center",
  114. justifyContent: "center",
  115. backgroundColor: '#111',
  116. },
  117. tipsText: {
  118. color: textLight,
  119. fontSize: 14,
  120. textAlign: "center"
  121. },
  122. flashLight: {
  123. bottom: 120,
  124. zIndex: 2,
  125. opacity: 0.7,
  126. padding: 8,
  127. position: 'absolute'
  128. }
  129. })