Canvas
Canvas

Reputation: 5897

Android Bitmap change Hue

I have got a Android bitmap and i'm trying to change the HUE of the image, as the image is a red block, I want to change that block to green by just changing the HUE, but I can't seem to find any code anywhere.

Anyone know how I can do this?

Canvas

Upvotes: 1

Views: 1952

Answers (3)

xjcl
xjcl

Reputation: 15309

If you wrap your Bitmap in an ImageView there is a very simple way:

ImageView iv = new ImageView(this);
iv.setImageBitmap(yourBitmap);
iv.setColorFilter(Color.RED);

You likely want to wrap it in an ImageView anyway if you want to display it on the screen.

Upvotes: 2

Miloslaw Smyk
Miloslaw Smyk

Reputation: 1323

Well, if all you are after is "change red to green", you can just switch R and G color components. Primitive, but may do the job for you.

private Bitmap redToGreen(Bitmap mBitmapIn)
{
    Bitmap bitmap = mBitmapIn.copy(mBitmapIn.getConfig(), true);

    int []raster = new int[bitmap.getWidth()];

    for(int line = 0; line < bitmap.getHeight(); line++) {
        bitmap.getPixels(raster, 0, bitmap.getWidth(), 0, line, bitmap.getWidth(), 1);

        for (int p = 0; p < bitmap.getWidth(); p++)
            raster[p] = Color.rgb(Color.green(raster[p]), Color.red(raster[p]), Color.blue(raster[p]));

        bitmap.setPixels(raster, 0, bitmap.getWidth(), 0, line, bitmap.getWidth(), 1);
    }

    return bitmap;
}

Upvotes: 1

HalR
HalR

Reputation: 11073

I'm confident that you will not find a simple "hue" dial to adjust on the colors of your image.

The closest approximation (and should work fine0 you can get is with ColorMatrix.

This question and its answers shed a lot of light on the subject.

Here is the technical description of what a ColorMatrix is:

ColorMatrix is a 5x4 matrix for transforming the color+alpha components of a Bitmap.
 The matrix is stored in a single array, and its treated as follows: 
  [ a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t ] 

 When applied to a color [r, g, b, a], the resulting color is computed as (after clamping)
         R' = a*R + b*G + c*B + d*A + e;
         G' = f*R + g*G + h*B + i*A + j;
         B' = k*R + l*G + m*B + n*A + o;
         A' = p*R + q*G + r*B + s*A + t; 

Upvotes: 0

Related Questions