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);
}
}
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With