从BitMap读取BITMAPFILEHEADER和BITMAPINFOHEADER

时间:2013-05-06 06:28:41

标签: java bitmap

我正在将C ++ Win32应用程序转换为Java(在linux中)我需要读取BITMAPFILEHEADER和 位图的BITMAPINFOHEADER,我怎么能这样做? 我找到了一个专门用于windows的jna(java native access)库(我认为)。 有人知道吗?

1 个答案:

答案 0 :(得分:0)

在网上搜索之后我决定自己实现这些结构并将位图文件读入一个字节数组并将它们解析为类数据成员,这里是代码

public class BitmapFileHeader {

char[] bfType;
int bfSize;
short bfReserved1;
short bfReserved2;
int bfOffBits;

private BitmapFileHeader() {

}

public static BitmapFileHeader readFromImage(byte[] image) {

    BitmapFileHeader bitmap = new BitmapFileHeader();

    bitmap.bfType = new char[2];
    int index = 0;
    bitmap.bfType[0] = (char) image[index++];
    bitmap.bfType[1] = (char) image[index++];

    bitmap.bfSize = (image[index + 3] & 0xff) << 24 | (image[index + 2] & 0xFF) << 16 | (image[index + 1] & 0xFF) << 8
            | (image[index + 0] & 0xFF);
    index += 4;

    bitmap.bfReserved1 = (short) (((image[index + 1] & 0xFF) << 8) | (image[index + 0] & 0xFF));
    index += 2;

    bitmap.bfReserved2 = (short) (((image[index + 1] & 0xFF) << 8) | (image[index + 0] & 0xFF));
    index += 2;

    bitmap.bfOffBits = (image[index + 3] & 0xff) << 24 | (image[index + 2] & 0xFF) << 16 | (image[index + 1] & 0xFF) << 8
            | (image[index + 0] & 0xFF);

    return bitmap;

}

}

public class BitmapInfoHeader {

int biSize;
long biWidth;
long biHeight;
short biPlanes;
short biBitCount;
int biCompression;
int biSizeImage;
long biXPelsPerMeter;
long biYPelsPerMeter;
int biClrUsed;
int biClrImportant;

private BitmapInfoHeader() {

}

public static BitmapInfoHeader readFromImage(byte[] image) {

    BitmapInfoHeader bitmap = new BitmapInfoHeader();
    // LittleEndian order ...
    int index = 14;

    bitmap.biSize = (image[index + 3] & 0xff) << 24 | (image[index + 2] & 0xFF) << 16 | (image[index + 1] & 0xFF) << 8
            | (image[index + 0] & 0xFF);
    index += 4;

    bitmap.biWidth = ((image[index + 3] & 0xff) << 24) | (image[index + 2] & 0xFF) << 16 | (image[index + 1] & 0xFF) << 8
            | (image[index + 0] & 0xFF);
    index += 4;

    bitmap.biHeight = (image[index + 3] & 0xff) << 24 | (image[index + 2] & 0xFF) << 16 | (image[index + 1] & 0xFF) << 8
            | (image[index + 0] & 0xFF);
    index += 4;

    bitmap.biPlanes = (short) (((image[index + 1] & 0xFF) << 8) | (image[index + 0] & 0xFF));
    index += 2;

    bitmap.biBitCount = (short) (((image[index + 1] & 0xFF) << 8) | (image[index + 0] & 0xFF));
    index += 2;

    bitmap.biCompression = (image[index + 3] & 0xff) << 24 | (image[index + 2] & 0xFF) << 16 | (image[index + 1] & 0xFF) << 8
            | (image[index + 0] & 0xFF);
    index += 4;

    bitmap.biSizeImage = (image[index + 3] & 0xff) << 24 | (image[index + 2] & 0xFF) << 16 | (image[index + 1] & 0xFF) << 8
            | (image[index + 0] & 0xFF);
    index += 4;

    bitmap.biXPelsPerMeter =
    // ByteBuffer.wrap(image, index, index +
    // 4).order(ByteOrder.LITTLE_ENDIAN).getLong();
    (int) (image[index + 3] & 0xff) << 24 | (image[index + 2] & 0xFF) << 16 | (image[index + 1] & 0xFF) << 8 | (image[index + 0] & 0xFF);
    index += 4;

    bitmap.biYPelsPerMeter = 
            //ByteBuffer.wrap(image, index, index + 4).order(ByteOrder.LITTLE_ENDIAN).getLong();
    (int) (image[index + 3] & 0xff) << 24 | (image[index + 2] & 0xFF) << 16 | (image[index + 1] & 0xFF) << 8 | (image[index + 0] & 0xFF);
    index += 4;

    bitmap.biClrUsed = (image[index + 3] & 0xff) << 24 | (image[index + 2] & 0xFF) << 16 | (image[index + 1] & 0xFF) << 8
            | (image[index + 0] & 0xFF);
    index += 4;

    bitmap.biClrImportant = (image[index + 3] & 0xff) << 24 | (image[index + 2] & 0xFF) << 16 | (image[index + 1] & 0xFF) << 8
            | (image[index + 0] & 0xFF);
    index += 4;

    index += 10;
    return bitmap;

}

}