Advertisement
zidniryi

pickerIma.js

Apr 25th, 2020
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.88 KB | None | 0 0
  1. // import React, {PureComponent} from 'react';
  2. // import {
  3. // AppRegistry,
  4. // StyleSheet,
  5. // Text,
  6. // TouchableOpacity,
  7. // View,
  8. // Image,
  9. // } from 'react-native';
  10. // import {RNCamera} from 'react-native-camera';
  11.  
  12. // class App extends PureComponent {
  13. // constructor(props) {
  14. // super(props);
  15. // this.state = {
  16. // imageUri: '',
  17. // isCaptture: false,
  18. // };
  19. // }
  20. // render() {
  21. // return (
  22. // <View style={styles.container}>
  23. // <RNCamera
  24. // ref={(ref) => {
  25. // this.camera = ref;
  26. // }}
  27. // style={styles.preview}
  28. // type={RNCamera.Constants.Type.back}
  29. // flashMode={RNCamera.Constants.FlashMode.on}
  30. // androidCameraPermissionOptions={{
  31. // title: 'Permission to use camera',
  32. // message: 'We need your permission to use your camera',
  33. // buttonPositive: 'Ok',
  34. // buttonNegative: 'Cancel',
  35. // }}
  36. // androidRecordAudioPermissionOptions={{
  37. // title: 'Permission to use audio recording',
  38. // message: 'We need your permission to use your audio',
  39. // buttonPositive: 'Ok',
  40. // buttonNegative: 'Cancel',
  41. // }}
  42. // onGoogleVisionBarcodesDetected={({barcodes}) => {
  43. // console.log(barcodes);
  44. // }}
  45. // />
  46. // <View style={{flex: 0, flexDirection: 'row', justifyContent: 'center'}}>
  47. // <TouchableOpacity
  48. // onPress={this.takePicture.bind(this)}
  49. // style={styles.capture}>
  50. // <Text style={{fontSize: 14}}> SNAP </Text>
  51. // </TouchableOpacity>
  52. // </View>
  53. // </View>
  54. // );
  55. // }
  56.  
  57. // takePicture = async () => {
  58. // if (this.camera) {
  59. // const options = {quality: 0.5, base64: true};
  60. // const data = await this.camera.takePictureAsync(options);
  61. // console.log(data.uri);
  62. // this.setState({isCaptture: true, imageUri: data.uri});
  63. // }
  64. // };
  65. // }
  66.  
  67. // const styles = StyleSheet.create({
  68. // container: {
  69. // flex: 1,
  70. // flexDirection: 'column',
  71. // backgroundColor: 'black',
  72. // },
  73. // preview: {
  74. // flex: 1,
  75. // justifyContent: 'flex-end',
  76. // alignItems: 'center',
  77. // },
  78. // capture: {
  79. // flex: 0,
  80. // backgroundColor: '#fff',
  81. // borderRadius: 5,
  82. // padding: 15,
  83. // paddingHorizontal: 20,
  84. // alignSelf: 'center',
  85. // margin: 20,
  86. // },
  87. // });
  88.  
  89. // export default App;
  90.  
  91. import React from 'react';
  92. import {
  93. Image,
  94. PixelRatio,
  95. StyleSheet,
  96. Text,
  97. TouchableOpacity,
  98. View,
  99. } from 'react-native';
  100. import ImagePicker from 'react-native-image-picker';
  101.  
  102. export default class App extends React.Component {
  103. state = {
  104. avatarSource: null,
  105. videoSource: null,
  106. };
  107.  
  108. constructor(props) {
  109. super(props);
  110.  
  111. this.selectPhotoTapped = this.selectPhotoTapped.bind(this);
  112. this.selectVideoTapped = this.selectVideoTapped.bind(this);
  113. }
  114.  
  115. selectPhotoTapped() {
  116. const options = {
  117. quality: 1.0,
  118. maxWidth: 500,
  119. maxHeight: 500,
  120. storageOptions: {
  121. skipBackup: true,
  122. },
  123. };
  124.  
  125. ImagePicker.launchCamera(options, (response) => {
  126. console.log('Response = ', response);
  127.  
  128. if (response.didCancel) {
  129. console.log('User cancelled photo picker');
  130. } else if (response.error) {
  131. console.log('ImagePicker Error: ', response.error);
  132. } else if (response.customButton) {
  133. console.log('User tapped custom button: ', response.customButton);
  134. } else {
  135. let source = {uri: response.uri};
  136.  
  137. // You can also display the image using data:
  138. // let source = { uri: 'data:image/jpeg;base64,' + response.data };
  139.  
  140. this.setState({
  141. avatarSource: source,
  142. });
  143. }
  144. });
  145. }
  146.  
  147. selectVideoTapped() {
  148. const options = {
  149. title: 'Video Picker',
  150. takePhotoButtonTitle: 'Take Video...',
  151. mediaType: 'video',
  152. videoQuality: 'medium',
  153. };
  154.  
  155. ImagePicker.showImagePicker(options, (response) => {
  156. console.log('Response = ', response);
  157.  
  158. if (response.didCancel) {
  159. console.log('User cancelled video picker');
  160. } else if (response.error) {
  161. console.log('ImagePicker Error: ', response.error);
  162. } else if (response.customButton) {
  163. console.log('User tapped custom button: ', response.customButton);
  164. } else {
  165. this.setState({
  166. videoSource: response.uri,
  167. });
  168. }
  169. });
  170. }
  171.  
  172. render() {
  173. return (
  174. <View style={styles.container}>
  175. <TouchableOpacity onPress={this.selectPhotoTapped.bind(this)}>
  176. <View
  177. style={[styles.avatar, styles.avatarContainer, {marginBottom: 20}]}>
  178. {this.state.avatarSource === null ? (
  179. <Text>Select a Photo</Text>
  180. ) : (
  181. <Image style={styles.avatar} source={this.state.avatarSource} />
  182. )}
  183. </View>
  184. </TouchableOpacity>
  185.  
  186. <TouchableOpacity onPress={this.selectVideoTapped.bind(this)}>
  187. <View style={[styles.avatar, styles.avatarContainer]}>
  188. <Text>Select a Video</Text>
  189. </View>
  190. </TouchableOpacity>
  191.  
  192. {this.state.videoSource && (
  193. <Text style={{margin: 8, textAlign: 'center'}}>
  194. {this.state.videoSource}
  195. </Text>
  196. )}
  197. </View>
  198. );
  199. }
  200. }
  201.  
  202. const styles = StyleSheet.create({
  203. container: {
  204. flex: 1,
  205. justifyContent: 'center',
  206. alignItems: 'center',
  207. backgroundColor: '#F5FCFF',
  208. },
  209. avatarContainer: {
  210. borderColor: '#9B9B9B',
  211. borderWidth: 1 / PixelRatio.get(),
  212. justifyContent: 'center',
  213. alignItems: 'center',
  214. },
  215. avatar: {
  216. borderRadius: 75,
  217. width: 150,
  218. height: 150,
  219. },
  220. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement