融云发送图片消息_融云IM SDK集成,Server API 发送图片消息时,缩略图生成逻辑说...

本文介绍了融云IM SDK集成中,发送图片消息时的缩略图生成逻辑。当原图任一边大于240像素时,会按照比例缩放并压缩至最大240像素、最小100像素,同时保持原图30%的质量。代码示例展示了Android端如何利用BitmapFactory和Matrix进行图片压缩和尺寸调整。
摘要由CSDN通过智能技术生成

该楼层疑似违规已被系统折叠 隐藏此楼查看此楼

大致压缩过程

原图宽或高只要有一边大于 240 则执行压缩处理

根据缩率图最大尺寸 240、最小尺寸 100 的要求加载图片文件(适度同比缩放原图)

将步骤 2 得到的图片按照原图 30% 的比例进行容量压缩

步骤 1、3 实现代码片段private static final int THUMB_COMPRESSED_SIZE = 240;private static final int THUMB_COMPRESSED_MIN_SIZE = 100;private static final int THUMB_COMPRESSED_QUALITY = 30;ImageMessage model = ...; // 图片消息String thumbPath = model.getThumUri().toString().substring(5);BitmapFactory.Options options = new BitmapFactory.Options();options.inJustDecodeBounds = true;BitmapFactory.decodeFile(thumbPath, options);String imageFormat = options.outMimeType != null ? options.outMimeType : "";// 1. 原图宽或高只要有一边大于 240 则执行压缩处理if (options.outWidth > THUMB_COMPRESSED_SIZE || options.outHeight > THUMB_COMPRESSED_SIZE) { // 2. 根据缩率图最大尺寸 240、最小尺寸 100 的要求加载图片文件(适度同比缩放原图) Bitmap bitmap = getThumbBitmap(getContext(), model.getThumUri(), THUMB_COMPRESSED_SIZE, THUMB_COMPRESSED_MIN_SIZE); if (bitmap != null) { ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); // 3. 将步骤 2 得到的图片按照原图 30% 的比例进行容量压缩 boolean success = bitmap.compress(Bitmap.CompressFormat.JPEG, THUMB_COMPRESSED_QUALITY, outputStream); // 在部分机型调用系统压缩转换png会有异常情况,修改后先进行判断是否压缩成功,如果压缩不成功则使用png方式进行二次压缩 if (!success) { bitmap.compress(Bitmap.CompressFormat.PNG, THUMB_COMPRESSED_QUALITY, outputStream); } model.setBase64(Base64.encodeToString(outputStream.toByteArray(), Base64.NO_WRAP)); outputStream.close(); model.setThumUri(Uri.parse("file://" + uri.toString() + IMAGE_THUMBNAIL_PATH + name)); if (!bitmap.isRecycled()) bitmap.recycle(); }}步骤 2 实现代码片段public static Bitmap getThumbBitmap(Context context, @NonNull Uri uri, int sizeLimit, int minSize) throws IOException { String path; Bitmap result; if (("file").equals(uri.getScheme())) { path = uri.toString().substring(5); } else if (("content").equals(uri.getScheme())) { Cursor cursor = context.getContentResolver().query(uri, new String[]{MediaStore.Images.Media.DATA}, null, null, null); if (cursor == null) { return null; } cursor.moveToFirst(); path = cursor.getString(0); cursor.close(); } else { return null; } ExifInterface exifInterface = new ExifInterface(path); Options options = new Options(); options.inJustDecodeBounds = true; BitmapFactory.decodeFile(path, options); int orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION, 0); int width = options.outWidth; int height = options.outHeight; int longSide = (width > height) ? width : height; int shortSide = (width > height) ? height : width; float scale = (float) longSide / shortSide; int sampleW = 1, sampleH = 1; int sampleSize = 1; if (scale > (float) sizeLimit / minSize) { while (shortSide / 2 > minSize) { shortSide /= 2; sampleSize <<= 1; } options = new Options(); options.inSampleSize = sampleSize; } else { while (width / 2 > sizeLimit) { width /= 2; sampleW <<= 1; } while (height / 2 > sizeLimit) { height /= 2; sampleH <<= 1; } options = new Options(); sampleSize = Math.max(sampleW, sampleH); options.inSampleSize = sampleSize; } Bitmap bitmap; try { bitmap = BitmapFactory.decodeFile(path, options); } catch (OutOfMemoryError e) { options.inSampleSize = options.inSampleSize << 1; bitmap = BitmapFactory.decodeFile(path, options); } Matrix matrix = new Matrix(); if (bitmap == null) { return null; } int w = bitmap.getWidth(); int h = bitmap.getHeight(); if (orientation == ExifInterface.ORIENTATION_ROTATE_90 || orientation == ExifInterface.ORIENTATION_ROTATE_270 || orientation == ExifInterface.ORIENTATION_TRANSPOSE || orientation == ExifInterface.ORIENTATION_TRANSVERSE) { int tmp = w; w = h; h = tmp; } switch (orientation) { case ExifInterface.ORIENTATION_ROTATE_90: matrix.setRotate(90, w / 2f, h / 2f); break; case ExifInterface.ORIENTATION_ROTATE_180: matrix.setRotate(180, w / 2f, h / 2f); break; case ExifInterface.ORIENTATION_ROTATE_270: matrix.setRotate(270, w / 2f, h / 2f); break; case ExifInterface.ORIENTATION_FLIP_HORIZONTAL: matrix.preScale(-1, 1); break; case ExifInterface.ORIENTATION_FLIP_VERTICAL: matrix.preScale(1, -1); break; case ExifInterface.ORIENTATION_TRANSPOSE: matrix.setRotate(90, w / 2f, h / 2f); matrix.preScale(1, -1); break; case ExifInterface.ORIENTATION_TRANSVERSE: matrix.setRotate(270, w / 2f, h / 2f); matrix.preScale(1, -1); break; } float sS = 0; float xS = 0; float yS = 0; if (scale > (float) sizeLimit / minSize) { shortSide = (bitmap.getWidth() > bitmap.getHeight()) ? bitmap.getHeight() : bitmap.getWidth(); sS = (float) minSize / shortSide; matrix.postScale(sS, sS); } else { xS = (float) sizeLimit / bitmap.getWidth(); yS = (float) sizeLimit / bitmap.getHeight(); matrix.postScale(Math.min(xS, yS), Math.min(xS, yS)); } int x = 0, y = 0; try { if (scale > (float) sizeLimit / minSize) { if (bitmap.getWidth() > bitmap.getHeight()) { h = bitmap.getHeight(); w = h * sizeLimit / minSize; x = (bitmap.getWidth() - w) / 2; y = 0; } else { w = bitmap.getWidth(); h = w * sizeLimit / minSize; x = 0; y = (bitmap.getHeight() - h) / 2; } } else { w = bitmap.getWidth(); h = bitmap.getHeight(); } result = Bitmap.createBitmap(bitmap, x, y, w, h, matrix, true); } catch (OutOfMemoryError e) { if (!bitmap.isRecycled()) bitmap.recycle(); return null; } if (!bitmap.isRecycled() && !bitmap.equals(result)) bitmap.recycle(); return result;}

注意:以上示例代码为 Android 端实现,且使用了 Android 平台图片处理相关 API

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值