I am having canvas which will generate rectangle co-ordinates in image.
In this image need to draw rectangle shape on above Icon using canvas co-ordinates
My sample code
#Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
Log.e("PICASA", "Loaded");
setImageBitmap(bitmap);
Bitmap drawableBitmap = bitmap.copy(Bitmap.Config.ARGB_8888, true);
Canvas canvas = new Canvas(drawableBitmap);
List<ClickableArea> clickableAreas = IBSTFragment.getClickableAreas();
for (ClickableArea clickArea : clickableAreas) {
Paint paint = new Paint();
paint.setColor(Color.TRANSPARENT);
paint.setStyle(Paint.Style.FILL);
int x1 = clickArea.getX();
int y1 = clickArea.getY();
int w = clickArea.getW();
int h = clickArea.getH();
Rect rect = new Rect(x1, y1, w, h);
// FILL
canvas.drawRect(rect, paint);
paint.setStrokeWidth(10);
paint.setColor(Color.BLACK);
paint.setStyle(Paint.Style.STROKE);
canvas.drawRect(rect, paint);
rect.width();
rect.height();
}
setImageBitmap(drawableBitmap);
}
My Co-ordinates
X Y Width Height
600, 100, 50, 50
440, 125, 50, 50
685, 270, 50, 50
420, 350, 50, 50
370, 245, 50, 50
170, 280, 50, 50
30, 280, 50, 50
570, 250, 50, 50
I got output like this
You are copying the Bitmap into memory and never displaying, draw on the one that you set as ImageResource:
setImageBitmap(bitmap);
Canvas canvas = new Canvas(bitmap);
If you need to copy as ARGB_8888 then
Bitmap drawableBitmap = bitmap.copy(Bitmap.Config.ARGB_8888, true);
Canvas canvas = new Canvas(drawableBitmap);
//Draw everything, then after (at end of method)
setImageBitmap(drawableBitmap);
As for your coordinates:
X Y Width Height
600, 100, 50, 50
440, 125, 50, 50
685, 270, 50, 50
420, 350, 50, 50
...
They are (X,Y,W,H) while Android Rectangles are (L,T,R,B)
To convert your coordinates use:
Rectangle area = new Rectangle(x, y, x + w, y + h);
Then draw the area into the Canvas.
Related
I need to draw stroke rectangle with rounded corners.
here is my code:
mLinePaint = new Paint();
mLinePaint.setColor(mDotColorTouched);
mLinePaint.setAntiAlias(true);
mLinePaint.setStrokeWidth(mLineWidth);
mLinePaint.setStyle(Paint.Style.STROKE);
mLinePaint.setStrokeCap(Paint.Cap.ROUND);
bitmap = Bitmap.createBitmap(300,
300, Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(bitmap);
RectF r = new RectF(0, 0, 300 , 300);
c.drawRoundRect(r, 30, 30, mLinePaint);
but as you can see the line width is bigger in the corners...
any idea why it happens and how to fix this?
here is screen
Try this set of code it's working.
Paint paint = new Paint();
paint.setStyle(Paint.Style.STROKE);
paint.setColor(Color.BLUE);
paint.setStrokeWidth(10);
Bitmap b = Bitmap.createBitmap(300, 300, Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(b);
RectF rectF = new RectF();
rectF.set(5,5,250,250);
c.drawRoundRect(rectF, 10, 10, paint);
imgView.setImageBitmap(b);
I'am trying to draw a circular bitmap into a canvas. The bitmap gets clipped correctly if the canvas i want to draw on is transparent, but not if i have drawn a rectangle before with a different color.
Heres what i got
paint.setColor(col.colors_Form[1]);
mCanvas.drawRect(0, 0, dim.titleContainer_Width, dim.titleContainer_Height, paint);
[...]
if (!mFormValues_BasicInformation.get(0).getAvatarImage().equals("")) {
mCanvas.save();
mCanvas.translate(300, 300);
byte[] byteArray = mFormValues_BasicInformation.get(0).getAvatarImage();
Bitmap bmp = Bitmap.createScaledBitmap(BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length), 250, 250, false);
final int color = col.colors_Form[1];
final Paint avatarPaint = new Paint();
final Rect rect = new Rect(0, 0, bmp.getWidth(),
bmp.getHeight());
avatarPaint.setAntiAlias(true);
mCanvas.drawARGB(0, 0, 0, 0);
avatarPaint.setColor(color);
mCanvas.drawCircle(bmp.getWidth() / 2,
bmp.getHeight() / 2, bmp.getWidth() / 2, paint);
avatarPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
mCanvas.drawBitmap(bmp, rect, rect, avatarPaint);
bmp.recycle();
mCanvas.restore();
}
Does anybody know how to fix that? Thx in advance! :)
BitmapShader did the trick:
[...]
byte[] byteArray = mFormValues_BasicInformation.get(0).getAvatarImage();
Bitmap bmp = Bitmap.createScaledBitmap(BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length), 500, 500, false);
final Paint avatarPaint = new Paint();
BitmapShader shader = new BitmapShader(bmp, BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP);
avatarPaint.setShader(shader);
avatarPaint.setAntiAlias(true);
float r = Math.min(bmp.getWidth(), bmp.getHeight()) / 2f;
mCanvas.drawCircle(r, r, r, avatarPaint);
bmp.recycle();
[...]
Additional Information:
CircleTranform
I'm trying to draw a half circle inside the ImageView but i can't control where draw it. The idea is to draw it inside another circle with another color using drawcircle (50, 50, 20, paint). This is the code i'm using:
<ImageView android:id="#+id/circle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="10sp" />
Bitmap bmp = Bitmap.createBitmap(100, 70, Bitmap.Config.RGB_565);
ImageView circle = (ImageView) findViewById (R.id.circle);
Paint paint = new Paint();
paint.setColor(Color.RED);
Canvas canvas = new Canvas (bmp);
RectF rectf = new RectF (0, 0, 40, 40);
canvas.drawArc(rectf, 0, 180, true, paint);
Thanks.
I think the problem you are having is: you do not understand the two different representations that is used by Circle and Arc.
For Circle, you need to give it the x, y position of the center, and the radius.
but for the Arc, you need the 4 edges of the container.
so it should look something like this:
//setting for Circle
paint.setColor(Color.RED);
int xPosition=50;
int yPosition=50;
int size = 40;
canvas.drawCircle(xPosition, yPosition, size/2, paint);
//setting for Arc
paint.setColor(Color.YELLOW);
size = size * 8 / 10; // make the Arc smaller
xPosition=xPosition-size/2;
yPosition=yPosition-size/2;
RectF rectf = new RectF (xPosition, yPosition, xPosition+size, yPosition+size);
canvas.drawArc(rectf, 0+45, 180, true, paint);
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
In my application I used canvas to draw the text. now I want to set the width of the draw text. I can set only x , y position. I can't set width and height.
My problem
update
for example "India is my Country" or if any length text it goes outside of canvas that is outside of the background image.
i want to print "India is
my country" if i set width mean i think it goes to next line
#Override
protected void onDraw(Canvas canvas1)
{
Paint paint = new Paint();
Bitmap myBitmap = BitmapFactory.decodeResource(getResources(),R.drawable.ban_background);
Bitmap resizeImage1=Bitmap.createScaledBitmap(myBitmap,590,350,false);
canvas1.drawColor(Color.BLACK);
canvas1.drawBitmap(resizeImage1,10,5, null);
paint.setStyle(Paint.Style.FILL);
paint.setAntiAlias(true);
paint.setTextSize(25);
paint.setColor(Color.BLUE);
paint.setFakeBoldText(true);
paint.setTextSize(20);
paint.setStyle(Paint.Style.FILL_AND_STROKE);
canvas1.drawText(CameraText, 100,175, paint);
}
}
i have used to create a bitmap as follows and iam able to set the width and height
Bitmap finalImage = Bitmap.createBitmap(IMAGE_WIDTH, IMAGE_HEIGHT,
Bitmap.Config.RGB_565);
Bitmap tempImage = Bitmap.createBitmap(IMAGE_WIDTH / 2, IMAGE_HEIGHT,
Bitmap.Config.RGB_565);
Canvas g = new Canvas(finalImage);
Canvas gtemp = new Canvas(tempImage);
g.drawColor(Color.WHITE);
gtemp.drawColor(Color.WHITE);
Paint pnt = new Paint();
pnt.setColor(Color.BLACK);
pnt.setTextAlign(Paint.Align.CENTER);
pnt.setTextSize(40);
pnt.setTypeface(font1);
g.drawText(input, IMAGE_WIDTH / 2, 40, pnt);
Rect grct = new Rect(0, 0, IMAGE_WIDTH, IMAGE_HEIGHT);
Rect grctTemp = new Rect(0, 0, IMAGE_WIDTH / 2, IMAGE_HEIGHT);
gtemp.drawBitmap(finalImage, grct, grctTemp, pnt);
`
i am not sure what you mean but you can try the setStrokeWidth(2.0f)
i hope this helps :D