ARCore单击按钮保存摄像机图像(Unity C#) [英] ARCore Save Camera Image (Unity C#) on Button click

查看:178
本文介绍了ARCore单击按钮保存摄像机图像(Unity C#)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I've a similiar question like the follow three SO questions:

  • Save AcquireCameraImageBytes() from Unity ARCore to storage as an image
  • Save Camera image from unity ARCore
  • ARCore for Unity save camera image

我的目的是保存摄像机图像(不包含增强对象)和单击按钮时摄像机的姿势.今天,我整天尝试使用ARCore保存摄像机图像.我尝试了与上面链接的三个问题不同的方法,但是没有用. 我的C#脚本(附在按钮上):

My aim is to save the camera image (without the augmented objects) and the pose of the camera on a button click. Today I tried the whole day on saving the camera image with ARCore. I tried the different approaches from the three questions linked above, but it didn't worke. My C# Script which is attached to a button:

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);
    }
}

目前,我得到的图像看起来像: 保存的图像 它看起来与我上面链接的第三个SO问题类似. 因此,仍然存在某些错误/遗漏.有人可以帮我怎么回事吗?缓冲区有东西吗?

Currently the image i get looks: Saved Image It looks similiar to the third SO question I linked above. So something is still wrong/missing. Can someone help me what's wrong? Something with the buffers?

更新: 在此期间,我设法退回了一张黑白照片: bw图片 这是我新的TaskOnClick函数:

Update: In the meantime I managed to get back a black/withe picture: bw picture Here my new TaskOnClick function:

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对话吗?我尝试了一些,但是图片中的颜色看起来总是错误的. 是否有比我的解决方案更简单的方法来保存当前帧中的摄像机图像?

Can someone tell, which is the acutal YUV to RGB conversation Google ARCore is useing? I tried some, but the colors in the pictures looked always wrong... Is there an easier way to save the camera image from the current frame than my solution?

推荐答案

您可以使用NatCam API轻松完成此操作:

You could do this pretty easily with the NatCam API: https://assetstore.unity.com/packages/tools/integration/natcam-webcam-api-52154

如果您想要视频,也可以使用NatCorder

There is also NatCorder if you want video

这篇关于ARCore单击按钮保存摄像机图像(Unity C#)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