JAVA:如何从byte []创建.PNG图像? [英] JAVA : How to create .PNG image from a byte[]?

查看:259
本文介绍了JAVA:如何从byte []创建.PNG图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我见过一些代码源,但我不明白...



我使用Java 7



<请> 如何将RGB (红色,绿色,蓝色)字节数组(或类似的)转换为.PNG文件格式



可以表示RGB像素的数组示例:

  byte [] aByteArray = {0xa,0x2,0xf}; 

重要方面:



我尝试仅从上一个现有文件中的字节[]生成.PNG文件



是不是可以使用现有的API吗? ;)



这是我的第一个代码:

  byte [] aByteArray = {0xa,0x2,0xf}; 
ByteArrayInputStream bais = new ByteArrayInputStream(aByteArray);
文件outputfile = new File(image.png);
ImageIO.write(bais,png,outputfile);

.... 错误:找不到合适的方法



这里的其他版本是从 Jeremy 修改的,但外观相似:

  byte [] aByteArray = {0xa,0x2,0xf}; 
ByteArrayInputStream bais = new ByteArrayInputStream(aByteArray);
final BufferedImage bufferedImage = ImageIO.read(newByteArrayInputStream(aByteArray));
ImageIO.write(bufferedImage,png,new File(image.png));

....多个错误:图片== null! ......当然可以?注意:我不搜索使用源文件

解决方案

Image I / O API处理图像,所以你需要在你写出之前先从你的字节数组中创建一个图像。

  byte [] aByteArray = {0xa,0x2,0xf, (字节)0xff的,(字节)0xff的,(字节)0xff的}; 
int width = 1;
int height = 2;

DataBuffer buffer = new DataBufferByte(aByteArray,aByteArray.length);

//每像素3个字节:红色,绿色,蓝色
WritableRaster raster = Raster.createInterleavedRaster(缓冲区,宽度,高度,3 *宽度,3,新int [] {0, 1,2},(Point)null);
ColorModel cm = new ComponentColorModel(ColorModel.getRGBdefault()。getColorSpace(),false,true,Transparency.OPAQUE,DataBuffer.TYPE_BYTE);
BufferedImage image = new BufferedImage(cm,raster,true,null);

ImageIO.write(image,png,new File(image.png));

这假设字节数组每个像素有三个字节(红色,绿色然后是蓝色)和范围值为0-255。


I have seen some code source, but I do not understand...

I use Java 7

Please, how to convert a RGB (Red,Green,Blue) Byte Array (or something similar) to a .PNG file format ?

Example from an array that could represent "a RGB pixel" :

byte[] aByteArray={0xa,0x2,0xf};

Important Aspect :

I try to generate a .PNG file only from a byte[] "not from a previous existing file"

is it possible with an existing API? ;)

Here my first code :

byte[] aByteArray={0xa,0x2,0xf}; 
ByteArrayInputStream bais = new ByteArrayInputStream(aByteArray); 
File outputfile = new File("image.png"); 
ImageIO.write(bais, "png", outputfile); 

....Error : No suitable Method Found

Here the other version modified from Jeremy but look similar :

byte[] aByteArray={0xa,0x2,0xf};
ByteArrayInputStream bais = new ByteArrayInputStream(aByteArray); 
final BufferedImage bufferedImage = ImageIO.read(newByteArrayInputStream(aByteArray));
ImageIO.write(bufferedImage, "png", new File("image.png")); 

....multiple Errors : image == null! ...... Sure ? Note : I do not search to use a source file

解决方案

The Image I/O API deals with images, so you need to make an image from your byte array first before you write it out.

byte[] aByteArray = {0xa,0x2,0xf,(byte)0xff,(byte)0xff,(byte)0xff};
int width = 1;
int height = 2;

DataBuffer buffer = new DataBufferByte(aByteArray, aByteArray.length);

//3 bytes per pixel: red, green, blue
WritableRaster raster = Raster.createInterleavedRaster(buffer, width, height, 3 * width, 3, new int[] {0, 1, 2}, (Point)null);
ColorModel cm = new ComponentColorModel(ColorModel.getRGBdefault().getColorSpace(), false, true, Transparency.OPAQUE, DataBuffer.TYPE_BYTE); 
BufferedImage image = new BufferedImage(cm, raster, true, null);

ImageIO.write(image, "png", new File("image.png"));

This assumes the byte array has three bytes per pixel (red, green then blue) and the range of values is 0-255.

这篇关于JAVA:如何从byte []创建.PNG图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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