繁体   English   中英

如何编码为 png 网络摄像头纹理?

[英]How to encode to png a webcam texture?

设置:Unity 2019

我正在尝试从飞机上获取纹理。

我在飞机上捕获了相机输入和 map。 然后我想连续阅读纹理。

我尝试过这样的事情。 PS:我是团结的新手。

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

public class CameraInput : MonoBehaviour
{

static WebCamTexture backCam;

void Start()
{
    if (backCam == null)
        backCam = new WebCamTexture();

    GetComponent<Renderer>().material.mainTexture = backCam;

    if (!backCam.isPlaying)
        backCam.Play();

}

void Update()
{

     byte[] bytes = GetComponent<Renderer>().material.mainTexture.EncodeToPNG();
         System.IO.File.WriteAllBytes(path, bytes);
         Debug.Log(bytes.Length/1024  + "Kb was saved as: " + path);

}

}

收到错误:

无法检索图像参考 UnityEngine.ImageConversion:EncodeToPNG(Texture2D)

我认为您缺少几个步骤获取可用设备列表将设备名称设置为网络摄像头在设置纹理之前播放网络摄像头

网络摄像头是一个纹理,你可以 encodetopng Texture2d。 所以解决这个问题的方法是:

Texture2D tex = new Texture2D(backCam.width, backCam.height);
tex.SetPixels(backCam.GetPixels());
tex.Apply();
byte[] bytes =  tex.EncodeToPNG();  

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM