gangrelg
gangrelg

Reputation: 113

React Native Camera - Multiple Photos

I am currently using react-native-camera as a library to take pictures. I managed to show and hide a one and only camera component depending on a specific state. I am working on an app that has multiple buttons to take a picture, for example:

I have been breaking my head on how to do this, but can't figure it out.

My code is the following:

import React, { Component } from 'react';
import { StyleSheet, Text, TouchableOpacity, View, Button } from 'react-native';
import { RNCamera } from 'react-native-camera';

export default class BadInstagramCloneApp extends Component {
  constructor(props){
    super(props);
    this.state = {
      isVisible: false,
      value1: null,
      value2: null
    }
  }

  render() {
    return (
        <View style={styles.subcontainer}>
          {this.state.isVisible === true
              ?
                <View style={styles.container}>
                  <RNCamera
                      ref={ref => {
                        this.camera = ref;
                      }}
                      style={styles.preview}
                      type={RNCamera.Constants.Type.back}
                      flashMode={RNCamera.Constants.FlashMode.on}
                      permissionDialogTitle={'Permission to use camera'}
                      permissionDialogMessage={'We need your permission to use your camera phone'}
                      onGoogleVisionBarcodesDetected={({ barcodes }) => {
                        console.log(barcodes);
                      }}
                  />
                  <View style={{ flex: 0, flexDirection: 'row', justifyContent: 'center' }}>
                    <TouchableOpacity onPress={this.takePicture.bind(this)} style={styles.capture}>
                      <Text style={{ fontSize: 14 }}> SNAP </Text>
                    </TouchableOpacity>
                  </View>
                </View>
              :
                <View>
                  <Button title='PHOTO 1' onPress={this.changeState}/>
                  <Button title='PHOTO 2' onPress={this.changeState2}/>
                  <Button title='SHOW RESULTS' onPress={this.showResults}/>
                </View>
          }
        </View>
    );
  }

  changeState = () =>{
    this.setState({isVisible: true})
  }

  changeState2 = () =>{
    this.setState({isVisible: true})
  }

  showResults = () => {
    console.log('VALUE1: ' + this.state.value1);
    console.log('VALUE2: ' + this.state.value2);
  }

  takePicture = async function() {
    if (this.camera) {
      const options = { quality: 0.5, base64: true };
      const data = await this.camera.takePictureAsync(options);
      console.log(data.uri);
      this.setState({isVisible: false});
    }
  };
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    flexDirection: 'column',
    backgroundColor: 'black'
  },
  subcontainer: {
    flex: 1,
    flexDirection: 'column',
  },
  preview: {
    flex: 1,
    justifyContent: 'flex-end',
    alignItems: 'center',
  },
  capture: {
    flex: 0,
    backgroundColor: '#fff',
    borderRadius: 5,
    padding: 15,
    paddingHorizontal: 20,
    alignSelf: 'center',
    margin: 20,
  },
});

Upvotes: 1

Views: 2361

Answers (1)

gbalduzzi
gbalduzzi

Reputation: 10186

I would use the state to distinguish which "camera" you are using.

Your initial state:

this.state = {
  isVisible: false,
  pictureType: null,
  value1: null,
  value2: null
}

The function to call when a button is called, where each button has a different pictureType:

initTakingPicture = (pictureType) => {
  this.setState({
    isVisible: true,
    pictureType: pictureType
  })
}

Your example button:

<Button title='PHOTO 1' onPress={() => this.initTakingPicture("A")}/>

Then in your takePicture function you can check the state to distinguish which type of picture you are taking and save it into the according field:

  takePicture = async function() {
    if (this.camera) {
      const options = { quality: 0.5, base64: true };
      const data = await this.camera.takePictureAsync(options);
      console.log(data.uri);
      let fieldToSave = "value1" // Fallback
      if (this.state.pictureType === "A") {
        // Operation you need to do for pictureType A
        fieldToSave = "value1"
      } else if (this.state.pictureType === "B") {
        // Operation you need to do for pictureType B
        fieldToSave = "value2"
      } 

      this.setState({
        isVisible: false,  
        pictureType: null,
        [fieldToSave]: data.uri
      });
    }
  };

Upvotes: 1

Related Questions