將Bitmap圖片轉換爲圓形,正方形,base64,並將base64轉換爲Bitmap,壓縮圖片
public class BitmapAndBase64 {
/**
* 圖片設置爲圓形
* @param bitmap
* @return
*/
public static Bitmap toRoundBitmap(Bitmap bitmap) {
int width = bitmap.getWidth();
int height = bitmap.getHeight();
float roundPx;
float left, top, right, bottom, dst_left, dst_top, dst_right, dst_bottom;
if (width <= height) {
roundPx = width / 2;
left = 0;
top = 0;
right = width;
bottom = width;
height = width;
dst_left = 0;
dst_top = 0;
dst_right = width;
dst_bottom = width;
} else {
roundPx = height / 2;
float clip = (width - height) / 2;
left = clip;
right = width - clip;
top = 0;
bottom = height;
width = height;
dst_left = 0;
dst_top = 0;
dst_right = height;
dst_bottom = height;
}
Bitmap output = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(output);
final int color = 0xff424242;
final Paint paint = new Paint();
final Rect src = new Rect((int) left, (int) top, (int) right, (int) bottom);
final Rect dst = new Rect((int) dst_left, (int) dst_top, (int) dst_right, (int) dst_bottom);
final RectF rectF = new RectF(dst);
paint.setAntiAlias(true);// 設置畫筆無鋸齒
canvas.drawARGB(0, 0, 0, 0); // 填充整個Canvas
paint.setColor(color);
// 以下有兩種方法畫圓,drawRounRect和drawCircle
// canvas.drawRoundRect(rectF, roundPx, roundPx, paint);// 畫圓角矩形,第一個參數爲圖形顯示區域,第二個參數和第三個參數分別是水平圓角半徑和垂直圓角半徑。
canvas.drawCircle(roundPx, roundPx, roundPx, paint);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));// 設置兩張圖片相交時的模式,參考http://trylovecatch.iteye.com/blog/1189452
canvas.drawBitmap(bitmap, src, dst, paint); //以Mode.SRC_IN模式合併bitmap和已經draw了的Circle
return output;
}
/**
* 圖片設置爲正方形
* @param bitmap
* @return
*/
public static Bitmap ImageCrop(Bitmap bitmap) {
int w = bitmap.getWidth(); // 得到圖片的寬,高
int h = bitmap.getHeight();
int wh = w > h ? h : w;// 裁切後所取的正方形區域邊長
int retX = w > h ? (w - h) / 2 : 0;//基於原圖,取正方形左上角x座標
int retY = w > h ? 0 : (h - w) / 2;
//下面這句是關鍵
return Bitmap.createBitmap(bitmap, retX, retY, wh, wh, null, false);
}
/**
* bitmap轉換爲Base64
* @param bitmap
* @return
*/
public static String bitmapToBase64(Bitmap bitmap) {
String result = null;
ByteArrayOutputStream baos = null;
try {
if (bitmap != null) {
baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
baos.flush();
baos.close();
byte[] bitmapBytes = baos.toByteArray();
result = Base64.encodeToString(bitmapBytes, Base64.DEFAULT);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (baos != null) {
baos.flush();
baos.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return result;
}
/**
* base64轉爲bitmap
*
* @param base64Data
* @return
*/
public static Bitmap base64ToBitmap(String base64Data) {
Bitmap bitmap = null;
try {
byte[]bitmapArray;
bitmapArray=Base64.decode(base64Data, Base64.DEFAULT);
bitmap=BitmapFactory.decodeByteArray(bitmapArray, 0, bitmapArray.length);
} catch (Exception e) {
e.printStackTrace();
}
// try {
// byte[] bitmapArray;
// bitmapArray = base64Data.getBytes("UTF-8");
// YuvImage yuvimage = new YuvImage(bitmapArray, ImageFormat.NV21, 20,
// 20, null); // 20、20分別是圖的寬度與高度
// ByteArrayOutputStream baos = new ByteArrayOutputStream();
// // 80--JPG圖片的質量[0-100],100最高
// yuvimage.compressToJpeg(new Rect(0, 0, 20, 20), 80, baos);
// byte[] jdata = baos.toByteArray();
// bitmap = BitmapFactory.decodeByteArray(jdata, 0, jdata.length);
// baos.close();
// } catch (Exception e) {
// e.printStackTrace();
// }
return bitmap;
}
//圖片壓縮
public static Bitmap compressImage(Bitmap image) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
image.compress(Bitmap.CompressFormat.JPEG, 100, baos);// 質量壓縮方法,這裏100表示不壓縮,把壓縮後的數據存放到baos中
int options = 90;
while (baos.toByteArray().length / 1024 > 100) { // 循環判斷如果壓縮後圖片是否大於100kb,大於繼續壓縮
baos.reset(); // 重置baos即清空baos
image.compress(Bitmap.CompressFormat.JPEG, options, baos);// 這裏壓縮options%,把壓縮後的數據存放到baos中
options -= 10;// 每次都減少10
}
ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray());// 把壓縮後的數據baos存放到ByteArrayInputStream中
Bitmap bitmap = BitmapFactory.decodeStream(isBm, null, null);// 把ByteArrayInputStream數據生成圖片
return bitmap;
}
}