Skip to content

Commit 794319d

Browse files
committed
修复三星手机拍照的竖图在编辑器中显示成横图的问题
1 parent ad2ddaa commit 794319d

File tree

2 files changed

+92
-6
lines changed

2 files changed

+92
-6
lines changed

richeditor/src/main/java/com/yuruiyin/richeditor/RichEditText.java

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,7 @@
3939
import com.yuruiyin.richeditor.model.RichEditorBlock;
4040
import com.yuruiyin.richeditor.model.StyleBtnVm;
4141
import com.yuruiyin.richeditor.span.BlockImageSpan;
42-
import com.yuruiyin.richeditor.utils.ClipboardUtil;
43-
import com.yuruiyin.richeditor.utils.FileUtil;
44-
import com.yuruiyin.richeditor.utils.ViewUtil;
45-
import com.yuruiyin.richeditor.utils.WindowUtil;
42+
import com.yuruiyin.richeditor.utils.*;
4643
import ren.qinc.edit.PerformEdit;
4744

4845
import java.io.File;
@@ -369,8 +366,16 @@ private void insertBlockImageInternal(Uri uri, @NonNull BlockImageSpanVm blockIm
369366
try {
370367
InputStream is = mContext.getContentResolver().openInputStream(
371368
uri);
372-
Bitmap bitmap = BitmapFactory.decodeStream(is);
373-
Drawable drawable = new BitmapDrawable(mContext.getResources(), bitmap);
369+
String filePath = FileUtil.getFileRealPath(mContext, uri);
370+
Bitmap resBitmap;
371+
int degree = BitmapUtil.readPictureDegree(filePath);
372+
if (degree > 0) {
373+
// 若图片角度大于0,则需要旋转角度
374+
resBitmap = BitmapUtil.rotateBitmap(degree, filePath);
375+
} else {
376+
resBitmap = BitmapFactory.decodeStream(is);
377+
}
378+
Drawable drawable = new BitmapDrawable(mContext.getResources(), resBitmap);
374379
drawable.setBounds(0, 0, drawable.getIntrinsicWidth(),
375380
drawable.getIntrinsicHeight());
376381
is.close();
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package com.yuruiyin.richeditor.utils;
2+
3+
import android.graphics.Bitmap;
4+
import android.graphics.BitmapFactory;
5+
import android.graphics.Matrix;
6+
import android.media.ExifInterface;
7+
8+
import java.io.File;
9+
import java.io.IOException;
10+
11+
/**
12+
* Title:
13+
* Description:
14+
*
15+
* @author yuruiyin
16+
* @version 2019-05-29
17+
*/
18+
public class BitmapUtil {
19+
20+
public static int readPictureDegree(String path) {
21+
int degree = 0;
22+
try {
23+
ExifInterface exifInterface = new ExifInterface(path);
24+
int orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
25+
switch (orientation) {
26+
case ExifInterface.ORIENTATION_ROTATE_90:
27+
degree = 90;
28+
break;
29+
case ExifInterface.ORIENTATION_ROTATE_180:
30+
degree = 180;
31+
break;
32+
case ExifInterface.ORIENTATION_ROTATE_270:
33+
degree = 270;
34+
break;
35+
default:
36+
degree = 0;
37+
break;
38+
}
39+
} catch (IOException e) {
40+
e.printStackTrace();
41+
}
42+
return degree;
43+
}
44+
45+
/**
46+
* 判断拍照 图片是否旋转
47+
*
48+
* @param degree 图片的角度
49+
* @param filePath 文件路径
50+
*/
51+
public static Bitmap rotateBitmap(int degree, String filePath) {
52+
if (degree > 0) {
53+
// 针对相片有旋转问题的处理方式
54+
try {
55+
//获取缩略图显示到屏幕上
56+
BitmapFactory.Options opts = new BitmapFactory.Options();
57+
opts.inSampleSize = 2;
58+
Bitmap bitmap = BitmapFactory.decodeFile(filePath, opts);
59+
return rotateBitmapInternal(degree, bitmap);
60+
} catch (Exception e) {
61+
e.printStackTrace();
62+
}
63+
}
64+
return null;
65+
}
66+
67+
/**
68+
* 旋转图片
69+
*/
70+
private static Bitmap rotateBitmapInternal(int angle, Bitmap bitmap) {
71+
//旋转图片 动作
72+
Matrix matrix = new Matrix();
73+
matrix.postRotate(angle);
74+
// 创建新的图片
75+
Bitmap resizedBitmap = Bitmap.createBitmap(bitmap, 0, 0,
76+
bitmap.getWidth(), bitmap.getHeight(), matrix, true);
77+
return resizedBitmap;
78+
}
79+
80+
81+
}

0 commit comments

Comments
 (0)