概述
/**
* 将bitmap转换成base64字符串
*
* @param bitmap
* @return base64 字符串
*/
public static String bitmap2String(Bitmap bitmap, int bitmapQuality) {
String string;
ByteArrayOutputStream bStream = new ByteArrayOutputStream();
bitmap.compress(CompressFormat.PNG, bitmapQuality, bStream);
byte[] bytes = bStream.toByteArray();
string = Base64.encodeToString(bytes, Base64.DEFAULT);
return string;
}
/**
* 将base64转换成bitmap图片
*
* @param string base64字符串
* @return bitmap
*/
public static Bitmap string2Bitmap(String string) {
Bitmap bitmap = null;
try {
byte[] bitmapArray;
bitmapArray = Base64.decode(string, Base64.DEFAULT);
bitmap = BitmapFactory.decodeByteArray(bitmapArray, 0,
bitmapArray.length);
} catch (Exception e) {
e.printStackTrace();
}
return bitmap;
}
public static Bitmap resource2Bitmap(int id){
return BitmapFactory.decodeResource(getResources(),id);
}
/**
* drawable转化成bitmap
* @param drawable
* @return
*/
public static Bitmap drawable2Bitmap(Drawable drawable) {
int w = drawable.getIntrinsicWidth();
int h = drawable.getIntrinsicHeight();
System.out.println("Drawable转Bitmap");
Bitmap.Config config =
drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888
: Bitmap.Config.RGB_565;
Bitmap bitmap = Bitmap.createBitmap(w, h, config);
//注意,下面三行代码要用到,否则在View或者SurfaceView里的canvas.drawBitmap会看不到图
Canvas canvas = new Canvas(bitmap);
drawable.setBounds(0, 0, w, h);
drawable.draw(canvas);
return bitmap;
}
/**
* bitmap转drawable
* @param bitmap
* @return
*/
public static Drawable bitmap2Drawable(Bitmap bitmap){
return new BitmapDrawable(bitmap);
}
/**
* bitmap转byte
* @param bitmap
* @return
*/
public static byte[] bitmap2Bytes(Bitmap bitmap){
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
return baos.toByteArray();
}
/**
* byte转bitmap
* @param b
* @return
*/
public static Bitmap bytes2Bimap(byte[] b) {
if (b.length != 0) {
return BitmapFactory.decodeByteArray(b, 0, b.length);
} else {
return null;
}
}
转载至链接:https://my.oschina.net/carbenson/blog/1923891
最后
以上就是悦耳早晨为你收集整理的Java将数字转bitmap_Bitmap转化的全部内容,希望文章能够帮你解决Java将数字转bitmap_Bitmap转化所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复