1

我有一个hue效果代码,我想将它与 a 一起使用seek barapplyHueFilter( bitmap, level )当我将搜索栏的圆圈向右移动时,色调过滤器会增加,而当我将它向左移动时,色调过滤器会减少(删除)..

public static Bitmap applyHueFilter(Bitmap source, int level) {
    // get image size
    int width = source.getWidth();
    int height = source.getHeight();
    int[] pixels = new int[width * height];
    float[] HSV = new float[3];
    // get pixel array from source
    source.getPixels(pixels, 0, width, 0, 0, width, height);

    int index = 0;
    // iteration through pixels
    for(int y = 0; y < height; ++y) {
        for(int x = 0; x < width; ++x) {
            // get current index in 2D-matrix
            index = y * width + x;
            // convert to HSV
            Color.colorToHSV(pixels[index], HSV);
            // increase Saturation level
            HSV[1] *= level;
            HSV[1] = (float) Math.max(0.0, Math.min(HSV[1], 1.0));
            // take color back
            pixels[index] |= Color.HSVToColor(HSV);
        }
    }
    // output bitmap                
    myBitmap.setPixels(pixels, 0, width, 0, 0, width, height);
    return myBitmap;        
}

当时我applyHueFilterprogressChanged..

    hueSB.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {

        public void onProgressChanged(SeekBar seekBar, int progress,
                boolean fromUser) {
            applyHueFilter(myBitmap, 100);
        }

        public void onStartTrackingTouch(SeekBar seekBar) {

        }

        public void onStopTrackingTouch(SeekBar seekBar) {

        }

    });

发生的情况是,当我将seekbar其向右移动时,色调适用,但当我将其向左移动时,它不会被移除(减少)..

4

2 回答 2

4
Canvas canvas = new Canvas(yourbitmap);

        Paint paint = new Paint();
        paint.setColorFilter(ColorFilterGenerator.adjustHue(seekbarvalue));
        canvas.drawBitmap(yourbitmap, 0, 0, paint);
return yourbitmap;


public class ColorFilterGenerator
{

    public static ColorFilter adjustHue( float value )
    {
        ColorMatrix cm = new ColorMatrix();

        adjustHue(cm, value);

        return new ColorMatrixColorFilter(cm);
    }


    public static void adjustHue(ColorMatrix cm, float value)
    {

        value = (value % 360.0f) * (float) Math.PI / 180.0f;

        if (value == 0)
        {
            return;
        }
        float cosVal = (float) Math.cos(value);
        float sinVal = (float) Math.sin(value);
        float lumR = 0.213f;
        float lumG = 0.715f;
        float lumB = 0.072f;
        float[] mat = new float[]
                {
                        lumR + cosVal * (1 - lumR) + sinVal * (-lumR), lumG + cosVal * (-lumG) + sinVal * (-lumG), lumB + cosVal * (-lumB) + sinVal * (1 - lumB), 0, 0,
                        lumR + cosVal * (-lumR) + sinVal * (0.143f), lumG + cosVal * (1 - lumG) + sinVal * (0.140f), lumB + cosVal * (-lumB) + sinVal * (-0.283f), 0, 0,
                        lumR + cosVal * (-lumR) + sinVal * (-(1 - lumR)), lumG + cosVal * (-lumG) + sinVal * (lumG), lumB + cosVal * (1 - lumB) + sinVal * (lumB), 0, 0,
                        0f, 0f, 0f, 1f, 0f,
                        0f, 0f, 0f, 0f, 1f };
        cm.postConcat(new ColorMatrix(mat));
    }
}

此代码使用滤色器有效地处理色调效果。

于 2016-04-25T04:47:37.133 回答
0

简单的逻辑错误。你有applyHueFilter(myBitmap, 100);......无论如何你都会增加100。

它应该是:

applyHueFilter(myBitmap, progress); 

看这个applyHueFilter方法,你也一直在做HSV[0] *= level;,所以无论如何,你总是在增加色调,只是增加了更少或更多的量。

于 2012-09-15T14:32:32.497 回答