首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将RawImage保存到文件

将RawImage保存到文件
EN

Stack Overflow用户
提问于 2022-03-29 08:03:23
回答 3查看 639关注 0票数 1

因此,我设置了一个RawImage,它可以从VR耳机的通过摄像头捕捉图像。

现在图片已经正确显示了,我想保存它.

代码语言:javascript
复制
public class CameraCaptureBehaviour : MonoBehaviour
{
    private const string Tag = "[CAMERA_CAPTURE]";
    private const int CaptureFrequency = 500;
    [field:SerializeField] private RawImage CameraImage { get; set; }

    private int count = 0;

    private void Update()
    {
        if (++count % CaptureFrequency != 0) return;
        AndroidHelper.LogCat("Trying to get image", Tag);
        try
        {
            SaveImage();
        }
        catch (Exception e)
        {
            AndroidHelper.LogCat(e.Message, Tag);
        }
    }

    private void SaveImage()
    {
        var texture = CameraImage.texture;
        if (texture == null)
        {
            AndroidHelper.LogCat("No camera image found.", Tag);
            return;
        }

        var texture2d = (Texture2D) texture;
        var imageBytes = texture2d.EncodeToJPG();
        
        AndroidHelper.LogCat($"imageBytes is null: {imageBytes == null}", Tag);
        if (imageBytes == null) return;
        
        var filename = DateTime.Now.ToString("yyyyMMddHHmmssffff") + ".jpg";
        var filepath = Path.Combine(Application.persistentDataPath, filename);
        AndroidHelper.LogCat($"{imageBytes.Length} to {filepath}", Tag);
        try
        {
            File.WriteAllBytes(filepath, imageBytes);
            AndroidHelper.LogCat("SAVED", Tag);
        }
        catch (Exception e)
        {
            AndroidHelper.LogCat(e.Message, Tag);
        }
    }
}

这给了我

代码语言:javascript
复制
[CAMERA_CAPTURE] Trying to get image
[CAMERA_CAPTURE] imageBytes is null: False
[CAMERA_CAPTURE] 0 to /storage/emulated/0/Android/data/my.app/files/202203290841482532.png
[CAMERA_CAPTURE] SAVED

我不知道为什么图像应该是空的。

让我们试试将Color32数组转换为字节数组以通过网络发送建议的将Color32数组转换为字节数组以通过网络发送

代码语言:javascript
复制
public class CameraCaptureBehaviour : MonoBehaviour
{
    private const string Tag = "[CAMERA_CAPTURE]";
    private const int CaptureFrequency = 500;
    [field:SerializeField] private RawImage CameraImage { get; set; }

    private int count = 0;

    private void Update()
    {
        if (++count % CaptureFrequency != 0) return;
        AndroidHelper.LogCat("Trying to get image", Tag);
        try
        {
            SaveImage();
        }
        catch (Exception e)
        {
            AndroidHelper.LogCat(e.Message, Tag);
        }
    }

    private void SaveImage()
    {
        var texture = CameraImage.texture;
        if (texture == null)
        {
            AndroidHelper.LogCat("No camera image found.", Tag);
            return;
        }

        var texture2d = (Texture2D) texture;

        var color32 = new Color32Array();
        color32.colors = texture2d.GetPixels32();
        var imageBytes = color32.byteArray;
        
        AndroidHelper.LogCat($"imageBytes is null: {imageBytes == null}", Tag);
        if (imageBytes == null) return;
        
        var filename = DateTime.Now.ToString("yyyyMMddHHmmssffff") + ".jpg";
        var filepath = Path.Combine(Application.persistentDataPath, filename);
        AndroidHelper.LogCat($"{imageBytes.Length} to {filepath}", Tag);
        try
        {
            File.WriteAllBytes(filepath, imageBytes);
            AndroidHelper.LogCat("SAVED", Tag);
        }
        catch (Exception e)
        {
            AndroidHelper.LogCat(e.Message, Tag);
        }
    }
}

