2000字范文,分享全网优秀范文,学习好帮手!
2000字范文 > android 图像处理(黑白 模糊 浮雕 圆角 镜像 底片 油画 灰白 加旧)...

android 图像处理(黑白 模糊 浮雕 圆角 镜像 底片 油画 灰白 加旧)...

时间:2020-11-11 19:51:02

相关推荐

android 图像处理(黑白 模糊 浮雕 圆角 镜像 底片 油画 灰白 加旧)...

独角兽企业重金招聘Python工程师标准>>>

圆角处理

publicstaticBitmapgetRoundedCornerBitmap(Bitmapbitmap,floatroundPx){Bitmapoutput=Bitmap.createBitmap(bitmap.getWidth(),bitmap.getHeight(),Config.ARGB_8888);Canvascanvas=newCanvas(output);finalintcolor=0xff424242;finalPaintpaint=newPaint();finalRectrect=newRect(0,0,bitmap.getWidth(),bitmap.getHeight());finalRectFrectF=newRectF(rect);paint.setAntiAlias(true);canvas.drawARGB(0,0,0,0);paint.setColor(color);canvas.drawRoundRect(rectF,roundPx,roundPx,paint);paint.setXfermode(newPorterDuffXfermode(Mode.SRC_IN));canvas.drawBitmap(bitmap,rect,rect,paint);returnoutput;}

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

灰白处理

publicstaticBitmaptoGrayscale(BitmapbmpOriginal){intwidth,height;height=bmpOriginal.getHeight();width=bmpOriginal.getWidth();BitmapbmpGrayscale=Bitmap.createBitmap(width,height,Bitmap.Config.RGB_565);Canvasc=newCanvas(bmpGrayscale);Paintpaint=newPaint();ColorMatrixcm=newColorMatrix();cm.setSaturation(0);ColorMatrixColorFilterf=newColorMatrixColorFilter(cm);paint.setColorFilter(f);c.drawBitmap(bmpOriginal,0,0,paint);returnbmpGrayscale;}

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

黑白处理

publicstaticBitmaptoHeibai(BitmapmBitmap){intmBitmapWidth=0;intmBitmapHeight=0;mBitmapWidth=mBitmap.getWidth();mBitmapHeight=mBitmap.getHeight();BitmapbmpReturn=Bitmap.createBitmap(mBitmapWidth,mBitmapHeight,Bitmap.Config.ARGB_8888);intiPixel=0;for(inti=0;i<mBitmapWidth;i++){for(intj=0;j<mBitmapHeight;j++){intcurr_color=mBitmap.getPixel(i,j);intavg=(Color.red(curr_color)+Color.green(curr_color)+Color.blue(curr_color))/3;if(avg>=100){iPixel=255;}else{iPixel=0;}intmodif_color=Color.argb(255,iPixel,iPixel,iPixel);bmpReturn.setPixel(i,j,modif_color);}}returnbmpReturn;}

其实看图片效果就能看出来,这张图片不同于灰白处理的那张,不同之处是灰白处理虽然没有了颜色,但是黑白的程度层次依然存在,而此张图片连层次都没有了,只有两个区别十分明显的黑白颜色。实现的算法也很简单,对于每个像素的rgb值求平均数,如果高于100算白色,低于100算黑色。不过感觉100这个标准值太大了,导致图片白色区域太多,把它降低点可能效果会更好。

镜像处理

