import React from 'react'; import { StyleSheet, Text, View, TouchableOpacity, Slider } from 'react-native'; import { RNCamera } from 'react-native-camera'; const landmarkSize = 2; const flashModeOrder = { off: 'on', on: 'auto', auto: 'torch', torch: 'off', }; const wbOrder = { auto: 'sunny', sunny: 'cloudy', cloudy: 'shadow', shadow: 'fluorescent', fluorescent: 'incandescent', incandescent: 'auto', }; export default class CameraScreen extends React.Component { state = { flash: 'off', zoom: 0, autoFocus: 'on', depth: 0, type: 'back', whiteBalance: 'auto', ratio: '16:9', ratios: [], photoId: 1, showGallery: false, photos: [], faces: [], }; getRatios = async function() { const ratios = await this.camera.getSupportedRatios(); return ratios; }; toggleView() { this.setState({ showGallery: !this.state.showGallery, }); } toggleFacing() { this.setState({ type: this.state.type === 'back' ? 'front' : 'back', }); } toggleFlash() { this.setState({ flash: flashModeOrder[this.state.flash], }); } setRatio(ratio) { this.setState({ ratio, }); } toggleWB() { this.setState({ whiteBalance: wbOrder[this.state.whiteBalance], }); } toggleFocus() { this.setState({ autoFocus: this.state.autoFocus === 'on' ? 'off' : 'on', }); } zoomOut() { this.setState({ zoom: this.state.zoom - 0.1 < 0 ? 0 : this.state.zoom - 0.1, }); } zoomIn() { this.setState({ zoom: this.state.zoom + 0.1 > 1 ? 1 : this.state.zoom + 0.1, }); } setFocusDepth(depth) { this.setState({ depth, }); } takePicture = async function() { if (this.camera) { this.camera.takePictureAsync().then(data => { console.log('data: ', data); }); } }; onFacesDetected = ({ faces }) => this.setState({ faces }); onFaceDetectionError = state => console.warn('Faces detection error:', state); renderFace({ bounds, faceID, rollAngle, yawAngle }) { return ( ID: {faceID} rollAngle: {rollAngle.toFixed(0)} yawAngle: {yawAngle.toFixed(0)} ); } renderLandmarksOfFace(face) { const renderLandmark = position => position && ( ); return ( {renderLandmark(face.leftEyePosition)} {renderLandmark(face.rightEyePosition)} {renderLandmark(face.leftEarPosition)} {renderLandmark(face.rightEarPosition)} {renderLandmark(face.leftCheekPosition)} {renderLandmark(face.rightCheekPosition)} {renderLandmark(face.leftMouthPosition)} {renderLandmark(face.mouthPosition)} {renderLandmark(face.rightMouthPosition)} {renderLandmark(face.noseBasePosition)} {renderLandmark(face.bottomMouthPosition)} ); } renderFaces() { return ( {this.state.faces.map(this.renderFace)} ); } renderLandmarks() { return ( {this.state.faces.map(this.renderLandmarksOfFace)} ); } renderCamera() { return ( { this.camera = ref; }} style={{ flex: 1, }} type={this.state.type} flashMode={this.state.flash} autoFocus={this.state.autoFocus} zoom={this.state.zoom} whiteBalance={this.state.whiteBalance} ratio={this.state.ratio} faceDetectionLandmarks={RNCamera.Constants.FaceDetection.Landmarks.all} onFacesDetected={this.onFacesDetected} onFaceDetectionError={this.onFaceDetectionError} focusDepth={this.state.depth} permissionDialogTitle={'Permission to use camera'} permissionDialogMessage={'We need your permission to use your camera phone'} > FLIP FLASH: {this.state.flash} WB: {this.state.whiteBalance} + - AF : {this.state.autoFocus} SNAP Gallery {this.renderFaces()} {this.renderLandmarks()} ); } render() { return {this.renderCamera()}; } } const styles = StyleSheet.create({ container: { flex: 1, paddingTop: 10, backgroundColor: '#000', }, navigation: { flex: 1, }, gallery: { flex: 1, flexDirection: 'row', flexWrap: 'wrap', }, flipButton: { flex: 0.3, height: 40, marginHorizontal: 2, marginBottom: 10, marginTop: 20, borderRadius: 8, borderColor: 'white', borderWidth: 1, padding: 5, alignItems: 'center', justifyContent: 'center', }, flipText: { color: 'white', fontSize: 15, }, item: { margin: 4, backgroundColor: 'indianred', height: 35, width: 80, borderRadius: 5, alignItems: 'center', justifyContent: 'center', }, picButton: { backgroundColor: 'darkseagreen', }, galleryButton: { backgroundColor: 'indianred', }, facesContainer: { position: 'absolute', bottom: 0, right: 0, left: 0, top: 0, }, face: { padding: 10, borderWidth: 2, borderRadius: 2, position: 'absolute', borderColor: '#FFD700', justifyContent: 'center', backgroundColor: 'rgba(0, 0, 0, 0.5)', }, landmark: { width: landmarkSize, height: landmarkSize, position: 'absolute', backgroundColor: 'red', }, faceText: { color: '#FFD700', fontWeight: 'bold', textAlign: 'center', margin: 10, backgroundColor: 'transparent', }, row: { flexDirection: 'row', }, });