Android图像处理之色彩特效处理

本文介绍了Android中利用ColorMatrix进行图像色彩处理,包括改变色光属性、实现灰度、反转、怀旧等效果,并探讨了像素点分析及处理,如底片、老照片和浮雕效果的算法实现。
摘要由CSDN通过智能技术生成

参考《Android群英传》6.6

一、改变色光属性

        Android系统中封装了ColorMatrix类,就是颜色矩阵,通过这个类可以很方便的改变矩阵值来处理颜色效果。创建一个ColorMatrix和创建普通类是一样的。

        ColorMatrix中改变色光属性的几个方法:

        setRotate(axis, degrees):设置颜色的色调。

        第一个参数:系统分别用了0,1,2代表Red,Green,Blue三种颜色的处理。

        第二个参数:需要处理的值。

       ColorMatrix hueMatrix = new ColorMatrix();
       hueMatrix.setRotate(0, hue);
       hueMatrix.setRotate(1, hue);
       hueMatrix.setRotate(2, hue);

        setSaturation(saturation):设置颜色的饱和度。参数就是颜色饱和度的值,饱和度为0时,图像就成灰度图像了。

		ColorMatrix saturationMatrix = new ColorMatrix();
		saturationMatrix.setSaturation(saturation);

        setScale(rScale, gScale, bScale, aScale):设置图像亮度,当三原色以相同比例混合时,就会显示出白色,当亮度为0时,图像就全黑了。

		ColorMatrix lumMatrix  = new ColorMatrix();
		lumMatrix.setScale(lum, hue, saturation, 1);

        postConcat(hueMatrix):将以上三种方式处理的效果叠加起来显示混合效果。

		ColorMatrix colorMatrix = new ColorMatrix();
		colorMatrix.postConcat(hueMatrix);
		colorMatrix.postConcat(saturationMatrix);
		colorMatrix.postConcat(lumMatrix);

实例代码:

package com.example.androidcolormatrix;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.ColorMatrix;
import android.graphics.ColorMatrixColorFilter;
import android.graphics.Paint;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;

public class SeekBarActivity extends Activity implements OnSeekBarChangeListener {
	private ImageView imageView;
	private Bitmap bitmap;
	private SeekBar seekbarHue,seekbarSaturation,seekbarLum;
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		//初始化组件
		imageView = (ImageView) findViewById(R.id.imageView);
		seekbarHue = (SeekBar) findViewById(R.id.seekbarHue);
		seekbarSaturation = (SeekBar) findViewById(R.id.seekbarSaturation);
		seekbarLum = (SeekBar) findViewById(R.id.seekbarLum);
		
		//给拖动条设置初值
		seekbarHue.setProgress(50);
		seekbarSaturation.setProgress(50);
		seekbarLum.setProgress(50);
		
		//给imageview添加位图
		bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.we);
		imageView.setImageBitmap(bitmap);
		
		//给拖动条添加监听
		seekbarHue.setOnSeekBarChangeListener(this);
		seekbarSaturation.setOnSeekBarChangeListener(this);
		seekbarLum.setOnSeekBarChangeListener(this);
		
	}
	
	
	//设置ColorMatrix返回设置之后的位图
	private Bitmap handleImage(Bitmap bm, float hue, float saturation, float lum){
		Bitmap bmp = Bitmap.createBitmap(bm.getWidth(), bm.getHeight(), Bitmap.Config.ARGB_8888);
		Canvas canvas = new Canvas(bmp);
		Paint paint = new Paint();
		
		ColorMatrix hueMatrix = new ColorMatrix();
		hueMatrix.setRotate(0, hue);
		hueMatrix.setRotate(1, hue);
		hueMatrix.setRotate(2, hue);
		
		ColorMatrix saturationMatrix = new ColorMatrix();
		saturationMatrix.setSaturation(saturation);
		
		ColorMatrix lumMatrix  = new ColorMatrix();
		lumMatrix.setScale(lum, hue, saturation, 1);

		
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值