Android Bitmap指定旋转角度进行矩形裁剪

主要原理:

  1. 创建画布
  2. 创建指定位置的矩形
  3. 根据所选矩形的中心点旋转
  4. 裁剪掉bitmap的空白处(此处借鉴了这个网站的代码,感谢)

代码部分:


import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.RectF;
import android.util.Log;

import java.util.Arrays;

/**
 * @author PanXiuJin
 * @date 2021/5/24
 */
public class BitmapUtil {
    public static final String TAG = "BitmapUtil";

    /**
     * 将除了圈中的地方外的地方清空且删除
     *
     * @param bitmap
     * @param x
     * @param y
     * @param width
     * @param height
     * @param rot
     * @return
     */
    public static Bitmap clipBitMap(Bitmap bitmap, float x, float y, float width, float height, float rot) {
        float left = x;
        float top = y;
        float right = x + width;
        float bottom = y + height;
        if (bitmap == null) {
            Log.e(TAG, "输入的bitmap是空的!");
            return Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_8888);
        }
        /**
         * 初始画布和空白bitmap
         */
        Bitmap bitmap1 = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);
        Canvas targetCanvas = new Canvas(bitmap1);
        /**
         *创建bitmap画笔
         */
        Paint bitmapPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        bitmapPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
        /**
         * 创建矩形
         */
        //创建好大小位置
        RectF rect = new RectF(left, top, right, bottom);
        //画布添加框
        targetCanvas.drawRect(rect, new Paint(Paint.ANTI_ALIAS_FLAG));
        /**
         * 将非选中区域删除
         */
        //旋转画布(注意要反方向旋转)
        targetCanvas.rotate(-rot, (left + right) / 2, (top + bottom) / 2);
        //画布添加bitmap
        targetCanvas.drawBitmap(bitmap, 0, 0, bitmapPaint);
        //裁剪bitmap
        Bitmap bitmap2 = crop(bitmap1);
        return bitmap2;
    }

    /**
     * 裁剪bitmap
     *
     * @param bitmap
     * @return
     */
    public static Bitmap crop(Bitmap bitmap) {
        int height = bitmap.getHeight();
        int width = bitmap.getWidth();
        int[] empty = new int[width];
        int[] buffer = new int[width];
        Arrays.fill(empty, 0);
        int top = 0;
        int left = 0;
        int bottom = height;
        int right = width;
        for (int y = 0; y < height; y++) {
            bitmap.getPixels(buffer, 0, width, 0, y, width, 1);
            if (!Arrays.equals(empty, buffer)) {
                top = y;
                break;
            }
        }
        for (int y = height - 1; y > top; y--) {
            bitmap.getPixels(buffer, 0, width, 0, y, width, 1);
            if (!Arrays.equals(empty, buffer)) {
                bottom = y;
                break;
            }
        }
        empty = new int[height];
        buffer = new int[height];
        Arrays.fill(empty, 0);
        for (int x = 0; x < width; x++) {
            bitmap.getPixels(buffer, 0, 1, x, 0, 1, height);
            if (!Arrays.equals(empty, buffer)) {
                left = x;
                break;
            }
        }
        for (int x = width - 1; x > left; x--) {
            bitmap.getPixels(buffer, 0, 1, x, 0, 1, height);
            if (!Arrays.equals(empty, buffer)) {
                right = x;
                break;
            }
        }
        return Bitmap.createBitmap(bitmap, left, top, right - left + 1, bottom - top + 1);
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值