获取微信的缩略图 供三步
1 获取一个较小的缩略图
public static Bitmap extractThumbNail(final String path, final int height, final int width, final boolean crop) {
Assert.assertTrue(path != null && !path.equals("") && height > 0 && width > 0);
BitmapFactory.Options options = new BitmapFactory.Options();
try {
options.inJustDecodeBounds = true;
Bitmap tmp = BitmapFactory.decodeFile(path, options);
if (tmp != null) {
tmp.recycle();
tmp = null;
}
Log.d(TAG, "extractThumbNail: round=" + width + "x" + height + ", crop=" + crop);
final double beY = options.outHeight * 1.0 / height;
final double beX = options.outWidth * 1.0 / width;
Log.d(TAG, "extractThumbNail: extract beX = " + beX + ", beY = " + beY);
options.inSampleSize = (int) (crop ? (beY > beX ? beX : beY) : (beY < beX ? beX : beY));
if (options.inSampleSize <= 1) {
options.inSampleSize = 1;
}
// NOTE: out of memory error
while (options.outHeight * options.outWidth / options.inSampleSize > MAX_DECODE_PICTURE_SIZE) {
options.inSampleSize++;
}
int newHeight = height;
int newWidth = width;
if (crop) {
if (beY > beX) {
newHeight = (int) (newWidth * 1.0 * options.outHeight / options.outWidth);
} else {
newWidth = (int) (newHeight * 1.0 * options.outWidth / options.outHeight);
}
} else {
if (beY < beX) {
newHeight = (int) (newWidth * 1.0 * options.outHeight / options.outWidth);
} else {
newWidth = (int) (newHeight * 1.0 * options.outWidth / options.outHeight);
}
}
options.inJustDecodeBounds = false;
Log.i(TAG, "bitmap required size=" + newWidth + "x" + newHeight + ", orig=" + options.outWidth + "x" + options.outHeight + ", sample=" + options.inSampleSize);
Bitmap bm = BitmapFactory.decodeFile(path, options);
if (bm == null) {
Log.e(TAG, "bitmap decode failed");
return null;
}
Log.i(TAG, "bitmap decoded size=" + bm.getWidth() + "x" + bm.getHeight());
final Bitmap scale = Bitmap.createScaledBitmap(bm, newWidth, newHeight, true);
if (scale != null) {
bm.recycle();
bm = scale;
}
if (crop) {
final Bitmap cropped = Bitmap.createBitmap(bm, (bm.getWidth() - width) >> 1, (bm.getHeight() - height) >> 1, width, height);
if (cropped == null) {
return bm;
}
bm.recycle();
bm = cropped;
Log.i(TAG, "bitmap croped size=" + bm.getWidth() + "x" + bm.getHeight());
}
return bm;
} catch (final OutOfMemoryError e) {
Log.e(TAG, "decode bitmap failed: " + e.getMessage());
options = null;
}
return null;
}
调用方法
thumbBmp = Util.extractThumbNail(thumbUrl, 300, 300, false);
2 裁剪该缩略图,防止是那种长图
public static Bitmap checkImageSize(Bitmap bitmap) {
//防止超长图文件大小超过微信限制,需要进行截取,暂定比例上限为5
final int MAX_RATIO = 5;
Bitmap result = bitmap;
if (bitmap != null) {
int width = bitmap.getWidth();
int height = bitmap.getHeight();
float ratio = width > height ? width*1.0f / height : height*1.0f / width;
if (ratio > MAX_RATIO) {
int size = Math.min(width, height);
result = Bitmap.createBitmap(bitmap, 0, 0, size, size);
}
}
return result;
}
3 压缩图片使其在32k以下
public static byte[] compressBitmapToData(Bitmap bmp,float size) {
ByteArrayOutputStream output = new ByteArrayOutputStream();
byte[] result;
try {
bmp.compress(Bitmap.CompressFormat.JPEG, 100, output);//质量压缩方法,这里100表示不压缩,把压缩后的数据存放到baos中
int options = 100;
while ( output.toByteArray().length / 1024 >= size) { //循环判断如果压缩后图片是否大于size kb,大于继续压缩
output.reset();//重置baos即清空baos
bmp.compress(Bitmap.CompressFormat.JPEG, options, output);//这里压缩options%,把压缩后的数据存放到baos中
if(options==1){
break;
}
options -= 10;//每次都减少20
if(options<=0){
options=1;
}
}
result = output.toByteArray();
Methods.log("compressBitmap return length = " + result.length);
return result;
} catch (Exception e) {
e.printStackTrace();
return null;
}finally {
try {
output.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}