文章标题
来源:互联网 发布:linux 递归授权 编辑:程序博客网 时间:2024/05/29 12:10
今天学习一下图片处理,为了自己以后也可以看看,加深记忆,这里只是一写图片处理的工具类,但是方法并不是很多,分享给大家,仅供大家参考。
package fly.wu.com.androidbook.Utils;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ColorMatrix;
import android.graphics.ColorMatrixColorFilter;
import android.graphics.Paint;
/**
* 图像处理的工具类
* Created by fly.wu on 2017/3/18.
*/
public class HandlerImageUtils {/** * @param bm 图片 * @param hue 色相 * @param stautration 饱和度 * @param lum 亮度 * @return Bitmap */public static Bitmap handlerImage(Bitmap bm, float hue, float stautration, float lum) { //不能在原图上直接处理图片 Bitmap bitmap = Bitmap.createBitmap(bm.getWidth(), bm.getHeight(), Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(bitmap); Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG); //色相 ColorMatrix hueMatrix = new ColorMatrix(); hueMatrix.setRotate(0, hue); hueMatrix.setRotate(1, hue); hueMatrix.setRotate(2, hue); //饱和度 ColorMatrix stautrationMatrix = new ColorMatrix(); stautrationMatrix.setSaturation(stautration); //亮度 ColorMatrix lumMatrix = new ColorMatrix(); lumMatrix.setScale(lum, lum, lum, 1); //整合 ColorMatrix matrix = new ColorMatrix(); matrix.postConcat(hueMatrix); matrix.postConcat(stautrationMatrix); matrix.postConcat(lumMatrix); //画到画布上 paint.setColorFilter(new ColorMatrixColorFilter(matrix)); canvas.drawBitmap(bm, 0, 0, paint); return bitmap;}//底片效果 negativepublic static Bitmap handleImageNegative(Bitmap bm) { int width = bm.getWidth(); int height = bm.getHeight(); int color; int r, g, b, a; 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, 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); } bmp.setPixels(newPx, 0, width, 0, 0, width, height); return bmp;}//怀旧效果 public static Bitmap handleImageNostalgia(Bitmap bm) { int width = bm.getWidth(); int height = bm.getHeight(); int color; int r, g, b, a,r1,b1,g1; 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, 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; 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; } else if (r1 < 0) { r1 = 0; } if (g1 > 255) { g1 = 255; } else if (g1 < 0) { g1= 0; } if (b1 > 255) { b1 = 255; } else if (b1 < 0) { b1 = 0; } newPx[i] = Color.argb(a, r1, g1, b1); } bmp.setPixels(newPx, 0, width, 0, 0, width, height); return bmp;}//浮雕效果public static Bitmap handleImageRelief(Bitmap bm) { int width = bm.getWidth(); int height = bm.getHeight(); int color,beforeColor; int r, g, b, a1,r1,b1,g1; 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, width, 0, 0, width, height); for (int i = 1; i < width * height; i++) { beforeColor = oldPx[i - 1]; r1 = Color.red(beforeColor); g1 = Color.green(beforeColor); b1 = Color.blue(beforeColor); a1 = Color.alpha(beforeColor); color = oldPx[i]; r = Color.red(color); g = Color.green(color); b = Color.blue(color); r1 = (r1 - r +127); g1 = (g1 - g +127); b1 = (b1 - b +127); if (r1 > 255) { r1 = 255; } else if (r1 < 0) { r1 = 0; } if (g1 > 255) { g1 = 255; } else if (g1 < 0) { g1= 0; } if (b1 > 255) { b1 = 255; } else if (b1 < 0) { b1 = 0; } newPx[i] = Color.argb(a1, r1, g1, b1); } bmp.setPixels(newPx, 0, width, 0, 0, width, height); return bmp;}
}
如果那里有错可以给我提出来,然后我在改,如果你们有更好的图片的处理技术也可以和我分享一下,谢谢。
1 0
- 文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题 文章标题 文章标题 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 工作中用到的零散知识-链接汇总
- Liunx常用命令解析(一)--导航命令
- Oracle lsnrctl命令使用总结
- Ubuntu搭建Caffe(仅CPU)
- Struts2学习1——概述
- 文章标题
- 数字和的运算算法剖析
- 校内互测 [from abclzr] T2 (DP)
- Python3 cookbook学习笔记-数据结构与算法6
- HTTPS和HTTP的区别
- 51nod 1158 全是1的最大子矩阵 单调栈
- MCU学习——无线遥控模块
- iOS 面试题
- 子网掩码及子网划分问题