The examples in this article describe the conversion implementation code between Android picture types. Share to everyone for your reference. Specifically as follows:
Android does some type of conversion when it comes to processing a picture resource, and now it's time to sort it out:
1, Drawable→bitmap
The Java code is as follows:
public static Bitmap Drawabletobitmap (drawable drawable) {Bitmap Bitmap = Bitmap. CreateBitmap (Drawable.get Intrinsicwidth (), Drawable.getintrinsicheight (), drawable.getopacity ()!= pixelformat.opaque?
Bitmap.Config.ARGB_8888:Bitmap.Config.RGB_565);
Canvas Canvas = new Canvas (bitmap);
Canvas.setbitmap (bitmap);
Drawable.setbounds (0, 0, drawable.getintrinsicwidth (), Drawable.getintrinsicheight ());
Drawable.draw (canvas);
return bitmap; public static Bitmap Drawabletobitmap (drawable drawable) {Bitmap Bitmap = Bitmap. CreateBitmap (drawable.g Etintrinsicwidth (), Drawable.getintrinsicheight (), drawable.getopacity ()!= pixelformat.opaque?
Bitmap.Config.ARGB_8888:Bitmap.Config.RGB_565);
Canvas Canvas = new Canvas (bitmap);
Canvas.setbitmap (bitmap);
Drawable.setbounds (0, 0, drawable.getintrinsicwidth (), Drawable.getintrinsicheight ());
Drawable.draw (canvas);
return bitmap;
}
2, from the resources to obtain bitmap
The Java code is as follows:
Resources res=getresources ();
Bitmap Bmp=bitmapfactory.decoderesource (res, r.drawable.pic);
Resources res=getresources ();
Bitmap Bmp=bitmapfactory.decoderesource (res, r.drawable.pic);
3, bitmap→byte[]
The Java code is as follows:
Private byte[] Bitmap2bytes (Bitmap BM) {
Bytearrayoutputstream BAOs = new Bytearrayoutputstream ();
Bm.compress (Bitmap.CompressFormat.PNG, BAOs);
return Baos.tobytearray ();
}
Private byte[] Bitmap2bytes (Bitmap BM) {
Bytearrayoutputstream BAOs = new Bytearrayoutputstream ();
Bm.compress (Bitmap.CompressFormat.PNG, BAOs);
return Baos.tobytearray ();
}
4, Byte[]→bitmap
The Java code is as follows:
Private Bitmap Bytes2bimap (byte[] b) {
if (b.length!=0) {return
Bitmapfactory.decodebytearray (b, 0, b.length);
else {return
null;
}
} Private Bitmap Bytes2bimap (byte[] b) {
if (b.length!=0) {return
Bitmapfactory.decodebytearray (b, 0, B.length) ;
}
else {return
null;
}
}
These are some of the transformations I have encountered in practice, and I will not have to look everywhere for similar problems.
I hope this article will help you with your Android program.