I wanna use android crop library(https://github.com/biokys/cropimage) with custom settings.
Making an example circle line that doesn't show on the cropped image is my goal.
First of all get the cropped image
Bitmap newbitmap = thecropedBitmap;
And once you get the newbitmap you can easily crop again but this time you are going to get the circle using the following method:
public Bitmap getCroppedBitmap(Bitmap bitmap) {
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());
paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(color);
// canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
canvas.drawCircle(bitmap.getWidth() / 2, bitmap.getHeight() / 2,
bitmap.getWidth() / 2, paint);
paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
canvas.drawBitmap(bitmap, rect, rect, paint);
//Bitmap _bmp = Bitmap.createScaledBitmap(output, 60, 60, false);
//return _bmp;
return output;
}
Add this line:
newbitmap = getCroppedBitmap(newbitmap);
Related
Hi guys I a have contact list view with Quick contact badge.It displaying square images but my requirement is to show circular quick contact badge (like below screen shot). Plz let me know how can I achieve this. Thanks in advance.
Below are two methods for creating circular images. you just have to pass bitmap image to make it circular.
/**
* To make image in a round shape. Use this method if you want to specify required height and width
*
* #param i
*/
public static Bitmap getRoundedShape(Bitmap scaleBitmapImage, int i) {
int targetWidth = i;
int targetHeight = i;
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), null);
return targetBitmap;
}
/**
* To make image in a round shape
*/
public static Bitmap getCroppedBitmap(Bitmap bitmap) {
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());
paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(color);
// canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
canvas.drawCircle(bitmap.getWidth() / 2, bitmap.getHeight() / 2,
bitmap.getWidth() / 2, paint);
paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
canvas.drawBitmap(bitmap, rect, rect, paint);
// Bitmap _bmp = Bitmap.createScaledBitmap(output, 60, 60, false);
// return _bmp;
return output;
}
Now a days, you should look into RoundedBitmapDrawable. A round drawable can easily be created by
Bitmap bitmap = <the bitmap you want to be circular>
RoundedBitmapDrawable drawable = RoundedBitmapDrawableFactory.create(getResources(), bitmap);
Don't forget (Thanks Gil SH for pointing this out):
drawable.setCornerRadius(Math.max(bitmap.getWidth(), bitmap.getHeight()) / 2.0f);
Source: https://developer.android.com/reference/android/support/v4/graphics/drawable/RoundedBitmapDrawable.html
I want to have my image in circle shape and a stroke around it.
I use this code to crop my bitmap in circle, but i don't know how to put a border/stroke around it.
public static Bitmap getCroppedBitmap(Bitmap bitmap) {
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());
paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(color);
// canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
canvas.drawCircle(bitmap.getWidth() / 2, bitmap.getHeight() / 2,
bitmap.getWidth() / 2, paint);
paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
canvas.drawBitmap(bitmap, rect, rect, paint);
//Bitmap _bmp = Bitmap.createScaledBitmap(output, 60, 60, false);
//return _bmp;
return output;
}
Paint paintStroke = new Paint(Paint.ANTI_ALIAS_FLAG); // create anti-aliased Paint
paintStroke.setStyle(Paint.Style.STROKE); // define paint style as Stroke
paintStroke.setStrokeWidth(1f); // set stroke widht to 1 px
paintStroke.setColor(0xFF00FF00); // set color
canvas.drawArc(rect, 0f, 360f, true, paintStroke); // draw arch from 0 to 360 degrees (i. e. closed circle)
I want to save the canvas in a bitmap so I could draw that bitmap on other canvas.
the problem is that i dont know how to set the bitmap size to be the whole canvas(not the whole screen, the size of a layout of mine)
how do i set the bitmap size? and then how do i save my canvas on it?
Thanks for the help :D
I use this procedure and you can set the width and height of your view instead of bitmap.getWidth() and bitmap.getHeight() as here I am overriding a bitmap
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 = pixels;
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);
Hope this helps :)
I have a demo which first takes an image from camera than ask user to crop the image.Cropping screen have a rectangle box in middle which on touch of the border of the 4 faces displays 4 arrows at midpoint of the faces to change the size of the box as user wants.As it is the inbuilt mechanism provided by using intent com.android.camera.action.CROP.
I want to make the same thing but for circular window.Which crops circular images.
Do any one have previous experience with this kind of cropping functionality.
Please help me out.
Have a look at this function...
Is this what you require??
public Bitmap getCroppedBitmap(Bitmap bitmap) {
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());
paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(color);
// canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
canvas.drawCircle(bitmap.getWidth() / 2, bitmap.getHeight() / 2,
bitmap.getWidth() / 2, paint);
paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
canvas.drawBitmap(bitmap, rect, rect, paint);
//Bitmap _bmp = Bitmap.createScaledBitmap(output, 60, 60, false);
//return _bmp;
return output;
}
Check this link.. crop image
If you are using Intents.. try using this
intent.setType("image/*");
intent.putExtra("crop", "true");
intent.putExtra("scale", scale);
intent.putExtra("return-data", return_data);
intent.putExtra("circleCrop", true);
I use the method quoted below to round the corners of any bitmap. Some time it works perfectly, some time not because of OutOfMemory exception. The exception often occur at the line
Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),
bitmap.getHeight(), Config.ARGB_8888);
Do I miss any special codes here? Please help me.
public static Bitmap roundBitmap(Bitmap bitmap, int roundedRadius) {
if (bitmap == null)
return null;
if (roundedRadius == 0) {
return bitmap;
}
// Bitmap output = bitmap.copy(Config.ARGB_8888, true);
Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),
bitmap.getHeight(), Config.ARGB_8888);
Canvas canvas = new Canvas(output);
final int color = 0xffffff00;
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 = roundedRadius;
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);
return output;
}
If you don't need the original bitmap after calling roundBitmap() you should call bitmap.recycle() to free the memory used by bitmap.
Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),
bitmap.getHeight(), new BitmapFactory.Options().inSampleSize=4);
Try above code to create bitmap.