How to upload an image using Expo Camera to Cloudinary

Published on Mar 31, 2021

13 min read

EXPO

cover_image

Originally Published at Jscrambler's Blog.

The camera feature in a mobile device allows it to capture pictures and record videos, making it very helpful in many circumstances. By using the expo-camera library the process of adding that camera feature to an application becomes seamless, which is why in this tutorial, we’ll take a look at how to use Expo Camera to take a picture and then upload that same picture to a real-time cloud service Cloudinary.

Prerequisites

🔗

To follow this tutorial, please make sure you are familiarized with JavaScript/ES6 and meet the following requirements in your local dev environment:

  • Have Node.js version >= 14.x.x installed.
  • Have access to one package manager such as npm or yarn or npx.
  • Have expo-cli installed, or use npx

The source code is available at this Github repository.

Create an Expo app

🔗

Start by creating a new Expo app and then install the dependency expo-camera. Execute the following commands in a terminal window:

npx expo init project-name
# select the blank template
cd project-name
expo install expo-camera

Create a custom camera component

🔗

The expo-camera library provides a React component that allows snapping pictures using a device's front or back camera. It exposes properties like zoom, autofocus, preview image after snapping, white balance, face detection, barcode scanning, and flash mode.

For this demo, let's create a component that when rendered renders the <Camera> component initially.

Start by adding the following import statements in the App.js file.

1import React, { useState, useRef, useEffect } from 'react';
2import {
3 StyleSheet,
4 Dimensions,
5 View,
6 Text,
7 TouchableOpacity
8} from 'react-native';
9import { Camera } from 'expo-camera';
10import { AntDesign, MaterialIcons } from '@expo/vector-icons';

The @expo/vector-icons is another package bundled with Expo SDK and allows the use of various icons from different icon sets. You can find the references to these icons at icons.expo.fyi.

The Dimensions from React Native is used to get the application’s windows width and height.

  • To display the camera in full-screen mode, let's get the height of the window on which the application is running.
  • Then, define a custom variable called CAPTURE_SIZE representing 80% of the window height. This variable is used in styles later.
  • Add the following code snippet before the App component.
1const WINDOW_HEIGHT = Dimensions.get('window').height;
2const CAPTURE_SIZE = Math.floor(WINDOW_HEIGHT * 0.08);

The expo-camera library exposes an API of methods. To invoke any of these methods, define a reference to the useRef React hook.

Add the following code snippet just after defining the App. Make sure to add a ref prop to the Camera component whose value is cameraRef.

1return (
2 <View style={styles.container}>
3 <Camera ref={cameraRef} style={styles.container} />
4 </View>
5);

Why use absoluteFillObject to position View component

🔗

The absoluteFillObject automatically sets a View component to be full screen and absolutely positioned. It also allows overriding the values such as top. For example, you may want to absolute position the View component with an offset like top: 30 to display it below the status bar.

Add the corresponding styles to the App component.

1const styles = StyleSheet.create({
2 container: {
3 ...StyleSheet.absoluteFillObject
4 },
5 text: {
6 color: '#fff'
7 }
8});

How to check for camera permissions

🔗

To use a device's camera, the application needs to ask a user to utilize the hardware functionality. This is done by asking the user to grant permission for camera access, and naturally, if the request gets denied, the application won't be able to use it.

  • First, define a state variable using the useState React hook called hasPermission.
  • Then, create a method called onHandlePermission. It is asynchronous and returns a Promise that resolves when the permissions are granted. To ask for permission, Camera.requestPermissionsAsync is used.
  • Update the state variable using the update function from the array if the promise is resolved and the permission has been granted.
  • Then, using a useEffect hook, invoke the method onHandlePermission.

Add the following code snippet in App component:

