Android - Correspondence between ImageView coordinates and Bitmap Pixels

Posted by Matteo on Stack Overflow See other posts from Stack Overflow or by Matteo
Published on 2012-07-04T20:14:07Z Indexed on 2012/07/04 21:15 UTC
Read the original article Hit count: 287

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 you can think of it as when you click with the mouse on your desktop and drag the mouse):

enter image description here

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?

© Stack Overflow or respective owner

Related posts about java

Related posts about android