Skip to content

Commit

Permalink
Add QR code detection to QRCamera
Browse files Browse the repository at this point in the history
- Add dependency on vision-camera-code-scanner
- Only activate camera when screen is focused
- FrameProcessor runs once per second since the docs state:
> A value of `1` indicates that the frame processor gets executed once per second, perfect for code scanning.
- But consider increasing to 5 in the future since the docs also state:
> For a QR Code Scanner, 5 FPS (200ms) might suffice
- Known issues: Lines 23-25 of QRCamera.tsx sometimes cause the following error when
navigating back from JoinOrg:
```
 ERROR  Warning: Can't perform a React state update on an unmounted component. This is a
no-op, but it indicates a memory leak in your application. To fix, cancel all
subscriptions and asynchronous tasks in a useEffect cleanup function.
    in QRCamera (at CameraControl.tsx:19)
 ```
  • Loading branch information
High5Apps committed Apr 12, 2022
1 parent 965be24 commit b8a7dc5
Show file tree
Hide file tree
Showing 5 changed files with 135 additions and 5 deletions.
21 changes: 19 additions & 2 deletions app/components/controls/QRCamera.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import React from 'react';
import { useIsFocused } from '@react-navigation/native';
import React, { useEffect } from 'react';
import { StyleSheet, Text } from 'react-native';
import { Camera, useCameraDevices } from 'react-native-vision-camera';
import { BarcodeFormat, useScanBarcodes } from 'vision-camera-code-scanner';
import useTheme from '../../Theme';
import FrameButton from './FrameButton';

Expand All @@ -18,17 +20,32 @@ const useStyles = () => {
};

export default function QRCamera() {
const [frameProcessor, barcodes] = useScanBarcodes([BarcodeFormat.QR_CODE], {
checkInverted: true,
});

useEffect(() => {
const contentData = barcodes.map((b) => b.content.data);
const contentDataSet = new Set(contentData);
const uniqueData = [...contentDataSet];
console.log({ uniqueData });
}, [barcodes]);

const { styles } = useStyles();

const devices = useCameraDevices();
const device = devices.back;

const isFocused = useIsFocused();

let content;
if (device) {
content = (
<Camera
device={device}
isActive
frameProcessor={frameProcessor}
frameProcessorFps={1}
isActive={isFocused}
style={StyleSheet.absoluteFill}
/>
);
Expand Down
7 changes: 6 additions & 1 deletion babel.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ module.exports = {
presets: ['module:metro-react-native-babel-preset'],
plugins: [
// Reanimated plugin must be listed last
'react-native-reanimated/plugin',
[
'react-native-reanimated/plugin',
{
globals: ['__scanCodes'],
},
],
],
};
90 changes: 90 additions & 0 deletions ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,65 @@ PODS:
- FlipperKit/FlipperKitNetworkPlugin
- fmt (6.2.1)
- glog (0.3.5)
- GoogleDataTransport (9.1.2):
- GoogleUtilities/Environment (~> 7.2)
- nanopb (~> 2.30908.0)
- PromisesObjC (< 3.0, >= 1.2)
- GoogleMLKit/BarcodeScanning (2.6.0):
- GoogleMLKit/MLKitCore
- MLKitBarcodeScanning (~> 1.7.0)
- GoogleMLKit/MLKitCore (2.6.0):
- MLKitCommon (~> 5.0.0)
- GoogleToolboxForMac/DebugUtils (2.3.2):
- GoogleToolboxForMac/Defines (= 2.3.2)
- GoogleToolboxForMac/Defines (2.3.2)
- GoogleToolboxForMac/Logger (2.3.2):
- GoogleToolboxForMac/Defines (= 2.3.2)
- "GoogleToolboxForMac/NSData+zlib (2.3.2)":
- GoogleToolboxForMac/Defines (= 2.3.2)
- "GoogleToolboxForMac/NSDictionary+URLArguments (2.3.2)":
- GoogleToolboxForMac/DebugUtils (= 2.3.2)
- GoogleToolboxForMac/Defines (= 2.3.2)
- "GoogleToolboxForMac/NSString+URLArguments (= 2.3.2)"
- "GoogleToolboxForMac/NSString+URLArguments (2.3.2)"
- GoogleUtilities/Environment (7.7.0):
- PromisesObjC (< 3.0, >= 1.2)
- GoogleUtilities/Logger (7.7.0):
- GoogleUtilities/Environment
- GoogleUtilities/UserDefaults (7.7.0):
- GoogleUtilities/Logger
- GoogleUtilitiesComponents (1.1.0):
- GoogleUtilities/Logger
- GTMSessionFetcher/Core (1.7.1)
- libevent (2.1.12)
- MLImage (1.0.0-beta2)
- MLKitBarcodeScanning (1.7.0):
- MLKitCommon (~> 5.0)
- MLKitVision (~> 3.0)
- MLKitCommon (5.0.0):
- GoogleDataTransport (~> 9.0)
- GoogleToolboxForMac/Logger (~> 2.1)
- "GoogleToolboxForMac/NSData+zlib (~> 2.1)"
- "GoogleToolboxForMac/NSDictionary+URLArguments (~> 2.1)"
- GoogleUtilities/UserDefaults (~> 7.0)
- GoogleUtilitiesComponents (~> 1.0)
- GTMSessionFetcher/Core (~> 1.1)
- Protobuf (~> 3.12)
- MLKitVision (3.0.0):
- GoogleToolboxForMac/Logger (~> 2.1)
- "GoogleToolboxForMac/NSData+zlib (~> 2.1)"
- GTMSessionFetcher/Core (~> 1.1)
- MLImage (= 1.0.0-beta2)
- MLKitCommon (~> 5.0)
- Protobuf (~> 3.12)
- nanopb (2.30908.0):
- nanopb/decode (= 2.30908.0)
- nanopb/encode (= 2.30908.0)
- nanopb/decode (2.30908.0)
- nanopb/encode (2.30908.0)
- OpenSSL-Universal (1.1.180)
- PromisesObjC (2.1.0)
- Protobuf (3.20.0)
- RCT-Folly (2021.06.28.00-v2):
- boost
- DoubleConversion
Expand Down Expand Up @@ -378,6 +435,9 @@ PODS:
- React-RCTImage
- RNSVG (12.3.0):
- React-Core
- vision-camera-code-scanner (0.2.0):
- GoogleMLKit/BarcodeScanning
- React-Core
- VisionCamera (2.13.1):
- React
- React-callinvoker
Expand Down Expand Up @@ -445,6 +505,7 @@ DEPENDENCIES:
- RNReanimated (from `../node_modules/react-native-reanimated`)
- RNScreens (from `../node_modules/react-native-screens`)
- RNSVG (from `../node_modules/react-native-svg`)
- vision-camera-code-scanner (from `../node_modules/vision-camera-code-scanner`)
- VisionCamera (from `../node_modules/react-native-vision-camera`)
- Yoga (from `../node_modules/react-native/ReactCommon/yoga`)

Expand All @@ -461,8 +522,21 @@ SPEC REPOS:
- Flipper-RSocket
- FlipperKit
- fmt
- GoogleDataTransport
- GoogleMLKit
- GoogleToolboxForMac
- GoogleUtilities
- GoogleUtilitiesComponents
- GTMSessionFetcher
- libevent
- MLImage
- MLKitBarcodeScanning
- MLKitCommon
- MLKitVision
- nanopb
- OpenSSL-Universal
- PromisesObjC
- Protobuf
- YogaKit

EXTERNAL SOURCES:
Expand Down Expand Up @@ -534,6 +608,8 @@ EXTERNAL SOURCES:
:path: "../node_modules/react-native-screens"
RNSVG:
:path: "../node_modules/react-native-svg"
vision-camera-code-scanner:
:path: "../node_modules/vision-camera-code-scanner"
VisionCamera:
:path: "../node_modules/react-native-vision-camera"
Yoga:
Expand All @@ -556,8 +632,21 @@ SPEC CHECKSUMS:
FlipperKit: d8d346844eca5d9120c17d441a2f38596e8ed2b9
fmt: ff9d55029c625d3757ed641535fd4a75fedc7ce9
glog: 85ecdd10ee8d8ec362ef519a6a45ff9aa27b2e85
GoogleDataTransport: 629c20a4d363167143f30ea78320d5a7eb8bd940
GoogleMLKit: 755661c46990a85e42278015f26400286d98ad95
GoogleToolboxForMac: 8bef7c7c5cf7291c687cf5354f39f9db6399ad34
GoogleUtilities: e0913149f6b0625b553d70dae12b49fc62914fd1
GoogleUtilitiesComponents: 679b2c881db3b615a2777504623df6122dd20afe
GTMSessionFetcher: 4577a4cc914a5a07c40a8a0ad0acc22080418c2d
libevent: 4049cae6c81cdb3654a443be001fb9bdceff7913
MLImage: a454f9f8ecfd537783a12f9488f5be1a68820829
MLKitBarcodeScanning: b8257854f6afc1c8443d61ec6b98c28b35625df6
MLKitCommon: 3bc17c6f7d25ce3660f030350b46ae7ec9ebca6e
MLKitVision: e87dc3f2e456a6ab32361ebd985e078dd2746143
nanopb: a0ba3315591a9ae0a16a309ee504766e90db0c96
OpenSSL-Universal: 1aa4f6a6ee7256b83db99ec1ccdaa80d10f9af9b
PromisesObjC: 99b6f43f9e1044bd87a95a60beff28c2c44ddb72
Protobuf: 66e2f3b26a35e4cc379831f4ced538274ee4b923
RCT-Folly: 803a9cfd78114b2ec0f140cfa6fa2a6bafb2d685
RCTRequired: 3c77b683474faf23920fbefc71c4e13af21470c0
RCTTypeSafety: 720b1841260dac692444c2822b27403178da8b28
Expand Down Expand Up @@ -587,6 +676,7 @@ SPEC CHECKSUMS:
RNReanimated: 32c91e28f5780937b8efc07ddde1bab8d373fe0b
RNScreens: 40a2cb40a02a609938137a1e0acfbf8fc9eebf19
RNSVG: 302bfc9905bd8122f08966dc2ce2d07b7b52b9f8
vision-camera-code-scanner: dda884a7f3ec8243a2a6d6489b91860648371bca
VisionCamera: 73658533c79f60ecba935d2128cff182f9e0cca0
Yoga: 90dcd029e45d8a7c1ff059e8b3c6612ff409061a
YogaKit: f782866e155069a2cca2517aafea43200b01fd5a
Expand Down
19 changes: 18 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@
"react-native-screens": "^3.13.1",
"react-native-svg": "^12.3.0",
"react-native-vector-icons": "^9.1.0",
"react-native-vision-camera": "^2.13.1"
"react-native-vision-camera": "^2.13.1",
"vision-camera-code-scanner": "^0.2.0"
},
"devDependencies": {
"@babel/core": "^7.12.9",
Expand Down

2 comments on commit b8a7dc5

@High5Apps
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This commit broke npm run ios because vision-camera-code-scanner doesn't support M1 macs yet. As a result, I needed to enable rosetta as suggested by this issue comment. I'm subscribed to that issue. So hopefully I can disable rosetta whenever vision-camera-code-scanner works for M1.

@High5Apps
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

^That only fixed building in Xcode. In order to fix npm run ios, I also needed to enable rosetta for Terminal

Please sign in to comment.