-
ColorMatrix
2012-12-06 10:20:20这些效果在android中有很好的支持,通过颜色矩阵(ColorMatrix)和坐标变换矩阵(Matrix)可以完美的做出上面的所说的效果。 颜色矩阵 android中可以通过颜色矩阵(ColorMatrix类)方面的操作颜色,颜色矩阵是一...在编程中有时候需要对图片做特殊的处理,比如将图片做出黑白的,或者老照片的效果,有时候还要对图片进行变换,以拉伸,扭曲等等。
这些效果在android中有很好的支持,通过颜色矩阵(ColorMatrix)和坐标变换矩阵(Matrix)可以完美的做出上面的所说的效果。
颜色矩阵
android中可以通过颜色矩阵(ColorMatrix类)方面的操作颜色,颜色矩阵是一个5x4 的矩阵(如图1.1)可以用来方面的修改图片中RGBA各分量的值,颜色矩阵以一维数组的方式存储如下:
[ a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t ]
他通过RGBA四个通道来直接操作对应颜色,如果会使用Photoshop就会知道有时处理图片通过控制RGBA各颜色通道来做出特殊的效果。这个矩阵对颜色的作用计算方式如1.3示:
矩阵的运算规则是矩阵A的一行乘以矩阵C的一列作为矩阵R的一行,C矩阵是图片中包含的ARGB信息,R矩阵是用颜色矩阵应用于C之后的新的颜色分量,运算结果如下:
R' = a*R + b*G + c*B + d*A + e;
G' = f*R + g*G + h*B + i*A + j;
B' = k*R + l*G + m*B + n*A + o;
A' = p*R + q*G + r*B + s*A + t;
颜色矩阵并不是看上去那么深奥,其实需要使用的参数很少,而且很有规律第一行决定红色第二行决定绿色第三行决定蓝色,第四行决定了透明度,第五列是颜色的偏移量。下面是一个实际中使用的颜色矩阵。
如果把这个矩阵作用于各颜色分量的话,R=A*C,计算后会发现,各个颜色分量实际上没有任何的改变(R'=R G'=G B'=B A'=A)。
图1.5所示矩阵计算后会发现红色分量增加100,绿色分量增加100,这样的效果就是图片偏黄,因为红色和绿色混合后得到黄色,黄色增加了100,图片当然就偏黄了。
改变各颜色分量不仅可以通过修改第5列的颜色偏移量也可如上面矩阵所示将对应的颜色值乘以一个倍数,直接放大。上图1.6是将绿色分量乘以2变为原来的2倍。相信读者至此已经明白了如何通过颜色矩阵来改变各颜色分量。
下面编写一段代码来,通过调整颜色矩阵来获得不同的颜色效果,JavaCode如下:
CMatrix类: public class CMatrix extends Activity { private Button change; private EditText [] et=new EditText[20]; private float []carray=new float[20]; private MyImage sv; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); change=(Button)findViewById(R.id.set); sv=(MyImage)findViewById(R.id.MyImage); for(int i=0;i<20;i++){ et[i]=(EditText)findViewById(R.id.indexa+i); carray[i]=Float.valueOf(et[i].getText().toString()); } change.setOnClickListener(l); } private Button.OnClickListener l=new Button.OnClickListener(){ @Override public void onClick(View arg0) { // TODO Auto-generated method stub getValues(); sv.setValues(carray); sv.invalidate(); } }; public void getValues(){ for(int i=0;i<20;i++){ carray[i]=Float.valueOf(et[i].getText().toString()); } } } MyImage类继承自View类: public class MyImage extends View { private Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG); private Bitmap mBitmap; private float [] array=new float[20]; private float mAngle; public MyImage(Context context,AttributeSet attrs) { super(context,attrs); mBitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.test); invalidate(); } public void setValues(float [] a){ for(int i=0;i<20;i++){ array[i]=a[i]; } } @Override protected void onDraw(Canvas canvas) { Paint paint = mPaint; paint.setColorFilter(null); canvas.drawBitmap(mBitmap, 0, 0, paint); ColorMatrix cm = new ColorMatrix(); //设置颜色矩阵 cm.set(array); //颜色滤镜,将颜色矩阵应用于图片 paint.setColorFilter(new ColorMatrixColorFilter(cm)); //绘图 canvas.drawBitmap(mBitmap, 0, 0, paint); Log.i("CMatrix", "--------->onDraw"); } }
CMatrix类主要负责 接收颜色矩阵的设置和重绘,没有要说的。MyImage类中进行绘图工作,首先设置颜色矩阵cm.set(..)从一维数组中读取数据20个数据给颜色矩 阵赋值,paint.setColorFilter(..)设置颜色滤镜,然后绘图,效果就出来了(这个过程和PS差不多)如下:
看到这里,相信大家对颜色矩阵的作用已经有了一个直观的感受,现在也可以尝试做一个照片特效的软件。但是各种效果并不能让用户手动调节颜色矩阵,这里需要计算公式,由于本人并不是做图形软件的也不能提供,可以参考这个链接:
http://www.adobe.com/devnet/flash/articles/matrix_transformations/ColorMatrixDemo.swf -
colorMatrix
2012-04-05 18:39:52package zhuojing.colorMatrix; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.EditText; public class MainActivity extends Activipackage zhuojing.colorMatrix;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
public class MainActivity extends Activity {
MyView myview=null;
EditText[] txts=new EditText[20];
float[] f={0,0,0,0,0,
0,0,0,0,0,
0,0,0,0,0,
0,0,0,0,0};
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
myview=(MyView)findViewById(R.id.myview);
for(int i=0;i<20;i++) {
txts[i]= (EditText) findViewById(R.id.txt11+i);
}
}
public void changeBtn(View v) {
for(int i=0;i<20;i++) {
f[i]=Float.parseFloat(""+txts[i].getText());
}
myview.setValue(f);
myview.postInvalidate();
}}
package zhuojing.colorMatrix;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.ColorMatrixColorFilter;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;
public class MyView extends View{
float[] f={1,0,0,0,0,
0,1,0,0,0,
0,0,1,0,0,
0,0,0,1,0};
Matrix matrix=null;
public MyView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public void setValue(float[] values)
{
this.f=values;
}
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
//绘制原图
Bitmap bitmap=BitmapFactory.decodeResource(this.getResources(), R.drawable.et04);
Paint paint=new Paint();
ColorMatrixColorFilter cm=new ColorMatrixColorFilter(f);
paint.setColorFilter(cm);
canvas.drawBitmap(bitmap, 0, 0, paint);
}
}<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<zhuojing.colorMatrix.MyView android:id="@+id/myview"
android:layout_width="fill_parent"
android:layout_height="200dip"
/>
<TableLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<Button android:id="@+id/btn1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="changeBtn"
android:text="变换"
/>
<TableRow >
<EditText android:id="@+id/txt11"
android:layout_width="50dip"
android:layout_height="wrap_content"
android:text="1"/>
<EditText android:id="@+id/txt12"
android:layout_width="50dip"
android:layout_height="wrap_content"
android:text="0"/>
<EditText android:id="@+id/txt13"
android:layout_width="50dip"
android:layout_height="wrap_content"
android:text="0"/>
<EditText android:id="@+id/txt14"
android:layout_width="50dip"
android:layout_height="wrap_content"
android:text="0"/>
<EditText android:id="@+id/txt15"
android:layout_width="50dip"
android:layout_height="wrap_content"
android:text="0"/>
</TableRow>
<TableRow >
<EditText android:id="@+id/txt21"
android:layout_width="50dip"
android:layout_height="wrap_content"
android:text="0"/>
<EditText android:id="@+id/txt22"
android:layout_width="50dip"
android:layout_height="wrap_content"
android:text="1"/>
<EditText android:id="@+id/txt23"
android:layout_width="50dip"
android:layout_height="wrap_content"
android:text="0"/>
<EditText android:id="@+id/txt24"
android:layout_width="50dip"
android:layout_height="wrap_content"
android:text="0"/>
<EditText android:id="@+id/txt25"
android:layout_width="50dip"
android:layout_height="wrap_content"
android:text="0"/>
</TableRow>
<TableRow >
<EditText android:id="@+id/txt31"
android:layout_width="50dip"
android:layout_height="wrap_content"
android:text="0"/>
<EditText android:id="@+id/txt32"
android:layout_width="50dip"
android:layout_height="wrap_content"
android:text="0"/>
<EditText android:id="@+id/txt33"
android:layout_width="50dip"
android:layout_height="wrap_content"
android:text="1"/>
<EditText android:id="@+id/txt34"
android:layout_width="50dip"
android:layout_height="wrap_content"
android:text="0"/>
<EditText android:id="@+id/txt35"
android:layout_width="50dip"
android:layout_height="wrap_content"
android:text="0"/>
</TableRow>
<TableRow >
<EditText android:id="@+id/txt41"
android:layout_width="50dip"
android:layout_height="wrap_content"
android:text="0"/>
<EditText android:id="@+id/txt42"
android:layout_width="50dip"
android:layout_height="wrap_content"
android:text="0"/>
<EditText android:id="@+id/txt43"
android:layout_width="50dip"
android:layout_height="wrap_content"
android:text="0"/>
<EditText android:id="@+id/txt44"
android:layout_width="50dip"
android:layout_height="wrap_content"
android:text="1"/>
<EditText android:id="@+id/txt45"
android:layout_width="50dip"
android:layout_height="wrap_content"
android:text="0"/>
</TableRow>
</TableLayout>
</LinearLayout> -
Android 矩阵ColorMatrix
2020-09-01 10:48:14主要介绍了Android 矩阵ColorMatrix的相关资料,需要的朋友可以参考下 -
使用colorMatrix
2014-09-22 13:50:00对图像进行颜色方面的处理,通过使用颜色矩阵(ColorMatrix)来实现。从而可以达到很多特效如黑白老照片、泛黄旧照片等等。 1.颜色矩阵(ColorMatrix) 这里有详细的介绍:...转载地址:http://blog.csdn.net/SJF0115/article/details/8698619
对图像进行颜色方面的处理,通过使用颜色矩阵(ColorMatrix)来实现。从而可以达到很多特效如黑白老照片、泛黄旧照片等等。
1.颜色矩阵(ColorMatrix)
这里有详细的介绍:http://developer.android.com/reference/android/graphics/ColorMatrix.html
不过是英文的,在这里我就先导读一下。
一张位图可以转换为一个5*4的矩阵,涉及到颜色和透明度。如图1所示。在Android中,颜色矩阵M是以一维数组m=[a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t]的方式进行存储的。
图1
在一张图片中,图像的RGBA(红色、绿色、蓝色、透明度)值决定了该图片所呈现出来的颜色效果。
而图像的RGBA值则存储在一个5*1的颜色分量矩阵C中,由颜色分量矩阵C可以控制图像的颜色效果。颜色分量矩阵C如图2所示。
图2
要想改变一张图片的颜色效果,只需要改变图像的颜色分量矩阵即可。通过颜色矩阵可以很方便的修改图像的颜色分量矩阵。假设修改后的图像颜色分量矩阵为C1,则有如图3所示的颜色分量矩阵计算公式。
图3
由此可见,通过颜色矩阵修改了原图像的RGBA值,从而达到了改变图片颜色效果的目的。并且,通过如图3所示的运算可知,颜色矩阵M的第一行参数abcde决定了图像的红色成分,第二行参数fghij决定了图像的绿色成分,第三行参数klmno决定了图像的蓝色成分,第四行参数pqrst决定了图像的透明度,第五列参数ejot是颜色的偏移量。
通常,改变颜色分量时可以通过修改第5列的颜色偏移量来实现,如图4所示的颜色矩阵M1,通过计算后可以得知该颜色矩阵的作用是使图像的红色分量和绿色分量均增加100,这样的效果就是图片泛黄(因为红色与绿色混合后得到黄色)。
图4
除此之外,也可以通过直接对颜色值乘以某一系数而达到改变颜色分量的目的。如图5所示的颜色矩阵M2,将绿色分量放大了2倍,这样的效果就是图片泛绿色。
图5
实例:
步骤一:我们首先自定义一个view,用来显示我们处理的图片。
ColorView.java
- package com.mycolor;
- import android.content.Context;
- import android.graphics.Bitmap;
- import android.graphics.BitmapFactory;
- import android.graphics.Canvas;
- import android.graphics.ColorMatrix;
- import android.graphics.ColorMatrixColorFilter;
- import android.graphics.Paint;
- import android.util.AttributeSet;
- import android.widget.ImageView;
- public class ColorView extends ImageView {
- private Paint myPaint = null;
- private Bitmap bitmap = null;
- private ColorMatrix myColorMatrix = null;
- private float[] colorArray = {1,0,0,0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,1,0};
- public ColorView(Context context, AttributeSet attrs)
- {
- super(context, attrs);
- bitmap = BitmapFactory.decodeResource(context.getResources(),R.drawable.a2);
- invalidate();
- }
- @Override
- protected void onDraw(Canvas canvas) {
- super.onDraw(canvas);
- //新建画笔对象
- myPaint = new Paint();
- //描画(原始图片)
- canvas.drawBitmap(bitmap,0, 0, myPaint);
- //新建颜色矩阵对象
- myColorMatrix = new ColorMatrix();
- //设置颜色矩阵的值
- myColorMatrix.set(colorArray);
- //设置画笔颜色过滤器
- myPaint.setColorFilter(new ColorMatrixColorFilter(myColorMatrix));
- //描画(处理后的图片)
- canvas.drawBitmap(bitmap,0,0,myPaint);
- invalidate();
- }
- //设置颜色数值
- public void setColorArray(float[] colorArray){
- this.colorArray = colorArray;
- }
- //设置图片
- public void setBitmap(Bitmap bitmap){
- this.bitmap = bitmap;
- }
- }
步骤二:自定义我们的布局main.xml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:id="@+id/colorView_layout"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:orientation="vertical" >
- <com.mycolor.ColorView
- android:id="@+id/myColorView"
- android:layout_width="480dp"
- android:layout_height="180dp"/>
- <LinearLayout
- android:id="@+id/colorlayout1"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:orientation="horizontal" >
- <EditText
- android:id="@+id/Edit1"
- android:layout_width="50dp"
- android:layout_height="40dp"
- android:layout_weight="1"
- android:text="1" />
- <EditText
- android:id="@+id/Edit2"
- android:layout_width="50dp"
- android:layout_height="40dp"
- android:layout_weight="1"
- android:text="0"
- />
- <EditText
- android:id="@+id/Edit3"
- android:layout_width="50dp"
- android:layout_height="40dp"
- android:layout_weight="1"
- android:text="0" />
- <EditText
- android:id="@+id/Edit4"
- android:layout_width="50dp"
- android:layout_height="40dp"
- android:layout_weight="1"
- android:text="0" />
- <EditText
- android:id="@+id/Edit5"
- android:layout_width="50dp"
- android:layout_height="40dp"
- android:layout_weight="1"
- android:text="0" />
- </LinearLayout>
- <LinearLayout
- android:id="@+id/colorlayout2"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:orientation="horizontal" >
- <EditText
- android:id="@+id/Edit6"
- android:layout_width="50dp"
- android:layout_height="40dp"
- android:layout_weight="1"
- android:text="0" />
- <EditText
- android:id="@+id/Edit7"
- android:layout_width="50dp"
- android:layout_height="40dp"
- android:layout_weight="1"
- android:text="1" />
- <EditText
- android:id="@+id/Edit8"
- android:layout_width="50dp"
- android:layout_height="40dp"
- android:layout_weight="1"
- android:text="0" />
- <EditText
- android:id="@+id/Edit9"
- android:layout_width="50dp"
- android:layout_height="40dp"
- android:layout_weight="1"
- android:text="0" />
- <EditText
- android:id="@+id/Edit10"
- android:layout_width="50dp"
- android:layout_height="40dp"
- android:layout_weight="1"
- android:text="0" />
- </LinearLayout>
- <LinearLayout
- android:id="@+id/colorlayout3"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:orientation="horizontal" >
- <EditText
- android:id="@+id/Edit11"
- android:layout_width="50dp"
- android:layout_height="40dp"
- android:layout_weight="1"
- android:text="0" />
- <EditText
- android:id="@+id/Edit12"
- android:layout_width="50dp"
- android:layout_height="40dp"
- android:layout_weight="1"
- android:text="0" />
- <EditText
- android:id="@+id/Edit13"
- android:layout_width="50dp"
- android:layout_height="40dp"
- android:layout_weight="1"
- android:text="1" />
- <EditText
- android:id="@+id/Edit14"
- android:layout_width="50dp"
- android:layout_height="40dp"
- android:layout_weight="1"
- android:text="0" />
- <EditText
- android:id="@+id/Edit15"
- android:layout_width="50dp"
- android:layout_height="40dp"
- android:layout_weight="1"
- android:text="0" />
- </LinearLayout>
- <LinearLayout
- android:id="@+id/colorlayout4"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:orientation="horizontal" >
- <EditText
- android:id="@+id/Edit16"
- android:layout_width="50dp"
- android:layout_height="40dp"
- android:layout_weight="1"
- android:text="0" />
- <EditText
- android:id="@+id/Edit17"
- android:layout_width="50dp"
- android:layout_height="40dp"
- android:layout_weight="1"
- android:text="0" />
- <EditText
- android:id="@+id/Edit18"
- android:layout_width="50dp"
- android:layout_height="40dp"
- android:layout_weight="1"
- android:text="0" />
- <EditText
- android:id="@+id/Edit19"
- android:layout_width="50dp"
- android:layout_height="40dp"
- android:layout_weight="1"
- android:text="1" />
- <EditText
- android:id="@+id/Edit20"
- android:layout_width="50dp"
- android:layout_height="40dp"
- android:layout_weight="1"
- android:text="0" />
- </LinearLayout>
- <Button
- android:id="@+id/Button"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_marginBottom="0dp"
- android:text="提交" />
- </LinearLayout>
步骤三:完成我们的Activity
- package com.mycolor;
- import android.app.Activity;
- import android.os.Bundle;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- import android.widget.EditText;
- public class ColorActivity extends Activity implements OnClickListener{
- private Button button = null;
- private ColorView colorView = null;
- private EditText[] editTextArray = null;
- private float colorArray[] = null;
- private int[] EditTextID = {R.id.Edit1,R.id.Edit2,R.id.Edit3,R.id.Edit4,R.id.Edit5,
- R.id.Edit6,R.id.Edit7,R.id.Edit8,R.id.Edit9,R.id.Edit10,
- R.id.Edit11,R.id.Edit12,R.id.Edit13,R.id.Edit14,R.id.Edit15,
- R.id.Edit16,R.id.Edit17,R.id.Edit18,R.id.Edit19,R.id.Edit20};
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- button = (Button)findViewById(R.id.Button);
- button.setOnClickListener(this);
- editTextArray = new EditText[20];
- colorArray = new float[20];
- for(int i = 0;i < 20;i++){
- editTextArray[i] = (EditText)findViewById(EditTextID[i]);
- }
- colorView = (ColorView)findViewById(R.id.myColorView);
- }
- @Override
- public void onClick(View v) {
- for(int i = 0;i < 20;i++){
- colorArray[i] = Float.valueOf(editTextArray[i].getText().toString().trim());
- System.out.println("i = " + i + ":" + editTextArray[i].getText().toString().trim());
- }
- colorView.setColorArray(colorArray);
- }
- }
这样就可以了。效果图:
改变值可以呈现不同的效果:
各种效果的值:
1, 泛黄的值 (把红色 跟 绿色分量都加100)
变成了
2.泛红 把红色的分量 X21,0,0,0,100, 0,1,0,0,100, 0,0,1,0,0, 0,0,0,1,0
那么 泛绿 泛蓝 分别就是2,0,0,0,0, 0,1,0,0,0, 0,0,1,0,0, 0,0,0,1,0
1,0,0,0,0, 0,2,0,0,0, 0,0,1,0,0, 0,0,0,1,0
1,0,0,0,0, 0,1,0,0,0, 0,0,2,0,0, 0,0,0,1,0
3.变成灰度:
1.把饱和度设置为0 就可以得到灰色的图片
myColorMatrix = new ColorMatrix(); myColorMatrix.setSaturation(0) ;
private Bitmap oldRemeber(Bitmap bmp) { // 速度测试 long start = System.currentTimeMillis(); int width = bmp.getWidth(); int height = bmp.getHeight(); Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565); int pixColor = 0; int pixR = 0; int pixG = 0; int pixB = 0; int newR = 0; int newG = 0; int newB = 0; int[] pixels = new int[width * height]; bmp.getPixels(pixels, 0, width, 0, 0, width, height); for (int i = 0; i < height; i++) { for (int k = 0; k < width; k++) { pixColor = pixels[width * i + k]; pixR = Color.red(pixColor); pixG = Color.green(pixColor); pixB = Color.blue(pixColor); newR = (int) (0.393 * pixR + 0.769 * pixG + 0.189 * pixB); newG = (int) (0.349 * pixR + 0.686 * pixG + 0.168 * pixB); newB = (int) (0.272 * pixR + 0.534 * pixG + 0.131 * pixB); int newColor = Color.argb(255, newR > 255 ? 255 : newR, newG > 255 ? 255 : newG, newB > 255 ? 255 : newB); pixels[width * i + k] = newColor; } } bitmap.setPixels(pixels, 0, width, 0, 0, width, height); long end = System.currentTimeMillis(); Log.d("may", "used time="+(end - start)); return bitmap; }
2.设置matrix值
0.213, 0.715, 0.072, 0.0, 0.0, 0.213, 0.715, 0.072, 0.0, 0.0, 0.213, 0.715, 0.072, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0
3.怀旧效果:
0.393f,0.769f,0.189f,0,0, 0.349f,0.686f,0.168f,0,0, 0.272f,0.534f,0.131f,0,0, 0,0,0,1,0};
或者: -
ColorMatrix的使用
2017-11-28 00:53:17一、概述 使用ColorMatrix绘制图片的简单代码如下: ColorMatrix colorMatrix1 = new ColorMatrix(); colorMatrix1.set(new float[]{ 1.0F, 0.0F, 0.0F, 0.0F, 0.0F, // r 0.0F, 0.一、概述
使用ColorMatrix绘制图片的简单代码如下:
ColorMatrix colorMatrix1 = new ColorMatrix(); colorMatrix1.set(new float[]{ 1.0F, 0.0F, 0.0F, 0.0F, 0.0F, // r 0.0F, 0.0F, 0.0F, 0.0F, 0.0F, // g 0.0F, 0.0F, 0.0F, 0.0F, 0.0F, // b 0.0F, 0.0F, 0.0F, 1.0F, 0.0F});// a ColorMatrixColorFilter filter1 = new ColorMatrixColorFilter(colorMatrix1); Paint p1 = new Paint(); p1.setXfermode(lightenXfermode); p1.setColorFilter(filter1); p1.setAlpha(255); canvas.drawBitmap(srcBmp, 0.0F, 0.0F, p1);
1、ColorMatrix由一个5*4的浮点数组构造而成,每一行的第五个参数代表颜色的偏移值,四行各自代表rgba四个通道。2、Paint 的setColorFilter方法
其声明如下:
ColorFilter有三个子类,ColorMatrixColorFilter、LightingColorFilter、PorterDuffFilter。主要是对图片的色彩进行类似滤镜的处理。public ColorFilter setColorFilter(ColorFilter filter)
二、使用ColorFilter模拟光学相机的色相差
1、色差出现的原因
由于不同波长的光线的焦距不同,或者镜头对不同波长的光线放大程度不同,导致不同波长的光线没有聚焦到同一个焦平面,在图片上形成色散。
2、代码模拟
主要通过ColorFilter分离r、g、b三个通道的颜色,用不同的矩形绘制出来,来简单模拟图片边缘的色散现象。
效果如下,可以看到图片的对比强烈的边缘有明显的红边或绿边。Bitmap tmpBmp = Bitmap.createBitmap(width, height, srcBmp.getConfig()); Canvas canvas = new Canvas(tmpBmp); PorterDuffXfermode lightenXfermode = new PorterDuffXfermode(PorterDuff.Mode.LIGHTEN); Integer i1 = Integer.valueOf((int)((double)width * 0.006)); Integer i2 = Integer.valueOf(i1.intValue() * 2); ColorMatrix colorMatrix1 = new ColorMatrix(); colorMatrix1.set(new float[]{ 1.0F, 0.0F, 0.0F, 0.0F, 0.0F, 0.0F, 0.0F, 0.0F, 0.0F, 0.0F, 0.0F, 0.0F, 0.0F, 0.0F, 0.0F, 0.0F, 0.0F, 0.0F, 1.0F, 0.0F}); ColorMatrixColorFilter filter1 = new ColorMatrixColorFilter(colorMatrix1); Paint p1 = new Paint(); p1.setXfermode(lightenXfermode); p1.setColorFilter(filter1); p1.setAlpha(255); canvas.drawBitmap(srcBmp, 0.0F, 0.0F, p1); ColorMatrix colorMatrix2 = new ColorMatrix(); colorMatrix2.set(new float[]{ 0.0F, 0.0F, 0.0F, 0.0F, 0.0F, 0.0F, 1.0F, 0.0F, 0.0F, 0.0F, 0.0F, 0.0F, 0.0F, 0.0F, 0.0F, 0.0F, 0.0F, 0.0F, 1.0F, 0.0F}); p1.setColorFilter(new ColorMatrixColorFilter(colorMatrix2)); RectF r1 = new RectF((float)(-i1 / 2), (float)(-i1 / 2), (float)(i1 + width), (float)(i1 + height)); canvas.drawBitmap(srcBmp, (Rect)null, r1, p1); ColorMatrix colorMatrix3 = new ColorMatrix(); colorMatrix3.set(new float[]{ 0.0F, 0.0F, 0.0F, 0.0F, 0.0F, 0.0F, 0.0F, 0.0F, 0.0F, 0.0F, 0.0F, 0.0F, 1.0F, 0.0F, 0.0F, 0.0F, 0.0F, 0.0F, 1.0F, 0.0F}); p1.setColorFilter(new ColorMatrixColorFilter(colorMatrix3)); RectF r2 = new RectF((float)(-i2 / 2), (float)(-i2 / 2), (float)(i2 + width), (float)(i2 + height)); canvas.drawBitmap(srcBmp, (Rect)null, r2, p1); srcBmp.recycle();
-
ColorMatrix 彩色矩阵
2019-10-01 12:26:34选择自 hbzxf 的 Blog 首先对装配脑袋给出上两片文章的友好回复,还有网友Fisherman一起探讨ColorMatrix话题表示感谢!ColorMatrix (彩色矩阵) 类位于System.Drawing.Imaging命名空间 先看看下面的代码 ... -
Paint之ColorMatrix
2018-05-18 18:19:59绘图的色彩矩阵这里 我只是大概写一下,大姐想具体看的,可以去看这位大神的博客生成色彩矩阵 ColorMatrix colorMatrix = new ColorMatrix(new float[]{ 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, ... -
ColorMatrix理解
2012-06-25 01:35:23虽然不同平台或标准中ColorMatrix的实现有所不同,但是基本原理和实现思路基本上是一样的。先分享几个链接,关于GDI+中ColorMatrix的实现原理: 上篇—ColorMatrix原理揭秘:... -
android colormatrix
2012-07-23 14:39:51这些效果在android中有很好的支持,通过颜色矩阵(ColorMatrix)和坐标变换矩阵(Matrix)可以完美的做出上面的所说的效果。 下面将分别介绍这两个矩阵的用法和相关的函数。 颜色矩阵 android中可以通过颜色... -
Remove function pointers from ColorMatrix
2020-11-24 18:15:39<div><p>In #1778 , mentioned that removing function pointers from the <code>ColorMatrix</code> class could lead to some perf benefits. I'll report that there have been some very miniscule ... -
ColorMatrix详解
2012-12-14 16:58:55ColorMatrix(色彩矩阵),是GDI+里用来调整图片色彩的矩阵。 什么是矩阵,说白了就是C#里的二维数组。 那么这个矩阵调整色彩的原理是什么,他是怎么来调整色彩的呢?这个要从线性代数里的矩阵相乘说起。 ... -
Derive correct ColorMatrix by checking CalibrationIlluminant
2020-11-27 20:48:17<p>Usually RT shows abnormal WB values for raws of non fully supported cameras (missing colormatrix data) or cameras with different than the expected colormatrix2 at D65 illumination in DNG exif.... -
ColorMatrix的简单使用
2020-05-08 12:39:55ColorMatrix,色彩矩阵,是Android中图片色彩处理的关键类,通过它可以实现图片的各种酷炫效果,学习色彩矩阵之前先来学习一下矩阵. 一:矩阵的来源: 我们都知道矩阵的形式, 但是矩阵的来源是什么呢,其实矩阵最早来自... -
绘图机制ColorMatrix
2016-03-31 22:43:42我们可以通过Android系统提供的API,来进行ColorMatrix的修改,也可以精确的修改矩阵的值来实现颜色的效果。 这里是第二种: public class PictureActivity extends AppCompatActivity {ViewGroup grid = null; ... -
Android图像处理ColorMatrix
2018-09-07 16:50:05Android图像处理ColorMatrix 1.颜色矩阵 可以看如下ColorMatix类的注释 /** * 4x5 matrix for transforming the color and alpha components of a Bitmap. * The matrix can be passed as single array, and ... -
ColorFilter与ColorMatrix
2015-10-23 11:48:28该类内部引用的有一个ColorMatrix对象,它的主要工作也是通过ColorMatrix完成的。而且主要工作原理也可以看ColorMatrix类的注释。示例如下: protected void onDraw(Canvas canvas) { super.onDraw(canvas -
colormatrix 示意图
2012-12-20 21:04:30.net 下的ColorMatrix 和android下的ColorMatrix使用可能不相同 c# 如下: android:http://blog.csdn.net/kufeiyun/article/details/6187858 -
Android 简单使用ColorMatrix
2018-08-06 18:56:39在编程中有时候需要对图片做特殊的处理,...这些效果在android中有很好的支持,通过颜色矩阵(ColorMatrix)和坐标变换矩阵(Matrix)可以完美的做出上面的所说的效果。下面将分别介绍这两个矩阵的用法和相... -
实战Android:图片处理之ColorMatrix和Matrix实例
2018-07-31 11:37:50一个综合演示Matrix和ColorMatrix的例子(https://blog.csdn.net/tanmx219/article/details/81298671) -
图像颜色处理(ColorMatrix)源码
2013-03-20 22:11:44图像颜色处理(ColorMatrix)源码 -
Matrix和ColorMatrix
2012-12-25 21:33:52这些效果在android中有很好的支持,通过颜色矩阵(ColorMatrix)和坐标变换矩阵(Matrix)可以完美的做出上面的所说的效果。 下面将分别介绍这两个矩阵的用法和相关的函数。 颜色矩阵 android中可以通过颜色... -
Android颜色矩阵——ColorMatrix
2019-12-20 14:49:48不仅仅可以通过Android系统提供的API来进行ColorMatrix的修改,同样可以精确地修改矩阵的值来实现颜色效果的处理。 下面我们就模拟一个4*5的颜色矩阵。通过修改矩阵中的值,一来验证全面所说的改变图像色彩效果的... -
Android颜色矩阵ColorMatrix详解
2018-09-18 08:09:35本篇转自 Idtk 的博客,详细的讲解了 android ColorMatrix的相关内容。一起来看看!希望大家喜欢。Idtk 的博客地址:http://www.idtk... -
Android中ColorMatrix的学习
2016-03-10 18:00:14在上篇Android中Matrix的学习的学习中,我知道Matrix是一个3*3的矩阵,并且知道了它的一些基本算法规则,那么ColorMatrix也是类似的一个矩阵不过它是一个4*5的矩阵,如图 这里有篇文章非常详细的介绍了... -
自定义控件三部曲之绘图篇(八)——Paint之ColorMatrix与滤镜效果
2016-04-19 09:11:40前言:虽然梦想为了现实暂时会妥协,但终有一天,它将会实现 ...这篇主要讲解ColorMatrix的相关知识,这里将涉及到矩阵乘法的相关知识。所以这篇是比较有难度的。 一、矩阵概述 1、定义 称为m... -
GDI+ ColorMatrix的完全揭秘
2017-03-09 11:12:00无论是用何种语言,只要使用过Windows的GDI+的人对ColorMatrix都不陌生,我的BLOG文章中也多次提到过,并在《GDI+ for VCL基础 -- 颜色调整矩阵ColorMatrix详解》一文中对其功能作了较为详细的讲解,虽然自认对...
-
Axios(Vue-Resource的取代者、拦截器)
-
ZigBee开发相关软件.zip
-
易语言开发通达信DLL公式接口
-
【数据分析-随到随学】Mysql数据库
-
阿里云云计算ACP考试必备教程
-
智能温度检测控制系统设计.zip
-
Kotlin协程极简入门与解密
-
微信支付2021系列之付款码支付一学就会java版
-
基于solidworks中的stewart平台建模
-
最新面试原型题HTTP与TCP/IP20连问,你能答出多少?
-
UNLOCK.iso加密解密软件和狗的一些资料
-
级联查询
-
burp suite1.7.rar
-
Xdebug helper.zip
-
【数据分析-随到随学】Tableau数据分 析+PowerBI
-
JavaFX:项目实战——贪吃蛇
-
项目范围管理论文-高项.docx
-
MFC开发简单聊天程序
-
6.数组完成快递的增删改查
-
商业的本质——杰克·韦尔奇著