Native Camera for Android & iOS [Open Source]

Implemented and runs like a charm just one question after the camera opens there is no way to “auto take” the pic?

Thanks!

NativeCamera launches the native camera app with some parameters like quality and then leaves the rest to the camera application itself. So, if the native camera application doesn’t have auto take functionality, then there is not much I can do.

Hi I am pretty new to unity c#, how do I use any of your app to create a user profile picture on a mobile app game in Unity. I want the player to be able to upload its picture profile from the phones gallery or from taking a photo with a camera

Create two buttons: “Camera” and “Gallery”. If user clicks Camera, use this NativeCamera plugin to capture a photo (example code). If user clicks Gallery, use my NativeGallery plugin to pick an image from the Gallery (example code).

Thanks for your prompt reply, do I need to add a script from the nativeGallery plugin to the gallery button, I already successfully imported the NativeGallery plugin and also created the button. I am confused on what to do next. Sorry for noob question, I am just getting a hang of unity

I also searched for the nativegallery script and tried to add it to the gallery button, it gives the following error “can’t add script behaviour Native Gallery. The script class cant be abstract!”

You don’t need to add NativeGallery or NativeCamera to any objects. You need to call them from your own script(s).

So, you should create your own script, create two public void functions in it (one for Gallery button and one for Camera button) and then add this script to an object in your scene. Then, add the Gallery function to Gallery button’s On Click event from the Inspector. Likewise, add the Camera function to Camera button’s On Click event.

Finally, fill the Gallery and Camera functions of your script. At this step, I believe the example codes I’ve posted in my previous post should provide enough info on how to call the NativeGallery and NativeCamera functions to get an image.

Hi @yasirkula , thanks for sharing your great work!

I’m looking for a WebcamTexture alternative for Android, and found your plugin. It looks promising, but I want to ask some of my requirement before diving into it.

  1. Does your plugin supports external usb cameras on Android?
  2. If it does, can I select a camera with a name or index?
  3. I need a live stream feed from a camera, and manipulate it with image processing. I don’t need to record and save the video into the disk. Is this possible? Or should I tweak your codes?

Even if it doesn’t suit my needs, this plugin is really great example. Thanks again for sharing it.

Thanks! Unfortunately, this plugin wouldn’t be useful in your use-case. It just launches the native camera app and waits for it to capture a photo or record a video. So, no live stream feed or camera selection option.

Oh, I see. Maybe I’ll have to build my own plugin then. Thanks for the quick reply!

1 Like

Hi

I’m using Native Camera plugin. Everything is ok when I record a video. But when I’m takeing picture there is popup information “No memory space”. Max image size is set to 512. After takeing photo path to the image is null. I have privileges to camera and storage. What can cause the problem?

My device is HMT1 (RealWear)

Is your internal storage nearly full? Maybe the camera app internally decides to save the recorded video to SD card but save the photo to internal storage and can’t find enough space in internal storage.

I have 10,1GB free and no SD card installed.

I wish I could help but NativeCamera simply starts the native camera app and leaves all the implementation details to that app. The “No memory space” error isn’t directly generated by NativeCamera and I’m not sure how this issue can be resolved.

P.S. This is the first time I’m encountering this issue in my plugins.

I tried to make a photo outside application and everything is ok. I’ve also tried to make photo from application (using NativeCamera) on my android phone and everything is ok. Problem exists only on HMT1 (RealWear)

