Android中图像之-平滑滤镜算法


前言

在开发中,难免会遇到一些对照片做处理的案例,而本文则借鉴一些网络上的资源,并且整理分享出来。

定义

滤镜主要是用来实现图像的各种特殊效果,一般在Photoshop中应用广泛。

平滑滤镜

平滑滤镜的算法工具类,用来对图片进行处理。
public class Box_PHUtil {
	static float [][] BOX_PH={//普通的box的矩阵
		{1/9f,1/9f,1/9f},
		{1/9f,1/9f,1/9f},
		{1/9f,1/9f,1/9f}
     }; 
	
	public static Bitmap LoadBitmap(Resources resources, int yuantu) {
		Bitmap result=BitmapFactory.decodeResource(resources, yuantu);
		   return result;
	}
    public static Bitmap gaosiprocess(Bitmap bm) {
		Bitmap bmTemp=processBitmap(bm,BOX_PH);
		return bmTemp;
	}
    private static Bitmap processBitmap(Bitmap bm, float[][] bOX_PH2) {
		Bitmap result=null;
		int w=bm.getWidth();
		int h=bm.getHeight();
		int []  pixels=new int[w*h];
		bm.getPixels(pixels, 0, w, 0, 0, w, h);//将图像的每个像素读到数组中
		int alpha[]=new int[w * h];//定义RGB的矩阵
	    int red[]=new int[w * h];
	    int green[]=new int[w * h];
	    int blue[]=new int[w * h];
	    for (int j = 0; j < h; j++) {//初始化相应的矩阵
	           for (int i = 0; i < w; i++){
	               int tempPixel=pixels[j * w + i];   //将图像的像素的值里面的RGB  分离 出来
	               alpha[j * w + i]=(tempPixel >> 24) & 0xff;//向右移动24位,与0xff(11111111)进行与运算
	               red[j * w + i]=(tempPixel >>16) & 0xff;  //向右移动16位,与0xff(11111111)进行与运算
	               green[j * w + i]=(tempPixel >> 8) & 0xff;//向右移动8位,与0xff(11111111)进行与运算
	               blue[j * w + i]=(tempPixel)>>0 & 0xff;      //与0xff(11111111)进行与运算
	          }}
	     red=processMatrix(red,w,h,bOX_PH2);//处理RGB矩阵     根据不同的矩阵得到不同的RGB的数值
	     green=processMatrix(green,w,h,bOX_PH2);  
	     blue=processMatrix(blue,w,h,bOX_PH2); 
	     int pixelsTarget[]=new int[w * h]; //恢复图
	     for (int j = 0; j < h; j++) {
	          for (int i = 0; i < w; i++) {
	                pixelsTarget[j * w + i]+=alpha[j * w + i]<<24;
	                pixelsTarget[j * w + i]+=red[j * w + i]<<16;
	                pixelsTarget[j * w + i]+=green[j * w + i]<<8;
	                pixelsTarget[j * w + i]+=blue[j * w + i];
	           }}
	       
	     result=Bitmap.createBitmap(pixelsTarget, w, h, Bitmap.Config.ARGB_8888); 
	     return result;
	}
    private static int[] processMatrix(int[] rgb, int w, int h,float[][] bOX_PH2) {
		float[] temprgb=new float[w * h];//滤镜处理RGB矩阵
		int[] temprgbInt=new int[w * h];
		int span=bOX_PH2.length;   //3
		for(int i=1;i<=w-2;i++){//对图片的每一个像素进行处理
			for(int j=1;j<=h-2;j++){     //根据矩阵运算
				temprgb[j*w+i]=0;
				for(int k=0;k<span;k++){
					for(int l=0;l<span;l++){
						temprgb[j*w+i]+=bOX_PH2[k][l]*rgb[(j-1+k)*w+(i-1+l)];
					}}
				if(temprgb[j*w+i]>=255){
					temprgb[j*w+i]=255;
				}
				else if(temprgb[j*w+i]<=0){
					temprgb[j*w+i]=0;
				}
               temprgbInt[j*w+i]=(int)temprgb[j*w+i];
			}}
	   return temprgbInt;
	}
}


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值