我可以使用设备的相机在Unity中拍照吗? [英] Can I take a photo in Unity using the device's camera?

查看:173
本文介绍了我可以使用设备的相机在Unity中拍照吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我完全不熟悉Unity3D的更复杂的功能集,并且很好奇它是否具有拍照并操作的功能.具体来说,我的愿望是让用户进行自拍,然后让他们围绕自己的脸部追踪以创建PNG,然后将其纹理映射到模型上.

I'm entirely unfamiliar with Unity3D's more complex feature set and am curious if it has the capability to take a picture and then manipulate it. Specifically my desire is to have the user take a selfie and then have them trace around their face to create a PNG that would then be texture mapped onto a model.

我知道人脸映射到模型很简单,但是我想知道是否需要将照片/雕刻功能写入到包含在内的Chrome应用程序中,或者是否可以全部在Unity中完成.我不需要有关如何执行此操作的教程,只需询问这是否可行即可.

I know that the face mapping onto a model is simple, but I'm wondering if I need to write the photo/carving functionality into the encompassing Chrome app, or if it can all be done from within Unity. I don't need a tutorial on how to do it, just asking if it's something that is possible.

推荐答案

是的,这是可能的.您将要查看 WebCamTexture 功能.

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

您创建一个WebCamTexture并调用其 Play()函数,该函数开始相机. WebCamTexture和任何Texture一样,允许您通过GetPixels()调用获取像素.这使您可以在需要时拍摄快照,并将其保存在Texture2D中.调用 EncodeToPNG()并随后写入文件应该可以使您到达那里.

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);
    }
}

这篇关于我可以使用设备的相机在Unity中拍照吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