1package cn.mucang.android.community.utils; 2 3import android.graphics.Bitmap; 4import android.graphics.BitmapFactory; 5import android.graphics.Matrix; 6 7import java.io.ByteArrayOutputStream; 8import java.io.File; 9import java.io.FileOutputStream; 10import java.io.IOException; 11 12/** 13 * 图像工具类,用于裁剪、放大缩小、质量调整等... 14 * Created by Sanders on 2014/11/3. 15 */ 16public class BitmapUtils { 17 18 /** 19 * 裁剪图片为当前指定的尺寸 20 * 21 * @param path 22 * @param newWidth 指定宽度 23 * @param newHeight 指定高度 24 * @return 返回宽度不大于指定宽度,高度不大于指定高度的Bitmap 25 */ 26 public static Bitmap tailorBitmap(String path, int newWidth, int newHeight) { 27 int maxSize = newWidth >= newHeight ? newWidth : newHeight; 28 Bitmap inBitmap = getValidationBitmap(path, maxSize); 29 return tailorBitmap(inBitmap, newWidth, newHeight); 30 } 31 32 /** 33 * 按最大边裁剪 34 * 35 * @param path 36 * @param maxSize 37 * @return 38 */ 39 public static Bitmap tailorBitmap(String path, int maxSize) { 40 Bitmap inBitmap = getValidationBitmap(path, maxSize); 41 return tailorBitmap(inBitmap, maxSize); 42 } 43 44 /** 45 * 参见图片为当前指定的尺寸 46 * 47 * @param inBitmap 48 * @param newWidth 指定宽度 49 * @param newHeight 指定高度 50 * @return 返回宽度不大于指定宽度,高度不大于指定高度的Bitmap 51 */ 52 public static Bitmap tailorBitmap(Bitmap inBitmap, int newWidth, int newHeight) { 53 float inWidth = inBitmap.getWidth(); 54 float inHeight = inBitmap.getHeight(); 55 float scale; 56 if (inWidth >= inHeight) { 57 scale = (float) newWidth / inWidth; 58 } else { 59 scale = (float) newHeight / inHeight; 60 } 61 if(scale == 1){ 62 return inBitmap; 63 } 64 Matrix matrix = new Matrix(); 65 matrix.postScale(scale, scale); 66 Bitmap newBitmap = Bitmap.createBitmap(inBitmap, 0, 0, (int) inWidth, (int) inHeight, matrix, true); 67 inBitmap.recycle(); 68 return newBitmap; 69 } 70 71 /** 72 * 验证图像不要内存溢出,在操作前先缩小下 73 * 74 * @param path 文件路径 75 * @param inMaxSize 一条边的最大长度 76 * @return 77 */ 78 public static Bitmap getValidationBitmap(String path, int inMaxSize) { 79 BitmapFactory.Options options = new BitmapFactory.Options(); 80 options.inJustDecodeBounds = true; 81 Bitmap bitmap = BitmapFactory.decodeFile(path, options); 82 int outWidth = options.outWidth; 83 int outHeight = options.outHeight; 84 int maxSize = outWidth >= outHeight ? outWidth : outHeight; 85 int sampleSize = maxSize / inMaxSize; 86 options.inSampleSize = sampleSize > 0 ? sampleSize : 1; 87 options.inJustDecodeBounds = false; 88 bitmap = BitmapFactory.decodeFile(path, options); 89 return bitmap; 90 } 91 92 /** 93 * 裁剪最大的边为传入边,最大边长不能大于maxSize 94 * 95 * @param inBitmap 位图 96 * @param maxSize 最大边长 97 * @return 98 */ 99 public static Bitmap tailorBitmap(Bitmap inBitmap, int maxSize) { 100 float inWidth = inBitmap.getWidth(); 101 float inHeight = inBitmap.getHeight(); 102 float scale; 103 if (inWidth >= inHeight) { 104 scale = (float) maxSize / inWidth; 105 106 } else { 107 scale = (float) maxSize / inHeight; 108 } 109 if(scale == 1){ 110 return inBitmap; 111 } 112 Matrix matrix = new Matrix(); 113 matrix.postScale(scale, scale); 114 Bitmap newBitmap = Bitmap.createBitmap(inBitmap, 0, 0, (int) inWidth, (int) inHeight, matrix, true); 115 inBitmap.recycle(); 116 return newBitmap; 117 } 118 119 /** 120 * 保存bitmap为图片 121 * 122 * @param bitmap 123 * @param filePath 124 * @param fileName 125 * @return 126 * @throws java.io.IOException 127 */ 128 public static File saveBitmapToFile(Bitmap bitmap, String filePath, String fileName) throws IOException { 129 File file = new File(filePath, fileName + ".jpg"); 130 FileOutputStream outputStream = new FileOutputStream(file); 131 bitmap.compress(Bitmap.CompressFormat.JPEG, 80, outputStream); 132 outputStream.close(); 133 bitmap.recycle(); 134 return file; 135 } 136 137 /** 138 * 将bitmap转换为Byte数组并压缩指定大小 139 * 140 * @param bitmap 141 * @param maxSize 指定大小 142 * @return 143 * @throws java.io.IOException 144 */ 145 public static byte[] getBitmapToBytes(Bitmap bitmap, long maxSize) throws IOException { 146 ByteArrayOutputStream outStream = new ByteArrayOutputStream(); 147 int options = 80; 148 bitmap.compress(Bitmap.CompressFormat.JPEG, options, outStream); 149 byte[] buffer = outStream.toByteArray(); 150 while (buffer.length > maxSize) { 151 outStream.reset(); 152 options -= 1; 153 bitmap.compress(Bitmap.CompressFormat.JPEG, options, outStream); 154 buffer = outStream.toByteArray(); 155 } 156 outStream.close(); 157 bitmap.recycle(); 158 return buffer; 159 } 160 161}
Android图片处理工具类(持续积累补充)
Stella981
2021-10-11
1128 0 0
点赞
收藏
评论区
加载中...