-1

Can someone explain me the way setPixel() method works in Android? I am trying to replace some pixels on a bitmap. I extract them by using getPixel() method and their individual colors, eg.green = Color.green(a[i][j]);, but I cannot set them new values back, so as to show the processed image onscreen.

Edit: This is where some of the processing occurs. I try to algorithmically convert to grayscale

 for (int i = 0; i < grayWidth; i++) {
            for (int j = 0; j < grayHeight; j++) {
                a[i][j] = myImage.getPixel(i, j);
                red = Color.red(a[i][j]);
                green = Color.green(a[i][j]);
                blue = Color.blue(a[i][j]);
                gray = (red + green + blue) / 3;
                a[i][j] = gray;
            }
        }

and then replace pixels:

for (int m = 0; m < grayHeight; m++) {
            for (int n = 0; n < grayWidth; n++) {
                grayScale.setPixel(m, n, a[m][n]);
            }
        } 

and finally show it on-screen

imageView.setImageBitmap(grayScale);

Sorry for not explaining it thoroughly in the first place/

max_tech91
  • 47
  • 5

1 Answers1

0
  1. Why posts questions without any code? Put yourself in our position, how can we help you, if we do not know what you are trying to do? You're only presenting your problem in English language, which does not equal to programming language in most cases. We want to see the latter, supported with english description of what you are doing and what's going on.

  2. Based on provided information and Android Dev Page for Bitmap, I can assume that your BitMap image might not be mutable. This would throw then IllegalStateException, but without seeing your LogCat / Code, I cannot be sure, whether this is the case.

  3. If the BitMap indeed is immutable, then you can try and look at converting immutable bitmap to mutable and try again.

Community
  • 1
  • 1
David Kasabji
  • 1,049
  • 3
  • 15
  • 32
  • I saw, but there is no try / catch clause to determine whether setPixel is throwing something. Also on the Android Dev page I linked in my answer, you have a method `setPixels()` which takes in an Array, so you don't need to iterate over each pixel. – David Kasabji Jan 06 '17 at 13:56
  • Ok i used setPixels, but I get the following error: java.lang.IllegalArgumentException: x + width must be <= bitmap.width(). The problem is that after debugging the app, the variable values match the image I am using. My example is a 20x12px image. If you check the code I've posted above, by using this line `grayScale.setPixels(b, 0, grayWidth, 0, 0, grayWidth, grayHeight);` should do the job, right? – max_tech91 Jan 06 '17 at 15:52