(转)android 压缩图片大小 – 热爱改变生活
我的GitHub GitHub |     登录
  • If you can't fly, then run; if you can't run, then walk; if you can't walk, then crawl
  • but whatever you do, you have to keep moving forward。
  • “你骗得了我有什么用,这是你自己的人生”
  • 曾有伤心之地,入梦如听 此歌

(转)android 压缩图片大小

Android工具 sinvader 4011℃ 0评论

  1. package com.img.util;
  2. import java.io.File;
  3. import java.io.FileNotFoundException;
  4. import java.io.FileOutputStream;
  5. import java.io.OutputStream;
  6. import android.content.Context;
  7. import android.database.Cursor;
  8. import android.graphics.Bitmap;
  9. import android.graphics.BitmapFactory;
  10. import android.graphics.Bitmap.CompressFormat;
  11. import android.net.Uri;
  12. import android.os.Environment;
  13. import android.provider.MediaStore.Images;
  14. import android.util.Log;
  15. /**
  16. * 图片压缩工具类
  17. *
  18. * @author Administrator
  19. *
  20. */
  21. public class ImageCompress {
  22. public static final String CONTENT = "content";
  23. public static final String FILE = "file";
  24. /**
  25. * 图片压缩参数
  26. *
  27. * @author Administrator
  28. *
  29. */
  30. public static class CompressOptions {
  31. public static final int DEFAULT_WIDTH = 400;
  32. public static final int DEFAULT_HEIGHT = 800;
  33. public int maxWidth = DEFAULT_WIDTH;
  34. public int maxHeight = DEFAULT_HEIGHT;
  35. /**
  36. * 压缩后图片保存的文件
  37. */
  38. public File destFile;
  39. /**
  40. * 图片压缩格式, 默认为jpg格式
  41. */
  42. public CompressFormat imgFormat = CompressFormat.JPEG;
  43. /**
  44. * 图片压缩比例 默认为30
  45. */
  46. public int quality = 30;
  47. public Uri uri;
  48. }
  49. public Bitmap compressFromUri(Context context,
  50. CompressOptions compressOptions) {
  51. // uri指向的文件路径
  52. String filePath = getFilePath(context, compressOptions.uri);
  53. if (null == filePath) {
  54. return null;
  55. }
  56. BitmapFactory.Options options = new BitmapFactory.Options();
  57. options.inJustDecodeBounds = true;
  58. Bitmap temp = BitmapFactory.decodeFile(filePath, options);
  59. int actualWidth = options.outWidth;
  60. int actualHeight = options.outHeight;
  61. int desiredWidth = getResizedDimension(compressOptions.maxWidth,
  62. compressOptions.maxHeight, actualWidth, actualHeight);
  63. int desiredHeight = getResizedDimension(compressOptions.maxHeight,
  64. compressOptions.maxWidth, actualHeight, actualWidth);
  65. options.inJustDecodeBounds = false;
  66. options.inSampleSize = findBestSampleSize(actualWidth, actualHeight,
  67. desiredWidth, desiredHeight);
  68. Bitmap bitmap = null;
  69. Bitmap destBitmap = BitmapFactory.decodeFile(filePath, options);
  70. // If necessary, scale down to the maximal acceptable size.
  71. if (destBitmap.getWidth() > desiredWidth
  72. || destBitmap.getHeight() > desiredHeight) {
  73. bitmap = Bitmap.createScaledBitmap(destBitmap, desiredWidth,
  74. desiredHeight, true);
  75. destBitmap.recycle();
  76. } else {
  77. bitmap = destBitmap;
  78. }
  79. // compress file if need
  80. if (null != compressOptions.destFile) {
  81. compressFile(compressOptions, bitmap);
  82. }
  83. return bitmap;
  84. }
  85. /**
  86. * compress file from bitmap with compressOptions
  87. *
  88. * @param compressOptions
  89. * @param bitmap
  90. */
  91. private void compressFile(CompressOptions compressOptions, Bitmap bitmap) {
  92. OutputStream stream = null;
  93. try {
  94. stream = new FileOutputStream(compressOptions.destFile);
  95. } catch (FileNotFoundException e) {
  96. Log.e("ImageCompress", e.getMessage());
  97. }
  98. bitmap.compress(compressOptions.imgFormat, compressOptions.quality,
  99. stream);
  100. }
  101. private static int findBestSampleSize(int actualWidth, int actualHeight,
  102. int desiredWidth, int desiredHeight) {
  103. double wr = (double) actualWidth / desiredWidth;
  104. double hr = (double) actualHeight / desiredHeight;
  105. double ratio = Math.min(wr, hr);
  106. float n = 1.0f;
  107. while ((n * 2) < = ratio) {
  108. n *= 2;
  109. }
  110. return (int) n;
  111. }
  112. private static int getResizedDimension(int maxPrimary, int maxSecondary,
  113. int actualPrimary, int actualSecondary) {
  114. // If no dominant value at all, just return the actual.
  115. if (maxPrimary == 0 && maxSecondary == 0) {
  116. return actualPrimary;
  117. }
  118. // If primary is unspecified, scale primary to match secondary's scaling
  119. // ratio.
  120. if (maxPrimary == 0) {
  121. double ratio = (double) maxSecondary / (double) actualSecondary;
  122. return (int) (actualPrimary * ratio);
  123. }
  124. if (maxSecondary == 0) {
  125. return maxPrimary;
  126. }
  127. double ratio = (double) actualSecondary / (double) actualPrimary;
  128. int resized = maxPrimary;
  129. if (resized * ratio > maxSecondary) {
  130. resized = (int) (maxSecondary / ratio);
  131. }
  132. return resized;
  133. }
  134. /**
  135. * 获取文件的路径
  136. *
  137. * @param scheme
  138. * @return
  139. */
  140. private String getFilePath(Context context, Uri uri) {
  141. String filePath = null;
  142. if (CONTENT.equalsIgnoreCase(uri.getScheme())) {
  143. Cursor cursor = context.getContentResolver().query(uri,
  144. new String[] { Images.Media.DATA }, null, null, null);
  145. if (null == cursor) {
  146. return null;
  147. }
  148. try {
  149. if (cursor.moveToNext()) {
  150. filePath = cursor.getString(cursor
  151. .getColumnIndex(Images.Media.DATA));
  152. }
  153. } finally {
  154. cursor.close();
  155. }
  156. }
  157. // 从文件中选择
  158. if (FILE.equalsIgnoreCase(uri.getScheme())) {
  159. filePath = uri.getPath();
  160. }
  161. return filePath;
  162. }
  163. }

调用方法:

  1. ImageCompress compress = new ImageCompress();
  2. ImageCompress.CompressOptions options = new ImageCompress.CompressOptions();
  3. options.uri = imgUri;
  4. options.maxWidth=getWindowManager().getDefaultDisplay().getWidth();
  5. options.maxHeight=getWindowManager().getDefaultDisplay().getHeight();
  6. Bitmap bitmap = compress.compressFromUri(this, options);
¥ 有帮助么?打赏一下~

转载请注明:热爱改变生活.cn » (转)android 压缩图片大小


本博客只要没有注明“转”,那么均为原创。 转载请注明链接:sumile.cn » (转)android 压缩图片大小

喜欢 (0)
发表我的评论
取消评论
表情

如需邮件形式接收回复,请注册登录

Hi,你需要填写昵称和邮箱~

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址