package com.huangfei.example;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ColorFilter;
import android.graphics.ColorMatrix;
import android.graphics.ColorMatrixColorFilter;
import android.graphics.Paint;
/**
* Created by Administrator on 2016/5/26.
* 图像处理工具类
*/
public class ImageHelper {
/**
* 改变图像色光属性
*
* @param bm
* @param hue 色调
* @param saturation 饱和度
* @param lum 亮度
* @return
*/
public static Bitmap handleImageEffect(Bitmap bm, float hue, float saturation, float lum) {
//设置颜色的色调
ColorMatrix hueMatrix = new ColorMatrix();
//第一个参数,系统分别使用0、1、2来代表Red、Green、Blue三种颜色的处理;而第二个参数,就是需要处理的值
hueMatrix.setRotate(0, hue);
hueMatrix.setRotate(1, hue);
hueMatrix.setRotate(2, hue);
//设置颜色的饱和度
ColorMatrix saturationMatrix = new ColorMatrix();
//saturation参数即代表设置颜色的饱和度的值,当饱和度为0时,图像就变成灰度图像了
saturationMatrix.setSaturation(saturation);
//设置颜色的亮度
ColorMatrix lumMatrix = new ColorMatrix();
lumMatrix.setScale(lum, lum, lum, 1);
//将矩阵的作用效果混合,从而叠加处理效果
ColorMatrix imageMatrix = new ColorMatrix();
imageMatrix.postConcat(hueMatrix);
imageMatrix.postConcat(saturationMatrix);
imageMatrix.postConcat(lumMatrix);
/**
* 设置好处理的颜色矩阵后,通过使用Paint类的setColorFilter()方法,将通过imageMatrix构造的
* ColorMatrixColorFilter对象传递进去,并使用这个画笔来绘制原来的图像,从而将颜色矩阵作用到原图中
*/
Paint paint = new Paint();
paint.setColorFilter(new ColorMatrixColorFilter(imageMatrix));
/**
* Android系统也不允许直接修改原图,类似Photoshop中的锁定,必须通过原图创建一个同样大小的Bitmap,并将
* 原图绘制到该Bitmap中,以一个副本的形式来修改图像。
*/
Bitmap bitmap = Bitmap.createBitmap(bm.getWidth(), bm.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
canvas.drawBitmap(bm, 0, 0, paint);
return bitmap;
}
//底片效果
public static Bitmap handleImageNegative(Bitmap bitmap) {
int width = bitmap.getWidth();
int height = bitmap.getHeight();
int color;
int r, g, b, a;
Bitmap btm = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
int[] oldPx = new int[width * height];
int[] newPx = new int[width * height];
bitmap.getPixels(oldPx, 0, width, 0, 0, width, height);
for (int i = 0; i < width * height; i++) {
color = oldPx[i];
r = Color.red(color);
g = Color.green(color);
b = Color.blue(color);
a = Color.alpha(color);
r = 255 - r;
g = 255 - g;
b = 255 - b;
if (r > 255)
r = 255;
else if (r < 0)
r = 0;
if (g > 255)
g = 255;
else if (g < 0)
g = 0;
if (b > 255)
b = 255;
else if (b < 0)
b = 0;
newPx[i] = Color.argb(a, r, g, b);
}
btm.setPixels(newPx, 0, width, 0, 0, width, height);
return btm;
}
//老照片效果
public static Bitmap handleImagePixelsOldPhoto(Bitmap bm) {
int width = bm.getWidth();
int height = bm.getHeight();
int color;
int r, g, b, a, r1, g1, b1;
Bitmap bmp = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
int[] oldPx = new int[width * height];
int[] newPx = new int[width * height];
bm.getPixels(oldPx, 0, bm.getWidth(), 0, 0, width, height);
for (int i = 0; i < width * height; i++) {
color = oldPx[i];
a = Color.alpha(color);
r = Color.red(color);
g = Color.green(color);
b = Color.blue(color);
r1 = (int) (0.393 * r + 0.769 * g + 0.189 * b);
g1 = (int) (0.349 * r + 0.686 * g + 0.168 * b);
b1 = (int) (0.272 * r + 0.534 * g + 0.131 * b);
if (r1 > 255) {
r1 = 255;
}
if (g1 > 255) {
g1 = 255;
}
if (b1 > 255) {
b1 = 255;
}
newPx[i] = Color.argb(a, r1, g1, b1);
}
bmp.setPixels(newPx, 0, width, 0, 0, width, height);
return bmp;
}
//浮雕效果
public static Bitmap handleImagePixelsRelief(Bitmap bm) {
int width = bm.getWidth();
int height = bm.getHeight();
int color, colorBefore;
int a, r, g, b;
int r1, g1, b1;
Bitmap bmp = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
int[] oldPx = new int[width * height];
int[] newPx = new int[width * height];
bm.getPixels(oldPx, 0, bm.getWidth(), 0, 0, width, height);
for (int i = 1; i < width * height; i++) {
colorBefore = oldPx[i - 1];
a = Color.alpha(colorBefore);
r = Color.red(colorBefore);
g = Color.green(colorBefore);
b = Color.blue(colorBefore);
color = oldPx[i];
r1 = Color.red(color);
g1 = Color.green(color);
b1 = Color.blue(color);
r = r - r1 + 127;
g = g - g1 + 127;
b = b - b1 + 127;
if (r > 255) {
r = 255;
}
if (g > 255) {
g = 255;
}
if (b > 255) {
b = 255;
}
newPx[i] = Color.argb(a, r, g, b);
}
bmp.setPixels(newPx, 0, width, 0, 0, width, height);
return bmp;
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
Android绘图机制与处理技巧(二)——Android图像处理之色彩特效处理
共31个文件
xml:12个
png:8个
java:7个
需积分: 16 20 下载量 52 浏览量
2016-05-27
08:38:48
上传
评论
收藏 1.02MB ZIP 举报
温馨提示
Android对于图片处理,最常使用到的数据结构是位图——Bitmap,它包含了一张图片所有的数据。整个图片都是由点阵和颜色值组成的,所谓点阵就是一个包含像素的矩阵,每一个元素对应着图片的一个像素。而颜色值——ARGB,分别对应透明图、红、绿、蓝这四个通道分量,它们共同决定了每个像素点显示的颜色。
资源推荐
资源详情
资源评论
收起资源包目录
app.zip (31个子文件)
app
src
androidTest
java
com
huangfei
example
ApplicationTest.java 363B
test
java
com
huangfei
example
ExampleUnitTest.java 327B
main
AndroidManifest.xml 973B
res
mipmap-hdpi
ic_launcher.png 3KB
values-v21
styles.xml 337B
mipmap-xxxhdpi
ic_launcher.png 10KB
mipmap-xhdpi
ic_launcher.png 5KB
menu
menu_main.xml 419B
mipmap-mdpi
ic_launcher.png 2KB
drawable
layout
activity_pixels_effect.xml 1KB
activity_primary_color.xml 1KB
activity_color_matrix.xml 1KB
activity_main.xml 1KB
values-w820dp
dimens.xml 364B
values
colors.xml 214B
strings.xml 134B
styles.xml 728B
dimens.xml 259B
mipmap-xxhdpi
ic_launcher.png 8KB
test1.png 412KB
test2.png 261KB
test3.png 322KB
java
com
huangfei
example
ImageHelper.java 6KB
MainActivity.java 1KB
PixelsEffectActivity.java 1KB
PrimaryColorActivity.java 3KB
ColorMatrixActivity.java 3KB
libs
app.iml 8KB
.gitignore 8B
build.gradle 696B
proguard-rules.pro 654B
共 31 条
- 1
资源评论
黄飞_hf
- 粉丝: 52
- 资源: 65
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 聊天系统(java+applet).zip
- 毕业设计:基于SSM的mysql-高校学生请假管理系统(源码 + 数据库 + 说明文档)
- 博客系统(struts+hibernate+spring).rar
- c语言学生成绩管理系统源码.zip
- 毕业设计:基于SSM的mysql-网约车用户服务平台(源码 + 数据库 + 说明文档)
- 内容管理系统(hibernate3+struts2+spring2)130224.rar
- 基于Java的班级管理系统课程设计源码
- 内容管理系统(hibernate3+struts2+spring2).rar
- 路由器刷breed Web控制台助手v5.8版本.rar
- Java 在 JEP 12 提供的特性预览
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功