Java Android将Base64二进制pgm解析为位图

Java Android将Base64二进制pgm解析为位图,java,android,bitmap,base64,pgm,Java,Android,Bitmap,Base64,Pgm,我需要将pgm格式的Base64二进制字符串转换为android中的位图。所以我没有一个普通的base64编码位图。 base64binary字符串来自xml文件 NDY4OJO9QEFDRUVHRKLLTE9OUFFTU1VWV1HZWLTZWVLZWLPBW1XDxMBGYMJZGNLZWRKZGRLZZZNZ2ZNAWPQA21UB29UBM9VB3BWCHBxCHFYC3FZCNJZNJYDH[…]Vlaw1xBWLTCxFxCxFxFxFxFxFxFxCxFxFxFxFxFxHYDH

我需要将pgm格式的Base64二进制字符串转换为android中的位图。所以我没有一个普通的base64编码位图。 base64binary字符串来自xml文件


NDY4OJO9QEFDRUVHRKLLTE9OUFFTU1VWV1HZWLTZWVLZWLPBW1XDxMBGYMJZGNLZWRKZGRLZZZNZ2ZNAWPQA21UB29UBM9VB3BWCHBxCHFYC3FZCNJZNJYDH[…]Vlaw1xBWLTCxFxCxFxFxFxFxFxFxCxFxFxFxFxFxHYDHYDH[…]VW1xWWWW

Pattern.compile("<ReferenceImage .*>((?s).*)<\\/ReferenceImage>");
...
String sub = r; //base64binary string pattern-matched from xml file
byte[] decodedString = Base64.decode(sub.getBytes(), Base64.NO_WRAP); //probably wrong decoding (needs to be ASCII to binary?)
Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length); //always null due to wrong byte-array
Pattern.compile((?s.*)”;
...
字符串sub=r//从xml文件匹配的Base64二进制字符串模式
byte[]decodedString=Base64.decode(sub.getBytes(),Base64.NO_WRAP)//可能解码错误(需要将ASCII转换为二进制?)
位图decodedByte=BitmapFactory.decodeByteArray(decodedString,0,decodedString.length)//由于字节数组错误,始终为空
我想我知道pgm图像通常存储为ASCII(就像在我的xml中一样)或二进制(0..255)。我还认为
Base64.decode
需要二进制变量,而不是我所拥有的ASCII码。 但是,
BitmapFactory.decodeByteArray
不理解解码的字节数组并返回null


那么,如何将我的Base64二进制pgm ASCII字符串转换为有效的字节数组以创建有效的位图?

我认为您的Base64解码很好。但是Android的
BitmapFactory
可能不直接支持PGM格式。我不知道如何添加对它的支持,但似乎可以使用
createBitmap(…)
工厂方法之一轻松创建
Bitmap

有关如何解析标头的详细信息,请参阅,或者请参阅(如果您环顾四周,您还会发现一个支持ASCII读取的类,如果需要的话)

也可能是没有标题,可以从XML中获取高度/宽度。在这种情况下,
dataOffset
将位于下面的
0

解析标头时,您知道宽度、高度以及图像数据的起始位置:

int width, height; // from header
int dataOffset; // == end of header

// Create pixel array, and expand 8 bit gray to ARGB_8888
int[] pixels = new int[width  * height];
for (int y = 0; y < height; y++) {
    for (int x = 0; x < width; x++) {
         int gray = decodedString[dataOffset + i] & 0xff;
         pixels[i] = 0xff000000 | gray << 16 | gray << 8 | gray;
    }
}

Bitmap pgm = Bitmap.createBitmap(metrics, pixels, width, height, BitmapConfig.Config. ARGB_8888);
int-width,height;//从页眉
int dataOffset;//==收割台末端
//创建像素阵列,并将8位灰度扩展为ARGB_8888
int[]像素=新int[宽度*高度];
对于(int y=0;y
我在您的代码中发现了一个小错误,索引I从未初始化或递增。
我更正了您的代码并进行了测试,以下是我的代码:

private static Bitmap getBitmapFromPgm(byte[] decodedString, int width, int height, int dataOffset){

    // Create pixel array, and expand 8 bit gray to ARGB_8888
    int[] pixels = new int[width  * height];
    int i = 0;
    for (int y = 0; y < height; y++) {
        for (int x = 0; x < width; x++) {
            int gray = decodedString[dataOffset + i] & 0xff;
            pixels[i] = 0xff000000 | gray << 16 | gray << 8 | gray;

            i++;
        }
    }
    Bitmap pgm = Bitmap.createBitmap(pixels, width, height, android.graphics.Bitmap.Config.ARGB_8888);
    return pgm;
}
私有静态位图getBitmapFromPgm(字节[]decodedString,int-width,int-height,int-dataOffset){
//创建像素阵列,并将8位灰度扩展为ARGB_8888
int[]像素=新int[宽度*高度];
int i=0;
对于(int y=0;y