Android changes the image saturation, brightness, and contrast.

Source: Internet
Author: User

Colormatrix is used.

Java code:

Package COM. figo. imgedit; import Java. io. filenotfoundexception; import android. app. activity; import android. graphics. bitmap; import android. graphics. bitmap. config; import android. graphics. bitmapfactory; import android. graphics. canvas; import android. graphics. colormatrix; import android. graphics. colormatrixcolorfilter; import android. graphics. paint; import android.net. uri; import android. OS. bundle; import Andro Id. util. log; import android. widget. imageview; import android. widget. seekbar; import android. widget. seekbar. onseekbarchangelistener; public class imgeditactivity extends activity {/** called when the activity is first created. */private bitmap srcbitmap, dstbitmap; private string pathname = "/sdcard/testimg.jpg"; private imageview dstimage = NULL; private seekbar saturationseekbar = NULL; private seekbar B Rightnessseekbar = NULL; private seekbar contrastseekbar = NULL; private int imgheight, imgwidth; public static final int picture = 0; public static final int max_width = 240; public static final int max_height = 240; private URI imageuri; @ overridepublic void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); dstimage = (imageview) findviewbyid (R. id. DST Imageview); saturationseekbar = (seekbar) findviewbyid (R. id. saturationseekbar); brightnessseekbar = (seekbar) findviewbyid (R. id. brightnessseekbar); contrastseekbar = (seekbar) findviewbyid (R. id. contrastseekbar); srcbitmap = bitmapfactory. decodefile (pathname); dstimage. setimagebitmap (srcbitmap); imgheight = srcbitmap. getheight (); imgwidth = srcbitmap. getwidth (); dstbitmap = bitmap. createbitmap (imgwidth, Im Gheight, config. argb_8888); saturationseekbar. setonseekbarchangelistener (New onseekbarchangelistener () {// This method is triggered when the slider position of the drag bar changes (seekbar arg0, int progress, Boolean fromuser) {// create a variable bitmap area of the same size to draw bitmap BMP = bitmap. createbitmap (imgwidth, imgheight, config. argb_8888); colormatrix cmatrix = new colormatrix (); // sets the saturation cmatrix. setsaturation (float) (Progress/100.0); P Aint paint = new paint (); paint. setcolorfilter (New colormatrixcolorfilter (cmatrix); canvas = new canvas (BMP); // draw an existing bitmap on the canvas. In this way, dstbitmap is the same as srcbitmap in canvas. drawbitmap (srcbitmap, 0, 0, paint); dstimage. setimagebitmap (BMP);} public void onstarttrackingtouch (seekbar bar) {}} public void onstoptrackingtouch (seekbar bar) {}}); brightnessseekbar. setonseekbarchangelistener (New onseekbarchangelistener () {// This method is triggered when the slider position of the drag bar changes public void onprogresschanged (seekbar arg0, int progress, Boolean fromuser) {bitmap BMP = bitmap. createbit Map (imgwidth, imgheight, config. argb_8888); int brightness = progress-127; colormatrix cmatrix = new colormatrix (); cmatrix. set (new float [] {1, 0, 0, 0, brightness, 0, 1, 0, 0, brightness, // change the brightness of 0, 0, 1, 0, brightness, 0, 0, 0, 1, 0}); paint = new paint (); paint. setcolorfilter (New colormatrixcolorfilter (cmatrix); canvas = new canvas (BMP); // draw an existing bitmap on the canvas. In this way, dstbitmap is the same as srcbitmap in canvas. drawbitmap (srcbitmap, 0, 0, paint); dstimage. setimagebitmap (BMP);} public void onstarttrackingtouch (seekbar bar) {}} public void onstoptrackingtouch (seekbar bar) {}}); contrastseekbar. setonseekbarchangelistener (New onseekbarchangelistener () {// This method is triggered when the slider position of the drag bar changes public void onprogresschanged (seekbar arg0, int progress, Boolean fromuser) {bitmap BMP = bitmap. createbitma P (imgwidth, imgheight, config. argb_8888); // int brightness = progress-127; float contrast = (float) (Progress + 64)/128.0); colormatrix cmatrix = new colormatrix (); cmatrix. set (new float [] {contrast, 0, 0, 0, 0, 0, 0, contrast, 0, 0, // change the contrast of 0, 0, contrast, 0, 0, 0, 0, 0, 1, 0}); paint = new paint (); paint. setcolorfilter (New colormatrixcolorfilter (cmatrix); canvas = new canvas (BMP );// Draw an existing bitmap on the canvas. In this way, dstbitmap is the same as srcbitmap in canvas. drawbitmap (srcbitmap, 0, 0, paint); dstimage. setimagebitmap (BMP);} public void onstarttrackingtouch (seekbar arg0) {// todo auto-generated method stub} public void onstoptrackingtouch (seekbar) {// todo auto-generated method stub});}/*** the image to be loaded may be a large image, we need to perform proper reduction processing for it ** @ Param imageuri */private bitmap getsrcimage (URI imageuri) {try {bitmapfactory. options Ops = new bitmapfactory. options (); ops. injustdecodebounds = true; bitmap BMP = bitmapfactory. decodestream (this. getcontentresolver (). openinputstream (imageuri), null, OPS); int wratio = (INT) math. ceil (Ops. outwidth/(float) max_width); int hratio = (INT) math. ceil (Ops. outheight/(float) max_height); If (wratio> 1 & hratio> 1) {If (wratio> hratio) {ops. insamplesize = wratio;} else {ops. insamplesize = hratio ;}} ops. injustdecodebounds = false; BMP = bitmapfactory. decodestream (this. getcontentresolver (). openinputstream (imageuri), null, OPS); Return BMP;} catch (filenotfoundexception e) {// todo auto-generated catch blocke. printstacktrace (); log. E (this. getclass (). getname (), E. getmessage () ;}return null ;}}

Layout file, relatively simple:

<? XML version = "1.0" encoding = "UTF-8"?> <Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" Android: layout_width = "fill_parent" Android: layout_height = "fill_parent" Android: Orientation = "vertical"> <imageview Android: id = "@ + ID/dstimageview" Android: scaletype = "fitcenter" Android: layout_width = "fill_parent" Android: layout_height = "wrap_content" Android: maxwidth = "240px" Android: maxheight = "240px"/> <linearlayout Android: layout_widt H = "fill_parent" Android: layout_height = "wrap_content" Android: Orientation = "vertical"> <linearlayout Android: layout_width = "fill_parent" Android: layout_height = "wrap_content" Android: orientation = "horizontal" Android: Orientation = "true" Android: gravity = "center_vertical"> <textview Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: TEXT = "@ string/saturation"/> <! -- Define a drag bar and change its slider appearance --> <seekbar Android: Id = "@ + ID/saturationseekbar" Android: layout_width = "fill_parent" Android: layout_height = "wrap_content" Android: Progress = "100" Android: max = "200"/> </linearlayout> <linearlayout Android: layout_width = "fill_parent" Android: layout_height = "wrap_content" Android: Orientation = "horizontal" Android: gravity = "center_vertical"> <textview Android: layout_width = "wrap_content" Android: Layout_height = "wrap_content" Android: text = "@ string/brightness"/> <! -- Define a drag bar and change its slider appearance --> <seekbar Android: Id = "@ + ID/brightnessseekbar" Android: layout_width = "fill_parent" Android: layout_height = "wrap_content" Android: Progress = "127" Android: max = "255"/> </linearlayout> <linearlayout Android: layout_width = "fill_parent" Android: layout_height = "wrap_content" Android: Orientation = "horizontal" Android: gravity = "center_vertical"> <textview Android: layout_width = "wrap_content" Android: Layout_height = "wrap_content" Android: text = "@ string/contrast"/> <! -- Define a drag bar and change its slider appearance --> <seekbar Android: Id = "@ + ID/contrastseekbar" Android: layout_width = "fill_parent" Android: layout_height = "wrap_content" Android: Progress = "63" Android: max = "127"/> </linearlayout>

Running result:

Related Article

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.