获取分享到微信的缩略图 thumbData
来源:互联网 发布:java 首字母大写方法 编辑:程序博客网 时间:2024/06/05 03:04
获取微信的缩略图 供三步
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(); } } }
0 0
- 获取分享到微信的缩略图 thumbData
- 微信分享图片checkArgs fail, thumbData is invalid的错误
- 获取图像的缩略图
- 获取视频的缩略图
- 获取视频文件的缩略图
- 获取视频的缩略图
- 缩略图thumb_media_id的获取
- 获取图片的缩略图
- 获取视频的缩略图
- 获取视频的缩略图
- 缩略图thumb_media_id的获取
- 微信分享失败之checkArgs fail, thumbData is invalid
- 29、Android 微信分享checkArgs fail, thumbData is invalid
- C#获取图片的缩略图
- 获取任意文件的缩略图
- 获取本地视频文件的缩略图
- Android 获取文件的缩略图
- iOS获取视频的缩略图
- Android中属性动画解析
- position:sticky实现iOS6+下的粘性布局
- HTML5基础之canvas绘图(一)
- mediawiki系列1:皮肤的自定义
- 编写JNI的两种应用层与JNI层方法映射方式
- 获取分享到微信的缩略图 thumbData
- SQL子查询并判断如果为NULL赋值0
- java 序列化
- VS201 LINK2001 ___CxxFrameHandler3、@__security_check_cookie编译报错的解决
- 在线修改大表为分区表或更改其索引
- github简单连接使用
- MVC架构
- Linux 下 mysql 的相关命令
- maven3常用命令、java项目搭建、web项目搭建详细图解