canvas.drawBitmap not working as expected - android

I am trying to add an image on top of another image. The one on the top is a blurred image. I am using the following code to try and achieve this.
//First image as a background(full size)
mCanvas.drawBitmap(canvasBackImage, 0, 0, null); //draws fine
Rect rectangle = new Rect(0,0,200,200);
//Second image on top blurred 200px x 200px rectangle
mCanvas.drawBitmap(blurBuilder.blur(appContext, canvasBackImage, mX, mY), null, rectangle, null);
The image is drawn fine with the above code at coordinate 0,0 of the canvas however, if I modify the above code's line three to the following, it doesn't add the image at the 100,100 coordinate of the canvas.
Rect rectangle = new Rect(100,100,200,200);
I also tried it with 50,50 coordnate and it works. So Changing it the following works too.
Rect rectangle = new Rect(50,50,200,200);
I have no idea why this is not working as I expect it to. Am I doing something wrong?
My ultimate objective is to blur the image at the exact location that the user touched. So if user touched in the middle of the screen then that part of the image will be blurred out.

I moved the above code in onDraw method and it seems to be working as expected now.
Before I had it in a different method which was being called manually on a button click.

Related

Android draw on a bitmap at specific coordinates of the Bitmap

I know the x and y coordinates in an bitmap and want to draw a rectangle there. I think this picture illustrates what I want.
Lets say I want to draw a green rectangle over the red one. I know the coordinates of the top left corner and the coordinates of the bottom left corner. Usually it should not be a problem. But I display the bitmap in an Imageview. I use this code to map the coordinates form the bitmap to the coordinates in the canvas of the imageview. For image_view_width and image_view_height I use the getHeight and getWidth form inside of the Imageview.
This solution works on my real device but on the emulator it shows the rectangle on the wrong position. Can someone help me and tell me what is wrong?

Android how to stretch a particular section of a Image?

I am developing a photography app in which I want to select a particular section of a photo and stretch that portion only. How to do that?
I have tried to stretch a photo using Canvas but failed to do so. Is it possible from android.graphics.NinePatch class?
Any suggestions?
You can use matrix to apply new dimension to your bitmap.
you could use setscale/postScale methods of a matrix object.
A rather ugly solution would be using a image cropping library. You can use it to temporarily crop a portion of the image and load it into another ImageView and then scaling it up.
Also, you can get things done without using that CropImageView. The idea is whenever user touches the original image view, you will be given a (x, y) at which user's finger resides. So, you can extract a bitmap image centered at (x, y) and with a given radius.
For applying a magnifier effect you could use a round/circular ImageView for showing magnified portion of the image.
Something like this:
Finally i found a solution of my problem below :
Bitmap stretchImage = Bitmap.createBitmap(w, h+progress, Bitmap.Config.ARGB_8888 );
c = new Canvas(stretchImage);
//draw top bit
c.drawBitmap(normalImage, new Rect(0,0,w,75), new Rect(0,0,w,75), null);
//draw middle bit
c.drawBitmap(normalImage, new Rect(0,75,w, 150), new Rect(0,75,w,150+progress), null);
//draw right bit
c.drawBitmap(normalImage, new Rect(0 ,150,w,225), new Rect(0 ,150+progress,w,225+progress), null);
myImage.setImageBitmap(stretchImage);

How can I crop an image in android with custom cropping layout?

I have an activity where I get an image and display it full screen, I want to draw a rectangle 600 x 600 for example over the image, I want the image to be darker and inside this rectangle to be bright 100% , and with my finger to change this rectangle's position (following my finger) and when iI click a button to be able to crop the image , to get only what it is inside this rectangle (bright area), how can I create this ?
You can store all the coordinates in an arraylist and draw a clipPath line in ActionMove of onTouchListener
clipPath = new Path();
clipPath.moveTo(tdownx,tdowny);
for(int i = 0; i<your_array_list.size();i++){
clipPath.lineTo(your_array_list.get(i).getY(),your_array_list.get(i).getX( ));
}
canvas.drawPath(clipPath, paint);
You can see the output at this YouTube video

Path . lineTo draws only half of length

Can somebody help me understand how does path.lineTo(x,y) actually work. I am trying to draw a simple straight line in a finger paint type of app. Here is an example I tried:
mPath.reset();
mPath.moveTo(0, 0);
mPath.lineTo(480, 800);
But the line goes only from 0,0 to 240, 400 - always only a half of distance as I move my finger over the screen. (the real programme has startX, startY and x,y touch coordinates instead of 0,0 & 480,800)
Maybe this piece of code will work for you:
Paint p = new Paint(Paint.ANTI_ALIAS_FLAG);
Path mPath= new Path();
mPath.moveTo(0, 0);
mPath.lineTo(480, 800);
p.setColor(0xff800000);
canvas.drawPath(mPath,p);
Also, the (480, 800) coordinate, is it still inside your screen?
The Path commands work exactly how you expect them to work and the above command should draw a line from top left to bottom righ corners in a view fully covering a 480x800 screen .
Check the follwoing:
The view covers all screen
The is no other view overlaping it
The coordinates are relative to top left coordinate of view. Check that view top left is on top left of screen
Regards.
Thanks to Goz and others who helped me truobleshoot this, it came out that Paint was doing it with this settings:
mPaint.setPathEffect(new CornerPathEffect(1000))
When this is removed or set to null, lineTo goes all the way. One just did not expect at first that Paint would do this but this corner rounding effect can shorten a straight line.

Android - Finger paint a bitmap only in specific areas

I have two images.
An image with a red rectangle, and image all white. I would like to paint with your finger on the white image only where the other image is the red rectangle.
The image with the red rectangle should not be visible.
How can I do?
Create bounds for each image, e.g. with a Rect set to the cords of each image (position & size). In the view where your overriding onDraw() in, set the onTouchListener to the view itself.
In onTouch() check the event.getX()/getY() is within the bounds of the white image. Then use whiteImage.setPixel() to set the individual pixels of the Bitmap image. Alternatively use Canvas.drawPoint() instead of manipulating white bitmap image itself.
In regards to not displaying the red rectangle... just don't draw it?
Edit:
To your comment about non square/rect shapes. I would still check for the touch event in the rect and then pass it to the image if it has hit the shape.
Within the shape (I'm assuming it is a bitmap) you would do Bitmap.getPixel(x, y) and see if it is == to Color.White, if it is.. change it to whatever colour!

Categories

Resources