如何在android中裁剪椭圆形或面罩形状的图像?

时间:2013-03-04 11:12:59

标签: android camera

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

6 个答案:

答案 0 :(得分:6)

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

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

输出如下: - Image

答案 1 :(得分:4)

我在我的一个项目中使用了以下内容。可能这对你有帮助。

public  Drawable getRoundedCornerImage(Drawable bitmapDrawable) {
        Bitmap bitmap = ((BitmapDrawable)bitmapDrawable).getBitmap();
        Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),
                bitmap.getHeight(), Config.ARGB_8888);
        Canvas canvas = new Canvas(output);

        final int color = 0xff424242;
        final Paint paint = new Paint();
        final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
        final RectF rectF = new RectF(rect);
        final float roundPx = 10;

        paint.setAntiAlias(true);
        canvas.drawARGB(0, 0, 0, 0);
        paint.setColor(color);
        canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
        paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
        canvas.drawBitmap(bitmap, rect, rect, paint);
        Drawable image = new BitmapDrawable(output);
        return image;

    }

答案 2 :(得分:2)

探索com.android.camera.CropImage.java sources。它可以裁剪圆形图像。

    // if we're circle cropping we'll want alpha which is the third param here
    464    mCroppedImage = Bitmap.createBitmap(width, height,
    465                    mCircleCrop ?
    466                            Bitmap.Config.ARGB_8888 :
    467                            Bitmap.Config.RGB_565);
    468    Canvas c1 = new Canvas(mCroppedImage);
    469    c1.drawBitmap(mBitmap, r, new Rect(0, 0, width, height), null);
    470
    471    if (mCircleCrop) {
    472        // OK, so what's all this about?
    473        // Bitmaps are inherently rectangular but we want to return something
    474        // that's basically a circle.  So we fill in the area around the circle
    475        // with alpha.  Note the all important PortDuff.Mode.CLEAR.
    476        Canvas c = new Canvas (mCroppedImage);
    477        android.graphics.Path p = new android.graphics.Path();
    478        p.addCircle(width/2F, height/2F, width/2F, android.graphics.Path.Direction.CW);
    479        c.clipPath(p, Region.Op.DIFFERENCE);
    480
    481        fillCanvas(width, height, c);
    482    }

答案 3 :(得分:2)

@vokilam你是对的;我刚刚探索了代码并找到了一种解决方法......

只需在主活动中包含此行 intent.putExtra(CropImage.CIRCLE_CROP, "circleCrop");

但你只会得到圆圈,而不是椭圆形;所以@amarnathreddy你不能用这个切割完美的人脸;而是去Grabcut of OpenCv

答案 4 :(得分:1)

尝试使用此...以人脸形状裁剪

Uri ImageCaptureUri = Uri.fromFile(new File("filepath"); 
Intent intent = new Intent("com.android.camera.action.CROP"); 
intent.setType("image/*"); 
intent.setData(ImageCaptureUri); 
intent.putExtra("outputX", 200); 
intent.putExtra("outputY", 200); 
intent.putExtra("aspectX", 1); 
intent.putExtra("aspectY", 1); 
intent.putExtra("scale", true); 
intent.putExtra("return-data", true); 
startActivityForResult(intent, 1);

答案 5 :(得分:0)

对于椭圆形状,请尝试此Android功能或 download demo here

enter image description here

public static Bitmap getOvalCroppedBitmap(Bitmap bitmap, int radius) {
        Bitmap finalBitmap;
        if (bitmap.getWidth() != radius || bitmap.getHeight() != radius)
            finalBitmap = Bitmap.createScaledBitmap(bitmap, radius, radius,
                    false);
        else
            finalBitmap = bitmap;
        Bitmap output = Bitmap.createBitmap(finalBitmap.getWidth(),
                finalBitmap.getHeight(), Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(output);

        Paint paint = new Paint();
        final Rect rect = new Rect(0, 0, finalBitmap.getWidth(),
                finalBitmap.getHeight());

        paint.setAntiAlias(true);
        paint.setFilterBitmap(true);
        paint.setDither(true);
        canvas.drawARGB(0, 0, 0, 0);
        paint.setColor(Color.parseColor("#BAB399"));
        RectF oval = new RectF(0, 0, 130, 150);
        canvas.drawOval(oval, paint);
        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
        canvas.drawBitmap(finalBitmap, rect, oval, paint);

        return output;
    }

上述函数以编程方式在android中创建一个统一的椭圆形状。 调用你的函数onCreate函数并将图像传递给裁剪椭圆形作为位图图像

<强> Read more