Skip to content

Instantly share code, notes, and snippets.

@brunopk
Last active November 5, 2020 15:48
Show Gist options
  • Save brunopk/ec4c7aa1907627e54490c93e15d081d3 to your computer and use it in GitHub Desktop.
Save brunopk/ec4c7aa1907627e54490c93e15d081d3 to your computer and use it in GitHub Desktop.
'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};
'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