Android-Image-Cropper icon indicating copy to clipboard operation
Android-Image-Cropper copied to clipboard

saving circular image with oval is not working (possible solve)

Open symphonyrecords opened this issue 7 years ago • 2 comments

library version: # 2.8.0

Modify BitmapCroppingWorkerTask class as below:

Find this:

Bitmap bitmap = BitmapUtils.resizeBitmap(bitmapSampled.bitmap, mReqWidth, mReqHeight, 
mReqSizeOptions);

Add below lines after:

Bitmap bitmap = BitmapUtils.resizeBitmap(bitmapSampled.bitmap, mReqWidth, mReqHeight, 
mReqSizeOptions);

if (mCropImageViewReference.get().getCropShape() == CropImageView.CropShape.OVAL) {
    bitmap = getCircularBitmap(bitmap);
}

getCircularBitmap

public static Bitmap getCircularBitmap(Bitmap square) {
    if (square == null) return null;
    Bitmap output = Bitmap.createBitmap(square.getWidth(), square.getHeight(), Bitmap.Config.ARGB_8888);

    final Rect rect = new Rect(0, 0, square.getWidth(), square.getHeight());
    Canvas canvas = new Canvas(output);

    int halfWidth = square.getWidth() / 2;
    int halfHeight = square.getHeight() / 2;

    final Paint paint = new Paint();
    paint.setAntiAlias(true);
    paint.setFilterBitmap(true);

    canvas.drawCircle(halfWidth, halfHeight, Math.min(halfWidth, halfHeight), paint);
    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
    canvas.drawBitmap(square, rect, rect, paint);
    return output;
}

save as PNG if you don't want the unwanted black background

Let me know if it works.

symphonyrecords avatar Jan 15 '19 18:01 symphonyrecords

issue is still not fixed in latest library update, will try you suggestion to modify the code.

paktech1 avatar Sep 04 '21 16:09 paktech1

your idea is cool but failed in case of Oval shape, this will be the code to crop oval shape.

public static Bitmap getOvalBitmap(Bitmap bitmap) { Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(output); final int color = 0xffff0000; final Paint paint = new Paint(); final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());// w ww .j ava 2s. co m final RectF rectF = new RectF(rect); paint.setAntiAlias(true); paint.setDither(true); paint.setFilterBitmap(true); canvas.drawARGB(0, 0, 0, 0); paint.setColor(color); canvas.drawOval(rectF, paint); paint.setColor(Color.BLUE); paint.setStyle(Paint.Style.STROKE); paint.setStrokeWidth((float) 4); paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN)); canvas.drawBitmap(bitmap, rect, rect, paint); return output; }

paktech1 avatar Sep 05 '21 00:09 paktech1