简体   繁体   English

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

[英]How to create an image from byte array and display it?

I want to get data from an image file and display that information in a texture that Unity can read. 我想从图像文件中获取数据并以Unity可以读取的纹理显示该信息。

I am able to get the pixel information into a byte array, but nothing ever displays on the screen. 我能够将像素信息放入字节数组,但是屏幕上什么也没有显示。 How do I actually get the image to display? 如何实际显示图像?

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

Well, (assuming you get the pixel data correctly) you have to asign that created texture to something... 好吧,(假设您正确地获得了像素数据)您必须将创建纹理的东西分配给某些东西...

I'ld use eg a RawImage since it doesn't need a Sprite (as the UI.Image component would do - also see the RawImage Manual ): 我将使用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;

Alternatively for using with normal Image component create a Sprite from your texture using Sprite.Create : 另外,与正常的Image组件一起使用时,可以使用Sprite.Create从您的纹理中创建一个Sprite

// 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;

Hint 1 提示1
In order to have the correct aspect ratio I used a little trick usually: 为了获得正确的长宽比,我通常使用一些技巧:

  1. Create a parent object for this RawImage 为此RawImage创建一个父对象
  2. Set the desired "maximal size" in the RectTransform of the parent 在父级的RectTransform中设置所需的“最大尺寸”
  3. Next to the RawImage / Image (on the child object) add an AspectRatioFitter (also see the AspectRatioFitter Manual ) and set AspectMode to FitInParent . RawImage / Image旁边(在子对象上)添加AspectRatioFitter (另请参见AspectRatioFitter手册 ),并将AspectMode设置为FitInParent
  4. Now in the code adjust the aspect ratio (you get it from the texture): 现在,在代码中调整纵横比(您可以从纹理中获取):

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

Hint 2 提示2
It is "cheaper" to call SetPixels once for all pixels than calling SetPixel repeatedly: 与重复调用SetPixel相比,为所有像素调用一次SetPixels是“便宜”的事情:

// ...

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

// ...

(I don't know how exactly your pcx format works but maybe you could even use LoadRawTextureData (我不知道您的pcx格式是如何工作的,但也许您甚至可以使用LoadRawTextureData

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM