Convert raw Android image to png -
on rooted android device, want take screenshot , convert raw format image png image save locally. far, managed access framebuffer, take screenshot , save raw image. problem when convert png format, image wrong.. bunch of white , grey lines. here's did:
public void putrawimageinarray (byte [] array, file f ) throws ioexception{ @suppresswarnings("resource") bufferedinputstream bufferedinputstream = new bufferedinputstream(new fileinputstream(f)); //the framebuffer raw image in file bufferedinputstream.read(array, 0, array.length);//read file } public void converttobitmap (byte [] rawarray) throws ioexception{ byte [] bits = new byte[rawarray.length*4]; int i; for(i=0;i<rawarray.length;i++) { bits[i*4] = bits[i*4+1] = bits[i*4+2] = (byte) ~rawarray[i]; bits[i*4+3] = -1;//0xff, that's alpha. } bitmap bm = bitmap.createbitmap(w, h, bitmap.config.argb_8888); bm.copypixelsfrombuffer(bytebuffer.wrap(bits)); file f = new file(environment.getexternalstoragedirectory(), "/pictures/picture.png"); f.createnewfile(); if (f.exists() == true) { f.delete(); } try{ outputstream fos=new fileoutputstream(f); bm.compress(compressformat.png, 100, fos); fos.close(); } catch (filenotfoundexception e) { log.d(tag, "file not found: " + e.getmessage()); } catch (ioexception e) { log.d(tag, "error accessing file: " + e.getmessage()); }
what doing wrong?
try removing
byte [] bits = new byte[rawarray.length*4]; int i; for(i=0;i<rawarray.length;i++) { bits[i*4] = bits[i*4+1] = bits[i*4+2] = (byte) ~rawarray[i]; bits[i*4+3] = -1;//0xff, that's alpha. }
and use rawarray directly
bitmap bm = bitmap.createbitmap(w, h, bitmap.config.argb_8888); bm.copypixelsfrombuffer(bytebuffer.wrap(rawarray));
and make sure color model using(bitmap.config.argb_8888) same color model of image data.
Comments
Post a Comment