0

我正在将 C++ Win32 应用程序转换为 Java(在 linux 中)我需要读取位图的 BITMAPFILEHEADER 和 BITMAPINFOHEADER,我该怎么做?我找到了一个专门用于 Windows 的 jna(Java 本机访问)库(我认为)。有人知道吗?

4

1 回答 1

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;

}

}
于 2013-05-07T04:25:44.393 回答