【问题标题】:ARCore Save Camera Image (Unity C#) on Button clickARCore 在按钮单击时保存相机图像(Unity C#)
【发布时间】:2018-09-30 04:14:58
【问题描述】:

我有一个类似的问题,例如以下三个 SO 问题:

我的目标是在单击按钮时保存相机图像(没有增强对象)和相机的姿势。今天我尝试了一整天用 ARCore 保存相机图像。我尝试了上面链接的三个问题的不同方法,但没有奏效。 附加到按钮的我的 C# 脚本:

using UnityEngine;
using UnityEngine.UI;
using GoogleARCore;
using System.IO;
public class takeimg : MonoBehaviour
{
    private Texture2D m_TextureRender;
    public Button yourButton;
    private byte[] m_EdgeDetectionResultImage = null;

    void Start()
    {
        Button btn = yourButton.GetComponent<Button>();
        btn.onClick.AddListener(TaskOnClick);
    }

    void TaskOnClick()
    {
        var image = Frame.CameraImage.AcquireCameraImageBytes();

        m_TextureRender = new Texture2D(image.Width, image.Height, TextureFormat.RGBA32, false, false);
        m_EdgeDetectionResultImage = new byte[image.Width * image.Height * 4];


        System.Runtime.InteropServices.Marshal.Copy(image.Y, m_EdgeDetectionResultImage, 0, image.Width * image.Height * 4);

        m_TextureRender.LoadRawTextureData(m_EdgeDetectionResultImage);
        m_TextureRender.Apply();

        var encodedJpg = m_TextureRender.EncodeToJPG();
        var path = Application.persistentDataPath;

        File.WriteAllBytes(path + "/test.jpg", encodedJpg);
    }
}

目前我得到的图像看起来: Saved Image 它看起来类似于我上面链接的第三个 SO 问题。 所以有些东西仍然是错误的/缺失的。有人可以帮我出什么问题吗?缓冲区有什么问题?

更新: 与此同时,我设法找回了一张黑色/白色的照片:bw picture 这是我的新 TaskOnClick 函数:

void TaskOnClick()
{
    var image = Frame.CameraImage.AcquireCameraImageBytes();

    byte[] bufferY = new byte[image.Width * image.Height];
    byte[] bufferU = new byte[image.Width * image.Height / 2];
    byte[] bufferV = new byte[image.Width * image.Height / 2];
    System.Runtime.InteropServices.Marshal.Copy(image.Y, bufferY, 0, image.Width * image.Height);
    System.Runtime.InteropServices.Marshal.Copy(image.U, bufferU, 0, image.Width * image.Height / 2);
    System.Runtime.InteropServices.Marshal.Copy(image.V, bufferV, 0, image.Width * image.Height / 2);


    m_TextureRender = new Texture2D(image.Width, image.Height, TextureFormat.RGBA32, false, false);


    Color c = new Color();
    for (int y = 0; y < image.Height; y++) {
        for (int x =0; x<image.Width;x++) {
            float Y = bufferY[y * image.Width + x];
            float U = bufferU[(y/2) * image.Width + x];
            float V = bufferV[(y/2) * image.Width + x];
            c.r = Y;
            c.g = Y;
            c.b = Y;

            c.r /= 255.0f;
            c.g /= 255.0f;
            c.b /= 255.0f;

            if (c.r < 0.0f) c.r = 0.0f;
            if (c.g < 0.0f) c.g = 0.0f;
            if (c.b < 0.0f) c.b = 0.0f;

            if (c.r > 1.0f) c.r = 1.0f;
            if (c.g > 1.0f) c.g = 1.0f;
            if (c.b > 1.0f) c.b = 1.0f;

            c.a = 1.0f;
            m_TextureRender.SetPixel(image.Width-1-x, y, c);      
        }
    }

    var encodedJpg = m_TextureRender.EncodeToJPG();
    var path = Application.persistentDataPath;
    File.WriteAllBytes(path + "/test.jpg", encodedJpg);
}

谁能告诉我们,Google ARCore 使用的实际 YUV 到 RGB 对话是什么?我试了一些,但图片中的颜色看起来总是不对的...... 有没有比我的解决方案更简单的方法来保存当前帧中的相机图像?

【问题讨论】:

    标签: c# android unity3d arcore


    【解决方案1】:

    您可以使用 NatCam API 轻松完成此操作:https://assetstore.unity.com/packages/tools/integration/natcam-webcam-api-52154

    如果你想要视频,还有 NatCorder

    【讨论】:

    • 很抱歉回答迟了。我买了另一个统一的相机资产(assetstore.unity.com/packages/tools/integration/…),问题是在拍摄照片期间,arcore 没有跟踪……所以当我开始“相机拍摄”然后走几米,拍照时,我的姿势错误.
    • 好的。听起来您应该将此标记为已回答,并使用您的新代码和新问题的详细信息打开一个新问题。
    猜你喜欢
    • 1970-01-01
    • 2012-10-16
    • 2016-04-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多