Android使用OpenCV来实现bitmap独立设置每个圆角

这篇博客介绍了如何在Android中利用OpenCV库为Bitmap图片的四个角设置独立的圆角。通过Java方法封装了OpenCV操作,包括创建Mat矩阵,设置矩形和圆形遮罩,最后将处理后的图像转换回Bitmap。提供了详细的代码示例,并展示了调用方法。适用于需要自定义圆角效果的Android开发者。
摘要由CSDN通过智能技术生成

Android使用OpenCV来实现bitmap独立设置每个圆角,
关于opencv集成请参考我的其他文章,这里方便起见已经封装成java方法供大家调用:
代码如下:

    public static Bitmap drawCircleRadius(int w, int h, int circleR, boolean topLeft, boolean topRight, boolean bottomLeft, boolean bottomRight, Bitmap bitmap) {
        if (bitmap == null || bitmap.isRecycled() || bitmap.getWidth() == 0)
            return null;

        Mat zeros = Mat.zeros(bitmap.getHeight(), bitmap.getWidth(), CvType.CV_8UC4);
        ByteBuffer allocate = ByteBuffer.allocate(bitmap.getByteCount());
        bitmap.copyPixelsToBuffer(allocate);
        zeros.put(0, 0, allocate.array());
        allocate.clear();
        bitmap.recycle();

        Mat ones = Mat.zeros(h, w, CvType.CV_8UC4);
        Imgproc.resize(zeros, ones, new Size(w, h));

        Mat mask = Mat.zeros(h, w, CvType.CV_8UC1);
        Imgproc.rectangle(mask, new Point(circleR, 0), new Point(w - circleR, h), Scalar.all(255), -1);
        Imgproc.rectangle(mask, new Point(0, circleR), new Point(w, h - circleR), Scalar.all(255), -1);
        if (topLeft) {
            Imgproc.circle(mask, new Point(circleR, circleR), circleR, Scalar.all(255), -1);
        } else {
            Imgproc.rectangle(mask, new Point(0, 0), new Point(circleR, circleR), Scalar.all(255), -1);
        }
        if (topRight) {
            Imgproc.circle(mask, new Point(w - circleR, circleR), circleR, Scalar.all(255), -1);
        } else {
            Imgproc.rectangle(mask, new Point(w - circleR, 0), new Point(w, circleR), Scalar.all(255), -1);
        }
        if (bottomRight) {
            Imgproc.circle(mask, new Point(w - circleR, h - circleR), circleR, Scalar.all(255), -1);
        } else {
            Imgproc.rectangle(mask, new Point(w - circleR, h - circleR), new Point(w, h), Scalar.all(255), -1);
        }

        if (bottomLeft) {
            Imgproc.circle(mask, new Point(circleR, h - circleR), circleR, Scalar.all(255), -1);
        } else {
            Imgproc.rectangle(mask, new Point(0, h - circleR), new Point(circleR, h), Scalar.all(255), -1);
        }

        Mat out = Mat.zeros(h, w, CvType.CV_8UC4);
        ones.copyTo(out, mask);

        Bitmap dst = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
        Utils.matToBitmap(out, dst);

        out.release();
        mask.release();
        ones.release();
        zeros.release();
        return dst;
    }

调用是也要首先获取宽度高度才行:例如imageview 用post方法执行;

                img.post(new Runnable() {
                    @Override
                    public void run() {
                        Bitmap bitmap = drawCircleRadius(img.getWidth(), img.getHeight(), 100, false, true, false, false, BitmapFactory.decodeResource(getResources(), R.mipmap.test_bg));
                        img.setImageBitmap(bitmap);
                    }
                });

转载请注明来源,记得 关注点赞谢谢~!

效果图:
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值