2013-05-06 57 views
0

我正在将C++ Win32应用程序转换为Java(在Linux中)我需要读取BITMAPFILEHEADER和 位图的BITMAPINFOHEADER,我怎么能做到这一点? 我找到一个专门针对windows(我认为)的jna(java native access)库。 有人有任何想法吗?从BitMap中读取BITMAPFILEHEADER和BITMAPINFOHEADER

回答

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; 

} 

}