From 0d3978088b8b0f9422f2c1cb20ea1fdf85c7ab10 Mon Sep 17 00:00:00 2001 From: Johnny Date: Fri, 22 Nov 2019 18:31:26 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9C=83=E8=A9=B1=E4=B8=AD=E5=82=B3=E9=80=81?= =?UTF-8?q?=E5=9C=96=E7=89=87=EF=BC=8C=E3=80=8C=E5=8E=9F=E5=9C=96=E3=80=8D?= =?UTF-8?q?=E5=8A=9F=E8=83=BD=E3=80=81resize=E3=80=81=E5=A3=93=E7=B8=AE?= =?UTF-8?q?=E4=BB=A5=E5=8F=8A=E4=BF=9D=E6=8C=81=E5=8E=9F=E5=A7=8B=E6=96=B9?= =?UTF-8?q?=E5=90=91=E7=9A=84=E5=8A=9F=E8=83=BD=E3=80=82Fix=20=E6=9B=B4?= =?UTF-8?q?=E6=96=B0=E9=A0=AD=E5=83=8F=E5=A4=B1=E6=95=97=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../chat/kit/conversation/ext/ImageExt.java | 38 ++++++--- .../chat/kit/third/utils/ImageUtils.java | 85 ++++++++++++++++++- 2 files changed, 111 insertions(+), 12 deletions(-) diff --git a/chat/kit/src/main/java/cn/wildfire/chat/kit/conversation/ext/ImageExt.java b/chat/kit/src/main/java/cn/wildfire/chat/kit/conversation/ext/ImageExt.java index e250bf837..db11c7f6d 100644 --- a/chat/kit/src/main/java/cn/wildfire/chat/kit/conversation/ext/ImageExt.java +++ b/chat/kit/src/main/java/cn/wildfire/chat/kit/conversation/ext/ImageExt.java @@ -14,6 +14,7 @@ import cn.wildfire.chat.kit.annotation.ExtContextMenuItem; import cn.wildfire.chat.kit.conversation.ext.core.ConversationExt; import cn.wildfire.chat.kit.third.utils.ImageUtils; +import cn.wildfire.chat.kit.third.utils.UIUtils; import cn.wildfirechat.chat.R; import cn.wildfirechat.message.TypingMessageContent; import cn.wildfirechat.model.Conversation; @@ -36,16 +37,25 @@ public void pickImage(View containerView, Conversation conversation) { protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (resultCode == Activity.RESULT_OK) { if (data != null) { - //是否发送原图 - boolean compress = data.getBooleanExtra(ImagePicker.EXTRA_COMPRESS, true); - ArrayList images = (ArrayList) data.getSerializableExtra(ImagePicker.EXTRA_RESULT_ITEMS); - for (ImageItem imageItem : images) { - File imageFileThumb; - File imageFileSource; - // FIXME: 2018/11/29 压缩, 不是发原图的时候,大图需要进行压缩 + + new Thread(new Runnable() { + @Override + public void run() { + //是否发送原图 + boolean compress = data.getBooleanExtra(ImagePicker.EXTRA_COMPRESS, true); + ArrayList images = (ArrayList) data.getSerializableExtra(ImagePicker.EXTRA_RESULT_ITEMS); + for (ImageItem imageItem : images) { + File imageFileThumb; + File imageFileSource; + // FIXME: 2018/11/29 压缩, 不是发原图的时候,大图需要进行压缩 + if (compress) { + imageFileSource = ImageUtils.compressImage(imageItem.path); + } else { + imageFileSource = new File(imageItem.path); + } // if (isOrig) { - imageFileSource = new File(imageItem.path); - imageFileThumb = ImageUtils.genThumbImgFile(imageItem.path); +// imageFileSource = new File(imageItem.path); + imageFileThumb = ImageUtils.genThumbImgFile(imageItem.path); // } else { // //压缩图片 // // TODO 压缩的有问题 @@ -53,8 +63,14 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) { // //imageFileThumb = ImageUtils.genThumbImgFile(imageFileSource.getAbsolutePath()); // imageFileThumb = imageFileSource; // } - messageViewModel.sendImgMsg(conversation, imageFileThumb, imageFileSource); - } +// messageViewModel.sendImgMsg(conversation, imageFileThumb, imageFileSource); + UIUtils.postTaskSafely(() -> messageViewModel.sendImgMsg(conversation, imageFileThumb, imageFileSource)); + + } + + } + }).start(); + } } } diff --git a/chat/kit/src/main/java/cn/wildfire/chat/kit/third/utils/ImageUtils.java b/chat/kit/src/main/java/cn/wildfire/chat/kit/third/utils/ImageUtils.java index e3ae5f805..082b35e42 100644 --- a/chat/kit/src/main/java/cn/wildfire/chat/kit/third/utils/ImageUtils.java +++ b/chat/kit/src/main/java/cn/wildfire/chat/kit/third/utils/ImageUtils.java @@ -2,6 +2,8 @@ import android.graphics.Bitmap; import android.graphics.BitmapFactory; +import android.graphics.Matrix; +import android.media.ExifInterface; import android.media.ThumbnailUtils; import android.os.SystemClock; @@ -17,6 +19,9 @@ */ public class ImageUtils { private static final String THUMB_IMG_DIR_PATH = UIUtils.getContext().getCacheDir().getAbsolutePath(); + private static final int IMG_WIDTH = 480; //超過此寬、高則會 resize圖片 + private static final int IMG_HIGHT = 800; + private static final int COMPRESS_QUALITY = 70; //壓縮 JPEG使用的品質(70代表壓縮率為 30%) public static File genThumbImgFile(String srcImgPath) { @@ -37,7 +42,7 @@ public static File genThumbImgFile(String srcImgPath) { imageFileThumb = new File(thumbImgDir, thumbImgName); imageFileThumb.createNewFile(); - FileOutputStream fosThumb = new FileOutputStream(thumbImgName); + FileOutputStream fosThumb = new FileOutputStream(imageFileThumb); bmpTarget.compress(Bitmap.CompressFormat.JPEG, 100, fosThumb); @@ -46,4 +51,82 @@ public static File genThumbImgFile(String srcImgPath) { } return imageFileThumb; } + + public static File compressImage(String srcImgPath) { + //先取得原始照片的旋轉角度 + int rotate = 0; + try { + ExifInterface exif = new ExifInterface(srcImgPath); + int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL); + switch (orientation) { + case ExifInterface.ORIENTATION_ROTATE_90: + rotate = 90; + break; + case ExifInterface.ORIENTATION_ROTATE_180: + rotate = 180; + break; + case ExifInterface.ORIENTATION_ROTATE_270: + rotate = 270; + break; + } + } catch (IOException e) { + e.printStackTrace(); + } + + //計算取 Bitmap時的參數"inSampleSize" + BitmapFactory.Options options = new BitmapFactory.Options(); + options.inJustDecodeBounds = true; + BitmapFactory.decodeFile(srcImgPath, options); + + final int height = options.outHeight; + final int width = options.outWidth; + int inSampleSize = 1; + + if (height > IMG_HIGHT || width > IMG_WIDTH) { + + final int halfHeight = height / 2; + final int halfWidth = width / 2; + + // Calculate the largest inSampleSize value that is a power of 2 and keeps both + // height and width larger than the requested height and width. + while ((halfHeight / inSampleSize) >= IMG_HIGHT + && (halfWidth / inSampleSize) >= IMG_WIDTH) { + inSampleSize *= 2; + } + } + + options.inJustDecodeBounds = false; + options.inSampleSize = inSampleSize; + + //取出原檔的 Bitmap(若寬高超過會 resize)並設定原始的旋轉角度 + Bitmap srcBitmap = BitmapFactory.decodeFile(srcImgPath, options); + Matrix matrix = new Matrix(); + matrix.postRotate(rotate); + Bitmap outBitmap = Bitmap.createBitmap(srcBitmap, 0, 0, srcBitmap.getWidth(), srcBitmap.getHeight(), matrix, false); + + //壓縮並存檔至 cache路徑下的 File + File tempImgDir = new File(THUMB_IMG_DIR_PATH); + if (!tempImgDir.exists()) { + tempImgDir.mkdirs(); + } + String compressedImgName = SystemClock.currentThreadTimeMillis() + FileUtils.getFileNameFromPath(srcImgPath); + File compressedImgFile = new File(tempImgDir, compressedImgName); + FileOutputStream fos = null; + try { + compressedImgFile.createNewFile(); + fos = new FileOutputStream(compressedImgFile); + outBitmap.compress(Bitmap.CompressFormat.JPEG, COMPRESS_QUALITY, fos); + } catch (IOException e) { + e.printStackTrace(); + } finally { + try { + srcBitmap.recycle(); + outBitmap.recycle(); + fos.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + return compressedImgFile; + } }