Thursday 15 July 2010

java - Android parse base64Binary pgm to Bitmap -



java - Android parse base64Binary pgm to Bitmap -

i need convert base64binary-string in format pgm bitmap in android. don't have usual base64-encoded bitmap. base64binary-string came xml file

<referenceimage type="base64binary" format="pgm" widthpx="309" heightpx="233" bytesperpixel="1" > ndy4ojo9qefdruvhrkllte9oufftu1vwv1hzwltzwvlzwlpbw1xdxmbgymjjzgnlzwrkzgrlzmznz2znawpqa21ub29ubm9vb3bwchbxchfyc3fzcnjzcnjydh[...]vlaw1xbwltcxfxcxfxd.

pattern.compile("<referenceimage .*>((?s).*)<\\/referenceimage>"); ... string sub = r; //base64binary string pattern-matched xml file byte[] decodedstring = base64.decode(sub.getbytes(), base64.no_wrap); //probably wrong decoding (needs ascii binary?) bitmap decodedbyte = bitmapfactory.decodebytearray(decodedstring, 0, decodedstring.length); //always null due wrong byte-array

i think understand pgm images typically stored ascii (like in xml) or binary (0..255). think base64.decode needs binary-variant, not ascii have. bitmapfactory.decodebytearray doesn't understand decoded byte-array , returns null.

so how can convert base64binary-pgm-ascii-string valid byte-array in order create valid bitmap?

i think base64-decoding fine. android's bitmapfactory has no direct back upwards pgm format. i'm not sure how add together back upwards it, seems create bitmap using 1 of createbitmap(...) mill methods quite easily.

see pgm spec details on how parse header, or see my implementation java se (if around, you'll find class supports ascii reading, if needed).

could there's no header, , can height/width xml. in case, dataoffset 0 below.

when header parsed, know width, height , image info starts:

int width, height; // header int dataoffset; // == end of header // create pixel array, , expand 8 bit grayness argb_8888 int[] pixels = new int[width * height]; (int y = 0; y < height; y++) { (int x = 0; x < width; x++) { int grayness = decodedstring[dataoffset + i] & 0xff; pixels[i] = 0xff000000 | grayness << 16 | grayness << 8 | gray; } } bitmap pgm = bitmap.createbitmap(metrics, pixels, width, height, bitmapconfig.config. argb_8888);

java android bitmap base64 pgm

No comments:

Post a Comment