简单的图片处理

注意是反转,不是翻转。



/**
* 图片处理
*
* @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;
}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值