问题
I have a hue
effect code and I want to use it with a seek bar
, the effect can be applied using applyHueFilter( bitmap, level )
I want when i move the circle of the seekbar to the right the hue filter increases and when i move it to the left the hue filter decreases ( removes ) ..
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;
}
at the time Im using this applyHueFilter
in the progressChanged
..
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) {
}
});
what happens is when i move the seekbar
to the right the hue applys but when i move it to the left it doesn't get removed ( decreased ) ..
回答1:
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));
}
}
this code is efficiently work for Hue effect using Color Filter.
回答2:
Simple logic error. You have applyHueFilter(myBitmap, 100);
... you are increasing it by 100 no matter what.
It should be:
applyHueFilter(myBitmap, progress);
Looking at the applyHueFilter
method, you are also always doing HSV[0] *= level;
, so no matter what, you are always increasing the hue, just by lesser or greater amounts.
来源:https://stackoverflow.com/questions/12430899/using-hue-image-effect-with-a-seekbar