1export default function App() {
2 const cameraRef = useRef();
3 const [hasPermission, setHasPermission] = useState(null);
4
5 useEffect(() => {
6 onHandlePermission();
7 }, []);
8
9 const onHandlePermission = async () => {
10 const { status } = await Camera.requestPermissionsAsync();
11 setHasPermission(status === 'granted');
12 };
13
14 if (hasPermission === null) {
15 return <View />;
16 }
17 if (hasPermission === false) {
18 return <Text style={styles.text}>No access to camera</Text>;
19 }
20
21 // ...
22}

In the above code snippet, the two if statements are used either when:

  • The permission hasn’t been requested.
  • A user denies the permission, in which case, a text message stating that there is no access to the camera will be displayed.

Here is how asking for permissions are prompted on an Android device:

ss1

After the permission is granted, the Camera is now accessible on the device:

ss2

Switching between Camera types

🔗

To switch between different types of cameras on a device, let's add a custom method. The Camera component has a prop called type and by using it, the type of camera currently in use on the device can be determined.

Start by defining a state variable called cameraType to track the camera's current type. Give it a default value of type back. It determines that the default camera mode type is going to be back. The camera type is accessible from Camera.Constants.Type.back.

Define another state variable called isPreview. It will determine whether the app is in camera mode or image preview mode. It is going to have a default value of boolean false.

Add a method called switchCamera in the App component. Then, check if it is in the preview mode. If yes, return nothing.

If it is in the camera mode, write the logic to handle the switch between the back and front camera mode by updating the state value of cameraType.

Then, on the Camera component add a prop type={cameraType}.

Define the state variable to determine whether the camera is ready to capture photos or not. Call it isCameraReady with a default value of boolean false. Then, add a method called onCameraReady to update its value. Also, add the prop onCameraReady={onCameraReady} on the Camera component.

1export default function App() {
2 const cameraRef = useRef();
3 const [hasPermission, setHasPermission] = useState(null);
4 const [cameraType, setCameraType] = useState(Camera.Constants.Type.back);
5 const [isPreview, setIsPreview] = useState(false);
6 const [isCameraReady, setIsCameraReady] = useState(false);
7
8 useEffect(() => {
9 onHandlePermission();
10 }, []);
11
12 const onHandlePermission = async () => {
13 const { status } = await Camera.requestPermissionsAsync();
14 setHasPermission(status === 'granted');
15 };
16
17 const onCameraReady = () => {
18 setIsCameraReady(true);
19 };
20
21 const switchCamera = () => {
22 if (isPreview) {
23 return;
24 }
25 setCameraType(prevCameraType =>
26 prevCameraType === Camera.Constants.Type.back
27 ? Camera.Constants.Type.front
28 : Camera.Constants.Type.back
29 );
30 };
31
32 if (hasPermission === null) {
33 return <View />;
34 }
35 if (hasPermission === false) {
36 return <Text style={styles.text}>No access to camera</Text>;
37 }
38
39 return (
40 <View style={styles.container}>
41 <Camera
42 ref={cameraRef}
43 style={styles.container}
44 type={cameraType}
45 onCameraReady={onCameraReady}
46 />
47 </View>
48 );
49}

To allow the Camera to switch, add a custom icon button to switch between two different camera types. The icon is used from the MaterialIcons set from the @expo/vector-icons library.

After the Camera component in JSX code, add a View component that wraps the buttons such as switch camera types and capture a picture.

Inside the View component, create an icon button using TouchableOpacity. The onPress prop on this component is used to trigger an action. In this case, it is used to invoke the switchCamera method.

Add a disabled prop on TouchableOpacity that disables the button depending on the value of isCameraReady. If its value is false, then this button will not function.

1<View style={styles.container}>
2 <Camera
3 ref={cameraRef}
4 style={styles.container}
5 type={cameraType}
6 onCameraReady={onCameraReady}
7 />
8 <View style={styles.container}>
9 {!isPreview && (
10 <View style={styles.bottomButtonsContainer}>
11 <TouchableOpacity disabled={!isCameraReady} onPress={switchCamera}>
12 <MaterialIcons name="flip-camera-ios" size={28} color="white" />
13 </TouchableOpacity>
14 </View>
15 )}
16 </View>
17</View>

