4

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


    }
}
lelloman
  • 13,883
  • 5
  • 63
  • 85
Edon Freiner
  • 338
  • 2
  • 17

1 Answers1

3

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.

Opiatefuchs
  • 9,800
  • 2
  • 36
  • 49
  • Thanks! There is a width and height, but the array is still empty for some reason – Edon Freiner Jun 16 '17 at 15:47
  • and I think you don´t need `createBitmap`. If decodeFromResource works, this should work too. – Opiatefuchs Jun 16 '17 at 15:50
  • you haven´t used the method I wrote in my anser. You are still with the old method.. – Opiatefuchs Jun 16 '17 at 15:58
  • I took out the `createBitmap` as well. it still puts it on the screen, but doesnt work. I have tried all these methods before, none really seem to get me the pixel location of the image – Edon Freiner Jun 16 '17 at 16:00
  • What do you mean, I took out the line `line = Bitmap.createBitmap(line.getWidth(), line.getHeight(), Bitmap.Config.ARGB_8888);`and replaced it with `line = createBitmap(line, 0, 0, line.getWidth(), line.getHeight());` – Edon Freiner Jun 16 '17 at 16:02
  • I think there is a misunderstanding from your side. Are your images .png files? Because if yes, there is transparency between and this transparency is 0. Make a for loop and check every pixel, you will see there is something inside. – Opiatefuchs Jun 16 '17 at 16:18
  • it is a png file, and even then I am trying to get the location of the pixels not the color. Also, I just tried, and they were all 0. – Edon Freiner Jun 16 '17 at 16:24
  • see my edit, your array pixelAmount is wrong...you need to use `line.getWidth()*line.getHeight()` not `line.getRowBytes` . And as a tip: in your Log, check if pix is !=0 and then make an output, not for all pixels. This could be too much to show in Android Monitor – Opiatefuchs Jun 16 '17 at 16:29
  • Thank you! I got an output, a lot of them are the same number: 2130706432 or -16777216. I might be a little confused as to what this method does. Does this method return a list of all the pixels and their location? because that is the goal I am trying to achieve – Edon Freiner Jun 16 '17 at 16:37
  • no, these numbers representing colors. But if you want the location and the color of this location, see in this answer on SO:https://stackoverflow.com/questions/7807360/how-to-get-pixel-colour-in-android – Opiatefuchs Jun 16 '17 at 16:54
  • Hi, can you give a little explanation on that. Sorry. it seems like the location of the pixels are coming from where the user touched. It doesnt seem like this is returning the location of ever pixel. Thanks again – Edon Freiner Jun 16 '17 at 17:08
  • the location of every pixel is a simple logic. You just have to go through width and height with a for loop inside a for loop. `for(int i=0;i – Opiatefuchs Jun 16 '17 at 17:14
  • with that the loop goes through a raster and takes every y pixel and checks every x pixel at the y coordinate – Opiatefuchs Jun 16 '17 at 17:16
  • Thanks! I got it! I have an array list with all the coordinates. – Edon Freiner Jun 16 '17 at 17:51
  • Do you know how to resize a bitmap and move it as well? – Edon Freiner Jun 16 '17 at 17:51
  • For resizing there is the method `createScaledBitmap.(bitmap,newWidth,newHeight,false)`. What do you mean with moving? Like an animation? Or drag and drop? or just to another x y coordinate? – Opiatefuchs Jun 17 '17 at 04:44
  • Can we chat off this feed so I can give you a better idea of what I would like to do? Then maybe we can find a suitable solution? Thanks – Edon Freiner Jun 18 '17 at 12:25
  • Where do you want to start a chat? – Edon Freiner Jun 20 '17 at 13:03
  • hope it works: https://chat.stackoverflow.com/rooms/147228/get-all-pixels-of-bitmap – Opiatefuchs Jun 21 '17 at 07:42