# Matrix 跟 ColorMatrix初识

www.MyException.Cn  网友分享于：2013-02-20  浏览：9次
Matrix 和 ColorMatrix初识
Matrix：

Float [] x={1.0f,0.0f,0.0f,0.0f,-1.0f,0.0f,0.0f,0.0f,0.0f};

Matrix matrix=new Matrix();

matrix.setValues(f);

matrix.setRotate(90);

matrix.setRotate(90,x,y);

matrix.setRotate(90,0,0);

Float [] x={

1.0f,0.0f,100.0f,

0.0f,-1.0f,100.0f,

0.0f,0.0f,0.0f};

X=aX1+bY1;

Y=dX1+eY1;

//锐化效果
public static Bitmap toSharp(Bitmap bit)
{
long start =System.currentTimeMillis();
// 拉普拉斯矩阵
int[] laplacian = new int[] { -1, -1, -1, -1, 9, -1, -1, -1, -1 };

int width = bit.getWidth();
int height = bit.getHeight();
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);

int pixR = 0;
int pixG = 0;
int pixB = 0;

int pixColor = 0;

int newR = 0;
int newG = 0;
int newB = 0;

int idx = 0;
float alpha = 0.3F;
int[] pixels = new int[width * height];
bit.getPixels(pixels, 0, width, 0, 0, width, height);
for (int i = 1, length = height - 1; i < length; i++)
{
for (int k = 1, len = width - 1; k < len; k++)
{
idx = 0;
for (int m = -1; m <= 1; m++)
{
for (int n = -1; n <= 1; n++)
{
pixColor = pixels[(i + n) * width + k + m];
pixR = Color.red(pixColor);
pixG = Color.green(pixColor);
pixB = Color.blue(pixColor);

newR = newR + (int) (pixR * laplacian[idx] * alpha);
newG = newG + (int) (pixG * laplacian[idx] * alpha);
newB = newB + (int) (pixB * laplacian[idx] * alpha);
idx++;
}
}

newR = Math.min(255, Math.max(0, newR));
newG = Math.min(255, Math.max(0, newG));
newB = Math.min(255, Math.max(0, newB));

pixels[i * width + k] = Color.argb(255, newR, newG, newB);
newR = 0;
newG = 0;
newB = 0;
}
}

bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
long end = System.currentTimeMillis();
//Log.d("may", "used time="+(end - start));
return bitmap;
}

//旋轉90度
public static Bitmap ToNinety(Bitmap bitmap){
int w=bitmap.getWidth();
int h=bitmap.getHeight();
float fw=((float)100/w);
float fh=((float)100/h);
Canvas canvas=new Canvas(bitmap);
Matrix matrix=new Matrix();
Paint paint=new Paint();
paint.setColor(Color.RED);

final float jingxiang[]={
0.0f,1.0f,0.0f,
-1.0f,0.0f,0.0f,
0.0f,0.0f,1.0f};

matrix.setValues(jingxiang);
//matrix.setRotate(90);
matrix.postScale(fw, fh);

canvas.drawBitmap(bitmap, matrix, paint);
Bitmap newbitmap = Bitmap.createBitmap(bitmap, 0, 0, w,h, matrix, true);
return newbitmap;
}

//把图片变成黑白
public static Bitmap toGrayscale(Bitmap bmpOriginal) {
int width, height;
height = bmpOriginal.getHeight();
width = bmpOriginal.getWidth();
Bitmap bmpGrayscale = Bitmap.createBitmap(width, height,
Bitmap.Config.RGB_565);
Canvas c = new Canvas(bmpGrayscale);
Paint paint = new Paint();
ColorMatrix cm = new ColorMatrix();
cm.setSaturation(0);
ColorMatrixColorFilter f = new ColorMatrixColorFilter(cm);
paint.setColorFilter(f);
c.drawBitmap(bmpOriginal, 0, 0, paint);
return bmpGrayscale;
}

ColorMatrix

1 ,0 ,0, 0, 0,

0 ,1 ,0 ,0 ,0,

0 ,0, 1, 0, 0,

0 ,0 ,0 ,1 ,0

public static Bitmap What(Bitmap bitmap) {
int w=bitmap.getWidth();
int h=bitmap.getHeight();
Bitmap result = Bitmap.createBitmap(w, h,
Bitmap.Config.RGB_565);
Canvas c = new Canvas(bmpGrayscale);
Paint paint = new Paint();
ColorMatrix cm = new ColorMatrix();
cm.set(new float[]{
1 ,0 ,0, 0, 0,

0 ,1 ,0 ,0 ,0,

0 ,0, 1, 0, 0,

0 ,0 ,0 ,1 ,0

});
ColorMatrixColorFilter f = new ColorMatrixColorFilter(cm);
paint.setColorFilter(f);
c.drawBitmap(bitmap, 0, 0, paint);
return result;
}

public static Bitmap toGrayscale(Bitmap bmpOriginal) {
int width, height;
height = bmpOriginal.getHeight();
width = bmpOriginal.getWidth();
Bitmap bmpGrayscale = Bitmap.createBitmap(width, height,
Bitmap.Config.RGB_565);
Canvas c = new Canvas(bmpGrayscale);
Paint paint = new Paint();
ColorMatrix cm = new ColorMatrix();
cm.setSaturation(0);
ColorMatrixColorFilter f = new ColorMatrixColorFilter(cm);
paint.setColorFilter(f);
c.drawBitmap(bmpOriginal, 0, 0, paint);
return bmpGrayscale;
}