我怎样才能实现搜索栏来改变对比度

How can i implement seek bar to change contrast

我正在尝试实现一个搜索栏来更改 android 中图像的对比度。有人帮我实现这个 please.There 还有其他图像处理选项吗? 有谁知道解决方法请帮帮我

提前致谢。

代码:

    public class MainActivity extends ActionBarActivity {
    ImageView imViewAndroid;
    private SeekBar seekbar;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        seekbar = (SeekBar) findViewById(R.id.seekbar);
        imViewAndroid = (ImageView) findViewById(R.id.imViewAndroid);


        seekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int i, boolean b) {

            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
                imViewAndroid.setImageBitmap(takeContrast(BitmapFactory.decodeResource(getResources(), R.drawable.dicom), 100));
            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });

    }
  public  Bitmap takeContrast(Bitmap src, double value) {
        // src image size
        int width = src.getWidth();
        int height = src.getHeight();
        // create output bitmap with original size
        Bitmap bmOut = Bitmap.createBitmap(width, height, src.getConfig());
        // color information
        int A, R, G, B;
        int pixel;
        // get contrast value
        double contrast = Math.pow((100 + value) / 100, 2);

        // scan through all pixels
        for(int x = 0; x < width; ++x) {
            for(int y = 0; y < height; ++y) {
                // get pixel color
                pixel = src.getPixel(x, y);
                A = Color.alpha(pixel);
                // apply filter contrast for every channel R, G, B
                R = Color.red(pixel);
                R = (int)(((((R / 255.0) - 0.5) * contrast) + 0.5) * 255.0);
                if(R < 0) { R = 0; }
                else if(R > 255) { R = 255; }

                G = Color.red(pixel);
                G = (int)(((((G / 255.0) - 0.5) * contrast) + 0.5) * 255.0);
                if(G < 0) { G = 0; }
                else if(G > 255) { G = 255; }

                B = Color.red(pixel);
                B = (int)(((((B / 255.0) - 0.5) * contrast) + 0.5) * 255.0);
                if(B < 0) { B = 0; }
                else if(B > 255) { B = 255; }

                // set new pixel color to output bitmap
                bmOut.setPixel(x, y, Color.argb(A, R, G, B));
            }
        }

        // return final image
        return bmOut;
    }
  1. 你在错误的地方调用了改变对比度的函数,它应该在这里

    @Override
    public void onProgressChanged(SeekBar seekBar, int progress, boolean b) {
        // call the method here, default progress is <0, 100>
    }
    
  2. 您使用非常慢的功能来更改对比度。 Whosebug 上已经有很多答案,看看 this answer先搜索再提问。

在我的 github 上查看这个完整的实现。

以上代码处理图像花费了太多时间。如果您使用我的代码处理图像,将花费更少的时间。

@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean b) {
    contrastAndBrightnessControler(bitmap,seekbar.getProgress()/10,0.7)
}


public static Bitmap contrastAndBrightnessControler(Bitmap bitmap, float contrast, float brightness)
{
    ColorMatrix cmatrix = new ColorMatrix(new float[]
    {
        contrast, 0, 0, 0, brightness,
        0, contrast, 0, 0, brightness,
        0, 0, contrast, 0, brightness,
        0, 0, 0, 1, 0
    });
    Bitmap ret =Bitmap.createBitmap(bitmap.getWidth(),bitmap.getHeight(), bitmap.getConfig());
    Canvas canvas = new Canvas(ret);
    Paint paint = new Paint();
    paint.setColorFilter(new ColorMatrixColorFilter(cmatrix));
    canvas.drawBitmap(bitmap, 0, 0, paint);
    return ret;
}