濾鏡在圖片處理裏面有不少的運用,尤爲是相機使用了大量的濾鏡,經過對顏色通道的調和,能夠呈現出各類各樣的效果html
對圖像進行必定的過濾加工處理,使用Paint設置濾鏡效果
不少高級UI使用時候須要關閉硬件加速,不關閉的話,有些API沒法支持java
MaskFilter
處理類
paint.setMaskFilter(maskfilter)
如下兩種處理基於下面的初始化web
//關閉硬件加速 setLayerType(View.LAYER_TYPE_SOFTWARE, null); Paint paint = new Paint(); paint.setColor(Color.RED); paint.setAntiAlias(true); Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.test);
BlurMaskFilter
類)構造函數原型canvas
public BlurMaskFilter(float radius, Blur style)
使用例子數組
paint.setMaskFilter(new BlurMaskFilter(20, BlurMaskFilter.Blur.NORMAL)); RectF rectF = new RectF(100, 100, 200, 200); canvas.drawRect(rectF, paint); canvas.drawBitmap(bitmap, 400, 100, paint);
設置參數是,有四個參數:NORMAL,INNER,OUTER,SOLID
其參數效果以下
app
EmbossMaskFilter
類)其構造函數原型爲ide
public EmbossMaskFilter(float[] direction, float ambient, float specular, float blurRadius)
direction
:指定長度爲xxx的數組標量[x,y,z],用來指定光源的位置
ambient
:指定周邊背景光源(0~1)
specular
:指鏡面反射係數
blurRadius
:指定模糊半徑
使用例子svg
float[] direction = {100, 100, 100}; paint.setMaskFilter(new EmbossMaskFilter(direction, 0.6F, 8, 20)); rectF = new RectF(100, 100, 300, 300); canvas.drawRect(rectF, paint); canvas.drawBitmap(bitmap, 500, 100, paint);
獲得的效果以下圖所示
函數
ColorMatrix
處理類
濾鏡的全部處理效果都是經過顏色矩陣的變換實現的。
好比:美顏相機實現的特效(高光、復古、黑白)
關於RGB濾鏡處理是基於矩陣變換的,那麼色彩信息矩陣是怎麼表示的呢
四階表示
若是想將色彩(0,255,0,255)更改成半透明時,可使用下面的的矩陣運算來表示
而在真正的運算時,採用的是五階矩陣
考慮下面這個變換:
一、紅色份量值更改成原來的2倍;
二、綠色份量增長100;
則使用4階矩陣的乘法沒法實現,因此,應該在四階色彩變換矩陣上增長一個"啞元座標",來實現所列的矩陣運算:
這個矩陣中,份量值用的是100
例如提取顏色,這裏只顯示綠色和透明度spa
public class RGBFliterView extends View { private RectF rectF; public RGBFliterView(Context context) { super(context); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); //關閉硬件加速 setLayerType(View.LAYER_TYPE_SOFTWARE, null); Paint paint = new Paint(); paint.setColor(Color.argb(200,200,200,200)); paint.setAntiAlias(true); Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.test); //過濾前 rectF = new RectF(100, 100, 300, 300); canvas.drawRect(rectF, paint); canvas.drawBitmap(bitmap, 500, 100, paint); //這個矩陣表明要提出綠色和透明度 ColorMatrix matrix = new ColorMatrix(new float[]{ 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0 }); //設置顏色過濾器 paint.setColorFilter(new ColorMatrixColorFilter(matrix)); //過濾後 rectF = new RectF(100, 600, 300, 800); canvas.drawRect(rectF, paint); canvas.drawBitmap(bitmap, 500, 600, paint); } }
獲得的效果圖以下
顏色加強:
反相效果:
黑白效果(R+G+B=1
):
去色原理:只要把RGB三通道的色彩信息設置成同樣;即:R=G=B,那麼圖像就變成了灰色,而且,爲了保證圖像亮度不變,同一個通道中的R+G+B=1:如:0.213+0.715+0.072=1; RGB=0.213, 0.715, 0.072;三個數字是根據色彩光波頻率及色彩心理學計算出來的
髮色效果(好比紅色和綠色交換,把第一行和第二行交換):
復古風格:
以上是使用矩陣本身去變換,但在實際中仍是有一些可用的API的
此時使用的話,按照以下方法使用
ColorMatrix matrix = new ColorMatrix(); matrix.set(src)
有能夠直接使用的方法
matrix.setScale(1.2F, 1.2F, 1.2F, 1);
matrix.setSaturation(value);`
//axis,表明繞哪個軸旋轉,0,1,2 (0紅色軸,1綠色,2藍色) //degrees:旋轉的度數 matrix.setRotate(axis, degrees);