try {
bufferedimage bi = null;
if (isurl == true) {
bi = imageio.read(new url(infilename));
}
else {
bi = imageio.read(new file(infilename));
}
int[] rgbarray = new int[101 * 29];//我测试用的图象文件象素值宽度是101宽,29长的
bi.getrgb(0, 0, 101, 29, rgbarray, 0, 101);
for (int i = 0; i < 101 * 29; i++) {//我做的只是双色图,所以这里查出每一个象素是1(黑)还是0(白)
if ((rgbarray[i] & 0xffffff) == 0x000000)
rgbarray[i] = 0;
else
rgbarray[i] = 1;
}
//以下是bmp文件格式的定义,更加具体的说明可以到http://www.wotsit.org找
int[] identifier = { 'b', 'm' };
int[] filesize = { 0x0e, 0x02, 0x00, 0x00 };
int[] reserved = { 0x00, 0x00, 0x00, 0x00 };
int[] bitmapdataoffset = { 0x3e, 0x00, 0x00, 0x00 };
int[] bitmapheadersize = { 0x28, 0x00, 0x00, 0x00 };
int[] width = { 0x65, 0x00, 0x00, 0x00 };
int[] height = { 0x1d, 0x00, 0x00, 0x00 };
int[] planes = { 0x01, 0x00 };
int[] bitsperpixel = { 0x01, 0x00 };
int[] compression = { 0x00, 0x00, 0x00, 0x00 };
int[] bitmapdatasize = { 0xd0, 0x01, 0x00, 0x00 };
int[] hresolution = { 0x00, 0x00, 0x00, 0x00 };
int[] vresolution = { 0x00, 0x00, 0x00, 0x00 };
int[] colors = { 0x00, 0x00, 0x00, 0x00 };
int[] importantcolors = { 0x00, 0x00, 0x00, 0x00 };
int[] palette = { 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0x00 };
//记住bmp没一个扫描行都要扩充到下一个4bytes的倍数,如101就要变成128,扩充的byte补0即可
int[] bitmapdata = new int[(101 + 27) * 29 / 8];
int n = 0;
for (int i = 0; i < 29; i++) { //bmp格式是从最后一行开始scan的
for (int j = 1; j <= 101; j++) {
if (rgbarray[(28 - i) * 101 + j - 1] == 1) {
bitmapdata[n] = bitmapdata[n] | (1 << (8 - j % 8) % 8);
}
if (j % 8 == 0) {
n++;
}
if (j == 101) {
n += 4;
}
}
}
randomaccessfile o = new randomaccessfile(outfilename, "rw");
o.setlength(0);
for (int i = 0; i < identifier.length; i++) {
o.write(identifier[i]);
}
for (int i = 0; i < filesize.length; i++) {
o.write(filesize[i]);
}
for (int i = 0; i < reserved.length; i++) {
o.write(reserved[i]);
}
for (int i = 0; i < bitmapdataoffset.length; i++) {
o.write(bitmapdataoffset[i]);
}
for (int i = 0; i < bitmapheadersize.length; i++) {
o.write(bitmapheadersize[i]);
}
for (int i = 0; i < width.length; i++) {
o.write(width[i]);
}
for (int i = 0; i < height.length; i++) {
o.write(height[i]);
}
for (int i = 0; i < planes.length; i++) {
o.write(planes[i]);
}
for (int i = 0; i < bitsperpixel.length; i++) {
o.write(bitsperpixel[i]);
}
for (int i = 0; i < compression.length; i++) {
o.write(compression[i]);
}
for (int i = 0; i < bitmapdatasize.length; i++) {
o.write(bitmapdatasize[i]);
}
for (int i = 0; i < hresolution.length; i++) {
o.write(hresolution[i]);
}
for (int i = 0; i < vresolution.length; i++) {
o.write(vresolution[i]);
}
for (int i = 0; i < colors.length; i++) {
o.write(colors[i]);
}
for (int i = 0; i < importantcolors.length; i++) {
o.write(importantcolors[i]);
}
for (int i = 0; i < palette.length; i++) {
o.write(palette[i]);
}
for (int i = 0; i < bitmapdata.length; i++) {
o.write(bitmapdata[i]);
}
o.close();
}
catch (malformedurlexception e) {
e.printstacktrace();
}
catch (ioexception e) {
e.printstacktrace();
}
如果有什么不明白的,请联系我[email protected],我将尽我所能的帮助你。