Android draw on a bitmap at specific coordinates of the Bitmap - android

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?

Related

canvas.drawBitmap not working as expected

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.

How to map a rectangle inside a scaled and rotated rectangle into the original bigger and unrotated rectangle

The situation is as follows:
I have a sampled image shown on screen, from which I can crop a portion of it after it has been rotated.
Once I've got the crop rectangle, I have to map it to the original image, which is unrotated and it's bigger.
Till now the scale factor hasn't been a problem.
This image shows more or less the situation(I know it's not showing the size difference):
Image that helps explains the question
So how can I get the mapped rectangle from Rect P to Rect Q?

Bitmap coordinates after canvas.scale

I need help at getting the correct coordinates when touching the canvas.
I have an image that is 1240x1756
The user can scroll around on this bitmap. It is the bitmap attached to the canvas. I do this by translating the canvas. canvas.translate()
The user can also place a new bitmap and move that anywhere around on the canvas. up to 256x256.
Up until this point I have no problem calculating the touch coordinates at scale factor of 1.0. I take the screen touch coords and add the offset of the image and then check to see if the 256x256 moveable bitmap intersects the touch coords.
However my issue is getting the correct coordinates after the canvas has been scaled. 0.1-1.0 are my minimum and maximum scaling values.
Can anyone point me in the right direction for a working algorithm?
You should be able to just multiply the pixel offset by 1/scale factor.

Cutting a multipoint ploygon out of Bitmap and placing it on transparency

I have a bitmap out of which I'm cutting out a multipi point polygon. I'm curious what the correct process is for taking the pixels within the arbitrary shape and copying them onto a new bitmap where the rest of the pixels are transparent. The objective is to allow the user to trace the shape and then remove everything outside the polygon.
I have the polygon part worked out (as a an array of points), but now am stumped as to how to transfer just the selected pixels to a new Bitmap.
TIA
Not sure how your code works, but here's an idea on how to do it:
Calculate the bounding rectangle of the selected area (find min x, min y, max x and max y from your points).
Crop your image to the bounding rectangle using any of the Bitmap or Canvas-methods.
Create a Path from your points, all moved into your new bitmap (x-=minX, y-=minY);
Set your Paths FillType to one that is inverse (fill the outside).
On your new cropped canvas, draw the Path using a paint with the Xfermode as PorterDuff.CLEAR, which removes all color.

Android - Set focus on a circle that I have drawn in my onDraw method

Android Question.
I have made a custom ImageView class and inside it I have an onDraw method which will draw a circle on particular pixels (using canvas). When I use this custom imageview and open up my image I would like to set the focus on the circle that I have drawn (e.g like google maps do with your current location. The focus is set to your current point)
What the map server does on google is deliver a customized set of tiles so that the center is displayed properly, the newer version is of course vector based so they simply draw the view so it's centered where they want it.
Without knowing the details of your application you probably need
Create your own container class, probably FrameLayout
public class myMapFrameLayout extends FrameLayout {
The override either onDraw or onDispatchDraw so that you can layout your tile appropriately
Figure out where to draw your bitmap so that the x,y you need will be in the center of the screen, then draw the other tiles that you need to fill in the blank space at the coordinates required dependent on which way the tile was moved to get centered
Think of a virtual screen that is larger than the actual screen with tiles all around it that are the same size
1 2 3
4 X 5
6 7 8
Assuming that X is the size of the display and represents the current tile you need to figure out which way to move the tile, and which other tiles 1,2,3,4,5,6,7 or 8 you need to fill in the empty space caused by move
If you had to draw the tile +x from 0,0 you need some of tile 4, drawing +y from 0,0 means some of 2 and both mean 1,2,4 are all needed and so on, so figure out the combinations and load the tiles you need, and figure out the drawing positions of each. That would give you your new virtual tile with the center displayed.
That's about as efficient as you can get I think with a bitmap drawing method on the client side.
UPDATE
Since your comment indicates you have only one very large image this is going to be a bit of a problem if the x,y you need as anything closer to the edges than the size of the display
None the less you can still draw the image where you need it, just measure the screen and draw the bitmap with the target x,y in the center
So if the screen was 500x500 and your image was 5000x5000 and the center was at position x=1000 y=1000 then
drawBitmap(Bitmap bitmap, Rect src, RectF dst, Paint paint)
where source rectangle would be 1000-250,1000-250,500,500 and dst rectangle would be 0,0,500,500
The 250 is the center x and center y of the display, 1000 are the target x/y coordinates, and 500 is the size of display.
Again, with targets that are at the edges you are going to have a blank polygon in your screen since you dont have an infinite map tile
Alternatively you could oversize your framelayout using layoutparams and just translate the canvas in the x and y to get the canvas centered to the x,y you need using similar calcs which may be more performant, not really sure
Keep in mind you are going to be using a lot of memory if your image is really big

Categories

Resources