Can I take a photo in Unity using the device's camera?

Yes, this is possible. You will want to look at the WebCamTexture functionality.

You create a WebCamTexture and call its Play() function which starts the camera. WebCamTexture, as any Texture, allows you to get the pixels via a GetPixels() call. This allows you to take a snapshot in when you like, and you can save this in a Texture2D. A call to EncodeToPNG() and subsequent write to file should get you there.

Do note that the code below is a quick write-up based on the documentation. I have not tested it. You might have to select a correct device if there are more than one available.

using UnityEngine;
using System.Collections;
using System.IO;

public class WebCamPhotoCamera : MonoBehaviour 
{
    WebCamTexture webCamTexture;

    void Start() 
    {
        webCamTexture = new WebCamTexture();
        GetComponent<Renderer>().material.mainTexture = webCamTexture; //Add Mesh Renderer to the GameObject to which this script is attached to
        webCamTexture.Play();
    }

    IEnumerator TakePhoto()  // Start this Coroutine on some button click
    {

    // NOTE - you almost certainly have to do this here:

     yield return new WaitForEndOfFrame(); 

    // it's a rare case where the Unity doco is pretty clear,
    // http://docs.unity3d.com/ScriptReference/WaitForEndOfFrame.html
    // be sure to scroll down to the SECOND long example on that doco page 

        Texture2D photo = new Texture2D(webCamTexture.width, webCamTexture.height);
        photo.SetPixels(webCamTexture.GetPixels());
        photo.Apply();

        //Encode to a PNG
        byte[] bytes = photo.EncodeToPNG();
        //Write out the PNG. Of course you have to substitute your_path for something sensible
        File.WriteAllBytes(your_path + "photo.png", bytes);
    }
}

For those trying to get the camera to render live feed, here's how I managed to pull it off. First, I edited Bart's answer so the texture would be assigned on Update rather than just on Start:

void Start()
{
    webCamTexture = new WebCamTexture();
    webCamTexture.Play();
}

void Update()
{
    GetComponent<RawImage>().texture = webCamTexture;
}

Then I attached the script to a GameObject with a RawImage component. You can easily create one by Right Click -> UI -> RawImage in the Hierarchy in the Unity Editor (this requires Unity 4.6 and above). Running it should show a live feed of the camera in your view. As of this writing, Unity 5 supports the use of webcams in the free personal edition of Unity 5.

I hope this helps anyone looking for a good way to capture live camera feed in Unity.


It is possible. I highly recommend you look at WebcamTexture Unity API. It has some useful functions:

  1. GetPixel() -- Returns pixel color at coordinates (x, y).
  2. GetPixels() -- Get a block of pixel colors.
  3. GetPixels32() -- Returns the pixels data in raw format.
  4. MarkNonReadable() -- Marks WebCamTexture as unreadable
  5. Pause() -- Pauses the camera.
  6. Play() -- Starts the camera.
  7. Stop() -- Stops the camera.

Tags:

Photo

Unity3D