Skip to content

Commit

Permalink
Merge pull request #216 from ieyoieyo/master
Browse files Browse the repository at this point in the history
會話中傳送圖片,「原圖」功能、resize、壓縮以及保持原始方向的功能。Fix 更新頭像失敗。
  • Loading branch information
imndx authored Nov 22, 2019
2 parents 83b0dcd + 0d39780 commit e1591ad
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -36,25 +37,40 @@ 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<ImageItem> images = (ArrayList<ImageItem>) 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<ImageItem> images = (ArrayList<ImageItem>) 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 压缩的有问题
// imageFileSource = ImageUtils.genThumbImgFileEx(imageItem.path);
// //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();

}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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) {
Expand All @@ -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);

Expand All @@ -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;
}
}

0 comments on commit e1591ad

Please sign in to comment.