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

问题描述 投票:0回答:2

设置:Unity 2019

我正在尝试从平面上获取纹理。

我捕获相机输入并将其映射到平面上。然后,我想连续读取纹理。

我尝试过类似的操作。 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)

c# unity3d encode texture-mapping render-to-texture
2个回答
0
投票

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


0
投票

网络摄像头是一个纹理,您可以将其编码为Texture2d。因此,解决方案是:

Texture2D tex = new Texture2D(backCam.width, backCam.height);
tex.SetPixels(backCam.GetPixels());
tex.Apply();
byte[] bytes =  tex.EncodeToPNG();  
© www.soinside.com 2019 - 2024. All rights reserved.