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; + } }