我们知道调整颜色矩阵可以改变一幅图像的色彩效果,图像处理很大程度上就是在寻找图像的颜色矩阵。我们不仅可以通过ColorMatrix处理图片,也可以精确地修改矩阵的值来实现颜色效果的处理。
下面模拟4X5的颜色矩阵,通过改变矩阵中的值,来改变一幅图的色彩效果。 通过GridLayout进行布局(不止这一种方式),弄4行5列,模拟4*5的效果。代码如下:
/**
* 利用颜色矩阵的矩阵值来改变图片
* 主要是利用矩阵的公式,改变其偏移量或者改变其基值 R红 G绿 B蓝 A透明度
* 将对应的颜色矩阵值作用到原图像上,从而形成新的额色彩风格的图像。
* @author fanshenxia
*
*/
public class ColorMatrixActivity extends Activity implements OnClickListener {
private ImageView mImg;
private GridLayout mGrid;
private Button mBtnUse, mBtnReset;
private Bitmap mBitmap;
private int mEtWidth, mEtHeight;
private EditText[] mEts = new EditText[20];
private float[] mColorMatrix = new float[20];
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_matrix_color);
init();
}
private void init() {
mImg = findViewById(R.id.iv_color);
mGrid = findViewById(R.id.grid_color);
mBtnUse = findViewById(R.id.use_color);
mBtnReset = findViewById(R.id.reset_color);
mBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.boyimg);
mBtnUse.setOnClickListener(this);
mBtnReset.setOnClickListener(this);
mGrid.post(new Runnable() {
@Override
public void run() {
// 获取宽高信息
mEtWidth = mGrid.getWidth() / 5;
mEtHeight = mGrid.getHeight() / 4;
addEts();
initMatrix();
}
});
}
// 添加EditText
private void addEts() {
for (int i = 0; i < 20; i++) {
EditText editText = new EditText(ColorMatrixActivity.this);
mEts[i] = editText;
mGrid.addView(editText, mEtWidth, mEtHeight);
}
}
// 初始化颜色矩阵为初始状态
private void initMatrix() {
for (int i = 0; i < 20; i++) {
if (i % 6 == 0) {
mEts[i].setText(String.valueOf(1));
} else {
mEts[i].setText(String.valueOf(0));
}
}
}
// 获取矩阵值
private void getMatrix() {
for (int i = 0; i < 20; i++) {
mColorMatrix[i] = Float.valueOf(mEts[i].getText().toString());
}
}
//将矩阵值设置到图像
private void setImgMatrix() {
Bitmap curBitmap = Bitmap.createBitmap(mBitmap.getWidth(), mBitmap.getHeight(), Config.ARGB_8888);
ColorMatrix colorMatrix = new ColorMatrix();
colorMatrix.set(mColorMatrix);
Canvas canvas = new Canvas(curBitmap);
Paint paint = new Paint();
paint.setColorFilter(new ColorMatrixColorFilter(colorMatrix));
canvas.drawBitmap(mBitmap, 0, 0, paint);
mImg.setImageBitmap(curBitmap);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.use_color:
getMatrix();
setImgMatrix();
break;
case R.id.reset_color:
initMatrix();
getMatrix();
setImgMatrix();
break;
default:
break;
}
}
}
布局文件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:id="@+id/iv_color"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/boyimg" />
<GridLayout
android:id="@+id/grid_color"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:columnCount="5"
android:rowCount="4" >
GridLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal" >
<Button
android:id="@+id/use_color"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_margin="5dp"
android:layout_weight="1"
android:text="use" />
<Button
android:id="@+id/reset_color"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_margin="5dp"
android:layout_weight="1"
android:text="reset" />
LinearLayout>
LinearLayout>
运行上述代码,通过改变矩阵值改变图片色彩效果。
常用的图像颜色矩阵处理效果:
0.33f,0.59f,0.11f,0,0
0.33f,0.59f,0.11f,0,0
0.33f,0.59f,0.11f,0,0
0 ,0 ,0 ,1,0
图像反转效果,颜色矩阵如下:
-1,0,0,1,1,
0,-1,0,1,1,
0,0,-1,1,1,
0,0,0,1,0
怀旧效果,颜色矩阵如下:
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
去色效果,颜色矩阵如下:
1.5f,1.5f,1.5f,0,-1,
1.5f,1.5f,1.5f,0,-1,
1.5f,1.5f,1.5f,0,-1,
0,0,0,1,0
高饱和效果,颜色矩阵如下:
1.438f,-0.122f,-0.016f,0,-0.03f,
-0.062f,1.378f,-0.016,0,0.05f,
-0.062f,-0.122f,1.483f,0,-0.02f,
0,0,0,1,0
为了方便看效果,我添加了几个按钮:
添加了一些代码:
private float[] mGreyMatrix,mReversalMatrix,mOldMatrix;
private void initSomethingMatirx() {
//灰度
mGreyMatrix = new float[]{
0.33f,0.59f,0.11f,0,0
,0.33f,0.59f,0.11f,0,0
,0.33f,0.59f,0.11f,0,0
,0 ,0 ,0 ,1,0};
//反转
mReversalMatrix = new float[]{
-1,0,0,1,1,
0,-1,0,1,1,
0,0,-1,1,1,
0,0,0,1,0
};
//怀旧
mOldMatrix = new float[]{
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
};
}
//然后在点击事件中设置矩阵
完整代码如下:
public class ColorMatrixActivity extends Activity implements OnClickListener {
private ImageView mImg;
private GridLayout mGrid;
private Button mBtnUse, mBtnReset, mBtnGrey, mBtnReversal,mBtnOld;
private Bitmap mBitmap;
private int mEtWidth, mEtHeight;
private EditText[] mEts = new EditText[20];
private float[] mColorMatrix = new float[20];
private float[] mGreyMatrix,mReversalMatrix,mOldMatrix;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_matrix_color);
init();
}
private void init() {
initSomethingMatirx();
mImg = findViewById(R.id.iv_color);
mGrid = findViewById(R.id.grid_color);
mBtnUse = findViewById(R.id.use_color);
mBtnReset = findViewById(R.id.reset_color);
mBtnGrey = findViewById(R.id.grey_color);
mBtnReversal = findViewById(R.id.reversal_color);
mBtnOld = findViewById(R.id.old_color);
mBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.boyimg);
mBtnUse.setOnClickListener(this);
mBtnReset.setOnClickListener(this);
mBtnGrey.setOnClickListener(this);
mBtnReversal.setOnClickListener(this);
mBtnOld.setOnClickListener(this);
mGrid.post(new Runnable() {
@Override
public void run() {
// 获取宽高信息
mEtWidth = mGrid.getWidth() / 5;
mEtHeight = mGrid.getHeight() / 4;
addEts();
initMatrix();
}
});
}
private void initSomethingMatirx() {
mGreyMatrix = new float[]{
0.33f,0.59f,0.11f,0,0
,0.33f,0.59f,0.11f,0,0
,0.33f,0.59f,0.11f,0,0
,0 ,0 ,0 ,1,0};
mReversalMatrix = new float[]{
-1,0,0,1,1,
0,-1,0,1,1,
0,0,-1,1,1,
0,0,0,1,0
};
mOldMatrix = new float[]{
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
};
}
// 添加EditText
private void addEts() {
for (int i = 0; i < 20; i++) {
EditText editText = new EditText(ColorMatrixActivity.this);
mEts[i] = editText;
mGrid.addView(editText, mEtWidth, mEtHeight);
}
}
// 初始化颜色矩阵为初始状态
private void initMatrix() {
for (int i = 0; i < 20; i++) {
if (i % 6 == 0) {
mEts[i].setText(String.valueOf(1));
} else {
mEts[i].setText(String.valueOf(0));
}
}
}
// 获取矩阵值
private void getMatrix() {
for (int i = 0; i < 20; i++) {
mColorMatrix[i] = Float.valueOf(mEts[i].getText().toString());
}
}
// 将矩阵值设置到图像
private void setImgMatrix() {
Bitmap curBitmap = Bitmap.createBitmap(mBitmap.getWidth(), mBitmap.getHeight(), Config.ARGB_8888);
ColorMatrix colorMatrix = new ColorMatrix();
colorMatrix.set(mColorMatrix);
Canvas canvas = new Canvas(curBitmap);
Paint paint = new Paint();
paint.setColorFilter(new ColorMatrixColorFilter(colorMatrix));
canvas.drawBitmap(mBitmap, 0, 0, paint);
mImg.setImageBitmap(curBitmap);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.use_color:
getMatrix();
setImgMatrix();
break;
case R.id.reset_color:
initMatrix();
getMatrix();
setImgMatrix();
break;
case R.id.grey_color:
mColorMatrix = mGreyMatrix;
setImgMatrix();
break;
case R.id.reversal_color:
mColorMatrix = mReversalMatrix;
setImgMatrix();
break;
case R.id.old_color:
mColorMatrix = mOldMatrix;
setImgMatrix();
break;
default:
break;
}
}
}