LoadRawTextureData() not enough data provided erro

2019-08-01 09:35发布

I am working on a project using ARcore.

I need a real world screen that is visible on the ARcore camera, formerly using the method of erasing UI and capturing.

But it was so slow that I found Frame.CameraImage.Texture in Arcore API

It worked normally in a Unity Editor environment.

But if you build it on your phone and check it out, Texture is null.

Texture2D snap = (Texture2D)Frame.CameraImage.Texture;

What is the reason? maybe CPU problem?

and I tried to do a different function.

public class TestFrameCamera : MonoBehaviour
{
    private Texture2D _texture;
    private TextureFormat _format = TextureFormat.RGBA32;

    // Use this for initialization
    void Start()
    {
        _texture = new Texture2D(Screen.width, Screen.height, _format, false, false);
    }

    // Update is called once per frame
    void Update()
    {

        using (var image = Frame.CameraImage.AcquireCameraImageBytes())
        {
            if (!image.IsAvailable) return;

            int size = image.Width * image.Height;
            byte[] yBuff = new byte[size];
            System.Runtime.InteropServices.Marshal.Copy(image.Y, yBuff, 0, size);

            _texture.LoadRawTextureData(yBuff);
            _texture.Apply();

            this.GetComponent<RawImage>().texture = _texture;

        }
    }
}

enter image description here

But if I change the texture format, it will come out.

private TextureFormat _format = TextureFormat.R8;

it is work, but i don't want to red color image, i want to rgb color

what i should do?

enter image description here

1条回答
我命由我不由天
2楼-- · 2019-08-01 10:05

R8 Just red data. You can use TextureFormat.RGBA32, and set buffer like this:

IntPtr _buff = Marshal.AllocHGlobal(width * height*4);
查看更多
登录 后发表回答