使用到了ColorMatrix。

Java代码:

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 android.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 BrightnessseekBar = 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; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); dstimage = (ImageView) findViewById(R.id.dstImageView); 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, imgHeight, Config.ARGB_8888); SaturationseekBar .setOnSeekBarChangeListener(new OnSeekBarChangeListener() { // 当拖动条的滑块位置发生改变时触发该方法 public void onProgressChanged(SeekBar arg0, int progress, boolean fromUser) { // 创建一个相同尺寸的可变的位图区,用于绘制调色后的图片 Bitmap bmp = Bitmap.createBitmap(imgWidth, imgHeight, Config.ARGB_8888); ColorMatrix cMatrix = new ColorMatrix(); // 设置饱和度 cMatrix.setSaturation((float) (progress / 100.0)); Paint paint = new Paint(); paint.setColorFilter(new ColorMatrixColorFilter(cMatrix)); Canvas canvas = new Canvas(bmp); // 在Canvas上绘制一个已经存在的Bitmap。这样,dstBitmap就和srcBitmap一摸一样了 canvas.drawBitmap(srcBitmap, 0, 0, paint); dstimage.setImageBitmap(bmp); } public void onStartTrackingTouch(SeekBar bar) { } public void onStopTrackingTouch(SeekBar bar) { } }); BrightnessseekBar .setOnSeekBarChangeListener(new OnSeekBarChangeListener() { // 当拖动条的滑块位置发生改变时触发该方法 public void onProgressChanged(SeekBar arg0, int progress, boolean fromUser) { Bitmap bmp = Bitmap.createBitmap(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,// 改变亮度 0, 0, 1, 0, brightness, 0, 0, 0, 1, 0 }); Paint paint = new Paint(); paint.setColorFilter(new ColorMatrixColorFilter(cMatrix)); Canvas canvas = new Canvas(bmp); // 在Canvas上绘制一个已经存在的Bitmap。这样,dstBitmap就和srcBitmap一摸一样了 canvas.drawBitmap(srcBitmap, 0, 0, paint); dstimage.setImageBitmap(bmp); } public void onStartTrackingTouch(SeekBar bar) { } public void onStopTrackingTouch(SeekBar bar) { } }); ContrastseekBar .setOnSeekBarChangeListener(new OnSeekBarChangeListener() { // 当拖动条的滑块位置发生改变时触发该方法 public void onProgressChanged(SeekBar arg0, int progress, boolean fromUser) { Bitmap bmp = Bitmap.createBitmap(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, contrast, 0, 0, 0,// 改变对比度 0, 0, contrast, 0, 0, 0, 0, 0, 1, 0 }); Paint paint = new Paint(); paint.setColorFilter(new ColorMatrixColorFilter(cMatrix)); Canvas canvas = new Canvas(bmp); // 在Canvas上绘制一个已经存在的Bitmap。这样,dstBitmap就和srcBitmap一摸一样了 canvas.drawBitmap(srcBitmap, 0, 0, paint); dstimage.setImageBitmap(bmp); } public void onStartTrackingTouch(SeekBar arg0) { // TODO Auto-generated method stub } public void onStopTrackingTouch(SeekBar seekBar) { // TODO Auto-generated method stub } }); } /** * 需要加载的图片可能是大图,我们需要对其进行合适的缩小处理 * * @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 block e.printStackTrace(); Log.e(this.getClass().getName(), e.getMessage()); } return null; } }布局文件,比较简单:

<?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_width="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:layout_alignParentBottom="true" android:gravity="center_vertical"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/Saturation" /> <!-- 定义一个拖动条,并改变它的滑块外观 --> <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" /> <!-- 定义一个拖动条,并改变它的滑块外观 --> <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" /> <!-- 定义一个拖动条,并改变它的滑块外观 --> <SeekBar android:id="@+id/Contrastseekbar" android:layout_width="fill_parent" android:layout_height="wrap_content" android:progress="63" android:max="127" /> </LinearLayout> </LinearLayout> </LinearLayout>
运行结果:

转载于:https://www.cnblogs.com/java315/archive/2011/11/28/2397404.html

[置顶] Android改变图像的饱和度、亮度和对比度相关推荐

  1. Android改变图像的饱和度 亮度和对比度

    分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow 也欢迎大家转载本篇文章.分享知识,造福人民,实现我们中华民族伟大复兴! 转自:h ...

  2. android listview标题置顶,Android仿QQ左滑删除置顶ListView操作

    最近闲来无事,于是研究了一下qq的左滑删除效果,尝试着实现了一下,先上效果图: 大致思路原理: - 通过设置margin实现菜单的显示与隐藏 - 监听onTouchEvent,处理滑动事件 上代码 i ...

  3. [置顶] Android自定义控件 芝麻信用分雷达图

    [置顶] Android自定义控件 芝麻信用分雷达图 标签: android自定义雷达芝麻信用 2016-10-23 20:11  3548人阅读  评论(24)  收藏  举报   分类: 自定义控 ...

  4. android列表实现置顶,Android利用RecyclerView实现全选、置顶和拖拽功能示例

    Android利用RecyclerView实现全选.置顶和拖拽功能示例 发布时间:2020-08-23 16:26:42 来源:脚本之家 阅读:159 作者:爱开发 前言 今天给大家分享是如何在Rec ...

  5. android imageview 锯齿,[置顶] android 自定义圆角ImageView以及锯齿的处理

    看到很多人开发过程中要使用圆角图片时,解决方法有: 1.重新绘制一张图片 2.通过布局来配置 3.通过重写View来实现 其中1,2在这里就不讲了,重点讲讲方法三的实现. 实现一:通过截取画布一个圆形 ...

  6. Android学习笔记之-:对Android图像色调饱和度亮度处理

    首先也简单介绍下图像的RGBA模型,R指红色(Red),G指绿色(Green),B指蓝色(Blue)及A指透明度(Alpha),由这四种元素搭配组合成了各种各样的颜色. 处理工具类及方法: publi ...

  7. [置顶]android ListView包含Checkbox滑动时状态改变

    题外话: 在xamarin android的开发中基本上所有人都会遇到这个小小的坎,的确有点麻烦,当时我也折腾了好一半天,如果你能看到这篇博客,说明你和我当初也是一样的焦灼,如果你想解决掉这个小小的坎 ...

  8. [置顶]Android 面试题汇总

    MicrosoftInternetExplorer402DocumentNotSpecified7.8 磅Normal0 面试题基础储备 1.Activity相关 a.Activity的特点 1.可见 ...

  9. [置顶] Android adb root权限

    永久root带文件 因为开发需要,我经常会用到adb这个工具(Android Debug Bridge),我们都知道adb shell默认是没有root权限的,修改系统文件就很不方便了,adb pus ...

  10. [置顶] Android玄铁剑之TextView之图文并茂

    传送门:上一节 玄铁剑         金庸武侠第一神剑!剑魔独孤求败四十岁前持之横行天下,后为神雕侠杨过所得,持之亦无敌于天下.神雕侠隐退前将此剑赠与小东邪郭襄.襄阳城破前,郭靖.黄蓉夫妇请高明工匠 ...

最新文章

  1. html背景mov,科技常识:html5自动播放mov格式视频的实例代码
  2. LOAM 代码部分的公式推导(前端里程计部分)
  3. Microsoft SQL server 2008 安装未取得权限操作
  4. python监控网页更新_python监控网页更新
  5. Opencv--findHomography 与 getPerspectiveTransform异同
  6. realme x2 深度测试打不开_realme 的产品到底是不是贴牌的?
  7. java 如何去掉http debug日志_你居然还去服务器上捞日志,搭个日志收集系统难道不香么!...
  8. 《3S 新闻周刊》No.14:从融资到裁员,灵图那些事儿
  9. java instanceof和isInstance的关系 精析
  10. 如何按照页面载入进度制作进度条??
  11. ViewFlipper(翻转视图)使用详解
  12. 软件工程——清华大学《软件工程》课程学习与分享
  13. pycharm的安装,简单使用
  14. 深蓝学院-视觉SLAM课程-第4讲作业(T5矩阵微分,T6手写高斯牛顿,T7批量MLE)
  15. X509数字证书格式
  16. Linux升级ilo,利用HP iLO4安装系统
  17. 用Java给您的图片瘦身之Thumbnailator技术
  18. 罗永浩跟罗振宇八个半小时都聊了些什么
  19. CSS-背景颜色 | background-color
  20. python话费充值_Python 登录移动查询话费

热门文章

  1. 汇编调用C函数--利用堆栈传递参数
  2. 初次见面C#排坑记录
  3. datatable删除行、列
  4. javascript基础知识(6) 对象
  5. Spark on YARN的部署
  6. MYSQL中5.7.10ROOT密码及创建用户
  7. Json.net说法——(四)序列化错误处理
  8. 一个Repeater的分页方法
  9. ROS:launch文件的语法规范
  10. MySQL多线程备份工具mydumper