Search code examples
androidimage-processingbitmapcontrast

Android Bitmap Contrast Implementation


I want to create an app which applies contrast to an image and then displaying that image in ImageView. I found this example code and it doesn't seem to work properly. After applying the contrast it just makes everything greeny. Here is what I have in my program:

public class ImageImprovementActivity extends ActionBarActivity {
    private ImageView imageView;
    private Button buttonLoad;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_image_improvement);

        buttonLoad = (Button) findViewById(R.id.buttonLoad);
        imageView = (ImageView) findViewById(R.id.imageView);

        imageView.setImageBitmap(getImage());

        buttonLoad.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                imageView.setImageBitmap(applyContrast(((BitmapDrawable)imageView.getDrawable()).getBitmap(), 0.3));
            }
        });
    }

    private Bitmap getImage() {
        final File imgFile = new File(Environment.getExternalStorageDirectory() + "/testImage2.jpg" );

        if (imgFile.exists()) {
            Bitmap bmp = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
            return bmp;
        }

        return null;
    }

    private Bitmap applyContrast(Bitmap image, double contrastVal) {
        final int width = image.getWidth();
        final int height = image.getHeight();
        final Bitmap contrastedImage = Bitmap.createBitmap(width, height, image.getConfig());

        int A, R, G, B;
        int pixel;

        double contrast = Math.pow((100 + contrastVal) / 100, 2);


        for (int x = 0; x < width; x++) {
            for (int y = 0; y < height; y++) {
                pixel = image.getPixel(x, y);
                A = Color.alpha(pixel);
                R = Color.red(pixel);
                R = (int)(((((R / 255.0) - 0.5) * contrast) + 0.5) * 255.0);
                R = truncate(R);

                G = Color.green(pixel);
                G = (int)(((((G / 255.0) - 0.5) * contrast) + 0.5) * 255.0);
                G = truncate(R);

                B = Color.blue(pixel);
                B = (int)(((((B / 255.0) - 0.5) * contrast) + 0.5) * 255.0);
                B = truncate(B);

                Log.i("ImageImprove", A + " " + R + " " + " " + G + " " + B);

                contrastedImage.setPixel(x, y, Color.argb(A, R, G, B));
            }
        }
        return contrastedImage;
    }

    private int truncate(int value) {
        if (value < 0) {
            return 0;
        } else if (value > 255) {
            return 255;
        }

        return value;
    }
}

Do you have an idea what the problem could be? Also if you have another example, please post it, it could be useful.

Edit Also it seems that the result every time is the same, no matter what value I give for contrastVal in applyContrast(Bitmap image, double contrastVal)

Edit2 I'm sorry, actually there is a visible difference when changing contrastVal. Still the image is greeny..

Edit3 I'm adding some images so you can get more clear what the problem is.

Here is the original image:

enter image description here

And here is after applying contrast with contrastVal 1:

enter image description here


Solution

  • You have a little error in your code (copy error?). Change

    G = truncate(R);
    

    To

    G = truncate(G);