publicstaticBitmapcreateReflectionImageWithOrigin(Bitmapbitmap){finalintreflectionGap=4;intwidth=bitmap.getWidth();intheight=bitmap.getHeight();Matrixmatrix=newMatrix();matrix.preScale(1,-1);BitmapreflectionImage=Bitmap.createBitmap(bitmap,0,height/2,width,height/2,matrix,false);BitmapbitmapWithReflection=Bitmap.createBitmap(width,(height+height/2),Config.ARGB_8888);Canvascanvas=newCanvas(bitmapWithReflection);canvas.drawBitmap(bitmap,0,0,null);PaintdeafalutPaint=newPaint();canvas.drawRect(0,height,width,height+reflectionGap,deafalutPaint);canvas.drawBitmap(reflectionImage,0,height+reflectionGap,null);Paintpaint=newPaint();LinearGradientshader=newLinearGradient(0,bitmap.getHeight(),0,bitmapWithReflection.getHeight()+reflectionGap,0x70ffffff,0x00ffffff,TileMode.CLAMP);paint.setShader(shader);//SettheTransfermodetobeporterduffanddestinationinpaint.setXfermode(newPorterDuffXfermode(Mode.DST_IN));//Drawarectangleusingthepaintwithourlineargradientcanvas.drawRect(0,height,width,bitmapWithReflection.getHeight()+reflectionGap,paint);returnbitmapWithReflection;}

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

加旧处理

publicstaticBitmaptestBitmap(Bitmapbitmap){Bitmapoutput=Bitmap.createBitmap(bitmap.getWidth(),bitmap.getHeight(),Config.RGB_565);Canvascanvas=newCanvas(output);Paintpaint=newPaint();ColorMatrixcm=newColorMatrix();float[]array={1,0,0,0,50,0,1,0,0,50,0,0,1,0,0,0,0,0,1,0};cm.set(array);paint.setColorFilter(newColorMatrixColorFilter(cm));canvas.drawBitmap(bitmap,0,0,paint);returnoutput;}

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

浮雕处理

publicstaticBitmaptoFuDiao(BitmapmBitmap){intmBitmapWidth=0;intmBitmapHeight=0;mBitmapWidth=mBitmap.getWidth();mBitmapHeight=mBitmap.getHeight();BitmapbmpReturn=Bitmap.createBitmap(mBitmapWidth,mBitmapHeight,Bitmap.Config.RGB_565);intpreColor=0;intprepreColor=0;preColor=mBitmap.getPixel(0,0);for(inti=0;i<mBitmapWidth;i++){for(intj=0;j<mBitmapHeight;j++){intcurr_color=mBitmap.getPixel(i,j);intr=Color.red(curr_color)-Color.red(prepreColor)+127;intg=Color.green(curr_color)-Color.red(prepreColor)+127;intb=Color.green(curr_color)-Color.blue(prepreColor)+127;inta=Color.alpha(curr_color);intmodif_color=Color.argb(a,r,g,b);bmpReturn.setPixel(i,j,modif_color);prepreColor=preColor;preColor=curr_color;}}Canvasc=newCanvas(bmpReturn);Paintpaint=newPaint();ColorMatrixcm=newColorMatrix();cm.setSaturation(0);ColorMatrixColorFilterf=newColorMatrixColorFilter(cm);paint.setColorFilter(f);c.drawBitmap(bmpReturn,0,0,paint);returnbmpReturn;}

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

油画处理

publicstaticBitmaptoYouHua(BitmapbmpSource){BitmapbmpReturn=Bitmap.createBitmap(bmpSource.getWidth(),bmpSource.getHeight(),Bitmap.Config.RGB_565);intcolor=0;intRadio=0;intwidth=bmpSource.getWidth();intheight=bmpSource.getHeight();Randomrnd=newRandom();intiModel=10;inti=width-iModel;while(i>1){intj=height-iModel;while(j>1){intiPos=rnd.nextInt(100000)%iModel;color=bmpSource.getPixel(i+iPos,j+iPos);bmpReturn.setPixel(i,j,color);j=j-1;}i=i-1;}returnbmpReturn;}

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

模糊处理

publicstaticBitmaptoMohu(BitmapbmpSource,intBlur){intmode=5;BitmapbmpReturn=Bitmap.createBitmap(bmpSource.getWidth(),bmpSource.getHeight(),Bitmap.Config.ARGB_8888);intpixels[]=newint[bmpSource.getWidth()*bmpSource.getHeight()];intpixelsRawSource[]=newint[bmpSource.getWidth()*bmpSource.getHeight()*3];intpixelsRawNew[]=newint[bmpSource.getWidth()*bmpSource.getHeight()*3];bmpSource.getPixels(pixels,0,bmpSource.getWidth(),0,0,bmpSource.getWidth(),bmpSource.getHeight());for(intk=1;k<=Blur;k++){for(inti=0;i<pixels.length;i++){pixelsRawSource[i*3+0]=Color.red(pixels[i]);pixelsRawSource[i*3+1]=Color.green(pixels[i]);pixelsRawSource[i*3+2]=Color.blue(pixels[i]);}intCurrentPixel=bmpSource.getWidth()*3+3;for(inti=0;i<bmpSource.getHeight()-3;i++){for(intj=0;j<bmpSource.getWidth()*3;j++){CurrentPixel+=1;intsumColor=0;sumColor=pixelsRawSource[CurrentPixel-bmpSource.getWidth()*3];sumColor=sumColor+pixelsRawSource[CurrentPixel-3];sumColor=sumColor+pixelsRawSource[CurrentPixel+3];sumColor=sumColor+pixelsRawSource[CurrentPixel+bmpSource.getWidth()*3];pixelsRawNew[CurrentPixel]=Math.round(sumColor/4);}}for(inti=0;i<pixels.length;i++){pixels[i]=Color.rgb(pixelsRawNew[i*3+0],pixelsRawNew[i*3+1],pixelsRawNew[i*3+2]);}}bmpReturn.setPixels(pixels,0,bmpSource.getWidth(),0,0,bmpSource.getWidth(),bmpSource.getHeight());returnbmpReturn;}

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

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。