[StructLayout(LayoutKind.Explicit)]
public struct Color32Array
{
    [FieldOffset(0)]
    public byte[] byteArray;

    [FieldOffset(0)]
    public Color32[] colors;
}

这给了我

代码语言:javascript
复制
[CAMERA_CAPTURE] Trying to get image
[CAMERA_CAPTURE] Texture '' is not configured correctly to allow GetPixels

我还发现了https://answers.unity.com/questions/1503328/save-rawimage-texture-to-file.html --就像所有听起来完全符合你需要但几年前被问到的问题一样--完全缺乏有用的信息。

我只想把我在护目镜里看到的这张该死的照片保存下来,那不会很困难吧?!

如果能提供一些帮助,我们将不胜感激。

如何保存RawImage

(注:texture.GetType().FullName给了我UnityEngine.Texture2D)

(注意:检索后我检查了纹理大小,它是600x600的,所以图片不只是0像素大)

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2022-03-31 10:07:27

将图像布利特RenderTexture,然后使用它创建一个新的Texture2D

代码语言:javascript
复制
private static Texture2D GetReadableTexture2d(Texture texture)
{
    var tmp = RenderTexture.GetTemporary(
        texture.width,
        texture.height,
        0,
        RenderTextureFormat.Default,
        RenderTextureReadWrite.Linear
    );
    Graphics.Blit(texture, tmp);

    var previousRenderTexture = RenderTexture.active;
    RenderTexture.active = tmp;

    var texture2d = new Texture2D(texture.width, texture.height);
    texture2d.ReadPixels(new Rect(0, 0, texture.width, texture.height), 0, 0);
    texture2d.Apply();

    RenderTexture.active = previousRenderTexture;
    RenderTexture.ReleaseTemporary(tmp);
    return texture2d;
}

然后,生成的Texture2D可以是EncodeToPNGd并保存:

代码语言:javascript
复制
public void SaveImage()
{
    SaveImage(GetReadableTexture2d(CameraImage.texture));
}

private static void SaveImage(Texture2D texture)
{
    var imageBytes = texture.EncodeToPNG();

    if (imageBytes.Length == 0)
    {
        AndroidHelper.LogCat($"Trying to save empty picture. Abort.", Tag);
        return;
    }

    var filename = DateTime.Now.ToString("yyyyMMddHHmmssffff") + ".png";
    var filepath = Path.Combine(Application.persistentDataPath, filename);
    AndroidHelper.LogCat($"{imageBytes.Length} to {filepath}", Tag);
    try
    {
        File.WriteAllBytes(filepath, imageBytes);
        AndroidHelper.LogCat("SAVED", Tag);
    }
    catch (Exception e)
    {
        AndroidHelper.LogCat(e.Message, Tag);
    }
}

不确定是否有更简单的方法,但这似乎是可行的,至少。

票数 0
EN

Stack Overflow用户

发布于 2022-03-29 08:24:57

你可以叫texture2d.EncodeToJPG到texture2D,你说得对。但是,您不能只是将任何纹理转换到texture2D中。嗯,你可以,但事情不一定会解决,因为你知道你自己;

要将RENDERtexture转换为Texture2D,可以执行以下操作:

代码语言:javascript
复制
RenderTexture.active = texture;
Texture2D texture2d = new Texture2D(texture.width, texture.height);
texture2d.ReadPixels(new Rect(0, 0, texture.width, texture.height),0, 0);
texture2d.Apply();
RenderTexture.active = null;

如果这不起作用,您可以尝试Graphics.CopyTexture(),如derHugo所指出的,也可以使用google将特定的纹理类型转换为Texture2D

票数 1
EN

Stack Overflow用户

发布于 2022-03-29 08:30:08

我认为您不能简单地将RawImage.texture转换为Texture2D。试着把它复制成实际的样子。

代码语言:javascript
复制
var texture2d = new Texture2D(texture.width, texture.hight);
Graphics.CopyTexture(texture, texture2d);    

请参阅Graphics.CopyTexture

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71658455

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档