fitztho
fitztho

Reputation: 3

Parsing Java-generated image in Android application

I am making an application that displays a picture of a room. When the user clicks the picture, an image pops up with the word of the object, and the device says the name. In order to do this, I load a pattern image that has colored blocks in the places of the objects, and a text file that has the different object names mapped to the specific color of the block.

I also made a Java application that can be used to create these two files. The user draws the rectangles over the image, and the application saves the rectangles drawn to a new image file.

Here is the Java code for creating/saving the image:

    BufferedImage i = new BufferedImage(img.getWidth(), img.getHeight(),        
            BufferedImage.TYPE_INT_RGB);
    Graphics g = i.getGraphics();
    g.setColor(Color.BLACK);
    g.fillRect(0, 0, i.getWidth(), i.getHeight());  

    //draw all the rectangles on the image
    for(Rectangle r: rects.keySet()){
        g.setColor(rects.get(r));   
        g.fillRect(r.x, r.y, r.width, r.height);
    }

    //write the BufferedImage to the file
    try {
        ImageIO.write(i, "png", saveTo);
    } catch (IOException e) {
        e.printStackTrace();
    }

When I open the picture in Photoshop, Paint, etc, it verifies that the colored blocks are the color they are supposed to be.

When I load them in the Android device, They are not the same color. They differ in a range from 1-3 on each color (so a color that is 25:0:0 is read as 24:0:0 or maybe 22:0:0).

On the device, I load the image as a bitmap, and use a TouchEvent.getX() and getY() to find the position on the image. I then use Bitmap.getPixel(x,y) to get the specific color.

If I use an editor like Photoshop or Paint, I get perfect images that read like normal. My user has asked for their own editor application, so it can create the config file as well.

Sorry for this being so long, and thanks for any help!

Upvotes: 0

Views: 179

Answers (1)

Plastic Sturgeon
Plastic Sturgeon

Reputation: 12527

Load you colored block as an asset, and not a resource. Android compresses and re-packages bitmaps in the resources folder to minimize their size. I believe the compression is what is causing your colors to shift.

It might be interesting to pull the image from the device using ADB to see if the on-devic e image is actually different. I expect it is.

Bitmaps contained in the assets folder are not processed at all. Use these for images that need to be maintained at their present quality.

Upvotes: 1

Related Questions