I wrote a imagebuffer to BMP flow code snippet, I hope to be useful to everyone.

Source: Internet
Author: User
Tags format format definition reserved
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];//The pixel value of the image file I tested is 101 wide and 29 long

Bi.getrgb (0, 0, Rgbarray, 0, 101);
for (int i = 0; i < i++) {//All I do is double color, so here we find out if each pixel is 1 (black) or 0 (white)
if ((Rgbarray[i] & 0xffffff) = = 0x000000)
Rgbarray[i] = 0;
Else
Rgbarray[i] = 1;
}
The following is a BMP file format definition, more specific instructions can be found in the 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};
Remember that BMP does not have a scan line to expand to the next 4bytes multiples, such as 101 will become 128, expanded byte 0 can
int[] BitmapData = new int[(101 + 27) * 29/8];
int n = 0;
for (int i = 0; i < i++) {The//bmp format starts at the last line scan
for (int j = 1; J <= + j) {
if (rgbarray[(28-i) * + 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 ();
}

If there is anything not clear, please contact me flcc@263.sina.com, I will do my best to help you.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.