Question

for my final year project I am developing an android app that can capture the image of a leaf and identify what type of tree it came from. I have a nearly completed PC version (developed in java) and i am starting the process of porting it to android. Although BufferedImage and Raster make up a key part of my program, this is a problem because java.awt is missing in android, so this means i have to alter the library.

I am using a library that was developed by my lecturer, the method below converts a BufferedImage to an IntImage(a type that is used throughout the package) using Raster.

I'm basically asking are there any android alternatives to java.awt that i can use?

Here's the code:

public static int[][] readAsInts(String fileName) throws IOException {
    int[][] pixels;

    System.out.println("File: " + fileName);

    String [] types = ImageIO.getReaderFileSuffixes();
    for (int l = 0; l < types.length; l++) {
        System.out.println("Type " + l + ": " + types[l]);
    }
    File f = new File(fileName);
    BufferedImage bi;
    bi = ImageIO.read(f);

    int cols = bi.getWidth();
    int rows = bi.getHeight();

    System.err.println("Number of bands: " + bi.getRaster().getNumBands());
    Raster rast = bi.getRaster();

    pixels = new int[rows][cols];
    for (int r = 0; r < rows; r++) {
        rast.getSamples(0, r, cols, 1, 0, pixels[r]);
    }
    return pixels;
} // readAsInts

Thanks guys!

EDIT: For anyone who doesn't know the raster stores the pixel values for the BufferedImage, along with the number of bands (red, green, blue, alpha), so this algorithm takes the pixel values ad stores them in a 2d array (pixels).

Was it helpful?

Solution

Sounds like you just want to work with bitmaps and be able to manipulate the pixels themselves in an array. The Bitmap class comes to mind along with its getPixels method. Remember though that you have much more limited memory on a mobile device. It's easy to run into out of memory problems when working with large bitmaps.

Another route would be to use the NDK. I don't have much experience there, but AndroidBitmap_lockPixels and AndroidBitmap_unlockPixels are probably a good place to start.

Lastly, yet another option that should perform pretty well is to use RenderScript.

If all you're looking to do is load an image into an int array though, Bitmap.getPixels should be quite sufficient.

OTHER TIPS

You can convert a BufferedImage to a PNG or JPEG, then send it via HTTP to Android which can then convert the PNG or JPEG back to a Bitmap and use it. When Android needs to send any image, send it as PNG or JPEG via network, and then convert the PNG to BufferedImage and use it in your program.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top