【问题标题】:Android Bitmap Contrast ImplementationAndroid位图对比实现
【发布时间】:2015-03-05 19:03:15
【问题描述】:

我想创建一个应用程序,将对比度应用于图像,然后在 ImageView 中显示该图像。我找到了this 示例代码,但它似乎无法正常工作。应用对比度后,它只会使一切变绿。这是我的程序中的内容:

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 我正在添加一些图像,以便您更清楚问题所在。

这是原图:

这是在使用 contrastVal 1 进行对比之后:

【问题讨论】:

    标签: android image-processing bitmap contrast


    【解决方案1】:

    您的代码中有一点错误(复制错误?)。改变

    G = truncate(R);
    

    G = truncate(G);
    

    【讨论】:

    • 哦,代码.. 我不知道我怎么会错过它。谢谢你。真的很管用!
    猜你喜欢
    • 2022-08-19
    • 2023-02-08
    • 2014-01-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-01
    • 2017-02-25
    • 1970-01-01
    相关资源
    最近更新 更多