【问题标题】:How to crop image in oval shape or facemask shape in android?如何在android中以椭圆形或面罩形状裁剪图像?
【发布时间】:2017-01-01 18:34:10
【问题描述】:

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

【问题讨论】:

    标签: android camera


    【解决方案1】:

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

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

    输出如下:-

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

        【解决方案3】:

        探索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    }
        

        【讨论】:

          【解决方案4】:

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

          只需在主 Activity 中包含这一行
          intent.putExtra(CropImage.CIRCLE_CROP, "circleCrop");

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

          【讨论】:

            【解决方案5】:

            尝试用这个...裁剪人脸形状

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

            【讨论】:

            • 它在询问 com.android.gallery 类
            • intent.setComponent(new ComponentName("com.android.camera" ,"com.android.camera.CropImage"));
            【解决方案6】:

            对于椭圆形,试试这个 android 函数或 download demo 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

            【讨论】:

              猜你喜欢
              • 2014-05-01
              • 2017-07-05
              • 2012-04-21
              • 1970-01-01
              • 1970-01-01
              • 2011-09-19
              • 2011-09-25
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多