转载请注明出处:http://blog.csdn.net/forwardyzk/article/details/44410833
下面介绍Android图像处理。
RGBA模型:red green blue alpha
1.改变其色像,透明度和亮度。
2.使用颜色矩阵处理图像
3.改变像素处理图像
1.使用ColorMatrix改变色相,透明度和亮度
获取新的图像的步骤:
(1)创建新的BitMap,使用createBitmap()方法
(2)使用ColorMatrix对象改变其色相,色值和亮度
/**
* Set the rotation on a color axis by the specified values.
* <code>axis=0</code> correspond to a rotation around the RED color
* <code>axis=1</code> correspond to a rotation around the GREEN color
* <code>axis=2</code> correspond to a rotation around the BLUE color
*/
setRotate(参数1,参数2)改变色相,第一个参数:0表示改变的红色,1表示的改变的绿色,2表示改变额是蓝色,第二个参数:表示的是深度。
setSaturation():设置透明度
postConcat():设置亮度
(3)使用画笔在画布上画出新的BitMap
给画笔设置颜色选择器,使颜色矩阵和画笔连接起来。
setColorFilter();
/**
* 获取改变了色值,透明度,亮度的BitMap
*
* @param bitmap
* @param rorate 色值
* @param saturation 透明度
* @param scale 亮度
* @return
*/
public static Bitmap handleImageEffect(Bitmap bitmap, float rorate, float saturation, float scale) {
Bitmap newBitMap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);
//色值
ColorMatrix rorateMatrix = new ColorMatrix();
rorateMatrix.setRotate(0, rorate);//red
rorateMatrix.setRotate(1, rorate);//green
rorateMatrix.setRotate(2, rorate);//blue
//透明度
ColorMatrix saturationMatrix = new ColorMatrix();
saturationMatrix.setSaturation(saturation);
//亮度
ColorMatrix scaleMatrix = new ColorMatrix();
scaleMatrix.setScale(scale, scale, scale, 1);
ColorMatrix colorMatrix = new ColorMatrix();
colorMatrix.postConcat(rorateMatrix);
colorMatrix.postConcat(saturationMatrix);
colorMatrix.postConcat(scaleMatrix);
Canvas canvas = new Canvas(newBitMap);
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setColorFilter(new ColorMatrixColorFilter(colorMatrix));
canvas.drawBitmap(bitmap, 0, 0, paint);
return newBitMap;
}
primary_color_activity.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_centerHorizontal="true"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ImageView
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp" />
<!--调节色象相-->
<SeekBar
android:id="@+id/seek_rorate"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:layout_below="@id/image" />
<!--调节透明度-->
<SeekBar
android:id="@+id/seek_saturation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:layout_below="@id/seek_rorate" />
<!--调节亮度-->
<SeekBar<span style="font-size:18px;">
android:id="@+id/seek_sacle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:layout_below="@id/seek_saturation" />
</RelativeLayout>
</span>
PrimaryColor.java
在initData()中获取ImageView显示的BitMap和给SeekBar显示最大值和默认显示的进度
以设置色相的为例:
颜色的最大值为255,最小值为0,
private final int MAX_VALUE = 255;
private final int MID_VALUE = 127;
seek_rorate.setMax(MAX_VALUE);
seek_rorate.setProgress(MID_VALUE);
给SeekBar设置进度改变监听器
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
switch (seekBar.getId()) {
case R.id.seek_rorate://色相
rorate = ((progress - MID_VALUE) * 1.0f / MID_VALUE * 180);
break;
case R.id.seek_sacle://亮度
sacle = progress * 1.0f / MID_VALUE;
break;
case R.id.seek_saturation://透明度
saturation = progress * 1.0f / MID_VALUE;
break;
}
image.setImageBitmap(ImageHelper.handleImageEffect(bitmap, rorate, saturation, sacle));
}
效果图:
第一个seekBar调节色相,第二个seekBar调节透明度,第三个seekBar调节亮度
2.使用颜色矩阵处理Android图片
颜色矩阵为4*5矩阵
使用颜色矩阵来设置图片
(1)创建新的BitMap
(2)使用ColorMatrix设置颜色矩阵
(3)使用画布联结新的BitMap
(4)设置画笔的颜色选择器-颜色矩阵
(5)使用画笔在画布上画出新的BitMap
/**
* 通过颜色矩阵获得BitMap
*
* @param bitmap
* @param flagColor 颜色矩阵
* @return
*/
public static Bitmap handleImageColorMatrix(Bitmap bitmap, float[] flagColor) {
Bitmap bitmap1 = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);
ColorMatrix matrix = new ColorMatrix();
matrix.set(flagColor);
Canvas canvas = new Canvas(bitmap1);
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setColorFilter(new ColorMatrixColorFilter(matrix));
canvas.drawBitmap(bitmap, 0, 0, paint);
return bitmap1;
}
matrix_color_activity.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center_horizontal"
tools:context=".MainActivity">
<ImageView
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:layout_weight="2" />
<GridLayout
android:layout_gravity="center_horizontal"
android:id="@+id/gridlayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="3"
android:rowCount="4"
android:columnCount="5"></GridLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="5dp"
android:layout_gravity="bottom">
<Button
android:id="@+id/change"
android:text="Change"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="@+id/reset"
android:text="Reset"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
初始化颜色色值
private float[] colorMatrix = new float[20];
private void initColorMatrix() {
for (int i = 0; i < 20; i++) {
if (i % 6 == 0) {
colorMatrix[i] = 1;
} else {
colorMatrix[i] = 0;
}
}
}
将EditText添加到GridLayout中
private void initEditTextMatrix() {
EditText editText;
for (int i = 0; i < 20; i++) {
editText = new EditText(MatrixColor.this);
editText.setText(String.valueOf(colorMatrix[i]));
editTextMatrix[i] = editText;
gridlayout.addView(editText);
}
}
为什么要默认是设置这样的矩阵值,因为这样的矩阵与其他的矩阵相乘,都不会改变原来矩阵值。
当改变了矩阵中的值,我们要点击Change,将新的矩阵值,设置给BitMap,
private void change(Bitmap bitmap, float[] flagColor) {
getEditText();
Bitmap bitmap1 = ImageHelper.handleImageColorMatrix(bitmap, flagColor);
//设置个ImageView
image.setImageBitmap(bitmap1);
}
当点击了Reset后,会把默认的值设置给BitMap
private void reset() {
initColorMatrix();
setEditText();
change(bitmap, colorMatrix);
}
private void setEditText() {
for (int i = 0; i < editTextMatrix.length; i++) {
editTextMatrix[i].setText(String.valueOf(colorMatrix[i]));
}
}
效果图:
第一行改变红色,第二行改变绿色,第三行改变蓝色
3.通过像素值处理图像
(1)创建新的BitMap
(2)获取当前BitMap的像素数组
(3)通过算法,获取新的像素数组
(4)给新的BitMap设置像素数组
/**
* 获取底片效果图-通过控制像素
*
* @param bitmap
* @return
*/
public static Bitmap handleImagePixel(Bitmap bitmap) {
//获取BitMap的长和宽,其实就是像素的值
int width = bitmap.getWidth();
int height = bitmap.getHeight();
//创建新的BitMap
Bitmap newBitMap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
int[] oldPixel = new int[width * height];
int[] newPixel = new int[width * height];
int r, g, b, a, r1, g1, b1;
//获取当前BitMap的像素
//接收像素的数组,获取像素数组的开始位置,获取像素的宽度,读取像素的开始的x坐标,读取像素的开始的y坐标,计算的像素的宽度范围,计算的像素的高度度范围
bitmap.getPixels(oldPixel, 0, width, 0, 0, width, height);
for (int i = 0; i < oldPixel.length; i++) {
//获取像素值
int color = oldPixel[i];
//将像素值转化成红色,绿色,蓝色和透明度
r = Color.red(color);
g = Color.green(color);
b = Color.blue(color);
a = Color.alpha(color);
//获取新的红色,绿色,蓝色和透明度的值,这只是其中的一个算法
r1 = 255 - r;
g1 = 255 - g;
b1 = 255 - b;
//因为颜色色值范围是0~255,不能超过这个范围
if (r1 < 0) {
r1 = 0;
} else if (r1 > 255) {
r1 = 255;
}
if (g1 < 0) {
g1 = 0;
} else if (g1 > 255) {
g1 = 255;
}
if (b1 < 0) {
b1 = 0;
} else if (b1 > 255) {
b1 = 255;
}
//将新的红色,绿色,蓝色和透明度转为ARGB颜色色值。
int newColor = Color.argb(a, r1, g1, b1);
//添加到新的像素数组
newPixel[i] = newColor;
}
//给新的BitMap设置像素数组
newBitMap.setPixels(newPixel, 0, width, 0, 0, width, height);
return newBitMap;
}
pixel_color_activity.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center_horizontal"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_weight="1">
<ImageView
android:id="@+id/image1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" />
<ImageView
android:id="@+id/image2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" />
</LinearLayout>
</LinearLayout>
PixelColor.java
bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.beauty);
image1 = (ImageView) findViewById(R.id.image1);
image2 = (ImageView) findViewById(R.id.image2);
image1.setImageBitmap(bitmap);
image2.setImageBitmap(ImageHelper.handleImagePixel(bitmap));
效果图:
源码下载: http://download.csdn.net/detail/forwardyzk/8512191