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

查看:29
本文介绍了我可以使用设备的相机在 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 与任何纹理一样,允许您通过 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天全站免登陆