Skip to content

Commit eca49a0

Browse files
author
fengjian
committed
add scale, rotate, thumbnail method to BitmapLess
1 parent 6a8e0fb commit eca49a0

2 files changed

Lines changed: 176 additions & 6 deletions

File tree

Lines changed: 141 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,165 @@
11
package com.jayfeng.lesscode.core;
22

3+
import android.content.Context;
4+
import android.graphics.Bitmap;
35
import android.graphics.BitmapFactory;
6+
import android.graphics.Matrix;
7+
8+
import java.io.InputStream;
49

510
public final class BitmapLess {
611

712
/**
813
* 根据reqWidth, reqHeight计算最合适的inSampleSize
14+
*
915
* @param options
10-
* @param reqWidth
11-
* @param reqHeight
16+
* @param maxWidth
17+
* @param maxHeight
1218
* @return
1319
*/
14-
public static int $inSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
20+
public static int $sampleSize(BitmapFactory.Options options, int maxWidth, int maxHeight) {
1521
// raw height and width of image
1622
int rawWidth = options.outWidth;
1723
int rawHeight = options.outHeight;
1824

1925
// calculate best sample size
2026
int inSampleSize = 0;
21-
if (rawHeight > reqHeight || rawWidth > reqWidth) {
22-
float ratioWidth = (float) rawWidth / reqWidth;
23-
float ratioHeight = (float) rawHeight / reqHeight;
27+
if (rawHeight > maxHeight || rawWidth > maxWidth) {
28+
float ratioWidth = (float) rawWidth / maxWidth;
29+
float ratioHeight = (float) rawHeight / maxHeight;
2430
inSampleSize = (int) Math.min(ratioHeight, ratioWidth);
2531
}
2632
inSampleSize = Math.max(1, inSampleSize);
2733

2834
return inSampleSize;
2935
}
36+
37+
/**
38+
* 更节省内存的读取raw资源成Bitmap
39+
*
40+
* @param context
41+
* @param rawId
42+
* @return
43+
*/
44+
public static Bitmap $raw(Context context, int rawId) {
45+
BitmapFactory.Options opt = new BitmapFactory.Options();
46+
opt.inPreferredConfig = Bitmap.Config.RGB_565;
47+
opt.inPurgeable = true;
48+
opt.inInputShareable = true;
49+
InputStream is = context.getResources().openRawResource(rawId);
50+
return BitmapFactory.decodeStream(is, null, opt);
51+
}
52+
53+
/**
54+
* 旋转Bitmap(默认回收传进来的原始Bitmap)
55+
*
56+
* @param originBitmap
57+
* @param angle
58+
* @return
59+
*/
60+
public static Bitmap $rotate(Bitmap originBitmap, int angle) {
61+
return $rotate(originBitmap, angle, true);
62+
}
63+
64+
/**
65+
* 旋转Bitmap
66+
*
67+
* @param angle
68+
* @param originBitmap
69+
* @param recycleOriginBitmap 是否回收传进来的原始Bitmap
70+
* @return
71+
*/
72+
public static Bitmap $rotate(Bitmap originBitmap, int angle, boolean recycleOriginBitmap) {
73+
Matrix matrix = new Matrix();
74+
matrix.postRotate(angle);
75+
Bitmap rotatedBitmap = Bitmap.createBitmap(originBitmap,
76+
0, 0, originBitmap.getWidth(), originBitmap.getHeight(), matrix, true);
77+
if (recycleOriginBitmap && originBitmap != null && !originBitmap.isRecycled()) {
78+
originBitmap.recycle();
79+
}
80+
return rotatedBitmap;
81+
}
82+
83+
/**
84+
* 缩放Bitmap(默认回收传进来的原始Bitmap)
85+
*
86+
* @param originBitmap
87+
* @param scaleX
88+
* @param scaleY
89+
* @return
90+
*/
91+
public static Bitmap $scale(Bitmap originBitmap, float scaleX, float scaleY) {
92+
return $scale(originBitmap, scaleX, scaleY, true);
93+
}
94+
95+
/**
96+
* 缩放Bitmap
97+
*
98+
* @param originBitmap
99+
* @param scaleX
100+
* @param scaleY
101+
* @param recycleOriginBitmap 是否回收传进来的原始Bitmap
102+
* @return
103+
*/
104+
public static Bitmap $scale(Bitmap originBitmap, float scaleX, float scaleY, boolean recycleOriginBitmap) {
105+
Matrix matrix = new Matrix();
106+
matrix.postScale(scaleX, scaleY);
107+
Bitmap rotatedBitmap = Bitmap.createBitmap(originBitmap,
108+
0, 0, originBitmap.getWidth(), originBitmap.getHeight(), matrix, true);
109+
if (recycleOriginBitmap && originBitmap != null && !originBitmap.isRecycled()) {
110+
originBitmap.recycle();
111+
}
112+
return rotatedBitmap;
113+
}
114+
115+
/**
116+
* 获取缩略图(默认关闭自动旋转)
117+
* @param path
118+
* @param maxWidth
119+
* @param maxHeight
120+
* @return
121+
*/
122+
public static Bitmap $thumbnail(String path, int maxWidth, int maxHeight) {
123+
return $thumbnail(path, maxWidth, maxHeight, false);
124+
}
125+
126+
/**
127+
* 获取缩略图
128+
* 1. 支持自动旋转
129+
* @param path
130+
* @param maxWidth
131+
* @param maxHeight
132+
* @param autoRotate
133+
* @return
134+
*/
135+
public static Bitmap $thumbnail(String path, int maxWidth, int maxHeight, boolean autoRotate) {
136+
137+
int angle = 0;
138+
if (autoRotate) {
139+
angle = ImageLess.$exifRotateAngle(path);
140+
}
141+
142+
BitmapFactory.Options options = new BitmapFactory.Options();
143+
options.inJustDecodeBounds = true;
144+
// 获取这个图片的宽和高, 此时返回bm为空
145+
Bitmap bitmap = BitmapFactory.decodeFile(path, options);
146+
options.inJustDecodeBounds = false;
147+
// 计算缩放比
148+
int sampleSize = $sampleSize(options, maxWidth, maxHeight);
149+
options.inSampleSize = sampleSize;
150+
options.inPreferredConfig = Bitmap.Config.RGB_565;
151+
options.inPurgeable = true;
152+
options.inInputShareable = true;
153+
154+
if (bitmap != null && !bitmap.isRecycled()) {
155+
bitmap.recycle();
156+
}
157+
bitmap = BitmapFactory.decodeFile(path, options);
158+
159+
if (autoRotate && angle != 0) {
160+
bitmap = $rotate(bitmap, angle);
161+
}
162+
163+
return bitmap;
164+
}
30165
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.jayfeng.lesscode.core;
2+
3+
import android.media.ExifInterface;
4+
5+
import java.io.IOException;
6+
7+
public class ImageLess {
8+
9+
/**
10+
* 获取图片的exif的旋转角度
11+
* @param path
12+
* @return
13+
*/
14+
public static int $exifRotateAngle(String path) {
15+
int angle = 0;
16+
try {
17+
ExifInterface exifInterface = new ExifInterface(path);
18+
int orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
19+
switch (orientation) {
20+
case ExifInterface.ORIENTATION_ROTATE_90:
21+
angle = 90;
22+
break;
23+
case ExifInterface.ORIENTATION_ROTATE_180:
24+
angle = 180;
25+
break;
26+
case ExifInterface.ORIENTATION_ROTATE_270:
27+
angle = 270;
28+
break;
29+
}
30+
} catch (IOException e) {
31+
e.printStackTrace();
32+
}
33+
return angle;
34+
}
35+
}

0 commit comments

Comments
 (0)