I understand but even googling “No memory space” doesn’t really provide any useful info to me. I don’t know where to look to resolve this issue with just the information I have.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class NewBehaviourScript : MonoBehaviour
{
public void Gallerybutton;

public void Camerabutton;

// Start is called before the first frame update
void Start()
{

}

// Update is called once per frame
void Update()
{
if (Input.GetMouseButtonDown(0))
{
// Don’t attempt to use the camera if it is already open
if (NativeCamera.IsCameraBusy())
return;

if (Input.mousePosition.x < Screen.width / 2)
{
// Take a picture with the camera
// If the captured image’s width and/or height is greater than 512px, down-scale it
TakePicture(512);
}
else
{
// Record a video with the camera
RecordVideo();
}
}
}

private void TakePicture(int maxSize)
{
NativeCamera.Permission permission = NativeCamera.TakePicture((path) =>
{
Debug.Log("Image path: " + path);
if (path != null)
{
// Create a Texture2D from the captured image
Texture2D texture = NativeCamera.LoadImageAtPath(path, maxSize);
if (texture == null)
{
Debug.Log("Couldn’t load texture from " + path);
return;
}

// Assign texture to a temporary quad and destroy it after 5 seconds
GameObject quad = GameObject.CreatePrimitive(PrimitiveType.Quad);
quad.transform.position = Camera.main.transform.position + Camera.main.transform.forward * 2.5f;
quad.transform.forward = Camera.main.transform.forward;
quad.transform.localScale = new Vector3(1f, texture.height / (float)texture.width, 1f);

Material material = quad.GetComponent().material;
if (!material.shader.isSupported) // happens when Standard shader is not included in the build
material.shader = Shader.Find(“Legacy Shaders/Diffuse”);

material.mainTexture = texture;

Destroy(quad, 5f);

// If a procedural texture is not destroyed manually,
// it will only be freed after a scene change
Destroy(texture, 5f);
}
}, maxSize);

Debug.Log("Permission result: " + permission);
}

private void RecordVideo()
{
NativeCamera.Permission permission = NativeCamera.RecordVideo((path) =>
{
Debug.Log("Video path: " + path);
if (path != null)
{
// Play the recorded video
Handheld.PlayFullScreenMovie(“file://” + path);
}
});

Debug.Log("Permission result: " + permission);
}

void Update()
{
if (Input.GetMouseButtonDown(0))
{
// Don’t attempt to use the camera if it is already open
if (NativeCamera.IsCameraBusy())
return;

if (Input.mousePosition.x < Screen.width / 2)
{
// Take a picture with the camera
// If the captured image’s width and/or height is greater than 512px, down-scale it
TakePicture(512);
}
else
{
// Record a video with the camera
RecordVideo();
}
}
}

private void TakePicture(int maxSize)
{
NativeCamera.Permission permission = NativeCamera.TakePicture((path) =>
{
Debug.Log("Image path: " + path);
if (path != null)
{
// Create a Texture2D from the captured image
Texture2D texture = NativeCamera.LoadImageAtPath(path, maxSize);
if (texture == null)
{
Debug.Log("Couldn’t load texture from " + path);
return;
}

// Assign texture to a temporary quad and destroy it after 5 seconds
GameObject quad = GameObject.CreatePrimitive(PrimitiveType.Quad);
quad.transform.position = Camera.main.transform.position + Camera.main.transform.forward * 2.5f;
quad.transform.forward = Camera.main.transform.forward;
quad.transform.localScale = new Vector3(1f, texture.height / (float)texture.width, 1f);

Material material = quad.GetComponent().material;
if (!material.shader.isSupported) // happens when Standard shader is not included in the build
material.shader = Shader.Find(“Legacy Shaders/Diffuse”);

material.mainTexture = texture;

Destroy(quad, 5f);

// If a procedural texture is not destroyed manually,
// it will only be freed after a scene change
Destroy(texture, 5f);
}
}, maxSize);

Debug.Log("Permission result: " + permission);
}

private void RecordVideo()
{
NativeCamera.Permission permission = NativeCamera.RecordVideo((path) =>
{
Debug.Log("Video path: " + path);
if (path != null)
{
// Play the recorded video
Handheld.PlayFullScreenMovie(“file://” + path);
}
});

Debug.Log("Permission result: " + permission);
}
}

I have the following errors, can you help with this

There is an error for the NativeCamera, TakePicture, RecordVideo,

This is relating to your instructions via this link Native Camera for Android & iOS [Open Source] page-3#post-4776890