如何在Android中以椭圆形或面罩形裁剪图像? [英] How to crop image in oval shape or facemask shape in android?

查看:150
本文介绍了如何在Android中以椭圆形或面罩形裁剪图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在应用相机。我具有方形的捕获和裁剪图像。但是我需要椭圆形或人脸形状。怎么了?

I am doing camera application.i have capture and crop image in square shape. But i need oval shape or human face shape. How is it come ?

推荐答案

我使用了以下方法,并将捕获的位图图像传递给此方法。

I have used following method and passed my captured bitmap image to this method. And it will work.

public Bitmap getRoundedShape(Bitmap scaleBitmapImage) {
        int targetWidth = 125;
        int targetHeight = 125;

        Bitmap targetBitmap = Bitmap.createBitmap(targetWidth,
                targetHeight, Bitmap.Config.ARGB_8888);

        Canvas canvas = new Canvas(targetBitmap);
        Path path = new Path();
        path.addCircle(
                ((float) targetWidth - 1) / 2,
                ((float) targetHeight - 1) / 2,
                (Math.min(((float) targetWidth), ((float) targetHeight)) / 2),
                Path.Direction.CCW);

        canvas.clipPath(path);
        Bitmap sourceBitmap = scaleBitmapImage;
        canvas.drawBitmap(
                sourceBitmap,
                new Rect(0, 0, sourceBitmap.getWidth(), sourceBitmap
                        .getHeight()), new Rect(0, 0, targetWidth,
                        targetHeight), p);
        return targetBitmap;
    }

输出如下:-

And the output is as follows:-

这篇关于如何在Android中以椭圆形或面罩形裁剪图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