要将bufferedimage实例保存为bmp文件,就需要知道bmp文件的格式,可以参考我转载的文章:《bmp文件格式》。
下面是我的将bufferedimage实例保存为24位色bmp文件的实现。
首先是bmp文件相关的两个头结构:bmpfileheader和bmpinfoheader。
/**//*
* created on 2005-6-21
*
* todo to change the template for this generated file go to
* window - preferences - java - code style - code templates
*/
package org.lotus.image.codec.bmp;
/**//**
* <p> title: bmp文件的头结构</p>
*
* <p> description: bmp文件的头结构固定是14个字节,其定义如下:</p>
* <p>
* byte[2] bftype; 指定文件类型,必须是0x424d,即字符串“bm”,也就是说所有.bmp文件的头两个字节都是“bm“
* byte[4] bfsize; 指定文件大小,包括这14个字节
* byte[2] bfreserved1; 保留字
* byte[2] bfreserved2; 保留字
* byte[4] bfoffbits; 为从文件头到实际的位图数据的偏移字节数
* </p>
*
* <p> copyright: copyright (c) 2005</p>
*
* <p> company: 21lotus</p>
*
* @author george hill
* @version 1.0
*/
class bmpfileheader {
// header data
private byte[] data = new byte[14];
public byte[] getdata() {
return this.data;
}
// bmp file size
private int size;
public int getsize() {
return this.size;
}
private int offset;
public int getoffset() {
return this.offset;
}
bmpfileheader(int size, int offset) {
this.size = size;
this.offset = offset;
data[0] = 'b';
data[1] = 'm';
int value = size;
data[2] = (byte) value;
value = value >>> 8;
data[3] = (byte) value;
value = value >>> 8;
data[4] = (byte) value;
value = value >>> 8;
data[5] = (byte) value;
value = offset;
data[10] = (byte) value;
value = value >>> 8;
data[11] = (byte) value;
value = value >>> 8;
data[12] = (byte) value;
value = value >>> 8;
data[13] = (byte) value;
}
}
/**//*
* created on 2005-6-21
*
* todo to change the template for this generated file go to
* window - preferences - java - code style - code templates
*/
package org.lotus.image.codec.bmp;
/**//**
* <p>title: bmp文件内容的头结构</p>
*
* <p>description: bmp文件内容的头结构固定是40个字节,其定义如下:</p>
* <p>
* byte[4] bisize; 指定这个结构的长度,为40
* byte[4] biwidth; 指定图象的宽度,单位是象素
* byte[4] biheight; 指定图象的高度,单位是象素
* byte[2] biplanes; 必须是1,不用考虑
* byte[2] bibitcount; 指定表示颜色时要用到的位数,常用的值为1(黑白二色图), 4(16色图), 8(256色), 24(真彩色图)
* byte[4] bicompression; 指定位图是否压缩
* byte[4] bisizeimage; 指定实际的位图数据占用的字节数
* byte[4] bixpelspermeter; 指定目标设备的水平分辨率,单位是每米的象素个数
* byte[4] biypelspermeter; 指定目标设备的垂直分辨率,单位是每米的象素个数
* byte[4] biclrused; 指定本图象实际用到的颜色数,如果该值为零,则用到的颜色数为2bibitcount
* byte[4] biclrimportant; 指定本图象中重要的颜色数,如果该值为零,则认为所有的颜色都是重要的
* </p>
*
* <p>copyright: copyright (c) 2005</p>
*
* <p>company: 21lotus</p>
*
* @author george hill
* @version 1.0
*/
class bmpinfoheader {
private byte[] data = new byte[40];
public byte[] getdata() {
return this.data;
}
private int width;
public int getwidth() {
return this.width;
}
private int height;
public int getheight() {
return this.height;
}
public int bitcount;
public int getbitcount() {
return this.bitcount;
}
public bmpinfoheader(int width, int height, int bitcount) {
this.width = width;
this.height = height;
this.bitcount = bitcount;
data[0] = 40;
int value = width;
data[4] = (byte) value;
value = value >>> 8;
data[5] = (byte) value;
value = value >>> 8;
data[6] = (byte) value;
value = value >>> 8;
data[7] = (byte) value;
value = height;
data[8] = (byte) value;
value = value >>> 8;
data[9] = (byte) value;
value = value >>> 8;
data[10] = (byte) value;
value = value >>> 8;
data[11] = (byte) value;
data[12] = 1;
data[14] = (byte) bitcount;
value = width * height * 3;
if (width % 4 != 0)
value += (width % 4) * height;
data[20] = (byte) value;
value = value >>> 8;
data[21] = (byte) value;
value = value >>> 8;
data[22] = (byte) value;
value = value >>> 8;
data[23] = (byte) value;
}
}
仿照com.sun.image.codec.jpeg.jpegimageencoder写的接口类bmpencoder。
/**//*
* created on 2005-6-21
*
* todo to change the template for this generated file go to
* window - preferences - java - code style - code templates
*/
package org.lotus.image.codec.bmp;
import java.awt.image.*;
import java.io.ioexception;
/**//**
* <p>title: </p>
*
* <p>description: </p>
*
* <p>copyright: copyright (c) 2005</p>
*
* <p>company: 21lotus</p>
*
* @author george hill
* @version 1.0
*/
public interface bmpencoder {
public void encode(bufferedimage bi) throws ioexception;
public static final int bit_count_blackwhite = 1;
public static final int bit_count_16colors = 4;
public static final int bit_count_256colors = 8;
public static final int bit_count_truecolors = 24;
}
bmpencoder接口的实现bmpencoderimpl。
/**//*
* created on 2005-6-21
*
* todo to change the template for this generated file go to
* window - preferences - java - code style - code templates
*/
package org.lotus.image.codec.bmp;
import java.awt.image.*;
import java.io.*;
/**//**
* <p>title: </p>
*
* <p>description: </p>
*
* <p>copyright: copyright (c) 2005</p>
*
* <p>company: 21lotus</p>
*
* @author george hill
* @version 1.0
*/
class bmpencoderimpl implements bmpencoder {
private outputstream out;
public bmpencoderimpl(outputstream out) {
this.out = out;
}
public void encode(bufferedimage bi) throws ioexception {
int width = bi.getwidth();
int height = bi.getheight();
boolean needblank = (width % 4 != 0);
int size = width * height * 3;
if (needblank) {
size += (width % 4) * height;
}
bmpfileheader fileheader = new bmpfileheader(size, 54);
bmpinfoheader infoheader = new bmpinfoheader(width, height, bit_count_truecolors);
byte[] rgbs = new byte[3];
byte[] blank = new byte[width %
闽公网安备 35060202000074号