Search code examples
javaandroidimagebufferedimagepixel

Getting coordinates from image in Java Android


I am using BufferedImage from Java in an Android project. In this project the user is allowed to select a small portion of an image.

For example: If you have an image of a park with a building. The user can make small selections of grass and the android program needs to save the coordinates of this selection.

I was wondering if there is functionality in BufferedImage that supports this: letting an user select small portions of the image (using touch screen) and getting coordinates from those locations in the image. If not, what else can I do?


Solution

  • You can store the pixel position (x, y) (i.e. cursor position) of the click in to SQLite database and you don't need to store all values around it. If you want to check a click which comes around this pixel (with a tolerance value), the following code will help you :

        int[] xyReceivedPixel = { 15, 20 };
        int[] xyOriginalPixel = { 30, 15 };
        int toleranceValue = 30;
    
        boolean status = (xyReceivedPixel[0] < xyOriginalPixel[0] + toleranceValue 
                || xyReceivedPixel[0] > xyOriginalPixel[0] - toleranceValue)
                && (xyReceivedPixel[1] < xyOriginalPixel[1] + toleranceValue 
                        || xyReceivedPixel[1] > xyOriginalPixel[1] - toleranceValue);
    
        System.out.println(status);
    

    Here, xyOriginalPixel is the original pixel point which is stored in the database and the xyReceivedPixel is the pixel point that is got when another click was made or a pixel point that is to be compared with it. The xyReceivedPixel is checked whether it is near by xyOriginalPixel with the maximum difference of toleranceValue.