Add the styles for the above code snippet:

1const styles = StyleSheet.create({
2 // ...
3 bottomButtonsContainer: {
4 position: 'absolute',
5 flexDirection: 'row',
6 bottom: 28,
7 width: '100%',
8 alignItems: 'center',
9 justifyContent: 'center'
10 }
11});

Here is how the switch button is displayed:

ss3

Take a picture from the Camera and preview it

🔗

Camera API from the expo-camera library uses a method called takePictureAsync() to take a picture. It saves the photographed image in the app's cache directory by default.

The method accepts a configuration object with different options such as quality, base64, skipProcessing, exif, etc. We will use two options:

  • quality to specify the compression rate of the image snapped
  • base64 to include the image data in Base64 format.

These options are passed as properties in a JavaScript object. This object is then further passed as an argument to the takePictureAsync method.

Start by adding a new asynchronous method called onSnap. Start by checking the value of the cameraRef.current. If available, then the following logic defined in the code snippet below to take a picture will execute from this method.

Then, define an object called options with the following properties:

  • quality and set its value to 0.7. This option selects a value between 0 to 1.
  • base64 and set its value to true. It accepts a boolean value of true or false

The takePictureAsync method, when invoked, returns a promise that resolves into an object. Store the value resolved in a variable called data. It contains the image data in form of the following properties:

  • uri of the image stored in the app's cache.
  • width and height of the image.
  • if the base64 option is enabled, it will return the base64 data of the image.

Store the base64 data of the image in another variable called source.

Next, add an if condition to check if the source exists. If it exists, pause the camera mode and set the image preview mode to true to show the current picture after it is taken.

1const onSnap = async () => {
2 if (cameraRef.current) {
3 const options = { quality: 0.7, base64: true };
4 const data = await cameraRef.current.takePictureAsync(options);
5 const source = data.base64;
6
7 if (source) {
8 await cameraRef.current.pausePreview();
9 setIsPreview(true);
10 }
11 }
12};

To go back from the image preview mode to camera mode, add a method called cancelPreview. When this method invokes, it resumes the camera mode.

1const cancelPreview = async () => {
2 await cameraRef.current.resumePreview();
3 setIsPreview(false);
4};

Add the onSnap method as an action on TouchableOpacity component as the value of onPress prop. This button is responsible for capturing an image and is wrapped by the View component when the image preview mode is false.

1<View style={styles.container}>
2 {!isPreview && (
3 <View style={styles.bottomButtonsContainer}>
4 <TouchableOpacity disabled={!isCameraReady} onPress={switchCamera}>
5 <MaterialIcons name="flip-camera-ios" size={28} color="white" />
6 </TouchableOpacity>
7 <TouchableOpacity
8 activeOpacity={0.7}
9 disabled={!isCameraReady}
10 onPress={onSnap}
11 style={styles.capture}
12 />
13 </View>
14 )}
15</View>

Add the styles for the above code snippet:

1const styles = StyleSheet.create({
2 // ...
3 capture: {
4 backgroundColor: '#5A45FF',
5 borderRadius: 5,
6 height: CAPTURE_SIZE,
7 width: CAPTURE_SIZE,
8 borderRadius: Math.floor(CAPTURE_SIZE / 2),
9 marginBottom: 28,
10 marginHorizontal: 30
11 }
12});

Here is how the capture button is shown. It can now take pictures.

ss4

Add JSX code to trigger the cancelPreview method as an action on a TouchableOpacity component. It wraps an icon component from AntDesign. This is shown when the application is in image preview mode.

1<View style={styles.container}>
2 {isPreview && (
3 <TouchableOpacity
4 onPress={cancelPreview}
5 style={styles.closeButton}
6 activeOpacity={0.7}
7 >
8 <AntDesign name='close' size={32} color='#fff' />
9 </TouchableOpacity>
10 )}
11 {!isPreview && (
12 // ...
13 )}
14</View>

