美文网首页
Android使用OpenCV来实现bitmap图片独立设置每个

Android使用OpenCV来实现bitmap图片独立设置每个

作者: 一航jason | 来源:发表于2021-08-24 10:31 被阅读0次

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

    相关文章

      网友评论

          本文标题:Android使用OpenCV来实现bitmap图片独立设置每个

          本文链接:https://www.haomeiwen.com/subject/vycdiltx.html