zoukankan      html  css  js  c++  java
  • android 图像处理(黑白,模糊,浮雕,圆角,镜像,底片,油画,灰白,加旧,哈哈镜,放大镜)

    原图:

    一:圆角处理

    效果:

    代码:

     

    1. public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, float roundPx)  
    2.     {  
    3.   
    4.         Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),  
    5.                 bitmap.getHeight(), Config.ARGB_8888);  
    6.         Canvas canvas = new Canvas(output);  
    7.   
    8.         final int color = 0xff424242;  
    9.         final Paint paint = new Paint();  
    10.         final Rect rect = new Rect(00, bitmap.getWidth(), bitmap.getHeight());  
    11.         final RectF rectF = new RectF(rect);  
    12.   
    13.         paint.setAntiAlias(true);  
    14.         canvas.drawARGB(0000);  
    15.         paint.setColor(color);  
    16.         canvas.drawRoundRect(rectF, roundPx, roundPx, paint);  
    17.   
    18.         paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));  
    19.         canvas.drawBitmap(bitmap, rect, rect, paint);  
    20.   
    21.         return output;  
    22.     }  

     

     理解:

    这个就简单了,实际上是在原图片上画了一个圆角遮罩。对于paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));方法我刚看到也是一知半解Mode.SRC_IN参数是个画图模式,该类型是指只显示两层图案的交集部分,且交集部位只显示上层图像。实际就是先画了一个圆角矩形的过滤框,于是形状有了,再将框中的内容填充为图片。该参数总过有十八种:

     

    public static final PorterDuff.Mode ADD

    public static final PorterDuff.Mode CLEAR

    public static final PorterDuff.Mode DST

    public static final PorterDuff.Mode DST_ATOP

    public static final PorterDuff.Mode DST_IN

    and so on;

     

    是区分不同的画图叠加效果,这个人的博客讲的很清楚:http://www.cnblogs.com/sank615/archive/2013/03/12/2955675.html。我也没做demo,所以不啰嗦了。

    二:灰白处理

    效果:

    代码:

     

    1. public static Bitmap toGrayscale(Bitmap bmpOriginal)  
    2.     {  
    3.         int width, height;  
    4.         height = bmpOriginal.getHeight();  
    5.         width = bmpOriginal.getWidth();  
    6.   
    7.         Bitmap bmpGrayscale = Bitmap.createBitmap(width, height,  
    8.                 Bitmap.Config.RGB_565);  
    9.         Canvas c = new Canvas(bmpGrayscale);  
    10.         Paint paint = new Paint();  
    11.         ColorMatrix cm = new ColorMatrix();  
    12.         cm.setSaturation(0);  
    13.         ColorMatrixColorFilter f = new ColorMatrixColorFilter(cm);  
    14.         paint.setColorFilter(f);  
    15.         c.drawBitmap(bmpOriginal, 00, paint);  
    16.         return bmpGrayscale;  
    17.     }  

    理解:

     

    这个也没什么好说的,就是利用了ColorMatrix 类自带的设置饱和度的方法setSaturation()。不过其方法内部实现的更深一层是利用颜色矩阵的乘法实现的,对于颜色矩阵的乘法下面还有使用。

    三 黑白处理

    效果:

    代码:

     

    1. public static Bitmap toHeibai(Bitmap mBitmap)  
    2.     {  
    3.         int mBitmapWidth = 0;  
    4.         int mBitmapHeight = 0;  
    5.   
    6.         mBitmapWidth = mBitmap.getWidth();  
    7.         mBitmapHeight = mBitmap.getHeight();  
    8.         Bitmap bmpReturn = Bitmap.createBitmap(mBitmapWidth, mBitmapHeight,  
    9.                 Bitmap.Config.ARGB_8888);  
    10.         int iPixel = 0;  
    11.         for (int i = 0; i < mBitmapWidth; i++)  
    12.         {  
    13.             for (int j = 0; j < mBitmapHeight; j++)  
    14.             {  
    15.                 int curr_color = mBitmap.getPixel(i, j);  
    16.   
    17.                 int avg = (Color.red(curr_color) + Color.green(curr_color) + Color  
    18.                         .blue(curr_color)) / 3;  
    19.                 if (avg >= 100)  
    20.                 {  
    21.                     iPixel = 255;  
    22.                 }  
    23.                 else  
    24.                 {  
    25.                     iPixel = 0;  
    26.                 }  
    27.                 int modif_color = Color.argb(255, iPixel, iPixel, iPixel);  
    28.   
    29.                 bmpReturn.setPixel(i, j, modif_color);  
    30.             }  
    31.         }  
    32.         return bmpReturn;  
    33.     }  

     

    理解:

    其实看图片效果就能看出来,这张图片不同于灰白处理的那张,不同之处是灰白处理虽然没有了颜色,但是黑白的程度层次依然存在,而此张图片连层次都没有了,只有两个区别十分明显的黑白颜色。实现的算法也很简单,对于每个像素的rgb值求平均数,如果高于100算白色,低于100算黑色。不过感觉100这个标准值太大了,导致图片白色区域太多,把它降低点可能效果会更好。(作者代码方法命名竟然是汉语拼音,原来是国人写的,是不是github也记不清了,我尊重原创,但是下载地址真的忘了。另外我把作者图片换了,额……)

    四:镜像处理

    效果:

    代码:

     

    1. public static Bitmap createReflectionImageWithOrigin(Bitmap bitmap)  
    2.     {  
    3.         final int reflectionGap = 4;  
    4.         int width = bitmap.getWidth();  
    5.         int height = bitmap.getHeight();  
    6.   
    7.         Matrix matrix = new Matrix();  
    8.         matrix.preScale(1, -1);  
    9.   
    10.         Bitmap reflectionImage = Bitmap.createBitmap(bitmap, 0, height / 2,  
    11.                 width, height / 2, matrix, false);  
    12.   
    13.         Bitmap bitmapWithReflection = Bitmap.createBitmap(width,  
    14.                 (height + height / 2), Config.ARGB_8888);  
    15.   
    16.         Canvas canvas = new Canvas(bitmapWithReflection);  
    17.         canvas.drawBitmap(bitmap, 00null);  
    18.         Paint deafalutPaint = new Paint();  
    19.         canvas.drawRect(0, height, width, height + reflectionGap, deafalutPaint);  
    20.   
    21.         canvas.drawBitmap(reflectionImage, 0, height + reflectionGap, null);  
    22.   
    23.         Paint paint = new Paint();  
    24.         LinearGradient shader = new LinearGradient(0, bitmap.getHeight(), 0,  
    25.                 bitmapWithReflection.getHeight() + reflectionGap, 0x70ffffff,  
    26.                 0x00ffffff, TileMode.CLAMP);  
    27.         paint.setShader(shader);  
    28.         // Set the Transfer mode to be porter duff and destination in  
    29.         paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN));  
    30.         // Draw a rectangle using the paint with our linear gradient  
    31.         canvas.drawRect(0, height, width, bitmapWithReflection.getHeight()  
    32.                 + reflectionGap, paint);  
    33.   
    34.         return bitmapWithReflection;  
    35.     }  

     

    理解:

    记得去年android入门时做过gallery的倒影特效,当时感觉很漂亮,想着需要作出反转和倾斜就可以了,原来他也是这么做的。原理就是将原图片反转一下,调整一   下它的颜色作出倒影效果,再将两张图片续加在一起,不过如果在反转的同时再利用Matrix加上一些倾斜角度就更好了,不过那样做的话加工后的图片的高度需要同比例计算出来,不能简单的相加了,否则就图片大小就容不下现有的像素内容。

    五:加旧处理

    效果:

    代码:

     

    1. public static Bitmap testBitmap(Bitmap bitmap)  
    2.     {  
    3.         Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),  
    4.                 bitmap.getHeight(), Config.RGB_565);  
    5.   
    6.         Canvas canvas = new Canvas(output);  
    7.   
    8.         Paint paint = new Paint();          
    9.         ColorMatrix cm = new ColorMatrix();  
    10.         float[] array = {1,0,0,0,50,  
    11.                 0,1,0,0,50,  
    12.                 0,0,1,0,0,  
    13.                 0,0,0,1,0};  
    14.         cm.set(array);  
    15.         paint.setColorFilter(new ColorMatrixColorFilter(cm));  
    16.   
    17.         canvas.drawBitmap(bitmap, 00, paint);  
    18.         return output;  
    19.     }  

     

    理解:

    其实每张图片的存储都是存的每个像素的rgba值,而对其操作的时候又将其四个数值定位一个5行1列的矩阵,最后一行值为1,这样一来利用矩阵对其操作确实方便了好多,矩阵的乘法可以轻松的实现某个或全部分量按比例或加常熟的增加或减少。正如这个博客所讲:http://www.cnblogs.com/leon19870907/articles/1978065.html  比如现有一张图片,其每个point的rgba值为{100,100,100,255}也就是灰色全图,我们希望其红色部位增加一倍,剩余部分增加十。就可以将其值虚拟为五行一列矩阵:{100   再让这个矩阵:{2,0,0,0,0     乘以它。得到{  200,110,100,100} 。  这个泛黄照片的处理算法原理就是让每个像素点rg值增加50,rg值相混合就得到了黄色。

    100                                 0,1,0,0,10

    100                                 0,0,1,0,10

    100 0,0,0,1,10

    1}

     

    六:哈哈镜处理

    效果:

    代码:

     

    1. jintArray Java_com_spore_meitu_jni_ImageUtilEngine_toHahajing  
    2.   (JNIEnv* env,jobject thiz, jintArray buf, jint width, jint height,jint centerX, jint centerY, jint radius, jfloat multiple)  
    3. {  
    4.     jint * cbuf;  
    5.         cbuf = (*env)->GetIntArrayElements(env, buf, 0);  
    6.   
    7.         int newSize = width * height;  
    8.         jint rbuf[newSize];   
    9.   
    10.         float xishu = multiple;  
    11.         int real_radius = (int)(radius / xishu);  
    12.   
    13.         int i = 0, j = 0;  
    14.         for (i = 0; i < width; i++)  
    15.         {  
    16.             for (j = 0; j < height; j++)  
    17.             {  
    18.                 int curr_color = cbuf[j * width + i];  
    19.   
    20.                 int pixR = red(curr_color);  
    21.                 int pixG = green(curr_color);  
    22.                 int pixB = blue(curr_color);  
    23.                 int pixA = alpha(curr_color);  
    24.   
    25.                 int newR = pixR;  
    26.                 int newG = pixG;  
    27.                 int newB = pixB;  
    28.                 int newA = pixA;  
    29.   
    30.                 int distance = (int) ((centerX - i) * (centerX - i) + (centerY - j) * (centerY - j));  
    31.                 if (distance < radius * radius)  
    32.                 {  
    33.                       
    34.                     int src_x = (int) ((float) (i - centerX) / xishu);  
    35.                     int src_y = (int) ((float) (j - centerY) / xishu);  
    36.                     src_x = (int)(src_x * (sqrt(distance) / real_radius));  
    37.                     src_y = (int)(src_y * (sqrt(distance) / real_radius));  
    38.                     src_x = src_x + centerX;  
    39.                     src_y = src_y + centerY;  
    40.   
    41.                     int src_color = cbuf[src_y * width + src_x];  
    42.                     newR = red(src_color);  
    43.                     newG = green(src_color);  
    44.                     newB = blue(src_color);  
    45.                     newA = alpha(src_color);  
    46.                 }  
    47.   
    48.                 newR = min(255, max(0, newR));  
    49.                 newG = min(255, max(0, newG));  
    50.                 newB = min(255, max(0, newB));  
    51.                 newA = min(255, max(0, newA));  
    52.   
    53.                 int modif_color = ARGB(newA, newR, newG, newB);  
    54.                 rbuf[j * width + i] = modif_color;  
    55.             }  
    56.         }  
    57.   
    58.         jintArray result = (*env)->NewIntArray(env, newSize);  
    59.         (*env)->SetIntArrayRegion(env, result, 0, newSize, rbuf);  
    60.         (*env)->ReleaseIntArrayElements(env, buf, cbuf, 0);  
    61.         return result;  
    62. }  

     

    理解:

    搞不懂一个图片处理,为什么作者要加到jni那层去,速度能提升多少,赤裸裸的嘲讽我们的技术。纯c代码真懒得看,心情好的时候再看,看完了再来更新,猜测实现原理是根据哈哈镜的半径,以中心点为圆心,每个像素点的坐标位移并扩展,离中心点越近的就扩展越大。

    七:放大镜处理

    直接给代码吧:

     

    1. jintArray Java_com_spore_meitu_jni_ImageUtilEngine_toFangdajing  
    2.   (JNIEnv* env,jobject thiz, jintArray buf, jint width, jint height,jint centerX, jint centerY, jint radius, jfloat multiple)  
    3. {  
    4.     jint * cbuf;  
    5.     cbuf = (*env)->GetIntArrayElements(env, buf, 0);  
    6.   
    7.     int newSize = width * height;  
    8.     jint rbuf[newSize]; // 鏂板浘鍍忓儚绱犲�  
    9.   
    10.     float xishu = multiple;  
    11.     int real_radius = (int)(radius / xishu);  
    12.   
    13.     int i = 0, j = 0;  
    14.     for (i = 0; i < width; i++)  
    15.     {  
    16.         for (j = 0; j < height; j++)  
    17.         {  
    18.             int curr_color = cbuf[j * width + i];  
    19.   
    20.             int pixR = red(curr_color);  
    21.             int pixG = green(curr_color);  
    22.             int pixB = blue(curr_color);  
    23.             int pixA = alpha(curr_color);  
    24.   
    25.             int newR = pixR;  
    26.             int newG = pixG;  
    27.             int newB = pixB;  
    28.             int newA = pixA;  
    29.   
    30.             int distance = (int) ((centerX - i) * (centerX - i) + (centerY - j) * (centerY - j));  
    31.             if (distance < radius * radius)  
    32.             {  
    33.                 // 鍥惧儚鏀惧ぇ鏁堟灉  
    34.                 int src_x = (int)((float)(i - centerX) / xishu + centerX);  
    35.                 int src_y = (int)((float)(j - centerY) / xishu + centerY);  
    36.   
    37.                 int src_color = cbuf[src_y * width + src_x];  
    38.                 newR = red(src_color);  
    39.                 newG = green(src_color);  
    40.                 newB = blue(src_color);  
    41.                 newA = alpha(src_color);  
    42.             }  
    43.   
    44.             newR = min(255, max(0, newR));  
    45.             newG = min(255, max(0, newG));  
    46.             newB = min(255, max(0, newB));  
    47.             newA = min(255, max(0, newA));  
    48.   
    49.             int modif_color = ARGB(newA, newR, newG, newB);  
    50.             rbuf[j * width + i] = modif_color;  
    51.         }  
    52.     }  
    53.   
    54.     jintArray result = (*env)->NewIntArray(env, newSize);  
    55.     (*env)->SetIntArrayRegion(env, result, 0, newSize, rbuf);  
    56.     (*env)->ReleaseIntArrayElements(env, buf, cbuf, 0);  
    57.     return result;  
    58. }  

     

    八:浮雕处理

    效果:

    代码:

     

    1. public static Bitmap toFuDiao(Bitmap mBitmap)  
    2.     {  
    3.           
    4.   
    5.         int mBitmapWidth = 0;  
    6.         int mBitmapHeight = 0;  
    7.   
    8.         mBitmapWidth = mBitmap.getWidth();  
    9.         mBitmapHeight = mBitmap.getHeight();  
    10.         Bitmap bmpReturn = Bitmap.createBitmap(mBitmapWidth, mBitmapHeight,  
    11.                 Bitmap.Config.RGB_565);  
    12.         int preColor = 0;  
    13.         int prepreColor = 0;  
    14.         preColor = mBitmap.getPixel(00);  
    15.   
    16.         for (int i = 0; i < mBitmapWidth; i++)  
    17.         {  
    18.             for (int j = 0; j < mBitmapHeight; j++)  
    19.             {  
    20.                 int curr_color = mBitmap.getPixel(i, j);  
    21.                 int r = Color.red(curr_color) - Color.red(prepreColor) +127;  
    22.                 int g = Color.green(curr_color) - Color.red(prepreColor) + 127;  
    23.                 int b = Color.green(curr_color) - Color.blue(prepreColor) + 127;  
    24.                 int a = Color.alpha(curr_color);  
    25.                 int modif_color = Color.argb(a, r, g, b);  
    26.                 bmpReturn.setPixel(i, j, modif_color);  
    27.                 prepreColor = preColor;  
    28.                 preColor = curr_color;  
    29.             }  
    30.         }  
    31.   
    32.         Canvas c = new Canvas(bmpReturn);  
    33.         Paint paint = new Paint();  
    34.         ColorMatrix cm = new ColorMatrix();  
    35.         cm.setSaturation(0);  
    36.         ColorMatrixColorFilter f = new ColorMatrixColorFilter(cm);  
    37.         paint.setColorFilter(f);  
    38.         c.drawBitmap(bmpReturn, 00, paint);  
    39.   
    40.         return bmpReturn;  
    41.     }  

     

    理解:

    观察浮雕就不难发现,其实浮雕的特点就是在颜色有跳变的地方就刻条痕迹。127,127,127为深灰色,近似于石头的颜色,此处取该颜色为底色。算法是将上一个点的rgba值减去当前点的rgba值然后加上127得到当前点的颜色。

    九:底片处理

    效果:

    代码:

     

    1. jintArray Java_com_spore_meitu_jni_ImageUtilEngine_toDipian  
    2.   (JNIEnv* env,jobject thiz, jintArray buf, jint width, jint height)  
    3. {  
    4.     jint * cbuf;  
    5.     cbuf = (*env)->GetIntArrayElements(env, buf, 0);  
    6.     LOGE("Bitmap Buffer %d %d",cbuf[0],cbuf[1]);  
    7.   
    8.     int newSize = width * height;  
    9.     jint rbuf[newSize];  
    10.   
    11.     int count = 0;  
    12.     int preColor = 0;  
    13.     int prepreColor = 0;  
    14.     int color = 0;  
    15.     preColor = cbuf[0];  
    16.   
    17.     int i = 0;  
    18.     int j = 0;  
    19.     int iPixel = 0;  
    20.     for (i = 0; i < width; i++) {  
    21.         for (j = 0; j < height; j++) {  
    22.             int curr_color = cbuf[j * width + i];  
    23.   
    24.             int r = 255 - red(curr_color);  
    25.             int g = 255 - green(curr_color);  
    26.             int b = 255 - blue(curr_color);  
    27.             int a = alpha(curr_color);  
    28.             int modif_color = ARGB(a, r, g, b);  
    29.             rbuf[j * width + i] = modif_color;  
    30.         }  
    31.     }  
    32.     jintArray result = (*env)->NewIntArray(env, newSize);   
    33.     (*env)->SetIntArrayRegion(env, result, 0, newSize, rbuf);   
    34.     (*env)->ReleaseIntArrayElements(env, buf, cbuf, 0);   
    35.     return result;  
    36. }  

    理解:

     

    算法实现是每个点grb值取为255之差,效果也真是底片效果,但是没有想通为什么这样运算就可以得到底片,回头更新。

    十:油画处理

    效果:

    代码:

     

    1. public static Bitmap toYouHua(Bitmap bmpSource)  
    2.     {  
    3.         Bitmap bmpReturn = Bitmap.createBitmap(bmpSource.getWidth(),  
    4.                 bmpSource.getHeight(), Bitmap.Config.RGB_565);  
    5.         int color = 0;  
    6.         int Radio = 0;  
    7.         int width = bmpSource.getWidth();  
    8.         int height = bmpSource.getHeight();  
    9.   
    10.         Random rnd = new Random();  
    11.         int iModel = 10;  
    12.         int i = width - iModel;  
    13.         while (i > 1)  
    14.         {  
    15.             int j = height - iModel;  
    16.             while (j > 1)  
    17.             {  
    18.                 int iPos = rnd.nextInt(100000) % iModel;  
    19.                 color = bmpSource.getPixel(i + iPos, j + iPos);  
    20.                 bmpReturn.setPixel(i, j, color);  
    21.                 j = j - 1;  
    22.             }  
    23.             i = i - 1;  
    24.         }  
    25.         return bmpReturn;  
    26.     }  

    理解:

     

    赞一下这个算法,其实应该说鄙视下自己,在看到效果图的时候,我会先猜一下原理,但是这个始终没有想出来。其实油画因为是用画笔画的,彩笔画的时候没有那么精确会将本该这点的颜色滑到另一个点处。算法实现就是取一个一定范围内的随机数,每个点的颜色是该点减去随机数坐标后所得坐标的颜色。

    十一:模糊处理

    效果:

    代码:

     

    1. public static Bitmap toMohu(Bitmap bmpSource, int Blur)  
    2.     {  
    3.         int mode = 5;  
    4.         Bitmap bmpReturn = Bitmap.createBitmap(bmpSource.getWidth(),  
    5.                 bmpSource.getHeight(), Bitmap.Config.ARGB_8888);  
    6.         int pixels[] = new int[bmpSource.getWidth() * bmpSource.getHeight()];  
    7.         int pixelsRawSource[] = new int[bmpSource.getWidth()  
    8.                 * bmpSource.getHeight() * 3];  
    9.         int pixelsRawNew[] = new int[bmpSource.getWidth()  
    10.                 * bmpSource.getHeight() * 3];  
    11.   
    12.         bmpSource.getPixels(pixels, 0, bmpSource.getWidth(), 00,  
    13.                 bmpSource.getWidth(), bmpSource.getHeight());  
    14.   
    15.         for (int k = 1; k <= Blur; k++)   
    16.         {  
    17.               
    18.             for (int i = 0; i < pixels.length; i++)  
    19.             {  
    20.                 pixelsRawSource[i * 3 + 0] = Color.red(pixels[i]);  
    21.                 pixelsRawSource[i * 3 + 1] = Color.green(pixels[i]);  
    22.                 pixelsRawSource[i * 3 + 2] = Color.blue(pixels[i]);  
    23.             }  
    24.               
    25.             int CurrentPixel = bmpSource.getWidth() * 3 + 3;  
    26.           
    27.             for (int i = 0; i < bmpSource.getHeight() - 3; i++)   
    28.             {  
    29.                 for (int j = 0; j < bmpSource.getWidth() * 3; j++)   
    30.                 {  
    31.                     CurrentPixel += 1;  
    32.                     int sumColor = 0;   
    33.                     sumColor = pixelsRawSource[CurrentPixel  
    34.                             - bmpSource.getWidth() * 3];   
    35.                     sumColor = sumColor + pixelsRawSource[CurrentPixel - 3];  
    36.                     sumColor = sumColor + pixelsRawSource[CurrentPixel + 3];   
    37.                     sumColor = sumColor  
    38.                             + pixelsRawSource[CurrentPixel  
    39.                                     + bmpSource.getWidth() * 3];   
    40.                     pixelsRawNew[CurrentPixel] = Math.round(sumColor / 4);  
    41.                 }  
    42.             }  
    43.   
    44.             for (int i = 0; i < pixels.length; i++)  
    45.             {  
    46.                 pixels[i] = Color.rgb(pixelsRawNew[i * 3 + 0],  
    47.                         pixelsRawNew[i * 3 + 1], pixelsRawNew[i * 3 + 2]);  
    48.             }  
    49.         }  
    50.   
    51.         bmpReturn.setPixels(pixels, 0, bmpSource.getWidth(), 00,  
    52.                 bmpSource.getWidth(), bmpSource.getHeight());   
    53.         return bmpReturn;  
    54.     }  

    理解:

     

    算法实现其实是取每三点的平均值做为当前点颜色,这样看上去就变得模糊了。这个算法是三点的平均值,如果能够将范围扩大,并且不是单纯的平均值,而是加权平均肯定效果会更好。不过处理速度实在是太慢了,而Muzei这种软件在处理的时候,不仅仅速度特别快,而且还有逐渐变模糊的变化过程,显然人家不是用这种算法实现的。他们的实现方法正在猜测中,实现后也来更新。

  • 相关阅读:
    Access使用记录
    html js 表单提交前检测数据
    asp.net mvc5 action多个参数
    asp.net mvc Areas 母版页动态获取数据进行渲染
    asp.net mvc 自定义全局过滤器 验证用户是否登录
    .net 报错汇总——持续更新
    数据库基础
    Python协程
    Python 线程
    Python 队列
  • 原文地址:https://www.cnblogs.com/mgstone/p/5842064.html
Copyright © 2011-2022 走看看