电脑故障

位置:IT落伍者 >> 电脑故障 >> 浏览文章

如何将BufferedImage实例保存为BMP文件


发布日期:2020/11/23
 

要将BufferedImage实例保存为BMP文件就需要知道BMP文件的格式可以参考我转载的文章《BMP文件格式》

下面是我的将BufferedImage实例保存为位色BMP文件的实现

首先是BMP文件相关的两个头结构BMPFileHeader和BMPInfoHeader

/**//*

* Created on

*

* TODO To change the template for this generated file go to

* Window Preferences Java Code Style Code Templates

*/

package decbmp;

/**//**

* <p> Title: BMP文件的头结构</p>

*

* <p> Description: BMP文件的头结构固定是个字节其定义如下</p>

* <p>

* byte[] bfType;指定文件类型必须是xD即字符串BM也就是说所有bmp文件的头两个字节都是BM

* byte[] bfSize;指定文件大小包括这个字节

* byte[] bfReserved;保留字

* byte[] bfReserved;保留字

* byte[] bfOffBits;为从文件头到实际的位图数据的偏移字节数

* </p>

*

* <p> Copyright: Copyright (c) </p>

*

* <p> Company: Lotus</p>

*

* @author George Hill

* @version

*/

class BMPFileHeader {

// Header data

private byte[] data = new byte[];

public byte[] getData() {

return thisdata;

}

// BMP file size

private int size;

public int getSize() {

return thissize;

}

private int offset;

public int getOffset() {

return thisoffset;

}

BMPFileHeader(int size int offset) {

thissize = size;

thisoffset = offset;

data[] = B;

data[] = M;

int value = size;

data[] = (byte) value;

value = value >>> ;

data[] = (byte) value;

value = value >>> ;

data[] = (byte) value;

value = value >>> ;

data[] = (byte) value;

value = offset;

data[] = (byte) value;

value = value >>> ;

data[] = (byte) value;

value = value >>> ;

data[] = (byte) value;

value = value >>> ;

data[] = (byte) value;

}

}

/**//*

* Created on

*

* TODO To change the template for this generated file go to

* Window Preferences Java Code Style Code Templates

*/

package decbmp;

/**//**

* <p>Title: BMP文件内容的头结构</p>

*

* <p>Description: BMP文件内容的头结构固定是个字节其定义如下</p>

* <p>

* byte[] biSize;指定这个结构的长度

* byte[] biWidth;指定图象的宽度单位是象素

* byte[] biHeight;指定图象的高度单位是象素

* byte[] biPlanes;必须是不用考虑

* byte[] biBitCount;指定表示颜色时要用到的位数常用的值为(黑白二色图) (色图) (色) (真彩色图)

* byte[] biCompression;指定位图是否压缩

* byte[] biSizeImage;指定实际的位图数据占用的字节数

* byte[] biXPelsPerMeter;指定目标设备的水平分辨率单位是每米的象素个数

* byte[] biYPelsPerMeter;指定目标设备的垂直分辨率单位是每米的象素个数

* byte[] biClrUsed;指定本图象实际用到的颜色数如果该值为零则用到的颜色数为biBitCount

* byte[] biClrImportant;指定本图象中重要的颜色数如果该值为零则认为所有的颜色都是重要的

* </p>

*

* <p>Copyright: Copyright (c) </p>

*

* <p>Company: Lotus</p>

*

* @author George Hill

* @version

*/

class BMPInfoHeader {

private byte[] data = new byte[];

public byte[] getData() {

return thisdata;

}

private int width;

public int getWidth() {

return thiswidth;

}

private int height;

public int getHeight() {

return thisheight;

}

public int bitCount;

public int getBitCount() {

return thisbitCount;

}

public BMPInfoHeader(int width int height int bitCount) {

thiswidth = width;

thisheight = height;

thisbitCount = bitCount;

data[] = ;

int value = width;

data[] = (byte) value;

value = value >>> ;

data[] = (byte) value;

value = value >>> ;

data[] = (byte) value;

value = value >>> ;

data[] = (byte) value;

value = height;

data[] = (byte) value;

value = value >>> ;

data[] = (byte) value;

value = value >>> ;

data[] = (byte) value;

value = value >>> ;

data[] = (byte) value;

data[] = ;

data[] = (byte) bitCount;

value = width * height * ;

if (width % != )

value += (width % ) * height;

data[] = (byte) value;

value = value >>> ;

data[] = (byte) value;

value = value >>> ;

data[] = (byte) value;

value = value >>> ;

data[] = (byte) value;

}

}

仿照decjpegJPEGImageEncoder写的接口类BMPEncoder

/**//*

* Created on

*

* TODO To change the template for this generated file go to

* Window Preferences Java Code Style Code Templates

*/

package decbmp;

import javaawtimage*;

import javaioIOException;

/**//**

* <p>Title: </p>

*

* <p>Description: </p>

*

* <p>Copyright: Copyright (c) </p>

*

* <p>Company: Lotus</p>

*

* @author George Hill

* @version

*/

public interface BMPEncoder {

public void encode(BufferedImage bi) throws IOException;

public static final int BIT_COUNT_BLACKWHITE = ;

public static final int BIT_COUNT_COLORS = ;

public static final int BIT_COUNT_COLORS = ;

public static final int BIT_COUNT_TRUECOLORS = ;

}

BMPEncoder接口的实现BMPEncoderImpl

/**//*

* Created on

*

* TODO To change the template for this generated file go to

* Window Preferences Java Code Style Code Templates

*/

package decbmp;

import javaawtimage*;

import javaio*;

/**//**

* <p>Title: </p>

*

* <p>Description: </p>

*

* <p>Copyright: Copyright (c) </p>

*

* <p>Company: Lotus</p>

*

* @author George Hill

* @version

*/

class BMPEncoderImpl implements BMPEncoder {

private OutputStream out;

public BMPEncoderImpl(OutputStream out) {

thisout = out;

}

public void encode(BufferedImage bi) throws IOException {

int width = bigetWidth();

int height = bigetHeight();

boolean needBlank = (width % != );

int size = width * height * ;

if (needBlank) {

size += (width % ) * height;

}

BMPFileHeader fileHeader = new BMPFileHeader(size );

BMPInfoHeader infoHeader = new BMPInfoHeader(width height BIT_COUNT_TRUECOLORS);

byte[] rgbs = new byte[];

byte[] blank = new byte[width %

上一篇:一个简单的自定义ClassLoader的实现

下一篇:去掉字符串前后的空格