Java 将XML位图数据转换为图像

Java 将XML位图数据转换为图像,java,python,image,image-processing,python-imaging-library,Java,Python,Image,Image Processing,Python Imaging Library,我需要从XML文件中提取图像数据,并将图像作为单独的文件输出 我已经处理了解析,但是我不知道如何转换为图像 XML如下所示: <Bitmap> <BitmapInfo BitWidth="40" BitHeight="40" ByteWidth="8" BitCount="1" ColorCount="2" Compression="true"> <ColorTable> <Color>0</C

我需要从XML文件中提取图像数据,并将图像作为单独的文件输出

我已经处理了解析,但是我不知道如何转换为图像

XML如下所示:

<Bitmap>
    <BitmapInfo BitWidth="40" BitHeight="40" ByteWidth="8" BitCount="1" ColorCount="2" Compression="true">
        <ColorTable>
            <Color>0</Color>
            <Color>16777215</Color>
        </ColorTable>
        <BitData>Af5/+/8B/h/7/wH+B/v/Af4B+/8C/gB//P8C/gAf/P8C/gAH/P8C/gAB/P8D/gAAf/3/A/4AAB/9/wP+AAAH/f8D/gAAAf3/AP7+AAB//v8A/v4AAB/+/wD+/gAAB/7/AP7+AAAD/v8A/v4AAAf+/wD+/gAAH/7/AP7+AAB//v8D/gAAAf3/A/4AAAf9/wP+AAAf/f8D/gAAf/3/Av4AAfz/Av4AB/z/Av4AH/z/Av4Af/z/Af4B+/8B/gf7/wH+H/v/Af5/s/8=</BitData>
    </BitmapInfo>
    <Area Left="4430000" Top="12690000" Right="4563333" Bottom="12823333" />
</Bitmap>
public void writeImageFile(String data, int imgWidth, int imgHeight, int byteWidth, int bitCount, int colorCount, int[] colors, String fileName) throws IOException {
    byte[] interleavedRGB = getColorTable(colors);
    new IndexColorModel(bitCount, colorCount, interleavedRGB, 0, false);
    BufferedImage buffImg = new BufferedImage(imgWidth, imgHeight, BufferedImage.TYPE_BYTE_INDEXED);
    WritableRaster raster = buffImg.getRaster();
    byte[] uncompressed = uncompress(decodeBase64(data), byteWidth);
    int pixels = imgHeight * imgWidth;
    for (int pos = 0; pos < pixels; pos += bitCount) {
        int y = pos % imgWidth;
        int x = pos / imgHeight;
        raster.setSample(x, y, 0, getValue(uncompressed, pos, bitCount));
    }
    ImageIO.write(buffImg, "png", new File(fileName));
}

0
16777215
2008年8月/7 7/7 7/7/7/whwh+B/v/4/4/4/4/8/8/4/4/4/4/7/7/7/7/7/7/7/7/8/8/8/8/8/8/8/8/8/8/8/8/8/8/8/8/7/8/7/7/7/7/7/7/7/7/8/8/8/8/8/8/8/8/8/8/8/8/8/8/8/8/8/8/7/8/8/8/8/8/8/8/8/8/8/8/8/8/8/8/8/8/8/8/8/8/8/8/8/8/8/8/8/8/8/8/8/8/8/8/8/8/8/8/8/8/8/8/8/8/8/8 wH+h/v/Af5/s/8=
另一个例子:

<Bitmap>
    <BitmapInfo BitWidth="24" BitHeight="14" ByteWidth="4" BitCount="1" ColorCount="2" Compression="true">
        <ColorTable>
            <Color>0</Color>
            <Color>16777215</Color>
        </ColorTable>
        <BitData>/f8u8+c5//PnOf/z5hn/8+bZ//Pm2f/z5Mn/8+Xp//Pl6f/z4eH/8+Px/4Bj8f8AM/n8/w==</BitData>
    </BitmapInfo>
    <Area Left="1043333" Top="13360000" Right="1123333" Bottom="13406667" />
</Bitmap>

0
16777215
/f8u8+c5//PnOf/z5hn/8+bZ//Pm2f/z5Mn/8+Xp//Pl6f/z4eH/8+Px/4Bj8f8AM/n8/w==

任何关于如何进行此操作的指导都会有所帮助。

在Java中,使用javax.imageio,您可以执行以下操作:

<Bitmap>
    <BitmapInfo BitWidth="40" BitHeight="40" ByteWidth="8" BitCount="1" ColorCount="2" Compression="true">
        <ColorTable>
            <Color>0</Color>
            <Color>16777215</Color>
        </ColorTable>
        <BitData>Af5/+/8B/h/7/wH+B/v/Af4B+/8C/gB//P8C/gAf/P8C/gAH/P8C/gAB/P8D/gAAf/3/A/4AAB/9/wP+AAAH/f8D/gAAAf3/AP7+AAB//v8A/v4AAB/+/wD+/gAAB/7/AP7+AAAD/v8A/v4AAAf+/wD+/gAAH/7/AP7+AAB//v8D/gAAAf3/A/4AAAf9/wP+AAAf/f8D/gAAf/3/Av4AAfz/Av4AB/z/Av4AH/z/Av4Af/z/Af4B+/8B/gf7/wH+H/v/Af5/s/8=</BitData>
    </BitmapInfo>
    <Area Left="4430000" Top="12690000" Right="4563333" Bottom="12823333" />
</Bitmap>
public void writeImageFile(String data, int imgWidth, int imgHeight, int byteWidth, int bitCount, int colorCount, int[] colors, String fileName) throws IOException {
    byte[] interleavedRGB = getColorTable(colors);
    new IndexColorModel(bitCount, colorCount, interleavedRGB, 0, false);
    BufferedImage buffImg = new BufferedImage(imgWidth, imgHeight, BufferedImage.TYPE_BYTE_INDEXED);
    WritableRaster raster = buffImg.getRaster();
    byte[] uncompressed = uncompress(decodeBase64(data), byteWidth);
    int pixels = imgHeight * imgWidth;
    for (int pos = 0; pos < pixels; pos += bitCount) {
        int y = pos % imgWidth;
        int x = pos / imgHeight;
        raster.setSample(x, y, 0, getValue(uncompressed, pos, bitCount));
    }
    ImageIO.write(buffImg, "png", new File(fileName));
}
如果您没有任何文档,那么解压方法尤其需要一些反向工程。查看这两个示例中的解码数据,可能是某个使用字节宽度的运行长度编码值。有几种情况,我无法导出对我来说有意义的图像数据,但我不知道图像数据的意图,因此我可能无法正确识别未压缩的数据

abstract byte[] decodeBase64(String encoded);
abstract byte[] uncompress(byte[] compressed, int byteWidth);
abstract int getValue(byte[] uncompressed, int pos, int bitCount);

对于第一个示例,我意识到的一件事是数据长度为1600位。如果图像的宽度和高度分别为40,颜色深度为1位,则也可以对其进行解压缩,即使XML表示将对其进行压缩

查看Pillow(它是PIL的替代品)。@JosepValls谢谢,但我在Pillow/PIL中找不到任何有助于压缩和字节宽度的东西。图像数据的压缩算法和位数据字符串的编码是什么?我不知道图像数据的压缩算法。位数据字符串似乎是base64编码的。像ByteWidth这样的字段代表什么?(我假设BitWidth、BitHeight、BitCount是2D大小(以像素为单位)和颜色深度)。面积坐标也是一样。为什么Java和Python都有标签?