最近要做图片处理,写了一个工具类。
1.图片压缩
2.图片尺寸缩放
3.图片切片
4.图片灰度变化
5.图片倒影
6.图片圆角制作
看一下代码:
/**
*
* @author gxx
*图片处理类
*1.图片压缩
*2.图片尺寸缩放
*3.图片切片
*4.图片灰度变化
*5.图片倒影
*6.图片圆角
*/
public class BitmapUtil {
// 根据质量压缩
public static Bitmap compressBitmap(Bitmap bitmap) {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
bitmap.compress(CompressFormat.JPEG, 100, bos);// 将图片压入流 就是将图片转化为字节数组
int options = 100;
while (bos.toByteArray().length / 1024 > 100) {
bos.reset();// 将管道流清空
options -= 10;
bitmap.compress(CompressFormat.JPEG, options, bos);// 重新压入
}
bitmap = BitmapFactory.decodeByteArray(bos.toByteArray(), 0,
bos.toByteArray().length);
return bitmap;
}
// 根据尺寸缩放
public static Bitmap compressBydensity(Bitmap bm, int newwidth,
int newheight) {
int xscale = newwidth / bm.getWidth();
int yscale = newheight / bm.getHeight();
Matrix matrix = new Matrix();
matrix.postScale(xscale, yscale);
Bitmap newbm = Bitmap.createBitmap(bm, 0, 0, bm.getWidth(),
bm.getHeight(), matrix, true);
return newbm;
}
// 彩色变黑白
public static Bitmap HuiSeJie(Bitmap bitmap) {
int width = bitmap.getWidth();
int height = bitmap.getHeight();
int[] piexs = new int[width * height];// 像素点数组
Log.i("gxx", (width*height)+"-----");
bitmap.getPixels(piexs, 0, width, 0, 0, width, height);// 将图片字节转化对应像素点
// 每一个像素点处理
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
int grey=piexs[width*i+j];
piexs[width * i + j] = doChangeHuidu(grey);
}
}
Bitmap newbm=Bitmap.createBitmap