Android Learning notes-: color saturation brightness processing for Android images

Source: Internet
Author: User

First, the RGBA model of the image is also briefly introduced, R refers to Red, G refers to Green, and B refers to Blue and a refers to transparency (ALPHA), which is combined into a variety of colors by the four elements.

Processing tool classes and methods:

public class Imagetools {
/**
* Processing of images
* @description:
* @date 2015-8-12 8:45:05
*/
public static Bitmap Getcolorimage (Bitmap Bitmap, float SX, float Bhd, float ld) {//Parameters are hue, saturation and brightness, respectively
Bitmap bmp = Bitmap.createbitmap (Bitmap.getwidth (), Bitmap.getheight (), Bitmap.Config.ARGB_8888);
Canvas canvas = new canvas (BMP);
Paint paint = new paint (Paint.anti_alias_flag);
ColorMatrix Sxmatrix = new ColorMatrix ();//Set Hue
Sxmatrix.setrotate (0, SX);
Sxmatrix.setrotate (1, SX);
Sxmatrix.setrotate (2, SX);

ColorMatrix Bhdmatrix = new ColorMatrix ();//Set saturation
Bhdmatrix.setsaturation (BHD);

ColorMatrix Ldmatrix = new ColorMatrix ();//Set brightness
Ldmatrix.setscale (LD, LD, LD, 1);

ColorMatrix Mixmatrix = new ColorMatrix ();//Set overall effect
Mixmatrix.postconcat (Sxmatrix);
Mixmatrix.postconcat (Bhdmatrix);
Mixmatrix.postconcat (Ldmatrix);
Paint.setcolorfilter (New Colormatrixcolorfilter (Mixmatrix));//Filter by Color filter

Canvas.drawbitmap (BMP, 0, 0, paint);//re-draw
return BMP;
}
}

Select the ImageView to be processed and the corresponding bitmap in the activity, and invoke the method in the tool class:

Private ImageView Coloriv;
Private SeekBar Sxbar, Bhdbar, Ldbar;
private static int min_color = 100;
private static int max_color = 255;
Private float SX, Bhd, LD;
Private Bitmap bmp;

@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.color_main);
Initviews ();
}

private void Initviews () {
Bmp=bitmapfactory.decoderesource (Getresources (), r.drawable.ic_launcher);
Coloriv = (ImageView) Findviewbyid (R.ID.COLOR_IV);
Coloriv.setimagebitmap (BMP);
Sxbar = (SeekBar) Findviewbyid (R.id.sx_seekbar);
Bhdbar = (SeekBar) Findviewbyid (R.id.bhd_seekbar);
Ldbar = (SeekBar) Findviewbyid (R.id.ld_seekbar);
Sxbar.setonseekbarchangelistener (this);
Sxbar.setmax (Max_color);//Set maximum value
Sxbar.setprogress (Min_color);//Set initial value (current value)
Bhdbar.setonseekbarchangelistener (this);
Bhdbar.setmax (Max_color);
Bhdbar.setprogress (Min_color);
Ldbar.setonseekbarchangelistener (this);
Ldbar.setmax (Max_color);
Ldbar.setprogress (Min_color);
}

@Override
public void onprogresschanged (SeekBar SeekBar, int progress, Boolean fromuser) {
Switch (Seekbar.getid ()) {
Case R.id.sx_seekbar:
SX = (progress-min_color) * 1.0f/min_color * 180;
Break
Case R.id.bhd_seekbar:
BHD = progress * 1.0F/MIN_COLOR;
Break
Case R.id.ld_seekbar:
LD = progress * 1.0F/MIN_COLOR;
Break
}
Coloriv.setimagebitmap (Imagetools.getcolorimage (BMP, SX, Bhd, LD));
}

@Override
public void Onstarttrackingtouch (SeekBar SeekBar) {
}


@Override
public void Onstoptrackingtouch (SeekBar SeekBar) {
}

XML layout file:

<relativelayout xmlns:android= "Http://schemas.android.com/apk/res/android"
Xmlns:tools= "Http://schemas.android.com/tools"
Android:layout_width= "Match_parent"
android:layout_height= "Match_parent" >

<imageview
Android:id= "@+id/color_iv"
Android:layout_width= "300DP"
android:layout_height= "300DP"
Android:layout_centerhorizontal= "true"
Android:layout_marginbottom= "15DP"
android:layout_margintop= "15DP"/>

<seekbar
Android:id= "@+id/sx_seekbar"
Android:layout_width= "Match_parent"
android:layout_height= "Wrap_content"
android:layout_below= "@id/color_iv"
/>

<seekbar
Android:id= "@+id/bhd_seekbar"
Android:layout_width= "Match_parent"
android:layout_height= "Wrap_content"
android:layout_below= "@id/sx_seekbar"
android:layout_margintop= "10DP"/>

<seekbar
Android:id= "@+id/ld_seekbar"
Android:layout_width= "Match_parent"
android:layout_height= "Wrap_content"
android:layout_below= "@id/bhd_seekbar"
android:layout_margintop= "10DP"/>

</RelativeLayout>

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Android Learning notes-: color saturation brightness processing for Android images

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.