QRScanner.js 3.6 KB

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