Add the styles for the above code snippet:

1const styles = StyleSheet.create({
2 // ...
3 closeButton: {
4 position: 'absolute',
5 top: 35,
6 right: 20,
7 height: 50,
8 width: 50,
9 borderRadius: 25,
10 justifyContent: 'center',
11 alignItems: 'center',
12 backgroundColor: '#5A45FF',
13 opacity: 0.7
14 }
15});

After taking a picture, here is how the image preview mode is displayed:

ss5

Setup the Cloudinary service

🔗

Before starting with this section, make sure you have a Cloudinary account set up. If you already have an account, log in here.

After logging in, you will be welcomed by a dashboard screen similar to below:

ss6

To upload an image to their service, two things are required.

First, an apiUrl which is constructed of the following base URL:

'https://api.cloudinary.com/v1_1/<your-cloud-name>/image/upload'

The value for the placeholder <your-cloud-name> is the cloud name you entered when creating a new account or as shown in the dashboard screen.

The second parameter required is called upload_preset. It is created by following the steps below:

  • From the Dashboard, click Settings in the menu bar and select the Upload tab.
  • Look for the section "Upload presets" and click "Add upload preset".
  • Enter the name of the upload preset. In the "Signing mode," select the value "Unsigned" from the drop-down menu.
  • Then click Save.

ss7

Upload an image to Cloudinary

🔗

To upload an image to the service, we need a few required presets. We will use JavaScript's fetch API to send a POST request to the Cloudinary API URL. A service that allows uploading base64 images requires the image data to be appended by the data:image/jpg;base64, prefix.

The request also requires a data object which has the image data as the file and the value of the upload_preset.

Modify the onSnap method inside as shown below.

1const onSnap = async () => {
2 if (cameraRef.current) {
3 const options = { quality: 0.7, base64: true };
4 const data = await cameraRef.current.takePictureAsync(options);
5 const source = data.base64;
6
7 if (source) {
8 await cameraRef.current.pausePreview();
9 setIsPreview(true);
10
11 let base64Img = `data:image/jpg;base64,${source}`;
12 let apiUrl =
13 'https://api.cloudinary.com/v1_1/<your-cloud-name>/image/upload';
14 let data = {
15 file: base64Img,
16 upload_preset: '<your-upload-preset>'
17 };
18
19 fetch(apiUrl, {
20 body: JSON.stringify(data),
21 headers: {
22 'content-type': 'application/json'
23 },
24 method: 'POST'
25 })
26 .then(async response => {
27 let data = await response.json();
28 if (data.secure_url) {
29 alert('Upload successful');
30 }
31 })
32 .catch(err => {
33 alert('Cannot upload');
34 });
35 }
36 }
37};

Take a picture and when it is successfully uploaded to the Cloudinary service, an alert message like below is displayed:

ss8

Using Camera2 api for Android

🔗

Android devices have a new package called android.hardware.camera2 that provides an interface to an individual camera. It replaces the deprecated Camera class.

To use the latest package using expo-camera, add the following prop with a value of boolean true on the Camera component.

1<Camera
2 // ...
3 useCamera2Api={true}
4/>

Conclusion

🔗

In this post, we have successfully used Expo Camera to take a picture and then upload it to a real-time service like Cloudinary. To add image saving functionality check out the expo-media-library.

The source code is available at this Github repository.

Originally Published at Jscrambler's Blog.


More Posts

Browse all posts

Aman Mittal author

I'm a software developer and a technical writer. In this blog, I write about Technical writing, Node.js, React Native and Expo.

Currently, working at Expo. Previously, I've worked as a Developer Advocate, and Senior Content Developer with companies like Draftbit, Vercel and Crowdbotics.


Copyright ©  2019-2022 Aman Mittal · All Rights Reserved.