Advertisement
Guest User

Untitled

a guest
Sep 16th, 2019
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.58 KB | None | 0 0
  1. import React from "react";
  2. import { Platform, StyleSheet, Text, View, Switch, TouchableOpacity, Image } from "react-native";
  3. import * as FileSystem from 'expo-file-system';
  4. import * as Permissions from 'expo-permissions';
  5. import * as Location from 'expo-location';
  6. import { Camera } from 'expo-camera';
  7. import Constants from 'expo-constants';
  8. import {
  9. Ionicons,
  10. MaterialIcons,
  11. Foundation,
  12. MaterialCommunityIcons,
  13. Octicons
  14. } from '@expo/vector-icons';
  15.  
  16. import ToSqlite from "./ToSqlite"
  17.  
  18.  
  19. const PHOTOS_DIR = FileSystem.cacheDirectory + 'Camera'
  20. console.log(PHOTOS_DIR)
  21.  
  22.  
  23.  
  24. export default class TakePicture extends React.Component {
  25. state = {
  26. switchValue: false,
  27. hasCameraPermission: null,
  28. type: Camera.Constants.Type.back,
  29. imageuri: "",
  30. location: null,
  31. errorMessage: null,
  32. photos: [],
  33. latitude: "",
  34. longitude: "",
  35. altitude: "",
  36. };
  37.  
  38.  
  39.  
  40.  
  41. /* ## Partie gestion des fichiers du cache #####################################
  42. ############################################################################# */
  43.  
  44. /* Lire l'ensemble des fichier present dans le dossier du cache */
  45. componentFileDidMount = async () => {
  46. const photos = await FileSystem.readDirectoryAsync(PHOTOS_DIR);
  47. this.setState({ photos });
  48.  
  49. };
  50.  
  51. /* Suprimer l'ensemble des fichiers dans le dossier du cache (non apellé) */
  52. componentDeleteFileSystem = async () => {
  53. await FileSystem.deleteAsync(PHOTOS_DIR)
  54. }
  55.  
  56.  
  57. /* ## Partie Gestion du gps #################################################
  58. ############################################################################# */
  59.  
  60. /* Gestion de la permission du gps */
  61. _getLocation = async () => {
  62. const { status } = await Permissions.askAsync(Permissions.LOCATION);
  63.  
  64. if (status !== 'granted') {
  65. console.log('Permission not granted');
  66.  
  67. this.setState({
  68. errorMessage: 'PERMISSION NOT GRANTED'
  69. })
  70. }
  71.  
  72. /* lancement du la recherche d'emplacement avec l'otpion grande precison */
  73. const location = await Location.getCurrentPositionAsync({
  74. enableHighAccuracy: true,
  75. });
  76.  
  77.  
  78. this.setState({
  79. location
  80. })
  81. this.setState({
  82. latitude: this.state.location.coords.latitude,
  83. longitude: this.state.location.coords.longitude ,
  84. altitude: this.state.location.coords.altitude,
  85.  
  86. })
  87. }
  88.  
  89.  
  90. /* ## Partie gestion de la camera ##############################################
  91. ############################################################################# */
  92.  
  93. /* Fonction qui permet de donner la permission pour activer la camera */
  94. async componentDidMount() {
  95. const { status } = await Permissions.askAsync(Permissions.CAMERA);
  96. this.setState({ hasCameraPermission: status === 'granted' });
  97. console.log('Permission camera')
  98. }
  99.  
  100. /*
  101. Fonction de prise de photo. Si this camera est True une photo est prise.
  102. let photo est egale au chemin de la photo sur le telephone. le state est
  103. changé avec le chemin de la photo pour un traitement futur.
  104. */
  105. snap = async () => {
  106. this._getLocation()
  107. this.componentFileDidMount()
  108.  
  109. if (this.camera) {
  110. let photo = await this.camera.takePictureAsync();
  111. if (photo) {
  112. this.setState({ imageuri: photo.uri });
  113. }
  114. }
  115. console.log("latitude", this.state.location.coords.latitude)
  116. };
  117.  
  118.  
  119. render() {
  120.  
  121. /* En fonction de l'etat de la camera declenche des actions
  122. (null, false, true) */
  123. const { hasCameraPermission } = this.state;
  124. if (hasCameraPermission === null) {
  125. return <View />;
  126. } else if (hasCameraPermission === false) {
  127. return <Text>No access to camera</Text>;
  128. } else {
  129. return (
  130.  
  131. /* ## Balise principal */
  132.  
  133. <View style={styles.container}>
  134. <ToSqlite
  135. imageURI = {this.state.imageuri}
  136. latitude ={this.state.latitude}
  137. longitude = {this.state.longitude}
  138. altitude = {this.state.altitude}
  139. photos ={this.state.photos}
  140. />
  141. <Camera
  142. style={styles.cameraview}
  143. type={this.state.type}
  144. ref={ref => { this.camera = ref; }}>
  145.  
  146. {/* Boutton prise de photo declenche la fonction snap de prise de photo */}
  147. <View style={styles.bottombutton}>
  148. <TouchableOpacity
  149. onPress={this.snap}>
  150. <Ionicons name="ios-radio-button-on" size={80} color="white" />
  151. </TouchableOpacity>
  152. </View>
  153. </Camera>
  154.  
  155.  
  156. </View>
  157. );
  158. }
  159. }
  160. }
  161.  
  162. const styles = StyleSheet.create({
  163. container: {
  164. flex: 1,
  165. },
  166.  
  167. cameraview: {
  168. flex: 1,
  169. },
  170.  
  171. bottombutton: {
  172. flex: 1,
  173. justifyContent: 'flex-end',
  174. alignItems: 'center',
  175. }
  176. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement