Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting all pixels of Bitmap in Android

I wanted to get a list of pixels for the bit map that I have.

I got the bitmap to show on the screen but for some reason every time I try to get the list of pixels the array is always 0.

I am not sure why. I have been searching and trying different answers for a few days. the length of the array is there, and it is not null. So I am not sure why this isnt working.

  @Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);

    line = BitmapFactory.decodeResource(getContext().getResources(), R.drawable.line);
    sun = BitmapFactory.decodeResource(getContext().getResources(), R.drawable.circle);

    line = Bitmap.createBitmap(line, 0, 0, line.getWidth(), line.getHeight());



    canvas.drawBitmap(line, 0, 0, null);
    canvas.drawBitmap(sun, 0,0, null);


    Log.d("BITMAP","WIDTH:"+line.getWidth()+" HEIGHT:"+line.getHeight());


    pixelAmount = new int[line.getHeight()*line.getRowBytes()];

    line.getPixels(pixelAmount,0,line.getWidth(),0,0,line.getWidth()-1,line.getHeight()-1);

    Log.d("Line", "Pixel: " + pixelAmount.length + " Index" + 0);
    Log.d("Line", "Pixel: " + pixelAmount[10] + " Index" + 0);
    Log.d("Line", "Pixel: " + pixelAmount[100] + " Index" + 0);
    Log.d("Line", "Pixel: " + pixelAmount[56] + " Index" + 0);
    Log.d("Line", "Pixel: " + pixelAmount[76] + " Index" + 0);


    }
}
like image 382
Edon Freiner Avatar asked Sep 20 '25 19:09

Edon Freiner


1 Answers

I guess this is because you are using the wrong method. By doing:

line = Bitmap.createBitmap(line.getWidth(), line.getHeight(), Bitmap.Config.ARGB_8888);

You just create a new, empty bitmap. But you must give a source bitmap, like this method:

line = createBitmap(line, 0, 0, line.getWidth(), line.getHeight());

Also, you should get sure the bitmap is created with the method decodeFromResource() . Just put a log which checks width and height like this:

Log.d("BITMAP","WIDTH:"+line.getWidth()+" HEIGHT:"+line.getHeight);

If there is a width and height, then you know creation was successfull.

EDIT

your next problem is here:

pixelAmount = new int[line.getHeight()*line.getRowBytes()];

must be:

pixelAmount = new int[line.getHeight()*line.getWidth()];

And check it like:

for(int i=0;i<pixelAmount.length;i++){

int pixel = pixelAmount[i];
if(pixel!=0){
Log.d("BITMAP","PIXELS:"+pixel[i]);
}

}

If the size is big, the output in Android Monitor could be too much and early outputs can be deleted.

like image 69
Opiatefuchs Avatar answered Sep 22 '25 10:09

Opiatefuchs