ArthurHub / ArthurHub/Android-Image-Cropper

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

Open
#642 2 comments 0 reactions 0 assignees View on GitHub
Dominant language
Java
Stars
6.4k
Forks
1.4k
PR merge metrics
No merged PRs in 30d

Description

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.

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.