如何从字节数组创建图像并显示它?

问题描述 投票:0回答:2

我想从图像文件中获取数据并在 Unity 可以读取的纹理中显示该信息。

我能够将像素信息放入字节数组中,但屏幕上没有显示任何内容。我如何实际显示图像?

     pcxFile = File.ReadAllBytes("Assets/5_ImageParser/bagit_icon.pcx");
     int startPoint = 128;
     int height = 152; 
     int width = 152; 
     target = new Texture2D(height, width);
     for (var y = 0; y < height; y++)
     {
         for (var x = 0; x < width; x++)
         {
             timesDone ++;
             pixels[x, y] = new Color(pcxFile[startPoint], pcxFile[startPoint+1], pcxFile[startPoint+2]);
             startPoint += 4;
             target.SetPixel(x, y, pixels[x, y]);
         }            
     }
     target.Apply();
     target.EncodeToJPG();
c# unity-game-engine textures texture2d
2个回答
4
投票

好吧,(假设你正确获取了像素数据)你必须将创建的纹理分配给某些东西......

我会使用例如RawImage,因为它不需要

Sprite
(就像
UI.Image
组件一样 - 另请参阅 RawImage 手册):

// Reference this in the Inspector
public RawImage image;

//...

pcxFile = File.ReadAllBytes("Assets/5_ImageParser/bagit_icon.pcx");
int startPoint = 128;
int height = 152; 
int width = 152; 
target = new Texture2D(height, width);
for (var y = 0; y < height; y++)
{
    for (var x = 0; x < width; x++)
    {
        timesDone ++;
        pixels[x, y] = new Color(pcxFile[startPoint], pcxFile[startPoint+1], pcxFile[startPoint+2]);
        startPoint += 4;
        target.SetPixel(x, y, pixels[x, y]);
    }            
}
target.Apply();
// You don't need this. Only if you are also going to save it locally
// as an actual *.jpg file or if you are going to
// e.g. upload it later via http POST
//
// In this case however you would have to asign the result 
// to a variable in order to use it later
//var rawJpgBytes = target.EncodeToJPG();

// Assign the texture to the RawImage component
image.texture = target;

或者与普通

Image
组件一起使用,使用
Sprite
 从纹理创建 
Sprite.Create

// Reference this in the Inspector
public Image image;

// ...

var sprite = Sprite.Create(target, new Rect(0.0f, 0.0f, target.width, target.height), new Vector2(0.5f, 0.5f), 100.0f);

image.sprite = sprite;

提示1
为了获得正确的纵横比,我通常使用一些小技巧:

  1. 为此 RawImage 创建一个父对象
  2. 在父级的
    RectTransform
    中设置所需的“最大尺寸”
  3. RawImage
    /
    Image
    旁边(在子对象上)添加
    AspectRatioFitter
    (另请参阅 AspectRatioFitter 手册)并将
    AspectMode
    设置为
    FitInParent
  4. 现在在代码中调整纵横比(从纹理中获取):

    public RawImage image;
    public AspectRatioFitter fitter;
    
    //...
    
    image.texture = target;
    
    var ratio = target.width / target.height;
    fitter.aspectRatio = ratio;
    

提示2
对所有像素调用

SetPixels
一次比重复调用
SetPixel
更“便宜”:

// ...

startPoint = 0;
pixels = new Color[width * height]();
for(int i = 0; i < pixels.Length; i++)
{
    pixels[i] = new Color(pcxFile[startPoint], pcxFile[startPoint+1], pcxFile[startPoint+2]);
    startPoint += 4;
}

target.SetPixels(pixels);
target.Apply();

// ...

(我不知道你的

pcx
格式到底是如何工作的,但也许你甚至可以使用
LoadRawTextureData


0
投票

我对提到的 pcx 格式不太了解,但我认为现在有一种不太复杂的方法可以做到这一点,基于 gamedev StackExchange 上的question

target = new Texture2D(width, height, TextureFormat.RGB24, false);
target.LoadImage(byteArray);
© www.soinside.com 2019 - 2024. All rights reserved.