Java读取BMP格式图片(源代码,转)

来源:互联网 发布:网络兼容模式怎么设置 编辑:程序博客网 时间:2024/05/02 01:42
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
/* Header/Footer Colours */ #Header { font-family: Arial, Helvetica, sans-serif; color: #000000; background-color:#EEEEFF } /* Line Number */ #LN { color: #BBBBBB; background-color:#FFFFFF } /* Link Colours */ #Classes A:link { color: #000000; } #Classes A:visited { color: #000000; } #Classes PRE { color: #000000; } /* Token Colours */ #CharacerLiteral { color: #FF00FF; } #StringLiteral { color: #FF00FF; } #SingleLineComment { color: #008000; } #FormalComment { color: #008000; } #MultiLineComment { color: #008000; } #Abstract { color: #0000FF ; font-weight: bold } #Boolean { color: #0000FF ; font-weight: bold } #Break { color: #0000FF ; font-weight: bold } #Byte { color: #0000FF ; font-weight: bold } #Case { color: #0000FF ; font-weight: bold } #Catch { color: #0000FF ; font-weight: bold } #Char { color: #0000FF ; font-weight: bold } #Class { color: #0000FF ; font-weight: bold } #Const { color: #0000FF ; font-weight: bold } #Continue { color: #0000FF ; font-weight: bold } #Default { color: #0000FF ; font-weight: bold } #Do { color: #0000FF ; font-weight: bold } #Double { color: #0000FF ; font-weight: bold } #Else { color: #0000FF ; font-weight: bold } #Extends { color: #0000FF ; font-weight: bold } #False { color: #0000FF ; font-weight: bold } #Final { color: #0000FF ; font-weight: bold } #Finally { color: #0000FF ; font-weight: bold } #Float { color: #0000FF ; font-weight: bold } #For { color: #0000FF ; font-weight: bold } #Goto { color: #0000FF ; font-weight: bold } #If { color: #0000FF ; font-weight: bold } #Implements { color: #0000FF ; font-weight: bold } #Import { color: #0000FF ; font-weight: bold } #InstanceOf { color: #0000FF ; font-weight: bold } #Int { color: #0000FF ; font-weight: bold } #Interface { color: #0000FF ; font-weight: bold } #Long { color: #0000FF ; font-weight: bold } #Native { color: #0000FF ; font-weight: bold } #New { color: #0000FF ; font-weight: bold } #Package { color: #0000FF ; font-weight: bold } #Private { color: #0000FF ; font-weight: bold } #Protected { color: #0000FF ; font-weight: bold } #Public { color: #0000FF ; font-weight: bold } #Return { color: #0000FF ; font-weight: bold } #Short { color: #0000FF ; font-weight: bold } #Static { color: #0000FF ; font-weight: bold } #Super { color: #0000FF ; font-weight: bold } #Switch { color: #0000FF ; font-weight: bold } #Synchronized { color: #0000FF ; font-weight: bold } #This { color: #0000FF ; font-weight: bold } #Throw { color: #0000FF ; font-weight: bold } #Throws { color: #0000FF ; font-weight: bold } #Transient { color: #0000FF ; font-weight: bold } #True { color: #0000FF ; font-weight: bold } #Try { color: #0000FF ; font-weight: bold } #Void { color: #0000FF ; font-weight: bold } #Volatile { color: #0000FF ; font-weight: bold } #While { color: #0000FF ; font-weight: bold } #StrictFP { color: #0000FF ; font-weight: bold } #IntegerLiteral { color: #000000 } #DecimalLiteral { color: #000000 } #HexLiteral { color: #000000 } #OctalLiteral { color: #000000 } #FloatPointLiteral { color: #000000 } Basic Objective    A windows BMP file is a common image format that Java does not handle. While BMP images are used only on windows machines, they are reasonably common. Reading these shows how to read complex structures in Java and how to alter they byte order from the big endian order used by Java to the little endian order used by the windows and the intel processor. --------------------------------------------------------////This code was taken and cleaned up from a//Javaworld tips and tricks column//import Java.sun.com/products/jdk/1.4/docs/api/Java/awt/Image.html">Java.awt.Image;import Java.sun.com/products/jdk/1.4/docs/api/Java/awt/Toolkit.html">Java.awt.Toolkit;import Java.sun.com/products/jdk/1.4/docs/api/Java/awt/image/MemoryImageSource.html">Java.awt.image.MemoryImageSource;import Java.sun.com/products/jdk/1.4/docs/api/Java/io/FileInputStream.html">Java.io.FileInputStream;import Java.sun.com/products/jdk/1.4/docs/api/Java/io/IOException.html">Java.io.IOException;import Java.sun.com/products/jdk/1.4/docs/api/Javax/swing/ImageIcon.html">Javax.swing.ImageIcon;import Java.sun.com/products/jdk/1.4/docs/api/Javax/swing/JFrame.html">Javax.swing.JFrame;import Java.sun.com/products/jdk/1.4/docs/api/Javax/swing/JLabel.html">Javax.swing.JLabel;import Java.sun.com/products/jdk/1.4/docs/api/Javax/swing/JScrollPane.html">Javax.swing.JScrollPane;////really just a collection of methods to read a BMP file//public class BMPLoader{    // build an int from a byte array - convert little to big endian    public static int constructInt(byte[] in, int offset) {        int ret = ((int) in[offset + 3] & 0xff);        ret = (ret << 8) | ((int) in[offset + 2] & 0xff);        ret = (ret << 8) | ((int) in[offset + 1] & 0xff);        ret = (ret << 8) | ((int) in[offset + 0] & 0xff);        return (ret);    }    // build an int from a byte array - convert little to big endian    // set high order bytes to 0xfff    public static int constructInt3(byte[] in, int offset) {        int ret = 0xff;        ret = (ret << 8) | ((int) in[offset + 2] & 0xff);        ret = (ret << 8) | ((int) in[offset + 1] & 0xff);        ret = (ret << 8) | ((int) in[offset + 0] & 0xff);        return (ret);    }    // build an int from a byte array - convert little to big endian    public static long constructLong(byte[] in, int offset) {        long ret = ((long) in[offset + 7] & 0xff);        ret |= (ret << 8) | ((long) in[offset + 6] & 0xff);        ret |= (ret << 8) | ((long) in[offset + 5] & 0xff);        ret |= (ret << 8) | ((long) in[offset + 4] & 0xff);        ret |= (ret << 8) | ((long) in[offset + 3] & 0xff);        ret |= (ret << 8) | ((long) in[offset + 2] & 0xff);        ret |= (ret << 8) | ((long) in[offset + 1] & 0xff);        ret |= (ret << 8) | ((long) in[offset + 0] & 0xff);        return (ret);    }    // build an double from a byte array - convert little to big endian    public static double constructDouble(byte[] in, int offset) {        long ret = constructLong(in, offset);        return (Double.longBitsToDouble(ret));    }    // build an short from a byte array - convert little to big endian    public static short constructShort(byte[] in, int offset) {        short ret = (short) ((short) in[offset + 1] & 0xff);        ret = (short) ((ret << 8) | (short) ((short) in[offset + 0] & 0xff));        return (ret);    }    // internal class representing a bitmap header structure    // with code to read it from a file    static class BitmapHeader {        public int nsize;        public int nbisize;        public int nwidth;        public int nheight;        public int nplanes;        public int nbitcount;        public int ncompression;        public int nsizeimage;        public int nxpm;        public int nypm;        public int nclrused;        public int nclrimp;        // read in the bitmap header        public void read(Java.sun.com/products/jdk/1.4/docs/api/Java/io/FileInputStream.html">FileInputStream fs) throws Java.sun.com/products/jdk/1.4/docs/api/Java/io/IOException.html">IOException        {            final int bflen = 14; // 14 byte BITMAPFILEHEADER            byte bf[] = new byte[bflen];            fs.read(bf, 0, bflen);            final int bilen = 40; // 40-byte BITMAPINFOHEADER            byte bi[] = new byte[bilen];            fs.read(bi, 0, bilen);            // Interperet data.            nsize = constructInt(bf, 2);            //  System.out.println("File type is :"+(char)bf[0]+(char)bf[1]);            //  System.out.println("Size of file is :"+nsize);            nbisize = constructInt(bi, 2);            //   System.out.println("Size of bitmapinfoheader is :"+nbisize);            nwidth = constructInt(bi, 4);            //   System.out.println("Width is :"+nwidth);            nheight = constructInt(bi, 8);            //  System.out.println("Height is :"+nheight);            nplanes = constructShort(bi, 12); //(((int)bi[13]&0xff)<<8) |            // (int)bi[12]&0xff;            //  System.out.println("Planes is :"+nplanes);            nbitcount = constructShort(bi, 14); //(((int)bi[15]&0xff)<<8) |            // (int)bi[14]&0xff;            //  System.out.println("BitCount is :"+nbitcount);            // Look for non-zero values to indicate compression            ncompression = constructInt(bi, 16);            //  System.out.println("Compression is :"+ncompression);            nsizeimage = constructInt(bi, 20);            //  System.out.println("SizeImage is :"+nsizeimage);            nxpm = constructInt(bi, 24);            // System.out.println("X-Pixels per meter is :"+nxpm);            nypm = constructInt(bi, 28);            // System.out.println("Y-Pixels per meter is :"+nypm);            nclrused = constructInt(bi, 32);            //  System.out.println("Colors used are :"+nclrused);            nclrimp = constructInt(bi, 36);            //  System.out.println("Colors important are :"+nclrimp);        }    }    public static Java.sun.com/products/jdk/1.4/docs/api/Java/awt/Image.html">Image read(Java.sun.com/products/jdk/1.4/docs/api/Java/io/FileInputStream.html">FileInputStream fs)    {        try {            BitmapHeader bh = new BitmapHeader();            bh.read(fs);            if (bh.nbitcount == 24)                return (readMap24(fs, bh));            if (bh.nbitcount == 32)                return (readMap32(fs, bh));            if (bh.nbitcount == 8)                return (readMap8(fs, bh));            fs.close();        } catch (Java.sun.com/products/jdk/1.4/docs/api/Java/io/IOException.html">IOException e) {            // System.out.println("Caught exception in loadbitmap!");        }        return (null);    }    /**     *      * readMap24 internal routine to read the bytes in a 24 bit bitmap     *      *      *      * Arguments:     *      * fs - file stream     *      * bh - header struct     *      * Returns:     *      * Image Object, be sure to check for (Image)null !!!!     *      *      *       */    protected static Java.sun.com/products/jdk/1.4/docs/api/Java/awt/Image.html">Image readMap32(Java.sun.com/products/jdk/1.4/docs/api/Java/io/FileInputStream.html">FileInputStream fs, BitmapHeader bh)            throws Java.sun.com/products/jdk/1.4/docs/api/Java/io/IOException.html">IOException    {        Java.sun.com/products/jdk/1.4/docs/api/Java/awt/Image.html">Image image;        // No Palatte data for 24-bit format but scan lines are        // padded out to even 4-byte boundaries.        int xwidth = bh.nsizeimage / bh.nheight;        int ndata[] = new int[bh.nheight * bh.nwidth];        byte brgb[] = new byte[bh.nwidth * 4 * bh.nheight];        fs.read(brgb, 0, bh.nwidth * 4 * bh.nheight);        int nindex = 0;        for (int j = 0; j < bh.nheight; j++)        {            for (int i = 0; i < bh.nwidth; i++)            {                ndata[bh.nwidth * (bh.nheight - j - 1) + i] = constructInt3(                        brgb, nindex);                nindex += 4;            }        }        image = Toolkit.getDefaultToolkit().createImage        (new Java.sun.com/products/jdk/1.4/docs/api/Java/awt/image/MemoryImageSource.html">MemoryImageSource(bh.nwidth, bh.nheight,        ndata, 0, bh.nwidth));        fs.close();        return (image);    }    /**     *      * readMap24 internal routine to read the bytes in a 24 bit bitmap     *      *      *      * Arguments:     *      * fs - file stream     *      * bh - header struct     *      * Returns:     *      * Image Object, be sure to check for (Image)null !!!!     *      *      *       */    protected static Java.sun.com/products/jdk/1.4/docs/api/Java/awt/Image.html">Image readMap24(Java.sun.com/products/jdk/1.4/docs/api/Java/io/FileInputStream.html">FileInputStream fs, BitmapHeader bh)            throws Java.sun.com/products/jdk/1.4/docs/api/Java/io/IOException.html">IOException    {        Java.sun.com/products/jdk/1.4/docs/api/Java/awt/Image.html">Image image;        // No Palatte data for 24-bit format but scan lines are        // padded out to even 4-byte boundaries.        int npad = (bh.nsizeimage / bh.nheight) - bh.nwidth * 3;        int ndata[] = new int[bh.nheight * bh.nwidth];        byte brgb[] = new byte[(bh.nwidth + npad) * 3 * bh.nheight];        fs.read(brgb, 0, (bh.nwidth + npad) * 3 * bh.nheight);        int nindex = 0;        for (int j = 0; j < bh.nheight; j++)        {            for (int i = 0; i < bh.nwidth; i++)            {                ndata[bh.nwidth * (bh.nheight - j - 1) + i] = constructInt3(                        brgb, nindex);                nindex += 3;            }            nindex += npad;        }        image = Toolkit.getDefaultToolkit().createImage        (new Java.sun.com/products/jdk/1.4/docs/api/Java/awt/image/MemoryImageSource.html">MemoryImageSource(bh.nwidth, bh.nheight,        ndata, 0, bh.nwidth));        fs.close();        return (image);    }    /**     *      * readMap8 internal routine to read the bytes in a 8 bit bitmap     *      *      *      * Arguments:     *      * fs - file stream     *      * bh - header struct     *      * Returns:     *      * Image Object, be sure to check for (Image)null !!!!     *      *      *       */    protected static Java.sun.com/products/jdk/1.4/docs/api/Java/awt/Image.html">Image readMap8(Java.sun.com/products/jdk/1.4/docs/api/Java/io/FileInputStream.html">FileInputStream fs, BitmapHeader bh)            throws Java.sun.com/products/jdk/1.4/docs/api/Java/io/IOException.html">IOException    {        Java.sun.com/products/jdk/1.4/docs/api/Java/awt/Image.html">Image image;        // Have to determine the number of colors, the clrsused        // parameter is dominant if it is greater than zero. If        // zero, calculate colors based on bitsperpixel.        int nNumColors = 0;        if (bh.nclrused > 0)        {            nNumColors = bh.nclrused;        }        else        {            nNumColors = (1 & 0xff) << bh.nbitcount;        }        //     System.out.println("The number of Colors is"+nNumColors);        // Some bitmaps do not have the sizeimage field calculated        // Ferret out these cases and fix 'em.        if (bh.nsizeimage == 0)        {            bh.nsizeimage = ((((bh.nwidth * bh.nbitcount) + 31) & ~31) >> 3);            bh.nsizeimage *= bh.nheight;            //          System.out.println("nsizeimage (backup) is"+nsizeimage);        }        // 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] = constructInt3(bpalette, nindex8);            nindex8 += 4;        }        // Read the image data (actually indices into the palette)        // Scan lines are still padded out to even 4-byte        // boundaries.        int npad8 = (bh.nsizeimage / bh.nheight) - bh.nwidth;        //    System.out.println("nPad is:"+npad8);        int ndata8[] = new int[bh.nwidth * bh.nheight];        byte bdata[] = new byte[(bh.nwidth + npad8) * bh.nheight];        fs.read(bdata, 0, (bh.nwidth + npad8) * bh.nheight);        nindex8 = 0;        for (int j8 = 0; j8 < bh.nheight; j8++)        {            for (int i8 = 0; i8 < bh.nwidth; i8++)            {                ndata8[bh.nwidth * (bh.nheight - j8 - 1) + i8] =                npalette[((int) bdata[nindex8] & 0xff)];                nindex8++;            }            nindex8 += npad8;        }        image = Toolkit.getDefaultToolkit().createImage        (new Java.sun.com/products/jdk/1.4/docs/api/Java/awt/image/MemoryImageSource.html">MemoryImageSource(bh.nwidth, bh.nheight,        ndata8, 0, bh.nwidth));        return (image);    }    /**     *      * load method - see read for details     *      *      *      * Arguments:     *      * sdir and sfile are the result of the FileDialog()     *      * getDirectory() and getFile() methods.     *      *      *      * Returns:     *      * Image Object, be sure to check for (Image)null !!!!     *      *      *       */    public static Java.sun.com/products/jdk/1.4/docs/api/Java/awt/Image.html">Image load(Java.sun.com/products/jdk/1.4/docs/api/Java/lang/String.html">String sdir, Java.sun.com/products/jdk/1.4/docs/api/Java/lang/String.html">String sfile) {        return (load(sdir + sfile));    }    /**     *      * load method - see read for details     *      *      *      * Arguments:     *      * sdir - full path name     *      *      *      * Returns:     *      * Image Object, be sure to check for (Image)null !!!!     *      *      *       */    public static Java.sun.com/products/jdk/1.4/docs/api/Java/awt/Image.html">Image load(Java.sun.com/products/jdk/1.4/docs/api/Java/lang/String.html">String sdir)    {        try        {            Java.sun.com/products/jdk/1.4/docs/api/Java/io/FileInputStream.html">FileInputStream fs = new Java.sun.com/products/jdk/1.4/docs/api/Java/io/FileInputStream.html">FileInputStream(sdir);            return (read(fs));        }        catch (Java.sun.com/products/jdk/1.4/docs/api/Java/io/IOException.html">IOException ex) {            return (null);        }    }    public static void main(Java.sun.com/products/jdk/1.4/docs/api/Java/lang/String.html">String[] args) throws Java.sun.com/products/jdk/1.4/docs/api/Java/io/IOException.html">IOException    {        if (args.length == 0) {            System.out.println("Usage >Java BMPLoader ImageFile.BMP");            System.exit(0);        }        Java.sun.com/products/jdk/1.4/docs/api/Java/io/FileInputStream.html">FileInputStream in = new Java.sun.com/products/jdk/1.4/docs/api/Java/io/FileInputStream.html">FileInputStream(args[0]);        Java.sun.com/products/jdk/1.4/docs/api/Java/awt/Image.html">Image TheImage = read(in);        Java.sun.com/products/jdk/1.4/docs/api/Javax/swing/JFrame.html">JFrame TheFrame = new Java.sun.com/products/jdk/1.4/docs/api/Javax/swing/JFrame.html">JFrame(args[0]);        Java.sun.com/products/jdk/1.4/docs/api/Javax/swing/JLabel.html">JLabel TheLabel = new Java.sun.com/products/jdk/1.4/docs/api/Javax/swing/JLabel.html">JLabel(new Java.sun.com/products/jdk/1.4/docs/api/Javax/swing/ImageIcon.html">ImageIcon(TheImage));        TheFrame.getContentPane().add(new Java.sun.com/products/jdk/1.4/docs/api/Javax/swing/JScrollPane.html">JScrollPane(TheLabel));        TheFrame.setSize(300, 300);        TheFrame.setVisible(true);    }    //end class BMPLoader}<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
原创粉丝点击