Android使用OpenCV來實(shí)現(xiàn)bitmap圖片獨(dú)立設(shè)置每個(gè)圓角

Android使用OpenCV來實(shí)現(xiàn)bitmap獨(dú)立設(shè)置每個(gè)圓角,
關(guān)于opencv集成請參考我的其他文章,這里方便起見已經(jīng)封裝成java方法供大家調(diào)用:
代碼如下:

    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;
    }

調(diào)用是也要首先獲取寬度高度才行:例如imageview 用post方法執(zhí)行;

                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);
                    }
                });

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容