Skip to content

Instantly share code, notes, and snippets.

@Malmousque
Created September 5, 2019 13:01
Show Gist options
  • Save Malmousque/0436532869b9d3426ea97f4fc6a84ff8 to your computer and use it in GitHub Desktop.
Save Malmousque/0436532869b9d3426ea97f4fc6a84ff8 to your computer and use it in GitHub Desktop.

Revisions

  1. Malmousque created this gist Sep 5, 2019.
    176 changes: 176 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,176 @@
    import React from "react";
    import { Platform, StyleSheet, Text, View, Switch, TouchableOpacity, Image } from "react-native";
    import * as FileSystem from 'expo-file-system';
    import * as Permissions from 'expo-permissions';
    import * as Location from 'expo-location';
    import { Camera } from 'expo-camera';
    import Constants from 'expo-constants';
    import {
    Ionicons,
    MaterialIcons,
    Foundation,
    MaterialCommunityIcons,
    Octicons
    } from '@expo/vector-icons';

    import ToSqlite from "./ToSqlite"


    const PHOTOS_DIR = FileSystem.cacheDirectory + 'Camera'
    console.log(PHOTOS_DIR)



    export default class TakePicture extends React.Component {
    state = {
    switchValue: false,
    hasCameraPermission: null,
    type: Camera.Constants.Type.back,
    imageuri: "",
    location: null,
    errorMessage: null,
    photos: [],
    latitude: "",
    longitude: "",
    altitude: "",
    };




    /* ## Partie gestion des fichiers du cache #####################################
    ############################################################################# */

    /* Lire l'ensemble des fichier present dans le dossier du cache */
    componentFileDidMount = async () => {
    const photos = await FileSystem.readDirectoryAsync(PHOTOS_DIR);
    this.setState({ photos });

    };

    /* Suprimer l'ensemble des fichiers dans le dossier du cache (non apellé) */
    componentDeleteFileSystem = async () => {
    await FileSystem.deleteAsync(PHOTOS_DIR)
    }


    /* ## Partie Gestion du gps #################################################
    ############################################################################# */

    /* Gestion de la permission du gps */
    _getLocation = async () => {
    const { status } = await Permissions.askAsync(Permissions.LOCATION);

    if (status !== 'granted') {
    console.log('Permission not granted');

    this.setState({
    errorMessage: 'PERMISSION NOT GRANTED'
    })
    }

    /* lancement du la recherche d'emplacement avec l'otpion grande precison */
    const location = await Location.getCurrentPositionAsync({
    enableHighAccuracy: true,
    });


    this.setState({
    location
    })
    this.setState({
    latitude: this.state.location.coords.latitude,
    longitude: this.state.location.coords.longitude ,
    altitude: this.state.location.coords.altitude,

    })
    }


    /* ## Partie gestion de la camera ##############################################
    ############################################################################# */

    /* Fonction qui permet de donner la permission pour activer la camera */
    async componentDidMount() {
    const { status } = await Permissions.askAsync(Permissions.CAMERA);
    this.setState({ hasCameraPermission: status === 'granted' });
    console.log('Permission camera')
    }

    /*
    Fonction de prise de photo. Si this camera est True une photo est prise.
    let photo est egale au chemin de la photo sur le telephone. le state est
    changé avec le chemin de la photo pour un traitement futur.
    */
    snap = async () => {
    this._getLocation()
    this.componentFileDidMount()

    if (this.camera) {
    let photo = await this.camera.takePictureAsync();
    if (photo) {
    this.setState({ imageuri: photo.uri });
    }
    }
    console.log("latitude", this.state.location.coords.latitude)
    };


    render() {

    /* En fonction de l'etat de la camera declenche des actions
    (null, false, true) */
    const { hasCameraPermission } = this.state;
    if (hasCameraPermission === null) {
    return <View />;
    } else if (hasCameraPermission === false) {
    return <Text>No access to camera</Text>;
    } else {
    return (

    /* ## Balise principal */

    <View style={styles.container}>
    <ToSqlite
    imageURI = {this.state.imageuri}
    latitude ={this.state.latitude}
    longitude = {this.state.longitude}
    altitude = {this.state.altitude}
    photos ={this.state.photos}
    />
    <Camera
    style={styles.cameraview}
    type={this.state.type}
    ref={ref => { this.camera = ref; }}>

    {/* Boutton prise de photo declenche la fonction snap de prise de photo */}
    <View style={styles.bottombutton}>
    <TouchableOpacity
    onPress={this.snap}>
    <Ionicons name="ios-radio-button-on" size={80} color="white" />
    </TouchableOpacity>
    </View>
    </Camera>


    </View>
    );
    }
    }
    }

    const styles = StyleSheet.create({
    container: {
    flex: 1,
    },

    cameraview: {
    flex: 1,
    },

    bottombutton: {
    flex: 1,
    justifyContent: 'flex-end',
    alignItems: 'center',
    }
    });