您的位置:首页 > 移动开发 > Android开发

android图像处理系列之二--图片旋转、缩放、反转

2011-12-14 23:57 686 查看
转自:http://blog.csdn.net/maylian7700/article/details/7071837

 

注意是反转,不是翻转。贴图:

原图:



处理后:



下面看代码:

package com.jacp.image.util;

import android.graphics.Bitmap;
import android.graphics.Matrix;

/**
* 图片处理
*
* @author maylian7700@126.com
*
*/
public class ImageHandler {

/**
* 图片旋转
*
* @param bmp
*            要旋转的图片
* @param degree
*            图片旋转的角度,负值为逆时针旋转,正值为顺时针旋转
* @return
*/
public static Bitmap rotateBitmap(Bitmap bmp, float degree) {
Matrix matrix = new Matrix();
matrix.postRotate(degree);
return Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), bmp.getHeight(), matrix, true);
}

/**
* 图片缩放
*
* @param bm
* @param scale
*            值小于则为缩小,否则为放大
* @return
*/
public static Bitmap resizeBitmap(Bitmap bm, float scale) {
Matrix matrix = new Matrix();
matrix.postScale(scale, scale);
return Bitmap.createBitmap(bm, 0, 0, bm.getWidth(), bm.getHeight(), matrix, true);
}

/**
* 图片缩放
*
* @param bm
* @param w
*            缩小或放大成的宽
* @param h
*            缩小或放大成的高
* @return
*/
public static Bitmap resizeBitmap(Bitmap bm, int w, int h) {
Bitmap BitmapOrg = bm;

int width = BitmapOrg.getWidth();
int height = BitmapOrg.getHeight();

float scaleWidth = ((float) w) / width;
float scaleHeight = ((float) h) / height;

Matrix matrix = new Matrix();
matrix.postScale(scaleWidth, scaleHeight);
return Bitmap.createBitmap(BitmapOrg, 0, 0, width, height, matrix, true);
}

/**
* 图片反转
*
* @param bm
* @param flag
*            0为水平反转,1为垂直反转
* @return
*/
public static Bitmap reverseBitmap(Bitmap bmp, int flag) {
float[] floats = null;
switch (flag) {
case 0: // 水平反转
floats = new float[] { -1f, 0f, 0f, 0f, 1f, 0f, 0f, 0f, 1f };
break;
case 1: // 垂直反转
floats = new float[] { 1f, 0f, 0f, 0f, -1f, 0f, 0f, 0f, 1f };
break;
}

if (floats != null) {
Matrix matrix = new Matrix();
matrix.setValues(floats);
return Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), bmp.getHeight(), matrix, true);
}

return null;
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息