From b4f0393bbd46ab6161f33a37cfabb16d2d4a7fcf Mon Sep 17 00:00:00 2001 From: Ran Greenberg Date: Mon, 13 Mar 2017 11:40:32 +0200 Subject: [PATCH] add CameraKitCameraScreen --- src/CameraKitCameraScreen.js | 309 +++++++++++++++++++++++++++++++++++ 1 file changed, 309 insertions(+) create mode 100644 src/CameraKitCameraScreen.js diff --git a/src/CameraKitCameraScreen.js b/src/CameraKitCameraScreen.js new file mode 100644 index 0000000..c1b5ccd --- /dev/null +++ b/src/CameraKitCameraScreen.js @@ -0,0 +1,309 @@ +import React, { Component } from 'react'; +import { + StyleSheet, + Text, + View, + TouchableOpacity, + Image +} from 'react-native'; + + +import _ from 'lodash'; + + +import CameraKitCamera from './CameraKitCamera'; + +const FLASH_MODE_AUTO = 'auto'; +const FLASH_MODE_ON = 'on'; +const FLASH_MODE_OFF = 'off'; + + + + +export default class CameraScreen extends Component { + + constructor(props) { + super(props); + this.currentFlashArrayPosition = 0; + this.flashArray = [{ + mode: FLASH_MODE_AUTO, + image: _.get(this.props, 'flashImages.auto') + }, + { + mode: FLASH_MODE_ON, + image: _.get(this.props, 'flashImages.on') + }, + { + mode: FLASH_MODE_OFF, + image: _.get(this.props, 'flashImages.off') + } + ]; + this.state = { + captureImages: [], + flashData: this.flashArray[this.currentFlashArrayPosition], + ratios: [], + cameraOptions: {}, + ratioArrayPosition: -1, + imageCaptured: undefined + }; + this.onSetFlash = this.onSetFlash.bind(this); + this.onSwitchCameraPressed = this.onSwitchCameraPressed.bind(this); + + } + + componentDidMount() { + const cameraOptions = this.getCameraOptions(); + let ratios = []; + if (this.props.cameraRatioOverlay) { + ratios = this.props.cameraRatioOverlay.ratios || []; + } + this.setState({ + cameraOptions, + ratios: (ratios || []), + ratioArrayPosition: ((ratios.length > 0) ? 0 : -1) + }); + } + + getCameraOptions() { + const cameraOptions = { + flashMode: 'auto', + focusMode: 'on', + zoomMode: 'on' + }; + if (this.props.cameraRatioOverlay) { + const overlay = this.props.cameraRatioOverlay; + cameraOptions.ratioOverlayColor = overlay.color || OVERLAY_DEFAULT_COLOR; + + if (overlay.ratios && overlay.ratios.length > 0) { + cameraOptions.ratioOverlay = overlay.ratios[0]; + } + } + + return cameraOptions; + } + + renderFlashButton() { + return ( + this.onSetFlash(FLASH_MODE_AUTO)}> + + + ); + } + + renderSwitchCameraButton() { + if (this.props.cameraFlipImage) { + return ( + + + + ); + } + return null; + } + + renderTopButtons() { + return ( + + {this.renderFlashButton()} + {this.renderSwitchCameraButton()} + + ); + } + + renderCamera() { + return ( + + this.camera = cam} + style={{ flex: 1, justifyContent: 'flex-end' }} + cameraOptions={this.state.cameraOptions} + /> + + ); + } + + + renderCaptureButton() { + return ( + + this.onCaptureImagePressed()} + > + + + + ); + } + + renderGap() { + return ( + + + ); + + } + + renderRatioStrip() { + if (this.state.ratios.length === 0) { + return null; + } + return ( + + + Your images look best at a {this.state.ratios[0] || ''} ratio + this.onRatioButtonPressed()} + > + {this.state.cameraOptions.ratioOverlay} + + + + ); + } + + onButtonPressed(type) { + this.props.onBottomButtonPressed({ type, captureImages: this.state.captureImages }) + } + + + renderBottomButton(type) { + const buttonText = _(this.props).get(`actions.${type}ButtonText`) + if (buttonText) { + return ( + this.onButtonPressed(type)} + > + {_.get(this.props, `actions.${type}ButtonText`, type)} + + ); + } + return ( + this.renderGap() + ); + } + + renderBottomButtons() { + return ( + + {this.renderBottomButton('left')} + {this.renderCaptureButton()} + {this.renderBottomButton('right')} + + ); + } + + + onSwitchCameraPressed() { + this.camera.changeCamera(); + } + + async onSetFlash() { + this.currentFlashArrayPosition = (this.currentFlashArrayPosition + 1) % 3; + const newFlashData = this.flashArray[this.currentFlashArrayPosition]; + this.setState({ flashData: newFlashData }); + this.camera.setFlashMode(newFlashData.mode); + } + + async onCaptureImagePressed() { + const image = await this.camera.capture(true); + + if (image) { + this.setState({ imageCaptured: image, captureImages: _.concat(this.state.captureImages, image) }); + } + + } + + onRatioButtonPressed() { + const newRatiosArrayPosition = ((this.state.ratioArrayPosition + 1) % this.state.ratios.length); + const newCameraOptions = _.update(this.state.cameraOptions, 'ratioOverlay', (val) => this.state.ratios[newRatiosArrayPosition]); + this.setState({ ratioArrayPosition: newRatiosArrayPosition, cameraOptions: newCameraOptions }); + } + + render() { + return ( + + {this.renderTopButtons()} + {this.renderCamera()} + {this.renderRatioStrip()} + {this.renderBottomButtons()} + + ); + } +} + +const styles = StyleSheet.create({ + textStyle: { + color: 'white', + fontSize: 20 + }, + ratioBestText: { + color: 'white', + fontSize: 18, + }, + ratioText: { + color: '#ffc233', + fontSize: 18 + }, + topButtons: { + flex: 1, + flexDirection: 'row', + justifyContent: 'space-between', + paddingTop: 8, + paddingBottom: 0 + }, + cameraContainer: { + flex: 10, + flexDirection: 'column' + }, + bottomButtons: { + flex: 2, + flexDirection: 'row', + justifyContent: 'space-between', + padding: 14 + }, + gap: { + flex: 1, + flexDirection: 'row', + justifyContent: 'flex-end', + alignItems: 'center', + padding: 10 + }, + captureButtonContainer: { + flex: 1, + justifyContent: 'center', + alignItems: 'center', + }, + captureButton: { + flex: 1, + alignSelf: 'center', + alignItems: 'center', + justifyContent: 'center' + }, + captureNumber: { + justifyContent: 'center', + color: 'black', + backgroundColor: 'transparent' + }, + bottomButton: { + flex: 1, + flexDirection: 'row', + alignItems: 'center', + padding: 10 + } +});