import React from 'react';
import { NavigationEvents } from 'react-navigation';
import { Button, Platform, StyleSheet, Text, View } from 'react-native';
import { ScreenOrientation } from 'expo';
import * as Svg from 'react-native-svg';
import * as Permissions from 'expo-permissions';
import { BarCodeScanner } from 'expo-barcode-scanner';
const BUTTON_COLOR = Platform.OS === 'ios' ? '#fff' : '#666';
interface State {
isPermissionsGranted: boolean;
type: any;
cornerPoints?: any[];
alerting: boolean;
haveDimensions: boolean;
canvasHeight?: number;
canvasWidth?: number;
boundingBox?: {
origin: {
x: number;
y: number;
};
size: {
width: number;
height: number;
};
};
}
export default class BarcodeScannerExample extends React.Component<{}, State> {
static navigationOptions = {
title: '',
};
canChangeOrientation = false;
readonly state: State = {
isPermissionsGranted: false,
type: BarCodeScanner.Constants.Type.back,
alerting: false,
haveDimensions: false,
};
componentDidFocus = async () => {
const { status } = await Permissions.askAsync(Permissions.CAMERA);
this.setState({ isPermissionsGranted: status === 'granted' });
}
toggleAlertingAboutResult = () => {
this.setState({ alerting: !this.state.alerting });
}
toggleScreenOrientationState = () => {
if (this.canChangeOrientation) {
ScreenOrientation.lockAsync(ScreenOrientation.OrientationLock.PORTRAIT_UP);
} else {
ScreenOrientation.lockAsync(ScreenOrientation.OrientationLock.ALL);
}
this.canChangeOrientation = !this.canChangeOrientation;
}
setCanvasDimensions = (e: any) => {
this.setState({
canvasWidth: e.nativeEvent.layout.width,
canvasHeight: e.nativeEvent.layout.height,
haveDimensions: true,
});
}
render() {
if (!this.state.isPermissionsGranted) {
return (
You have not granted permission to use the camera on this device!
);
}
const circles = [];
if (this.state.cornerPoints) {
for (const point of this.state.cornerPoints) {
circles.push(
);
}
}
return (
{this.state.haveDimensions && (
{this.state.boundingBox && (
)}
{circles}
)}
);
}
toggleType = () =>
this.setState({
type:
this.state.type === BarCodeScanner.Constants.Type.back
? BarCodeScanner.Constants.Type.front
: BarCodeScanner.Constants.Type.back,
})
handleBarCodeScanned = (data: any) => {
if (this.state.alerting) {
requestAnimationFrame(() => {
alert(JSON.stringify(data));
});
}
this.setState({ cornerPoints: data.cornerPoints, boundingBox: data.bounds });
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
},
preview: {
...StyleSheet.absoluteFillObject,
backgroundColor: 'black',
},
toolbar: {
position: 'absolute',
bottom: 0,
left: 0,
right: 0,
paddingVertical: 10,
paddingHorizontal: 10,
flexDirection: 'row',
justifyContent: 'space-between',
backgroundColor: 'rgba(255,255,255,0.2)',
},
svg: {
position: 'absolute',
borderWidth: 2,
borderColor: 'red',
},
});