Android Bitmap Color Change changeHue(Bitmap bmp, int hue, int width, int height)

Here you can find the source of changeHue(Bitmap bmp, int hue, int width, int height)

Description

Change hue of bitmap.

Parameter

Parameter Description
bmp the bmp
hue the hue
width the width
height the height

Return

the bitmap

Declaration

public static Bitmap changeHue(Bitmap bmp, int hue, int width,
        int height) 

Method Source Code

//package com.java2s;

import android.graphics.Bitmap;

import android.graphics.Color;

public class Main {
    /**//w  w w. ja v  a2s . c o m
     * Change hue of bitmap.
     * 
     * @param bmp
     *            the bmp
     * @param hue
     *            the hue
     * @param width
     *            the width
     * @param height
     *            the height
     * @return the bitmap
     * @author siddhesh
     */
    public static Bitmap changeHue(Bitmap bmp, int hue, int width,
            int height) {
        if (bmp == null) {
            return null;
        }
        // int srcwidth = bmp.getWidth();
        // int srcheight = bmp.getHeight();
        // create output bitmap

        // create a mutable empty bitmap
        Bitmap bitmap = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(),
                bmp.getHeight());
        if ((hue < 0) || (hue > 360)) {
            return null;
        }

        int size = width * height;
        int[] all_pixels = new int[size];
        int top = 0;
        int left = 0;
        int offset = 0;
        int stride = width;

        bitmap.getPixels(all_pixels, offset, stride, top, left, width,
                height);

        int pixel = 0;
        int alpha = 0;
        float[] hsv = new float[3];

        for (int i = 0; i < size; i++) {
            pixel = all_pixels[i];
            alpha = Color.alpha(pixel);
            Color.colorToHSV(pixel, hsv);

            // You could specify target color including Saturation for
            // more precise results
            hsv[0] = hue;
            hsv[1] = 0.2f;

            all_pixels[i] = Color.HSVToColor(alpha, hsv);
        }

        bitmap.setPixels(all_pixels, offset, stride, top, left, width,
                height);
        return bitmap;
    }
}

Related

  1. adjustedContrast(Bitmap src, double value)
  2. boost(Bitmap src)
  3. brightness(Bitmap src, int value)
  4. createAnaglyphBitmap(Bitmap bitmap)
  5. createSaturatedBitmap(final Bitmap bitmap)
  6. dilate(Bitmap bitmap, int[][] se)
  7. dilate(Bitmap source, Bitmap target, int[][] se)