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

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

问题描述

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



任何人都可以推荐代码示例吗?



感谢,
Mike

解决方案

假设您知道身高和

  BufferedImage img = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB); (int c = 0; 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)| (绿色< 8)|蓝色;
img.setRGB(c,r,rgb);
}

粗略地说。这假设像素数据被编码为一组行;并且颜色的长度是3 * width * height(这应该是有效的)。

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?

Thanks, Mike

解决方案

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

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中将字节数组转换为Image的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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