I want to crop a picture in current window,maybe circle or rectangle.
Here are my code.I first get the whole picture in current window.Then to crop circle or rectangle.
private Bitmap getBitmap()
{
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
View v1 = getWindow().getDecorView().getRootView();
v1.setDrawingCacheEnabled(true);
Bitmap mBackgroundBitmap = Bitmap.createBitmap(v1.getDrawingCache());
v1.setDrawingCacheEnabled(false);
mRect = ChooseView.cropRect; // the mRect is the crop area
int width = mRect.width();
int height = mRect.height();
Bitmap whiteBgBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(whiteBgBitmap);
Rect dstRect = new Rect(0, 0, width, height);
if(!TextUtils.isEmpty(CutParam.shape) && CutParam.shape.equals(CutParam.RECTANGLE))
{
canvas.drawColor(Color.WHITE);
canvas.drawBitmap(mBackgroundBitmap, mRect, dstRect, null);
}
else
{
final Paint paint = new Paint();
paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
canvas.drawCircle(mRect.left + mRect.width() / 2, mRect.top + mRect.height() / 2, CutParam.radius, paint);
paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
canvas.drawBitmap(mBackgroundBitmap, mRect, dstRect, null);
}
return whiteBgBitmap;
}
The rectangle work will.But when I want to crop a circle,it will be a black image. I am fresh at Android.And I have read Cropping circular area from bitmap in Android .But I can't solve my question.Can you help me?
PS:If crop a circle ,the mRect will be a square.
Try this one from lvillani on github. It's simple, and gets you out from the muddy world of pictures in android.
Related
`
There is an requirement in my project to set any bitmap into mug and also edit bitmap and wright some text on image then past this bitmap to mug just like attached image .
I need a solution in Android or any supported library, Before I have tried to do this with Android Canvas and its method to draw arc But not reached my requirement.
/**
* Draw one image over other using the canvas paint and path drawing.
*/
public Bitmap createMaskedImageInImageCenterRightMug(Drawable back,
Bitmap bitmapToDrawInTheCenter) {
Bitmap backgroundBitmap = ((BitmapDrawable) back).getBitmap();
int hieghtBack = backgroundBitmap.getHeight();
int widthBack = backgroundBitmap.getWidth();
int hieghtFront = bitmapToDrawInTheCenter.getHeight();
int widthFront = bitmapToDrawInTheCenter.getWidth();
int widthToDrawOnMug = widthBack / 2;
backgroundBitmap = Bitmap.createScaledBitmap(backgroundBitmap, (int) canvas_width, hieghtBack, true);
// Create mask
Bitmap backgroundBitmapMask = Bitmap.createBitmap(backgroundBitmap, 7, 0, (int) (canvas_width / 2), hieghtBack);
if (widthToDrawOnMug <= widthFront) {
bitmapToDrawInTheCenter = Bitmap.createBitmap(
bitmapToDrawInTheCenter, (widthFront * 40) / 100, 0,
(widthFront * 60) / 100, hieghtFront);
}
Bitmap backgroundBitmapScaledMask = Bitmap.createScaledBitmap(
backgroundBitmapMask, widthToDrawOnMug, hieghtBack - 50, true);
bitmapToDrawInTheCenter = Bitmap.createScaledBitmap(
bitmapToDrawInTheCenter, backgroundBitmapScaledMask.getWidth(),
backgroundBitmapScaledMask.getHeight(), true);
Bitmap resultBitmap = Bitmap.createBitmap(
backgroundBitmapScaledMask.getWidth(),
backgroundBitmapScaledMask.getHeight(),
backgroundBitmapScaledMask.getConfig());
Canvas canvas = new Canvas(resultBitmap);
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
canvas.drawBitmap(backgroundBitmapScaledMask, 0, 0, null);
canvas.drawBitmap(bitmapToDrawInTheCenter, 0, 0, paint);
return resultBitmap;
}
/**
* Draw one image over other using the canvas paint and path drawing.
*/
public Bitmap pasteOverMugForRightMug(Drawable back,
Bitmap bitmapToDrawInTheCenter) {
Bitmap backgroundBitmap = ((BitmapDrawable) back).getBitmap();
Bitmap resultBitmap = Bitmap.createBitmap(backgroundBitmap.getWidth(), backgroundBitmap.getHeight(), backgroundBitmap.getConfig());
Canvas canvas = new Canvas(resultBitmap);
canvas.drawBitmap(backgroundBitmap, new Matrix(), null);
Paint paint = new Paint();
canvas.drawBitmap(bitmapToDrawInTheCenter, 0, 25, paint);
return resultBitmap;
}
Here is my code . Some hard code values are used according plain Mug image Aspect Ratio.
Also Tried to do this with Open GL but it was very complex .
And and could not find method for cylinder shape in one of the image processing library Image magic.
if any one have an idea please share .
I'm currently drawing a rectangle under a bitmap using the Canvas in Android. I am using the following code
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
Bitmap bitmap = BitmapFactory.decodeFile(file.getPath(), options);
Bitmap b = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight() + 100, Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(b);
c.drawBitmap(bitmap, 0, 0, null);
Paint myPaint = new Paint();
myPaint.setColor(Color.rgb(0, 0, 0));
myPaint.setStrokeWidth(10);
Rect r = new Rect(0, bitmap.getHeight(), bitmap.getWidth(), bitmap.getHeight() + 100);
c.drawRect(r, myPaint);
Paint textPaint = new Paint();
textPaint.setTextSize(20);
textPaint.setColor(Color.WHITE);
textPaint.setTextAlign(Paint.Align.CENTER);
int width = r.width();
int numOfChars = textPaint.breakText(description,true,width,null);
int start = (altText.length()-numOfChars)/2;
c.drawText(description,start,start+numOfChars,r.exactCenterX(),r.exactCenterY(),textPaint);
iv.setImageBitmap(b);
The bitmap draws fine and the rectangle draws directly below it. The text draws centered inside the rectangle. My issue is that the text (which varies in length) draws outside the rectangle. What I would like to happen is have the text skip to a new line when it reaches the end of the rectangle (which is the same width as the bitmap)
This a xkcd comic reader, the alt text is the text I am trying to fit in the rectangle, the image is the comic itself which varies in width (and height)
I have a square bitmap being displayed underneath a semi-transparent circle. The user can touch and drag the bitmap to position it. I want to be able to crop what ever part of the bitmap is under the circle. How can I do this?
have a look at RoundedBitmapDrawable in the support library
all you have to do is give it the bitmap and the corner radius
RoundedBitmapDrawable img = RoundedBitmapDrawableFactory.create(getResources(),bitmap);
img.setCornerRadius(radius);
imageView.setImageDrawable(img);
You Can make your imageview circular using RoundedBitmapDrawable
here is the code for achieving roundedImageview:
ImageView profilePic=(ImageView)findViewById(R.id.user_image);
//get bitmap of the image
Bitmap imageBitmap=BitmapFactory.decodeResource(getResources(), R.drawable.large_icon);
RoundedBitmapDrawable roundedBitmapDrawable=
RoundedBitmapDrawableFactory.create(getResources(), imageBitmap);
roundedBitmapDrawable.setCornerRadius(50.0f);
roundedBitmapDrawable.setAntiAlias(true);
profilePic.setImageDrawable(roundedBitmapDrawable);
You can use the power of PorterDuff to get your bitmap in any shape or path...
Here is an example:
public static Bitmap getCircular(Bitmap bm, int cornerRadiusPx) {
int w = bm.getWidth();
int h = bm.getHeight();
int radius = (w < h) ? w : h;
w = radius;
h = radius;
Bitmap bmOut = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bmOut);
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setColor(0xff424242);
Rect rect = new Rect(0, 0, w, h);
RectF rectF = new RectF(rect);
canvas.drawARGB(0, 0, 0, 0);
canvas.drawCircle(rectF.left + (rectF.width()/2), rectF.top + (rectF.height()/2), radius / 2, paint);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
canvas.drawBitmap(bm, rect, rect, paint);
return bmOut;
}
Here is a link to a sample project. It has a transparent square over an image. You can pinch zoom or drag the bottom image and can corp it.
https://github.com/tcking/ImageCroppingView.
The square is made by using canvas. you can change it to any shape as u desired by changing canvas. Hop it helps you.
please help me to draw a text with rounded rectangle as background. I have to draw many texts on a canvas and the text has rounded background. SO what I am trying is to write a function "createTextBitmap" which return a bitmap image so that we can draw image(which return by the function) on a main canvas.
the 'createTextBitmap'function can return a created bitmap, the bitmap image is the one,which contains the text with rounded edge background...
i have tried one, which gives below.
private Bitmap ProcessingBitmap(String text,Paint paint, boolean lastPoint){
Bitmap bm1 = null;
Bitmap newBitmap = null;
Rect bounds = new Rect();
paint.getTextBounds(text, 0, text.length(), bounds);
float width = bounds.width();
float height =bounds.height();
float radius;
if (width > height){
radius = height/4;
}else{
radius = width/4;
}
Paint paint1 = new Paint();
paint1.setColor(Color.GREEN);
paint1.setStrokeWidth(5);
paint1.setStyle(Paint.Style.FILL);
float center_x, center_y;
center_x = width/4;
center_y = height/4;
final RectF rect = new RectF();
rect.set(center_x - radius,
center_y - radius,
center_x + radius,
center_y + radius);
Canvas canvas2 = new Canvas();
canvas2.drawRoundRect(rect, 0, 0, paint);
canvas2.drawText(text, 0, 0, paint);
return newBitmap;
}
and my question is How can we convert this canvas2 to a bitmap image? and image has the size of text bounds,
which look like
to convert your canvas into bitmap please do the following :
public Bitmap convertCanvasToBitmap(int width , int height) {
Bitmap drawnBitmap = null;
try {
drawnBitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);
Canvas canvas = new Canvas(drawnBitmap);
// now draw anything you want to the canvas
} catch (Exception e) {
e.printStackTrace();
}
return drawnBitmap;
}
so the idea is just pass the bitmap to the canvas , draw with the canvas it will be drawn into your bitmap .
and please refer to this answer here to see how to deal with the text size in the bitmap .
and please give me some feedback
Hope that helps .
you can create a bitmap, then call draw on that bitmap, something like this:
newBitmap = Bitmap.createBitmap(rect.width, rect.height, Bitmap.Config.ARGB_8888);
Canvas canvas2 = new Canvas(newBitmap);
I am doing camera application.i have capture and crop image in square shape. But i need oval shape or human face shape. How is it come ?
I have used following method and passed my captured bitmap image to this method. And it will work.
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;
}
And the output is as follows:-
I used following in one of my project. May be this helps you.
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;
}
Explore com.android.camera.CropImage.java sources. It can crop circle images.
// 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 }
#vokilam you are right; I just explored into code and found a way to work around...
Just include this line in the main Activity
intent.putExtra(CropImage.CIRCLE_CROP, "circleCrop");
But you will only get circles, not oval; so #amarnathreddy you cannot cut perfect human face with this; instead go for Grabcut of OpenCv
Try with this ...to crop in human face shape
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);
For an oval shape try this android function or 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;
}
The above function creates a uniform oval shape in android programmatically.
call your function onCreate function and pass the image to crop oval shape as a bitmap image
Read more