How To Switch Between Front And Back Camera In Android Unity Using Webcam Texture
Tape video and Capture photos in app — Unity Android
This is nearly how to create a unity projection to capture photos from the mobile phone camera and as well record videos from the phone's camera without using the native photographic camera application.
Afterward searching the internet for hours, I was almost ready to requite upwardly on this, but and so I constitute some useful plugins which help to practise this task.
This awarding uses the WebCamTexture from unity to obtain the camera feed and therefore does not use the mobile'southward native camera application.
Permit's divide this process into two sections.
- Capture Photos
- Tape Videos
To do the higher up two tasks, outset, you lot need to create a unity project (2D or 3D does not matter) and then switch the platform to android (File -> Build Settings).
(I also adopt to change the aspect ratio to 1280x720 Portrait. You can do this in the game tab).
Side by side import the below two plugins from the unity assets store:
- Native Gallery for Android & iOS | Integration | Unity Asset Shop
- NatCorder — Video Recording API | Integration | Unity Asset Shop
Past the way, big thank for the developers of these plugins.
Subsequently importing these plugins, you need to set the minimum API level to 21 in order for them to work.
To practise this, go to Edit -> Project Settings -> Thespian -> Other Settings, observe Minimum API level under Identification section, and set the minimum API level to 21.
Now let's start coding.
Capture Photos
Offset, let create a canvas and a console.
Next, let'due south create a raw image (as a child of the console) to view the camera feed in our application. Later on that create a button to capture photos.
I fix the raw paradigm's rotation z to -90 since the camera feed is rotated for some reason past default.
Next, I accept aligned and scaled these objects and I fix the push button text to "Capture Photo".
After doing these steps, your projection structure should look like this:
Now let'south salvage this scene as Scene1 (give any proper name you lot desire).
Next, allow'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 demand to add together the following packet imports:
using NatSuite.Recorders; using NatSuite.Recorders.Clocks;
Then, we demand to create the following elements inside the class.
public RawImage rawimage; private WebCamTexture webcamTexture;
Next, we demand to obtain the camera devices of the phone and assign a camera to the WebCamTexture nosotros created. Also, note that y'all can change which photographic 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 photographic camera of my telephone.
Subsequently that, we demand 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; //Ready a camera to the webcamTexture webcamTexture = new WebCamTexture(cam_devices[0].proper name, 480, 640, 30); //Set the webcamTexture to the texture of the rawimage rawimage.texture = webcamTexture; rawimage.material.mainTexture = webcamTexture; //Start the camera
webcamTexture.Play(); }
Adjacent, we need to create a function to capture and save an epitome.
To exercise this, outset, create a Texture2D and set the webcamTexture's pixels to this texture. And then we need to use the NativeGallery plugin to save the prototype to the Gallery by passing the texture to the NativeGallery.SaveImageToGallery method.
Since when assigning the webcamtexture pixels, nosotros need to wait till the end of the frame, we need an IEnumerator method. Let's create this method every bit below:
individual 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 epitome to the Texture2D texture.SetPixels(webcamTexture.GetPixels()); //texture = RotateTexture(texture, -90); texture.Use(); yield return new WaitForEndOfFrame(); // Save the screenshot to Gallery/Photos NativeGallery.Permission permission = NativeGallery.SaveImageToGallery(texture, "CameraTest", "CaptureImage.png", (success, path) => Debug.Log("Media save result: " + success + " " + path)); // To avoid memory leaks Destroy(texture); }
Here, notation that past default, the flick is relieve rotated past 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 hither.
Now let's create another method to handle the capture button click and proper name it as clickCapture. This method volition call the SaveImage method we previously created.
public void clickCapture() { StartCoroutine(SaveImage()); }
Next, I created an empty gameobject every bit a child of the panel in order to assign the camera script as a component. I adopt to practise it this way but you tin can also add this script to whatever other object equally well (eg. the rawImage).
After creating the gameobject, add the camera.cs script to this gameobject every bit a component. Then you volition see the script requires a Rawimage object. Drag the RawImage from the scene and assign it hither.
Now the project should look every bit 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 so select the clickCapture function from camera.
Now we are done. Adjacent, build the project and run it on your android device.
My app looks like this on my phone:
Click capture and you lot can find the image in your gallery.
This marks the finish of the starting time section "Capture Photo". The second section on "Record Video" tin be establish hither.
Also please note that I am not an practiced in unity, and so please exist kind enough to tolerate any mistakes and exit your feedback. Cheers!
Source: https://nirmalmendis.medium.com/record-video-and-capture-photos-in-app-unity-android-d2e6729284cd
Posted by: kilbyindart.blogspot.com
0 Response to "How To Switch Between Front And Back Camera In Android Unity Using Webcam Texture"
Post a Comment