最近項目中遇到這么一個需求,需要裁剪掉圖片的透明區域。找了很久,最后確定,只能通過自己讀取Bitmap的像素點來讀取圖片的邊界來裁剪。下面記錄一下過程。
PorterDuffXfermode
最開始想的是使用PorterDuffXfermode來處理,因為這種方式其實很快的,但是,雖然這種方式可以用來處理圖片,但是無法滿足獲取圖片邊界的需求。
代碼如下:
public static Bitmap cutStickerBitmap(String name, Context context, Bitmap bitmap) {
Calendar calendar = Calendar.getInstance();
Bitmap bit = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);
bit.eraseColor(Color.WHITE);
Canvas canvas = new Canvas(bit);
Paint paint = new Paint();
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));
canvas.drawBitmap(bitmap, 0, 0, paint);
Paint paintRect = new Paint();
paintRect.setColor(Color.RED);
paintRect.setAntiAlias(true);
paintRect.setStyle(Paint.Style.STROKE);
paintRect.setStrokeWidth(4);
Rect rect = new Rect();
//畫一個框表示邊界
rect.set(0, 0, bitmap.getWidth(), bitmap.getHeight());
canvas.drawRect(rect, paintRect);
Log.i("xx", " 運行時間 cutStickerBitmap " + (Calendar.getInstance().getTimeInMillis() - calendar.getTimeInMillis()));
return bit;
}
代碼處理的時間,可以看到時間很快,可惜不符合需求:
處理的結果如下:
自己寫算法讀像素
關于這個算法,其實,我寫過兩個版本。 自己寫算法的話,需要自己評估你自己的需求和結果。一開始,為了防止圖形是不連續的,我采取的思路是從圖片四周來獲取邊界。思路如圖:
這么做的確是可以保證百分百正確的,但是這么做有一個問題,那就是因為每次得到的圖片分辨率不一樣,在有的時候處理一次需要10s以上,這實在是太慢了。
后來仔細觀察之后發現,其實不需要那么嚴格的,因為得到的所有的圖形都是連續的,所以新的算法就是從中間開始處理的。
沿著中線,先向右邊獲取邊界,再沿著中線從左邊獲取邊界,同時為了加快速度,采取的一次讀兩個像素的方式,如果要求嚴謹可以改成每次讀一個像素,算法代碼如下。
//處理貼紙
public static Bitmap cutStickerBitmap(Bitmap bitmap) {
Calendar calendar = Calendar.getInstance();
int top = 0;
int left = 0;
int right = bitmap.getWidth() - 1;
int bottom = 0;
for (int w = bitmap.getWidth() / 2; w < bitmap.getWidth(); w += 2) {
int h = 0;
for (h = 0; h < bitmap.getHeight(); h+=2) {
int color = bitmap.getPixel(w, h);
if (color != 0) {
if (top == 0) {
top = h;
}
top = Math.min(top, h);
break;
}
}
if (h >= bitmap.getHeight() - 1) {
right = w;
break;
}
}
for (int w = bitmap.getWidth() / 2 - 1; w >= 0; w -= 2) {
int h = 0;
for (h = 0; h < bitmap.getHeight(); h+=2) {
int color = bitmap.getPixel(w, h);
if (color != 0) {
if (top == 0) {
top = h;
}
top = Math.min(top, h);
break;
}
}
if (h >= bitmap.getHeight() - 1) {
left = w;
break;
}
}
for (int w = left; w <= right; w++) {
for (int h = bitmap.getHeight() - 1; h >= top; h--) {
int color = bitmap.getPixel(w, h);
if (color != 0) {
bottom = Math.max(bottom, h);
break;
}
}
}
//對邊界值做一次判斷,保證嚴謹
int x = (int) Math.max(left , 0f);
int y = (int) Math.max(top , 0f);
int w = (int) Math.min(right - left , bitmap.getWidth() - x);
int h = (int) Math.min(bottom - top , bitmap.getHeight() - y);
if (x + w > bitmap.getWidth()) {
x = 0;
w = bitmap.getWidth();
}
if (y + h > bitmap.getHeight()) {
y = 0;
h = bitmap.getHeight();
}
Log.i("xx", " 運行時間 cutStickerBitmap " + (Calendar.getInstance().getTimeInMillis() - calendar.getTimeInMillis()));
return Bitmap.createBitmap(bitmap, x, y, w, h);
}
代碼處理的時間如下: