3

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

4

6 回答 6

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

输出如下: - 图片

于 2013-12-17T07:00:24.707 回答
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;

    }
于 2013-03-04T11:23:17.637 回答
2

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

    // 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    }
于 2013-03-04T11:22:43.533 回答
2

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

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

但是你只会得到圆形,而不是椭圆形;所以@amarnathreddy 你不能用这个剪出完美的人脸;而是选择OpenCv 的 Grabcut

于 2013-12-27T07:25:48.677 回答
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);
于 2013-03-04T11:17:42.260 回答
0

对于椭圆形,请尝试此 android 功能或在此处下载演示

在此处输入图像描述

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 函数并传递图像以将椭圆形裁剪为位图图像

阅读更多

于 2017-01-01T10:34:10.790 回答