Last active
November 5, 2020 15:48
-
-
Save brunopk/ec4c7aa1907627e54490c93e15d081d3 to your computer and use it in GitHub Desktop.
React Native component using https://reactnavigation.org/docs/drawer-based-navigation/ and https://react-native-camera.github.io/react-native-camera/
This file contains hidden or 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
'use strict'; | |
import React, {PureComponent} from 'react'; | |
import {StyleSheet, View, Alert} from 'react-native'; | |
import {RNCamera} from 'react-native-camera'; | |
class Scanner extends PureComponent { | |
constructor(props) { | |
super(props); | |
this.state = { | |
navigation: props.navigation, | |
backScreen: props.route.params.backScreen, | |
toSendBackWithCod: props.route.params.toSendBackWithCod, | |
toSendBackAsCopy: props.route.params.toSendBackAsCopy, | |
}; | |
} | |
render() { | |
return ( | |
<RNCamera | |
ref={(ref) => { | |
this.camera = ref; | |
}} | |
style={styles.preview} | |
type={RNCamera.Constants.Type.back} | |
flashMode={RNCamera.Constants.FlashMode.off} | |
captureAudio={false} | |
androidCameraPermissionOptions={{ | |
title: 'Permission to use camera', | |
message: 'We need your permission to use your camera', | |
buttonPositive: 'Ok', | |
buttonNegative: 'Cancel', | |
}} | |
onGoogleVisionBarcodesDetected={this.onGoogleVisionBarcodesDetected}> | |
<View style={styles.greenBorder} /> | |
</RNCamera> | |
); | |
} | |
onGoogleVisionBarcodesDetected = ({barcodes}) => { | |
if (barcodes.length === 1) { | |
let data = null; | |
// Case: error | |
try { | |
data = JSON.parse(barcodes[0].data); | |
if (typeof data === 'number') { | |
throw Error(); | |
} | |
// Case: success | |
} catch { | |
data = barcodes[0].data; | |
let params = {}; | |
params[this.state.toSendBackWithCod] = data; | |
this.state.toSendBackAsCopy.keys.forEach( | |
(k, i) => (params[k] = {...this.state.toSendBackAsCopy.values[i]}), | |
); | |
this.state.navigation.navigate(this.state.backScreen, {...params}); | |
} | |
} | |
}; | |
} | |
const styles = StyleSheet.create({ | |
greenBorder: { | |
top: '32%', | |
left: '21%', | |
height: '38%', | |
width: '58%', | |
zIndex: 1, | |
borderColor: 'lightgreen', | |
borderWidth: 5, | |
}, | |
preview: { | |
backgroundColor: 'blue', | |
height: '100%', | |
zIndex: -1, | |
}, | |
}); | |
export {Scanner}; |
This file contains hidden or 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
'use strict'; | |
import React from 'react'; | |
import {Button} from 'react-native' | |
function Btn() { | |
return ( | |
<Button | |
onPress={() => | |
navigation.navigate('ScannerComponentScreen', { | |
backScreen: 'ScannerCallerScreen', | |
toSendBackWithCod: 'code', | |
toSendBackAsCopy: { | |
values: [myObject], | |
keys: ['myObjectKey'], | |
}, | |
}) | |
} | |
/> | |
) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment