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?
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 .
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.
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With