java 如何从java中的像素字节数组制作bmp图像
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10061029/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to make bmp image from pixel byte array in java
提问by waiting_for_peace
I have a byte array containing pixel values from a .bmp file. It was generated by doing this:
我有一个包含来自 .bmp 文件的像素值的字节数组。它是通过这样做生成的:
BufferedImage readImage = ImageIO.read(new File(fileName));
byte imageData[] = ((DataBufferByte)readImage.getData().getDataBuffer()).getData();
Now I need to recreate the .bmp image. I tried to make a BufferedImage and set the pixels of the WritableRaster by calling the setPixels
method. But there I have to provide an int[], float[] or double[] array. Maybe I need to convert the byte array into one of these. But I don't know how to do that. I also tried the setDataElements
method. But I am not sure how to use this method either.
现在我需要重新创建 .bmp 图像。我尝试制作一个 BufferedImage 并通过调用该setPixels
方法设置 WritableRaster 的像素。但是我必须提供一个 int[]、float[] 或 double[] 数组。也许我需要将字节数组转换为其中之一。但我不知道该怎么做。我也试过这个setDataElements
方法。但我也不确定如何使用这种方法。
Can anyone explain how to create a bmp image from a byte array?
谁能解释如何从字节数组创建 bmp 图像?
Edit:@Perception
编辑:@Perception
This is what I have done so far:
这是我到目前为止所做的:
private byte[] getPixelArrayToBmpByteArray(byte[] pixelData, int width, int height, int depth) throws Exception{
int[] pixels = byteToInt(pixelData);
BufferedImage image = null;
if(depth == 8) {
image = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_GRAY);
}
else if(depth == 24){
image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
}
WritableRaster raster = (WritableRaster) image.getData();
raster.setPixels(0, 0, width, height, pixels);
image.setData(raster);
return getBufferedImageToBmpByteArray(image);
}
private byte[] getBufferedImageToBmpByteArray(BufferedImage image) {
byte[] imageData = null;
try {
ByteArrayOutputStream bas = new ByteArrayOutputStream();
ImageIO.write(image, "bmp", bas);
imageData = bas.toByteArray();
bas.close();
} catch (Exception e) {
e.printStackTrace();
}
return imageData;
}
private int[] byteToInt(byte[] data) {
int[] ints = new int[data.length];
for (int i = 0; i < data.length; i++) {
ints[i] = (int) data[i] & 0xff;
}
return ints;
}
The thing is this works just fine for gray images, but for colored images the color is not correct. I think this is something to do with the byteToInt method.. If someone knows how to improve it or have got a new and easier solution, ur help will be appreciated.. :)
问题是这适用于灰色图像,但对于彩色图像,颜色不正确。我认为这与 byteToInt 方法有关..如果有人知道如何改进它或有一个新的更简单的解决方案,我们将不胜感激..
回答by daveb
You need to pack three bytes into each integer you make. Depending on the format of the buffered image, this will be 0xRRGGBB.
您需要将三个字节打包到您创建的每个整数中。根据缓冲图像的格式,这将是 0xRRGGBB。
byteToInt will have to consume three bytes like this:
byteToInt 将不得不像这样消耗三个字节:
private int[] byteToInt(byte[] data) {
int[] ints = new int[data.length / 3];
int byteIdx = 0;
for (int pixel = 0; pixel < ints.length) {
int rByte = (int) pixels[byteIdx++] & 0xFF;
int gByte = (int) pixels[byteIdx++] & 0xFF;
int bByte = (int) pixels[byteIdx++] & 0xFF;
int rgb = (rByte << 16) | (gByte << 8) | bByte
ints[pixel] = rgb;
}
}
You can also use ByteBuffer.wrap(arr, offset, length).toInt()
你也可以使用ByteBuffer.wrap(arr, offset, length).toInt()
回答by Tommy Chan
Having just a byte array is not enough. You also need to construct a header (if you are reading from a raw format, such as inside a DICOM file).
只有一个字节数组是不够的。您还需要构建一个标头(如果您从原始格式中读取数据,例如在 DICOM 文件中)。