Android Bitmap Contrast implementation

I want to create an application that will contrast with an image and then display that image in an ImageView. I found this example code and it doesn't work as expected. After applying the contrast, it just turns everything green. This 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 is? Also if you have another example please post it, it might be helpful.

Edit Also it seems that the result is the same every time, no matter what value I give for contrastVal

inapplyContrast(Bitmap image, double contrastVal)

Edit2 Sorry, there is actually a noticeable difference when modified contrastVal

. However, the image is green.

Edit3 I am adding some images so you can understand what the problem is.

Here's the original image:

enter image description here

And after applying contrast with contrast Val 1:

enter image description here

+3


source to share


1 answer


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

G = truncate(R);

      



For

G = truncate(G);

      

+1


source







All Articles