前言
在开发中,难免会遇到一些对照片做处理的案例,而本文则借鉴一些网络上的资源,并且整理分享出来。
定义
滤镜主要是用来实现图像的各种特殊效果,一般在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;
}
}