Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android alternative to java.awt (BufferedImage and Raster)

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).

like image 620
John Avatar asked Feb 20 '14 23:02

John


1 Answers

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.

like image 127
kabuko Avatar answered Oct 04 '22 22:10

kabuko