Java 读取 BMP 格式图片的源代码
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.awt.Image;
import java.awt.Toolkit;
import java.awt.image.MemoryImageSource;
import java.io.FileInputStream;
import java.io.IOException;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import 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(FileInputStream fs) throws 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);
is :"+(char)bf[0]+(char)bf[1]);
//
System.out.println("File type
//
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);
compression
is :"+ncompression);
is :"+nsizeimage);
is :"+nxpm);
is :"+nypm);
are :"+nclrused);
are :"+nclrimp);
}
}
// Look for non-zero values to indicate
ncompression = constructInt(bi, 16);
//
System.out.println("Compression
nsizeimage = constructInt(bi, 20);
//
System.out.println("SizeImage
nxpm = constructInt(bi, 24);
// System.out.println("X-Pixels per meter
nypm = constructInt(bi, 28);
// System.out.println("Y-Pixels per meter
nclrused = constructInt(bi, 32);
//
System.out.println("Colors used
nclrimp = constructInt(bi, 36);
//
System.out.println("Colors important
public static Image read(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 (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 Image readMap32(FileInputStream fs,
BitmapHeader bh)
throws IOException
{
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++)
i] = constructInt3(
{
}
ndata[bh.nwidth * (bh.nheight - j - 1) +
brgb, nindex);
nindex += 4;
}
image = Toolkit.getDefaultToolkit().createImage
(new 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 Image readMap24(FileInputStream fs,
BitmapHeader bh)
throws IOException
{
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];