import React, { Component } from 'react'; import { AppRegistry, StyleSheet, Text, View, ListView, TouchableOpacity, Image, } from 'react-native'; import _ from 'lodash'; import Immutable from 'seamless-immutable'; import { CameraKitGallery, CameraKitCamera, } from 'react-native-camera-kit'; const FLASH_MODE_AUTO = "auto"; const FLASH_MODE_ON = "on"; const FLASH_MODE_OFF = "off"; class example extends Component { constructor(props) { super(props); const ds = new ListView.DataSource({rowHasChanged: (r1, r2) => r1 !== r2}); this.state = { albums:[], albumsDS: ds, shouldOpenCamera: false, shouldShowListView: false, image:{imageURI:""}, flashMode:FLASH_MODE_AUTO } } render() { return ( get albums {this.state.shouldOpenCamera ? "close camera" : "open camera"} {this._renderListView()} {this._renderCameraView()} ); } _renderListView() { if (this.state.shouldShowListView) { return( this._renderRow(rowData) } /> ) } } _renderRow(rowData) { //console.log('ran', rowData); return ( {rowData.albumName} ) } async onGetAlbumsPressed() { const albums = await CameraKitGallery.getAlbums(); const albumsNames = _.map(albums, 'albumName'); const albumsThumbnails = _.map(albums, 'albumName'); this.setState({...this.state, albumsDS:this.state.albumsDS.cloneWithRows(albums), albums:albums, shouldShowListView: true}); } async onAlbumNamePressed(albumName) { let base64Image = await CameraKitGallery.getThumbnailForAlbumName(albumName); let newAlbums = _.uniq(this.state.albums); let albumWithImage = base64Image = 'data:image/png;base64,' + base64Image; let album = _.find(newAlbums, function(o) { return o.albumName === albumName; }); const albumIndex = _.indexOf(newAlbums, album); album = {...album, image:base64Image }; newAlbums[albumIndex] = album; this.setState({albumsDS:this.state.albumsDS.cloneWithRows(newAlbums), albums:newAlbums}); } _renderCameraView() { if (this.state.shouldOpenCamera) { return ( { this.camera = cam; }} style={{flex: 1}} cameraOptions= {{ flashMode: 'auto', focusMode: 'on' }} /> switch camera flash auto flash on flash off ) } } async onSwitchCameraPressed() { const success = await this.camera.changeCamera(); } async onSetFlash(flashMode) { const success = await this.camera.setFleshMode(flashMode); } async onTakeIt() { const imageURI = await this.camera.capture(false); let newImage = {imageURI: imageURI}; this.setState({...this.state, image:newImage}); } onOpenCameraPressed() { this.setState({shouldOpenCamera:!this.state.shouldOpenCamera}); } } const styles = StyleSheet.create({ container: { flex: 1, //justifyContent: 'center', //alignItems: 'center', backgroundColor: '#F5FCFF', marginTop: 20 }, welcome: { fontSize: 20, textAlign: 'center', margin: 10, }, instructions: { textAlign: 'center', color: '#333333', marginBottom: 5, }, row: { flexDirection: 'row', justifyContent: 'center', padding: 10, backgroundColor: '#F6F6F6', }, text: { flex: 1, }, button: { fontSize: 18, alignSelf: 'center', backgroundColor: 'green' }, listView: { //flex:1, //flexDirection:'column', margin: 8, backgroundColor: '#D6DAC2', //alignSelf: 'stretch' }, }); AppRegistry.registerComponent('example', () => example);