1

I have a small Andriod application with a sub function that searches green pixels in a PNG file.

The program first processes the file:

private Bitmap buildSource(Context context) {
    Paint paint = new Paint();

    Logger.info("buildSource");
    Bitmap form = BitmapFactory.decodeResource(context.getResources(),
            R.drawable.form);
    // float d = context.getResources().getDisplayMetrics().density;
    Bitmap source = Bitmap.createBitmap(form.getWidth(), form.getHeight(),
            Config.ARGB_8888);
    Canvas canvas = new Canvas(source);
    canvas.drawBitmap(form, 0, 0, paint);       
    form.recycle();
    Logger.info("buildSource done " + source);
}

And then it searches for green pixels:

for (int y = 0; y < mSource.getHeight(); y++) {
    for (int x = source.getWidth() - 1; x > 0; x--) {
        int pixel = source.getPixel(x, y);    
        int green = Color.green(pixel);
        int red = Color.red(pixel);
        if (0 == red && green > 210 && green < 220) {
            // Save pixel position
        }

For some reason on a specific machine I'm running this code doesn't work, when I print out the pixels one by one, no pixel matches the condition that red=0 and green is ~215. This worked fine up until now.

The image itself is attached:

image with green pixels

I'm quite new to Android programming so I'm a bit stuck. Would appreciate any help.

Update: I've saved the source bitmap and the form bitmap to the gallery (Android programming is fun). When I open the images, I clearly see that the green pixels were replaced with gray one. is there some kind of background filter I should adjust? Thanks Attached is the form picture: enter image description here

omer schleifer
  • 3,897
  • 5
  • 31
  • 42
  • Have you missed an ampersand in your condition statement near `green > 210 & green < 220`? – vokilam Mar 04 '13 at 08:07
  • Could this somehow be related to the background property? I've tried playing with it according to this post: http://stackoverflow.com/questions/2748830/how-to-change-background-color-in-android-app , was unsuccessful and gave it up. maybe there is a remaining impact on the device configuration? – omer schleifer Mar 04 '13 at 08:26
  • Does it still work wrong? What is the result? – vokilam Mar 04 '13 at 08:42
  • No it still doesn't work. like I've said the problem is not with the code going over the bitmap, the bitmap itself does not contain green pixels, I've outputed it all to log file to make sure – omer schleifer Mar 04 '13 at 08:54
  • 1
    Could it be that the image is scaled on some devices (depending on the resolution) and that the green pixels are lost? What if you try to disable scaling as in http://stackoverflow.com/questions/7330097/how-do-you-tell-android-not-to-scale-images ? – Michael Mar 04 '13 at 09:54
  • Thanks for the suggestion,it did not work. However, it seems to be required to disable scaling anyway. – omer schleifer Mar 04 '13 at 10:16
  • 1
    I wonder if it might be caused by the 'crunching' that's done during a build - i.e. it does some compression of the images. – MrChaz Mar 04 '13 at 14:34
  • @MrChaz, I think you might be right. this code was transferred to me from another programmer. when he compiled it in the past - it did work. when I compile it - it seems not to work on every device I've tried. – omer schleifer Mar 04 '13 at 15:20
  • You could try compiling against an older SDK version - might alter behaviour? – MrChaz Mar 04 '13 at 15:52
  • I'm not sure if this will help since there was no change in the SDK. he worked with IDEA-intellij , and I work with eclipse. other than that I'm not sure what the differences are – omer schleifer Mar 04 '13 at 16:33
  • Ok, the problem was indded because of image compression,though I still can't find a way to solve this problem what I've done for now is to replace each single green pixel with a group of green pixels, this does the job, though not quite the way I wanted to. thanks – omer schleifer Mar 05 '13 at 08:40

0 Answers0