今天做的图片压缩,差点把宝宝难死。。。。主要是因为各种马虎。。不过最终还是成功了。
下面做的不知道是哪位大神的原创了,因为打开了若干个页面,所有如果冒犯到了,与我联系,我会做出相应处理。
应用场景:公司自己搭的服务器,产品有PC端,Android端和IOS端,三者要实现通信。
举一个:当Android端和Android端通信的时候,拍照发送图片,要上传给服务器,上传之前需要对图片进行缩小处理,不然上传速度慢不说,在Android端的接收者极易出现OOM
/**
* 通过降低图片的质量来压缩图片
* @param bmp
* 要压缩的图片位图对象
* @param maxSize
* 压缩后图片大小的最大值,单位KB
* @return 压缩后的图片位图对象
*/
public static Bitmap compressByQuality(Bitmap bitmap, int maxSize) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int quality = 100;
bitmap.compress(CompressFormat.JPEG, quality, baos);
Tools.error("图片压缩前大小:" + baos.toByteArray().length + "byte");
boolean isCompressed = false;
while (baos.toByteArray().length / 1024 > maxSize) {
quality -= 10;
baos.reset();
bitmap.compress(CompressFormat.JPEG, quality, baos);
Tools.error("质量压缩到原来的" + quality + "%时大小为:"
+ baos.toByteArray().length + "byte");
isCompressed = true;
}
Tools.error("图片压缩后大小:" + baos.toByteArray().length + "byte");
if (isCompressed) {
Bitmap compressedBitmap = BitmapFactory.decodeByteArray(
baos.toByteArray(), 0, baos.toByteArray().length);
recycleBitmap(bitmap);
return compressedBitmap;
} else {
return bitmap;
}
}
/**
* 回收位图对象
*
* @param bitmap
*/
public static void recycleBitmap(Bitmap bitmap) {
if (bitmap != null && !bitmap.isRecycled()) {
bitmap.recycle();
System.gc();
bitmap = null;
}
}
/**
* 通过压缩图片的尺寸来压缩图片大小
* @param bitmap
* 要压缩图片
* @param targetWidth
* 缩放的目标宽度
* @param targetHeight
* 缩放的目标高度
* @return 缩放后的图片
*/
public static Bitmap compressBySize(String imgPath, int targetWidth,
int targetHeight) {
BitmapFactory.Options newOpts = new BitmapFactory.Options();
// 开始读入图片,此时把options.inJustDecodeBounds 设回true,即只读边不读内容
newOpts.inPreferredConfig = Config.RGB_565;
Bitmap bitmap = BitmapFactory.decodeFile(imgPath,newOpts);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(CompressFormat.JPEG, 100, baos);
BitmapFactory.Options opts = new BitmapFactory.Options();
opts.inJustDecodeBounds = true;
bitmap = BitmapFactory.decodeByteArray(baos.toByteArray(), 0,
baos.toByteArray().length, opts);
// 得到图片的宽度、高度;
int imgWidth = opts.outWidth;
int imgHeight = opts.outHeight;
// 分别计算图片宽度、高度与目标宽度、高度的比例;取大于该比例的最小整数;
int widthRatio = (int) Math.ceil(imgWidth / (float) targetWidth);
int heightRatio = (int) Math.ceil(imgHeight / (float) targetHeight);
if (widthRatio > 1 || heightRatio > 1) {
if (widthRatio > heightRatio) {
opts.inSampleSize = widthRatio;
} else {
opts.inSampleSize = heightRatio;
}
}
// 设置好缩放比例后,加载图片进内存;
opts.inJustDecodeBounds = false;
Bitmap compressedBitmap = BitmapFactory.decodeByteArray(
baos.toByteArray(), 0, baos.toByteArray().length, opts);
recycleBitmap(bitmap);
return compressedBitmap;
}
//保存图片到sdcard中
public static void saveMyBitmap(String bitName,Bitmap mBitmap){
File f = new File(Environment.getExternalStorageDirectory() + "/DCIM/Camera/" + bitName);
try {
f.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
Tools.error("在保存图片时出错:"+e.toString());
}
FileOutputStream fOut = null;
try {
fOut = new FileOutputStream(f);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
mBitmap.compress(Bitmap.CompressFormat.JPEG, 100, fOut);
try {
fOut.flush();
} catch (IOException e) {
e.printStackTrace();
}
try {
fOut.close();
} catch (IOException e) {
e.printStackTrace();
}
}
压缩后把图片保存到本地,然后再上传到服务器