Record video and Capture photos in app — Unity Android

Nirmal Mendis
5 min readJul 31, 2021

--

This is about how to create a unity project to capture photos from the mobile phone camera and also record videos from the phone’s camera without using the native camera application.

After searching the internet for hours, I was almost ready to give up on this, but then I found some useful plugins which help to do this task.

This application uses the WebCamTexture from unity to obtain the camera feed and therefore does not use the mobile’s native camera application.

Let’s divide this process into two sections.

  1. Capture Photos
  2. Record Videos

To do the above two tasks, first, you need to create a unity project (2D or 3D does not matter) and then switch the platform to android (File -> Build Settings).

(I also prefer to change the aspect ratio to 1280x720 Portrait. You can do this in the game tab).

Next import the below two plugins from the unity assets store:

  1. Native Gallery for Android & iOS | Integration | Unity Asset Store
  2. NatCorder — Video Recording API | Integration | Unity Asset Store

By the way, big thank for the developers of these plugins.

After importing these plugins, you need to set the minimum API level to 21 in order for them to work.

To do this, go to Edit -> Project Settings -> Player -> Other Settings, find Minimum API level under Identification section, and set the minimum API level to 21.

Now let's start coding.

Capture Photos

First, let create a canvas and a panel.

Next, let's create a raw image (as a child of the panel) to view the camera feed in our application. After that create a button to capture photos.

I set the raw image’s rotation z to -90 since the camera feed is rotated for some reason by default.

Next, I have aligned and scaled these objects and I set the button text to “Capture Photo”.

After doing these steps, your project structure should look like this:

Now let's save this scene as Scene1 (give any name you want).

Next, let's create a C# script named “camera” and open it in visual studio.

(Right-click on Assets section of Project window ->Create -> C# script)

First we need to add the following package imports:

using NatSuite.Recorders;using NatSuite.Recorders.Clocks;

Then, we need to create the following elements inside the class.

public RawImage rawimage;private WebCamTexture webcamTexture;

Next, we need to obtain the camera devices of the phone and assign a camera to the WebCamTexture we created. Also, note that you can change which camera you want to access by changing the value in the cam_devices[] array. Here I have used cam_devices[0] which is the back camera of my phone.

After that, we need to set the rawimage.texture property to the webcamTexture. Let’s do this in the Start() method.

void Start(){
//Obtain camera devices available
WebCamDevice[] cam_devices = WebCamTexture.devices;//Set a camera to the webcamTexturewebcamTexture = new WebCamTexture(cam_devices[0].name, 480, 640, 30);//Set the webcamTexture to the texture of the rawimagerawimage.texture = webcamTexture;rawimage.material.mainTexture = webcamTexture;//Start the camera
webcamTexture.Play();
}

Next, we need to create a function to capture and save an image.

To do this, first, create a Texture2D and set the webcamTexture’s pixels to this texture. Then we need to use the NativeGallery plugin to save the image to the Gallery by passing the texture to the NativeGallery.SaveImageToGallery method.

Since when assigning the webcamtexture pixels, we need to wait till the end of the frame, we need an IEnumerator method. Let's create this method as below:

private IEnumerator SaveImage(){//Create a Texture2D with the size of the rendered image on the screen.Texture2D texture = new Texture2D(rawimage.texture.width, rawimage.texture.height, TextureFormat.ARGB32, false);//Save the image to the Texture2Dtexture.SetPixels(webcamTexture.GetPixels());//texture = RotateTexture(texture, -90);texture.Apply();yield return new WaitForEndOfFrame();// Save the screenshot to Gallery/PhotosNativeGallery.Permission permission = NativeGallery.SaveImageToGallery(texture, "CameraTest", "CaptureImage.png", (success, path) => Debug.Log("Media save result: " + success + " " + path));// To avoid memory leaksDestroy(texture);}

Here, note that by default, the picture is save rotated by 90 degrees. Therefore if you need the picture to be saved in the correct orientation, uncomment the line which contains “texture = RotateTexture(texture, -90);” and create the RotateTexture method provided here.

Now let's create another method to handle the capture button click and name it as clickCapture. This method will call the SaveImage method we previously created.

public void clickCapture(){StartCoroutine(SaveImage());}

Next, I created an empty gameobject as a child of the panel in order to assign the camera script as a component. I prefer to do it this way but you can also add this script to any other object as well (eg. the rawImage).

After creating the gameobject, add the camera.cs script to this gameobject as a component. Then you will see the script requires a Rawimage object. Drag the RawImage from the scene and assign it here.

Now the project should look as follows:

Next, select the button, add a click event listener by clicking the “+” icon in the On Click() section. Next, drag the GameObject to the click event and then select the clickCapture function from camera.

Now we are done. Next, build the project and run it on your android device.

My app looks like this on my phone:

Click capture and you can find the image in your gallery.

This marks the end of the first section “Capture Photo”. The second section on “Record Video” can be found here.

Also please note that I am not an expert in unity, so please be kind enough to tolerate any mistakes and leave your feedback. Thank you!

--

--

No responses yet