Search code examples
javaandroidimagebitmapgetpixel

Are there similar android methods of java getData().getPixels()?


I'm trying to use the following code of java to read image data on android. Since ImageIO is not supported, what are some ways I may use getData().getPixels()?

By changing BufferedImage into Bitmap, I can get all other codes working besides bi.getData().getPixels()?

Any method in the android library I may use to replace it?

In android Bitmap class: public void getPixels (int[] pixels, int offset, int stride, int x, int y, int width, int height)

This method supports int[], but not double[]. I'm not familiar with image processing will they be different?

Thank you.

    private double[] getImageData(String imageFileName) throws FaceRecError {
    BufferedImage bi = null;
    double[] inputFace = null;
    try{
        bi = ImageIO.read(new File(imageFileName));
    }catch(IOException ioe){
        throw new FaceRecError(ioe.getMessage());
    }
    if (bi != null){
    int imageWidth = bi.getWidth();
    int imageHeight = bi.getHeight();
    inputFace = new double[imageWidth * imageHeight];
    bi.getData().getPixels(0, 0, imageWidth, imageHeight,inputFace);
    }
    return inputFace;
}   

Solution

  • This code is used for getting color of all pixels in Bitmap in android.This return array of color

        Bitmap bitmap=BitmapFactory.decodeFile("file path");
        int height=bitmap.getHeight();
        int width=bitmap.getWidth();
        int[] pixel=new int[height*width];
        bitmap.getPixels(pixel, 0, width, 0, width, width, height);
    

    So your color will be saved in pixel array. getPixels() have many parameter to customize which pixel color you want

    Updated For Casting Integer array to double

    public  double[] getDoubleNumbers(int[] numbers) 
    //changed double to double[]
    {double[] newNumbers = new double[numbers.length]; //changed 99 to numbers.length
    for (int index = 0; index < numbers.length; index++)
    newNumbers[index] = (double)numbers[index];
    
    return newNumbers;
    }
    }