Advertisement
douglaswilliamn

camera

Oct 21st, 2019
280
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import React from 'react';
  2. import {
  3.   Image,
  4.   PixelRatio,
  5.   StyleSheet,
  6.   Text,
  7.   TouchableOpacity,
  8.   View,
  9. } from 'react-native';
  10. import ImagePicker from 'react-native-image-picker';
  11.  
  12. export default class App extends React.Component {
  13.   state = {
  14.     avatarSource: null,
  15.     videoSource: null,
  16.   };
  17.  
  18.   constructor(props) {
  19.     super(props);
  20.  
  21.     this.selectPhotoTapped = this.selectPhotoTapped.bind(this);
  22.     this.selectVideoTapped = this.selectVideoTapped.bind(this);
  23.   }
  24.  
  25.   selectPhotoTapped() {
  26.     const options = {
  27.       quality: 1.0,
  28.       maxWidth: 500,
  29.       maxHeight: 500,
  30.       storageOptions: {
  31.         skipBackup: true,
  32.       },
  33.     };
  34.  
  35.     ImagePicker.showImagePicker(options, response => {
  36.       console.log('Response = ', response);
  37.  
  38.       if (response.didCancel) {
  39.         console.log('User cancelled photo picker');
  40.       } else if (response.error) {
  41.         console.log('ImagePicker Error: ', response.error);
  42.       } else if (response.customButton) {
  43.         console.log('User tapped custom button: ', response.customButton);
  44.       } else {
  45.         let source = {uri: response.uri};
  46.  
  47.         // You can also display the image using data:
  48.         // let source = { uri: 'data:image/jpeg;base64,' + response.data };
  49.  
  50.         this.setState({
  51.           avatarSource: source,
  52.         });
  53.       }
  54.     });
  55.   }
  56.  
  57.   selectVideoTapped() {
  58.     const options = {
  59.       title: 'Video Picker',
  60.       takePhotoButtonTitle: 'Take Video...',
  61.       mediaType: 'video',
  62.       videoQuality: 'medium',
  63.     };
  64.  
  65.     ImagePicker.showImagePicker(options, response => {
  66.       console.log('Response = ', response);
  67.  
  68.       if (response.didCancel) {
  69.         console.log('User cancelled video picker');
  70.       } else if (response.error) {
  71.         console.log('ImagePicker Error: ', response.error);
  72.       } else if (response.customButton) {
  73.         console.log('User tapped custom button: ', response.customButton);
  74.       } else {
  75.         this.setState({
  76.           videoSource: response.uri,
  77.         });
  78.       }
  79.     });
  80.   }
  81.  
  82.   render() {
  83.     return (
  84.       <View style={styles.container}>
  85.         <TouchableOpacity onPress={this.selectPhotoTapped.bind(this)}>
  86.           <View
  87.             style={[styles.avatar, styles.avatarContainer, {marginBottom: 20}]}>
  88.             {this.state.avatarSource === null ? (
  89.               <Text>Select a Photo</Text>
  90.             ) : (
  91.               <Image style={styles.avatar} source={this.state.avatarSource} />
  92.             )}
  93.           </View>
  94.         </TouchableOpacity>
  95.  
  96.         <TouchableOpacity onPress={this.selectVideoTapped.bind(this)}>
  97.           <View style={[styles.avatar, styles.avatarContainer]}>
  98.             <Text>Select a Video</Text>
  99.           </View>
  100.         </TouchableOpacity>
  101.  
  102.         {this.state.videoSource && (
  103.           <Text style={{margin: 8, textAlign: 'center'}}>
  104.             {this.state.videoSource}
  105.           </Text>
  106.         )}
  107.       </View>
  108.     );
  109.   }
  110. }
  111.  
  112. const styles = StyleSheet.create({
  113.   container: {
  114.     flex: 1,
  115.     justifyContent: 'center',
  116.     alignItems: 'center',
  117.     backgroundColor: '#F5FCFF',
  118.   },
  119.   avatarContainer: {
  120.     borderColor: '#9B9B9B',
  121.     borderWidth: 1 / PixelRatio.get(),
  122.     justifyContent: 'center',
  123.     alignItems: 'center',
  124.   },
  125.   avatar: {
  126.     borderRadius: 75,
  127.     width: 150,
  128.     height: 150,
  129.   },
  130. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement