In my application I want the user to be able to select some content of an Image contained inside an ImageView
.
To select the content I subclassed the ImageView
class making it implement the OnTouchListener
so to draw over it a rectangle with borders decided by the user.
Here is an example of the result of the drawing (to have an idea of how it works you can think of it as when you click with the mouse on your desktop and drag the mouse):
Now I need to determine which pixels of the Bitmap
image correspond to the selected part. It's kind of easy to determine which are the points of the ImageView
belonging to the rectangle, but I don't know how to get the correspondent pixels, since the ImageView
has a different aspect ratio than the original image.
I followed the approach described especially here, but also here, but am not fully satisfied because in my opinion the correspondence made is 1 on 1 between pixels and points on the ImageView
and does not give me all the correspondent pixels on the original image to the selected area.
Calling hoveredRect
the rectangle on the ImageView
the points inside of it are:
class Point {
float x, y;
@Override
public String toString() {
return x + ", " + y;
}
}
Vector<Point> pointsInRect = new Vector<Point>();
for( int x = hoveredRect.left; x <= hoveredRect.right; x++ ){
for( int y = hoveredRect.top; y <= hoveredRect.bottom; y++ ){
Point pointInRect = new Point();
pointInRect.x = x;
pointInRect.y = y;
pointsInRect.add(pointInRect);
}
}
How can I obtain a Vector<Pixels> pixelsInImage
containing the correspondent pixels of the Bitmap
image?
ADDED EXPLANATIONS
I'll explain a little better the context of my issue:
I need to do some image processing on the selected part, and want to be sure that all the pixels in the rectangle get processed.
The image processing will be done on a server but it needs to know exactly which pixels to process. Server works with image with real dimensions, android app just tells which pixels to process to the server by passing a vector containing the pixel coordinates
And why I don't like the solutions proposed in the links above:
The answers given transform coordinates with a 1 to 1 fashion. This approach clearly is not valid for my task, since an area of say 50 points in the
ImageView
of a certain size on the screen cannot correspond to an area of the same number of pixels in the real image, but should consider the different aspect ratio.
As example this is the area that should be selected if the image is smaller than the ImageView
shown on the app: