安卓位图对比度实现

3

我想创建一个应用程序,可以对图像应用对比度,然后在ImageView中显示该图像。我找到了这个示例代码,但它似乎不能正常工作。应用对比度后,它只会让一切变得绿色。以下是我程序中的内容:

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

你有想法这个问题可能是什么吗?如果你有另一个例子,请发布它,这可能会有用。

编辑 此外,似乎每次的结果都是相同的,无论我在applyContrast(Bitmap image, double contrastVal)中给出什么值作为contrastVal

编辑2 对不起,实际上在改变contrastVal时确实有明显的差异。但图像仍然是绿色的。

编辑3 我正在添加一些图片,这样你就可以更清楚地了解问题所在。

这是原始图像:

enter image description here

这是应用对比度后的图像,对比度值为1:

enter image description here

1个回答

1

你的代码中有一个小错误(复制错误?)。请更改

G = truncate(R);

G = truncate(G);

哦,代码...我不知道我怎么会错过那个。谢谢你。它真的起作用了! - dephinera

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接