Lorem ipsum dolor sit amet, consectetur adipiscing elit. Test link

TUNE IMAGE CLASS IN ANDROID

TUNE IMAGE CLASS IN ANDROID


import android.graphics.Bitmap;
import android.graphics.ColorMatrix;
import android.graphics.ColorMatrixColorFilter;
import android.graphics.Paint;
import android.graphics.drawable.BitmapDrawable;
import android.widget.ImageView;

public class ImageControl {
    private ImageView imageView;
    private Bitmap originalBitmap;
    private Bitmap filteredBitmap;

    public ImageControl(ImageView imageView) {
        this.imageView = imageView;
        this.originalBitmap = ((BitmapDrawable) imageView.getDrawable()).getBitmap();
        this.filteredBitmap = Bitmap.createBitmap(originalBitmap.getWidth(), originalBitmap.getHeight(), originalBitmap.getConfig());
    }

    public void adjustBrightness(int value) {
        ColorMatrix colorMatrix = new ColorMatrix();
        colorMatrix.set(new float[]{
                1, 0, 0, 0, value,
                0, 1, 0, 0, value,
                0, 0, 1, 0, value,
                0, 0, 0, 1, 0});
        applyColorMatrix(colorMatrix);
    }

    public void adjustContrast(float value) {
        ColorMatrix colorMatrix = new ColorMatrix();
        float scale = value + 1.f;
        float translate = (-.5f * scale + .5f) * 255.f;
        colorMatrix.set(new float[]{
                scale, 0, 0, 0, translate,
                0, scale, 0, 0, translate,
                0, 0, scale, 0, translate,
                0, 0, 0, 1, 0});
        applyColorMatrix(colorMatrix);
    }

    public void adjustSaturation(float value) {
        ColorMatrix colorMatrix = new ColorMatrix();
        colorMatrix.setSaturation(value);
        applyColorMatrix(colorMatrix);
    }

    public void adjustAmbience(float value) {
        float ambience = value / 50.f;
        float highlight = 1.f - ambience;
        ColorMatrix colorMatrix = new ColorMatrix();
        colorMatrix.set(new float[]{
                highlight, 0, 0, 0, 0,
                0, highlight, 0, 0, 0,
                0, 0, highlight, 0, 0,
                0, 0, 0, 1, 0});
        applyColorMatrix(colorMatrix);

        Paint paint = new Paint();
        paint.setARGB((int) (ambience * 255.f), 255, 255, 255);
        imageView.setLayerType(ImageView.LAYER_TYPE_HARDWARE, paint);
    }

    public void adjustHighlights(float value) {
        float highlight = value / 50.f;
        float shadow = 1.f - highlight;
        ColorMatrix colorMatrix = new ColorMatrix();
        colorMatrix.set(new float[]{
                shadow, 0, 0, 0, 0,
                0, shadow, 0, 0, 0,
                0, 0, shadow, 0, 0,
                0, 0, 0, 1, 0});
        applyColorMatrix(colorMatrix);

        Paint paint = new Paint();
        paint.setARGB((int) (highlight * 255.f), 255, 255, 255);
        imageView.setLayerType(ImageView.LAYER_TYPE_HARDWARE, paint);
    }

    public void adjustShadows(float value) {
        float shadow = value / 50.f;
        float highlight = 1.f - shadow;
       
ColorMatrix colorMatrix = new ColorMatrix();
colorMatrix.set(new float[]{
highlight, 0, 0, 0, 0,
0, highlight, 0, 0, 0,
0, 0, highlight, 0, 0,
0, 0, 0, 1, 0});
applyColorMatrix(colorMatrix);
    Paint paint = new Paint();
    paint.setARGB((int) (shadow * 255.f), 0, 0, 0);
    imageView.setLayerType(ImageView.LAYER_TYPE_HARDWARE, paint);
}

public void adjustWarmth(float value) {
    float warmth = value / 100.f;
    float coolness = 1.f - warmth;
    ColorMatrix colorMatrix = new ColorMatrix();
    colorMatrix.set(new float[]{
            warmth, 0, 0, 0, 0,
            0, coolness, 0, 0, 0,
            0, 0, coolness, 0, 0,
            0, 0, 0, 1, 0});
    applyColorMatrix(colorMatrix);
}

private void applyColorMatrix(ColorMatrix colorMatrix) {
    Bitmap originalBitmap = ((BitmapDrawable) imageView.getDrawable()).getBitmap();
    ColorMatrixColorFilter colorFilter = new ColorMatrixColorFilter(colorMatrix);
    Paint paint = new Paint();
    paint.setColorFilter(colorFilter);
    imageView.setImageBitmap(filteredBitmap);
    filteredBitmap = Bitmap.createBitmap(originalBitmap.getWidth(), originalBitmap.getHeight(), originalBitmap.getConfig());
    paint.setBitmap(filteredBitmap);
    paint.setFilterBitmap(true);
    imageView.draw(new Paint());
}

        

Post a Comment