|
1 | 1 | package com.jayfeng.lesscode.core; |
2 | 2 |
|
| 3 | +import android.content.Context; |
| 4 | +import android.graphics.Bitmap; |
3 | 5 | import android.graphics.BitmapFactory; |
| 6 | +import android.graphics.Matrix; |
| 7 | + |
| 8 | +import java.io.InputStream; |
4 | 9 |
|
5 | 10 | public final class BitmapLess { |
6 | 11 |
|
7 | 12 | /** |
8 | 13 | * 根据reqWidth, reqHeight计算最合适的inSampleSize |
| 14 | + * |
9 | 15 | * @param options |
10 | | - * @param reqWidth |
11 | | - * @param reqHeight |
| 16 | + * @param maxWidth |
| 17 | + * @param maxHeight |
12 | 18 | * @return |
13 | 19 | */ |
14 | | - public static int $inSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) { |
| 20 | + public static int $sampleSize(BitmapFactory.Options options, int maxWidth, int maxHeight) { |
15 | 21 | // raw height and width of image |
16 | 22 | int rawWidth = options.outWidth; |
17 | 23 | int rawHeight = options.outHeight; |
18 | 24 |
|
19 | 25 | // calculate best sample size |
20 | 26 | 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; |
24 | 30 | inSampleSize = (int) Math.min(ratioHeight, ratioWidth); |
25 | 31 | } |
26 | 32 | inSampleSize = Math.max(1, inSampleSize); |
27 | 33 |
|
28 | 34 | return inSampleSize; |
29 | 35 | } |
| 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 | + } |
30 | 165 | } |
0 commit comments