Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access the camera - React Native

Tags:

This should be included in the react-native APIs but I cannot seem to find any API included out of the box.

I want to open up the camera on the click of a button. I can see some APIs just for iOS but react-native should make things cross-platform.

Does anyone know how to access the camera (not the gallery) using react-native?

like image 936
Virk Bilal Avatar asked Jul 23 '17 19:07

Virk Bilal


People also ask

How do I access camera react-native?

You just have to import the core React Native components such as View and Alert as well as RNCamera from react-native-camera . Then, create a class component App that is going to render the JSX that uses a hardware camera on the device's screen. This going to be done by wrapping the RNCamera component inside a View .

Does react-native support camera?

What Is React Native Camera? React Native Camera (RNCamera) is the go-to component when it comes to implementing camera functionality in a React Native app. This component helps you communicate with the native OS through some simple functions so you can use device hardware.


1 Answers

You might like to use react-native-camera module for this.

Here's an example usage of the library:

'use strict'; import React, { Component } from 'react'; import {   AppRegistry,   Dimensions,   StyleSheet,   Text,   TouchableHighlight,   View } from 'react-native'; import Camera from 'react-native-camera';  class BadInstagramCloneApp extends Component {   render() {     return (       <View style={styles.container}>         <Camera           ref={(cam) => {             this.camera = cam;           }}           style={styles.preview}           aspect={Camera.constants.Aspect.fill}>           <Text style={styles.capture} onPress={this.takePicture.bind(this)}>[CAPTURE]</Text>         </Camera>       </View>     );   }    takePicture() {     const options = {};     //options.location = ...     this.camera.capture({metadata: options})       .then((data) => console.log(data))       .catch(err => console.error(err));   } }  const styles = StyleSheet.create({   container: {     flex: 1,     flexDirection: 'row',   },   preview: {     flex: 1,     justifyContent: 'flex-end',     alignItems: 'center'   },   capture: {     flex: 0,     backgroundColor: '#fff',     borderRadius: 5,     color: '#000',     padding: 10,     margin: 40   } });  AppRegistry.registerComponent('BadInstagramCloneApp', () => BadInstagramCloneApp); 
like image 102
Trishant Pahwa Avatar answered Dec 07 '22 15:12

Trishant Pahwa