如何在 Java SE 中将字节数组转换为图像 [英] How to convert array of bytes into Image in Java SE

查看:42
本文介绍了如何在 Java SE 中将字节数组转换为图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Java SE 中将原始字节数组转换为 Image 的正确方法是什么.数组由字节组成,每三个字节代表一个像素,每个字节代表对应的RGB分量.

What is the right way to convert raw array of bytes into Image in Java SE. array consist of bytes, where each three bytes represent one pixel, with each byte for corresponding RGB component.

有人可以推荐一个代码示例吗?

Can anybody suggest a code sample?

谢谢,迈克

推荐答案

假设您知道图像的高度和宽度.

Assuming you know the height and width of the image.

BufferedImage img=new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
for(int r=0; r<height; r++)
for(int c=0; c<width; c++)
{
  int index=r*width+c;
  int red=colors[index] & 0xFF;
  int green=colors[index+1] & 0xFF;
  int blue=colors[index+2] & 0xFF;
  int rgb = (red << 16) | (green << 8) | blue;
  img.setRGB(c, r, rgb);
}

大致上.这假设像素数据被编码为一组行;并且颜色的长度是 3 * 宽 * 高(应该是有效的).

Roughly. This assumes the pixel data is encoded as a set of rows; and that the length of colors is 3 * width * height (which should be valid).

这篇关于如何在 Java SE 中将字节数组转换为图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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