Last active
November 10, 2021 02:35
-
-
Save srolija/f5ab14ea720eb0e00efa3ca9e490fb3c to your computer and use it in GitHub Desktop.
Running RNCamera for high performance scanning or QR codes. You'll need to follow this guide: https://react-native-community.github.io/react-native-camera/docs/installation and setup the Firebase, but only on Android. There is no need to use MLKit on iOS as its internal code detection is performant enough even on old devices.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* eslint-disable react/jsx-props-no-spreading */ | |
import React from 'react'; | |
import PropTypes from 'prop-types'; | |
import { RNCamera } from 'react-native-camera'; | |
const RNCameraAndroid = ({ callback, children, ...props }) => { | |
const barcodeHandler = ({ barcodes }) => { | |
if (barcodes.length === 0) { | |
return; | |
} | |
callback(barcodes[0].data); | |
}; | |
return ( | |
<RNCamera | |
// By default the RNCamera uses the old code detection on Android when using onBarCodeRead, | |
// therefore we need to configure it to use MLKit to get better performance. | |
onGoogleVisionBarcodesDetected={barcodeHandler} | |
googleVisionBarcodeType={RNCamera.Constants.GoogleVisionBarcodeDetection.BarcodeType.QR_CODE} | |
{...props}> | |
{children} | |
</RNCamera> | |
); | |
}; | |
RNCameraAndroid.propTypes = { | |
callback: PropTypes.func.isRequired, | |
children: PropTypes.node, | |
}; | |
RNCameraAndroid.defaultProps = { | |
children: null, | |
}; | |
export default RNCameraAndroid; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* eslint-disable react/jsx-props-no-spreading */ | |
import React from 'react'; | |
import PropTypes from 'prop-types'; | |
import { RNCamera } from 'react-native-camera'; | |
const RNCameraIOS = ({ callback, children, ...props }) => ( | |
<RNCamera | |
onBarCodeRead={code => callback(code.data)} | |
barCodeTypes={[RNCamera.Constants.BarCodeType.qr]} | |
{...props}> | |
{children} | |
</RNCamera> | |
); | |
RNCameraIOS.propTypes = { | |
callback: PropTypes.func.isRequired, | |
children: PropTypes.node, | |
}; | |
RNCameraIOS.defaultProps = { | |
children: null, | |
}; | |
export default RNCameraIOS; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ... | |
// Import the Camera wrapper instead of RNCamera | |
import Camera from './camera'; | |
// ... | |
// As props are passed through; use Camera as drop in replacement for RNCamera | |
<Camera | |
// Only special function is callback which will be called each time code is detected, | |
// which could easily be multiple times a second -- consider debouncing. | |
callback={debounceCallback} | |
// For best UX you will want to override the pending view to show informational message. | |
pendingAuthorizationView={pendingAuthorizationView()} | |
// As well as non authorized view -- on iOS you can link to settings app. | |
// You can check the example in older version of this code: | |
// https://gist.github.com/srolija/9b5b52236c73bdf88f1240ceabe54e1d#file-scene-js | |
notAuthorizedView={notAuthorizedView()} | |
// For Android (SDK23+) you will want to implement the popup messages. | |
androidCameraPermissionOptions={{ | |
title: 'Camera access', | |
message: 'Permission is required to scan QR Codes.', | |
buttonPositive: 'OK', | |
buttonNegative: 'Cancel', | |
}} | |
flashMode={torch ? RNCamera.Constants.FlashMode.torch : RNCamera.Constants.FlashMode.off} | |
captureAudio={false} | |
keepAwake | |
> | |
{ renderScanningMarks() } | |
</Camera> | |
// ... |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment