将图像从Stream/StreamReader加载到Image或RawImage组件 [英] Load Image from Stream/StreamReader to Image OR RawImage component

查看:103
本文介绍了将图像从Stream/StreamReader加载到Image或RawImage组件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用AWS Unity(v3.3.50.0):S3 SDK(AWSSDK.S3.3.3.5.4.unitypackage)从

I'm using AWS Unity (v3.3.50.0): S3 SDK (AWSSDK.S3.3.3.5.4.unitypackage) downloaded from https://aws.amazon.com/mobile/sdk/. My Unity version is 5.5.1.

我要下载放置在S3存储桶上的映像,存储桶已配置并且可以下载.而且我将字符串视为响应数据.

I want to download an image placed on S3 bucket, bucket is configured and can be downloaded. And I see the string as data in response.

但是我无法在S3示例GetObject()函数中将返回的StreamReader转换为UnityEngine.UI.Image.sprite或UnityEngine.UI.RawImage.texture.

But I cannot able to convert the returned StreamReader to UnityEngine.UI.Image.sprite OR UnityEngine.UI.RawImage.texture in S3 sample GetObject() function.

private void GetObject()
{
    ResultText.text = string.Format("fetching {0} from bucket {1}", SampleFileName, S3BucketName);
    Client.GetObjectAsync(S3BucketName, SampleFileName, (responseObj) =>
        {
            string data = null;
            var response = responseObj.Response;
            if (response.ResponseStream != null)
            {
                using (StreamReader reader = new StreamReader(response.ResponseStream))
                {
                    data = reader.ReadToEnd();
                }

                ResultText.text += "\n";
                ResultText.text += data;
            }
            Debug.Log("GetObject: " + data);
        });
}

对此需要帮助:)

S3存储桶上的图像为PNG格式.但是在将来的JPG中,必须启用JPEG格式支持.

Images on S3 bucket are in PNG format. But in future JPG, JPEG format support have to enable.

推荐答案

StreamReader用于文本而不是二进制数据(例如要下载的图像).我可以说出为什么要使用它,也不能说出为什么要对图像执行Debug.Log("GetObject: " + data);.

StreamReader is used for text not binary data like the image you want to download. I can tell why you are using it and can't also tell why you perform Debug.Log("GetObject: " + data); on an image.

下载图像,然后使用Texture2D.LoadImage将其转换为Texture2D,然后可以将其加载到RawImage中进行显示.

Download the image,then use Texture2D.LoadImage to convert it to Texture2D, you can then load that to your RawImage to display.

public RawImage displayTexture;

private void GetObject()
{
    ResultText.text = string.Format("fetching {0} from bucket {1}", SampleFileName, S3BucketName);
    Client.GetObjectAsync(S3BucketName, SampleFileName, (responseObj) =>
    {
        byte[] data = null;
        var response = responseObj.Response;
        if (response.ResponseStream != null)
        {
            using (StreamReader reader = new StreamReader(response.ResponseStream))
            {
                using (var memstream = new MemoryStream())
                {
                    var buffer = new byte[512];
                    var bytesRead = default(int);
                    while ((bytesRead = reader.BaseStream.Read(buffer, 0, buffer.Length)) > 0)
                        memstream.Write(buffer, 0, bytesRead);
                    data = memstream.ToArray();
                }
            }

            //Display Image
            displayTexture.texture = bytesToTexture2D(data);
        }
    });
}

public Texture2D bytesToTexture2D(byte[] imageBytes)
{
    Texture2D tex = new Texture2D(2, 2);
    tex.LoadImage(imageBytes);
    return tex;
}

就像我上面提到的,对于二进制数据,使用StreamReader不好.您可以使用MemoryStream来做到这一点.在这种情况下,新的GetObject函数应如下所示:

Like I mentioned above, using StreamReader is not good for binary data. You can just use MemoryStream to do that. In that case, your new GetObject function should look like this:

private void GetObject()
{
    ResultText.text = string.Format("fetching {0} from bucket {1}", SampleFileName, S3BucketName);
    Client.GetObjectAsync(S3BucketName, SampleFileName, (responseObj) =>
    {
        byte[] data = null;
        var response = responseObj.Response;
        Stream input = response.ResponseStream;

        if (response.ResponseStream != null)
        {
            byte[] buffer = new byte[16 * 1024];
            using (MemoryStream ms = new MemoryStream())
            {
                int read;
                while ((read = input.Read(buffer, 0, buffer.Length)) > 0)
                {
                    ms.Write(buffer, 0, read);
                }
                data = ms.ToArray();
            }

            //Display Image
            displayTexture.texture = bytesToTexture2D(data);
        }
    });
}

这篇关于将图像从Stream/StreamReader加载到Image或RawImage组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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