Package com.cn.trs.util.img; Import Java.awt.Image; Import Java.awt.Toolkit; Import Java.awt.Graphics2D; Import Java.awt.image.BufferedImage; Import Java.awt.image.MemoryImageSource; Import Java.io.File; Import Java.io.FileInputStream; Import Java.io.FileOutputStream; Import java.io.IOException; Import java.util.Hashtable; Import Javax.imageio.ImageIO; public class Bmpparse {private File srcimage; private graphics2d graph; private Hashtable infomap = new Hashtable (); publ IC Bmpparse (String imagefile) {srcimage = new File (imagefile);} public Bmpparse (File imagefile) {srcimage = ImageFile; The public Hashtable Getinfomap () {return infomap;}/** LoadBitmap () method converted the from Windows C code. Reads only uncompressed 24-and 8-bit images. Tested with images saved using Microsoft Paint in Windows 95. If the image isn't a 24-or 8-bit image, the program refuses to even try. I guess one could include 4-bit images by masking of the byte by A, and 1100 0011. I am not really interestEd in such images. If a compressed image is attempted, the routine would probably fail by generating a ioexception. Look for variable ncompression to being different from 0 to indicate compression is present. Arguments:sdir and Sfile are the result of the FileDialog () getdirectory () and GetFile () methods. Returns:image Object, is sure to check for (IMAGE) NULL!!!! * * Public Image LoadBitmap (graphics2d graph) {image image;//system.out.println ("Loading:" + srcimage.getname ()); try {FileInputStream fs = new FileInputStream (srcimage); int bflen = n//byte bitmapfileheader byte bf[] = new byte [Bflen]; Fs.read (BF, 0, Bflen); int Bilen = 40; 40-byte bitmapinfoheader byte bi[] = new Byte[bilen]; Fs.read (BI, 0, Bilen); Interperet data. int nsize = (((int) bf[5] & 0xff) << 24) | (((int) bf[4] & 0xff) << 16) | (((int) bf[3] & 0xff) << 8) | (int) bf[2] & 0xFF; Infomap.put ("Size", new Integer (nsize)); System.out.println ("File type is:" + (ChaR) bf[0] + (char) bf[1]); System.out.println ("Size of File is:" + nsize); Head info Data int nbisize = (((int) bi[3] & 0xff) << 24 | (((int) bi[2] & 0xff) << 16) | (((int) bi[1] & 0xff) << 8) | (int) bi[0] & 0xFF; Infomap.put ("Bitmapinfoheader", New Integer (nbisize)); System.out.println ("Size of Bitmapinfoheader is:" + nbisize); int nwidth = (((int) bi[7] & 0xff) << 24) | (((int) bi[6] & 0xff) << 16) | (((int) bi[5] & 0xff) << 8) | (int) bi[4] & 0xFF; Infomap.put ("width", new Integer (nwidth)); System.out.println ("Width is:" + nwidth); int nheight = (((int) bi[11] & 0xff) << 24) | (((int) bi[10] & 0xff) << 16) | (((int) bi[9] & 0xff) << 8) | (int) bi[8] & 0xFF; Infomap.put ("height", new Integer (nheight)); System.out.println ("Height is:" + nheight); Number of planes in this bitmap int nplanes = ((int) bi[13] & 0xff) << 8 | (int) bi[12] &Amp 0xFF; Infomap.put ("Planes", New Integer (Nplanes)); System.out.println ("Planes is:" + nplanes); Bits per pixel used to store palette entry//information. This is also identifies in a indirect way//the number of possible colors. Possible values Are:1, 4, 8,, int nbitcount = (((int) bi[15] & 0xff) << 8) | (int) bi[14] & 0xFF; Infomap.put ("Bitcount", New Integer (Nbitcount)); System.out.println ("Bitcount is:" + nbitcount); Look for Non-zero values to indicate compression int ncompression = (((int) bi[19]) << 24) | ((((int) bi[18]) << 16) | ((((int) bi[17]) << 8) | (int) bi[16]; Infomap.put ("Compression", new Integer (ncompression)); System.out.println ("Compression is:" + ncompression); int nsizeimage = (((int) bi[23] & 0xff) << 24) | (((int) bi[22] & 0xff) << 16) | (((int) bi[21] & 0xff) << 8) | (int) bi[20] & 0xFF; Infomap.put ("Sizeimage", New Integer (Nsizeimage)); System.out.prinTLN ("Sizeimage is:" + nsizeimage); int nxpm = (((int) bi[27] & 0xff) << 24) | (((int) bi[26] & 0xff) << 16) | (((int) bi[25] & 0xff) << 8) | (int) bi[24] & 0xFF; Infomap.put ("X-pixels", New Integer (NXPM)); System.out.println ("X-pixels per meter are:" + nxpm); int nypm = (((int) bi[31] & 0xff) << 24) | (((int) bi[30] & 0xff) << 16) | (((int) bi[29] & 0xff) << 8) | (int) bi[28] & 0xFF; Infomap.put ("Y-pixels", New Integer (NYPM)); System.out.println ("Y-pixels per meter are:" + nypm); int nclrused = (((int) bi[35] & 0xff) << 24) | (((int) bi[34] & 0xff) << 16) | (((int) bi[33] & 0xff) << 8) | (int) bi[32] & 0xFF; Infomap.put ("colorsused", New Integer (nclrused)); System.out.println ("Colors used are:" + nclrused); int nclrimp = (((int) bi[39] & 0xff) << 24) | (((int) bi[38] & 0xff) << 16) | (((int) bi[37] & 0xff) << 8) | (int) bi[36] & 0xFF; Infomap.put ("Colorsimportant", New Integer (Nclrimp)); System.out.println ("Colors important are:" + nclrimp); Some bitmaps don't have the sizeimage field calculated//Ferret out these cases and fix ' em. if (Nsizeimage = = 0) {Nsizeimage = (((nwidth * nbitcount) + to) & ~31) >> 3); Nsizeimage *= nheight P.put ("Nsizeimagebackup", New Integer (Nsizeimage)); System.out.println ("Nsizeimage (Backup) is" + nsizeimage); } if (Nbitcount = =) {//No Palatte data for 32-bit format but scan lines//are out to padded even. int npad = (nsizeimage/nheight)-nwidth * 4; int ndata[] = new int[nheight * nwidth]; byte brgb[] = new byte[(nwidth + npad) * 4 * nheight]; Fs.read (Brgb, 0, (nwidth + npad) * 4 * nheight); int nindex = 0; for (int j = 0; J < nheight; J +) {for (int i = 0; i < nwidth; i++) {Ndata[nwidth * (Nheight-j-1) + i] = (255 & 0xff) << 24 | (((int) Brgb[nindex + 2] & 0xff) << 16) | (((INT) Brgb[nindex + 1] & 0xff) << 8 | (int) Brgb[nindex] & 0xFF; /* SYSTEM.OUT.PRINTLN ("encoded Color at (" + i + "," + j + ") is:" + Brgb + "(r,g,b) = (" + ((int) (brgb[2]) & 0xff) + "," + ((int) brgb[1] & 0xff) + "," + ((int) brgb[0] & 0xff) + ")"); * * nindex + + 4; } nindex + = Npad; Image = Toolkit.getdefaulttoolkit (). CreateImage (New MemoryImageSource (nwidth, nheight, Ndata, 0, nwidth)); else if (Nbitcount =) {//No Palatte data for 24-bit format but scan lines are//padded out to even 4-byte ies. int npad = (nsizeimage/nheight)-nwidth * 3; int ndata[] = new int[nheight * nwidth]; byte brgb[] = new byte[(nwidth + npad) * 3 * nheight]; Fs.read (Brgb, 0, (nwidth + npad) * 3 * nheight); int nindex = 0; for (int j = 0; J < nheight; J +) {for (int i = 0; i < nwidth; i++) {Ndata[nwidth * (Nheight-j-1) + i] = (255 & 0xff) << 24 | (((int) Brgb[nindex + 2] & 0xff) << 16) | (((int) Brgb[nindex + 1] & 0xff) << 8) | (int) Brgb[nindex] & 0xFF; /* SYSTEM.OUT.PRINTLN ("encoded Color at (" + i + "," + j + ") is:" + Brgb + "(r,g,b) = (" + ((int) (brgb[2]) & 0xff) + "," + ((int) brgb[1] & 0xff) + "," + ((int) brgb[0] & 0xff) + ")";/nindex + + 3; } nindex + = Npad; Image = Toolkit.getdefaulttoolkit (). CreateImage (New MemoryImageSource (nwidth, nheight, Ndata, 0, nwidth)); else if (Nbitcount = =) {//No Palatte data for 16-bit format but scan lines are//padded out to even 4-byte ies. int npad = (nsizeimage/nheight)-nwidth * 2; int ndata[] = new int[nheight * nwidth]; byte brgb[] = new byte[(nwidth + npad) * 2 * nheight]; Fs.read (Brgb, 0, (nwidth + npad) * 2 * nheight); int nindex = 0; for (int j = 0; J < nheight; J +) {for (int i = 0; i < nwidth; i++) {Ndata[nwidth * (Nheight-j-1) + i] = (255 & 0xff) << 24 | ((((((int) Brgb[nindex + 1] >>> 2) & 0x3f) | 0x60) <<3 << 16) | ((((int) ((Brgb[nindex + 1]&0x3) <<3 | ((BRGB[NINDEX]&0XE0) >>>5)) | 0x60) <<3 << 8) | (((int) Brgb[nindex] & 0x1f) | 0x60) <<3); Nindex + 2; } nindex + = Npad; Image = Toolkit.getdefaulttoolkit (). CreateImage (New MemoryImageSource (nwidth, nheight, Ndata, 0, nwidth)); else if (Nbitcount = 8) {//Have to determine the number of colors, the clrsused//parameter are dominant if it is GRE Ater than zero. If//Zero1, calculate colors based on BitsPerPixel. int nnumcolors = 0; if (nclrused > 0) {nnumcolors = nclrused} else {nnumcolors = (1 & 0xff) << Nbitcount; Infomap.put ("Colo Rsnumber ", New Integer (nnumcolors)); System.out.println ("The number of Colors is" + nnumcolors); Read the Palatte colors. int npalette[] = new Int[nnumcolors]; byte bpalette[] = new byte[nnumcolors * 4]; Fs.read (Bpalette, 0, nnumcolors * 4); int nindex8 = 0; for (int n = 0; n < nnumcolors n++) {Npalette[n] = (255 & 0xff) << 24 | (((int) bpalette[nindex8 + 2] & 0xff) << 16 | (((int) bpalette[nindex8 + 1] & 0xff) << 8) | (int) bpalette[nindex8] & 0xFF; /* SYSTEM.OUT.PRINTLN ("Palette Color" + N + "is:" + npalette[n] + "(res,r,g,b) = (" + ((int) (bpalette[nindex8 + 3]) &A mp 0xFF) + "," + ((int) (bpalette[nindex8 + 2]) & 0xff) + "," + (int) bpalette[nindex8 + 1] & 0xff) + "," + (int ) bpalette[nindex8] & 0xff) + ")"; * * nindex8 + + 4; //Read The image data (actually indices into the palette)//Scan lines are still-padded out to even 4-byte//Boundari Es. int Npad8 = (nsizeimage/nheight)-nwidth; Infomap.put ("Npad", New Integer (Npad8)); System.out.println ("Npad is:" + Npad8); int ndata8[] = new int[nwidth * nheight]; byte bdata[] = new byte[(nwidth + npad8) * nheight]; Fs.read (bdata, 0, (nwidth + npad8) * nheight); nindex8 = 0; for (int j8 = 0; J8 < nheight, j8++) {for (int i8 = 0; i8 < nwidth; i8++) {Ndata8[nwidth * (nheight-j8-1) + i8 ] = npalette[((int) bdata[nindex8] &amP 0xFF)]; nindex8++; } nindex8 + = Npad8; Image = Toolkit.getdefaulttoolkit (). CreateImage (New MemoryImageSource (nwidth, nheight, ndata8, 0, nwidth)); else if (Nbitcount = 4) {//Have to determine the number of colors, the clrsused//parameter are dominant if it is GRE Ater than zero. If//Zero1, calculate colors based on BitsPerPixel. int nnumcolors = 0; if (nclrused > 0) {nnumcolors = nclrused} else {nnumcolors = (1 & 0xff) << Nbitcount; Infomap.put ("Colo Rsnumber ", New Integer (nnumcolors)); System.out.println ("The number of Colors is" + nnumcolors); Read the Palatte colors. int npalette[] = new Int[nnumcolors]; byte bpalette[] = new byte[nnumcolors * 4]; Fs.read (Bpalette, 0, nnumcolors * 4); int nindex4 = 0; for (int n = 0; n < nnumcolors n++) {Npalette[n] = (255 & 0xff) << 24 | (((int) bpalette[nindex4 + 2] & 0xff) << 16) | (((int) bpalette[nindex4 + 1] & 0xff) << 8) | (int) bpalette[nindex4] & 0xFF; /* System.Out.println ("Palette Color" + N + "is:" + npalette[n] + "(res,r,g,b) = (" + ((int) (bpalette[nindex8 + 3]) & 0xFF) + "," + (int) (bpalette[nindex8 + 2]) & 0xff) + "," + (int) bpalette[nindex8 + 1] & 0xff) + "," + (int) Bpal ETTE[NINDEX8] & 0xff) + ")"; * * nindex4 + + 4; }//Scan line are padded with zeroes a multiple of four bytes int scanlinesize = ((nwidth * nbitcount) +) ; ~31) >> 3; Read the image data (actually indices into the palette)//Scan lines are still-padded out to even 4-byte//boundaries . int npad4 = (nsizeimage/nheight)-NWIDTH/2; /*int npad4 = 0; if ((nwidth%2) = = 0) npad4 = SCANLINESIZE-NWIDTH/2; else Npad4 = SCANLINESIZE-NWIDTH/2-1; System.out.println ("Npad is:" + npad4); */int ndata4[] = new int[nwidth * nheight]; byte bdata[] = new byte[scanlinesize * nheight]; byte blinedata[] = new Byte[scanlinesize]; Fs.read (bdata, 0, scanlinesize * nheight); nindex4 = 0; for (int j4 = 0; J4 < nheight; j4++) {fs.read (blinedata, 0, scanlinesize); nindex4 = 0; for (int i4 = 0; i4 < nwidth; i4++) {if nwidth * (Nheight-j4- 1) + i4 > nwidth * nheight-1) break; if (nindex4 > Scanlinesize * nheight-1) break; for (int pixperbyte = 0; pixperbyte < 2; pixperbyte++) {if (Pixperbyte = = 0) {Ndata4[nwidth * (nheight-j4-1) + I4 = npalette[((int) (BLINEDATA[NINDEX4]>>4) & 0xf)]; i4++; if (I4 >= nwidth) break; else {Ndata4[nwidth * (nheight-j4-1) + I4] = npalette[((int) blinedata[nindex4] & 0xf)];} nindex4++; } image = Toolkit.getdefaulttoolkit (). CreateImage (New MemoryImageSource (nwidth, nheight, ndata4, 0, nwidth)); else if (Nbitcount = 1) {//Have to determine the number of colors, the clrsused//parameter are dominant if it is GRE Ater than zero. If//Zero1, calculate colors based on BitsPerPixel. int nnumcolors = 0; if (nclrused > 0) {nnumcolors = nclrused} else {nnumcolors = (1 & 0xff) << Nbitcount; Infomap.put ("Colo RSnumber ", New Integer (nnumcolors)); System.out.println ("The number of Colors is" + nnumcolors); Read the Palatte colors. int npalette[] = new Int[nnumcolors]; byte bpalette[] = new byte[nnumcolors * 4]; Fs.read (Bpalette, 0, nnumcolors * 4); int nindex1 = 0; for (int n = 0; n < nnumcolors n++) {Npalette[n] = (255 & 0xff) << 24 | (((int) Bpalette[nindex1 + 2] & 0xff) << 16) | (((int) Bpalette[nindex1 + 1] & 0xff) << 8) | (int) bpalette[nindex1] & 0xFF; Nindex1 + 4; }//Scan line are padded with zeroes a multiple of four bytes int scanlinesize = ((nwidth * nbitcount) +) ; ~31) >> 3; int ndata1[] = new int[nwidth * nheight]; byte bdata[] = new byte[scanlinesize * nheight]; byte blinedata[] = new Byte[scanlinesize]; Fs.read (bdata, 0, scanlinesize * nheight); nindex1 = 0; for (int j1 = 0; J1 < nheight j1++) {fs.read (blinedata, 0, scanlinesize); nindex1 = 0; for (int i1 = 0; I1 < Nwidt H i1++) {if (Nwidth * (nheight-j1-1) + i1 > nwidth * nheight-1) break; if (Nindex1 > Scanlinesize * nheight-1) break; for (int pixperbyte = 0; pixperbyte < 8; pixperbyte++) {int shift = 8-PIXPERBYTE-1; Ndata1[nwidth * (nheight-j1 -1) + I1] = npalette[((int) (Blinedata[nindex1]>>shift) & 0x1)]; if (Pixperbyte!= 7) {i1++; if (I1 >= nwidth) break;} nindex1++; } image = Toolkit.getdefaulttoolkit (). CreateImage (New MemoryImageSource (nwidth, nheight, ndata1, 0, nwidth)); else {//system.out.println ("not a 32-bit, 24-bit, 16-bit, 8-bit, 4-bit and 1-bit Windows Bitmap, aborting ..."); Image = (image) null; } fs.close (); return image; catch (Exception e) {e.printstacktrace (System.out); System.out.println ("Caught exception in loadbitmap!"); return (Image) null; public static void Main (string[] ages) {Bmpparse bmpparse = new Bmpparse ("C://img.bmp"); BufferedImage img = new BufferedImage (300,BUFFEREDIMAGE.TYPE_INT_RGB); Image image = Bmpparse.loadbitmap (null); Img.Getgraphics (). DrawImage (Image.getscaledinstance (300,image.scale_smooth), 0, 0, NULL); try {imageio.write (img, "JPEG", New File ("c://2.jpg")); System.out.print (Image.getheight (null)); catch (IOException e) {//TODO auto-generated catch block E.printstacktrace ();}} }