android bitmap compress(图片压缩)

android bitmap compress

android的照相功能随着手机硬件的发展,变得越来越强大,能够找出很高分辨率的图片。

有些场景中,需要照相并且上传到服务,但是由于图片的大小太大,那么就上传就会很慢(在有些网络情况下),而且很耗流量,要想速度快,那么就需要减小图片的大小。减少图片的大小有两种方法,1. 照小图片; 2. 压缩大图片。 照相时获取小图片一般不太符合要求,因为,图片的清晰度会很差,但是这种情况有个好处就是应用速度会快些; 压缩图片,就是把大图片压缩小,降低图片的质量,在一定范围内,降低图片的大小,并且满足需求(图片仍就清晰)。下面组要是介绍图片的压缩:

1. 照相请查看http://blog.csdn.net/luhuajcdd/article/details/8826587 ->想要保存图片到制定目录,启动Camera应用时,需要指定文件

2. 压缩过程:

2.1 从图片路径中读取图片(图片很大,不能全部加在到内存中处理,要是全部加载到内存中会内存溢出)

[java] view plaincopy

  1. final BitmapFactory.Options options = new BitmapFactory.Options();
  2. options.inJustDecodeBounds = true;
  3. BitmapFactory.decodeFile(filePath, options);
  4. // Calculate inSampleSize
  5. options.inSampleSize = calculateInSampleSize(options, 480, 800);
  6. // Decode bitmap with inSampleSize set
  7. options.inJustDecodeBounds = false;
  8. Bitmap bm = BitmapFactory.decodeFile(filePath, options);

2.2 处理图片旋转

[java] view plaincopy

  1. int degree = readPictureDegree(filePath);
  2. bm = rotateBitmap(bm,degree) ;

[java] view plaincopy

  1. private static int readPictureDegree(String path) {
  2. int degree  = 0;
  3. try {
  4. ExifInterface exifInterface = new ExifInterface(path);
  5. int orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
  6. switch (orientation) {
  7. case ExifInterface.ORIENTATION_ROTATE_90:
  8. degree = 90;
  9. break;
  10. case ExifInterface.ORIENTATION_ROTATE_180:
  11. degree = 180;
  12. break;
  13. case ExifInterface.ORIENTATION_ROTATE_270:
  14. degree = 270;
  15. break;
  16. }
  17. } catch (IOException e) {
  18. e.printStackTrace();
  19. }
  20. return degree;
  21. }

[java] view plaincopy

  1. private static Bitmap rotateBitmap(Bitmap bitmap, int rotate){
  2. if(bitmap == null)
  3. return null ;
  4. int w = bitmap.getWidth();
  5. int h = bitmap.getHeight();
  6. // Setting post rotate to 90
  7. Matrix mtx = new Matrix();
  8. mtx.postRotate(rotate);
  9. return Bitmap.createBitmap(bitmap, 0, 0, w, h, mtx, true);
  10. }

2.3压缩图片

[java] view plaincopy

  1. bm.compress(Bitmap.CompressFormat.JPEG, 30, baos);//30 是压缩率,表示压缩70%; 如果不压缩是100,表示压缩率为0

完整的方法代码:

[java] view plaincopy

  1. public static Bitmap getSmallBitmap(String filePath) {
  2. final BitmapFactory.Options options = new BitmapFactory.Options();
  3. options.inJustDecodeBounds = true;
  4. BitmapFactory.decodeFile(filePath, options);
  5. // Calculate inSampleSize
  6. options.inSampleSize = calculateInSampleSize(options, 480, 800);
  7. // Decode bitmap with inSampleSize set
  8. options.inJustDecodeBounds = false;
  9. Bitmap bm = BitmapFactory.decodeFile(filePath, options);
  10. if(bm == null){
  11. return  null;
  12. }
  13. int degree = readPictureDegree(filePath);
  14. bm = rotateBitmap(bm,degree) ;
  15. ByteArrayOutputStream baos = null ;
  16. try{
  17. baos = new ByteArrayOutputStream();
  18. bm.compress(Bitmap.CompressFormat.JPEG, 30, baos);
  19. }finally{
  20. try {
  21. if(baos != null)
  22. baos.close() ;
  23. } catch (IOException e) {
  24. e.printStackTrace();
  25. }
  26. }
  27. return bm ;
  28. }

[java] view plaincopy

  1. private static int calculateInSampleSize(BitmapFactory.Options options,
  2. int reqWidth, int reqHeight) {
  3. // Raw height and width of image
  4. final int height = options.outHeight;
  5. final int width = options.outWidth;
  6. int inSampleSize = 1;
  7. if (height > reqHeight || width > reqWidth) {
  8. // Calculate ratios of height and width to requested height and
  9. // width
  10. final int heightRatio = Math.round((float) height
  11. / (float) reqHeight);
  12. final int widthRatio = Math.round((float) width / (float) reqWidth);
  13. // Choose the smallest ratio as inSampleSize value, this will
  14. // guarantee
  15. // a final image with both dimensions larger than or equal to the
  16. // requested height and width.
  17. inSampleSize = heightRatio < widthRatio ? widthRatio : heightRatio;
  18. }
  19. return inSampleSize;
  20. }
时间: 2025-01-22 05:40:47

android bitmap compress(图片压缩)的相关文章

Android不错的图片压缩方法

Android不错的图片压缩方法 一.图片质量压缩 /** * 质量压缩方法 * * @param image * @return */ public static Bitmap compressImage(Bitmap image) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); image.compress(Bitmap.CompressFormat.JPEG, 100, baos);// 质量压缩方法,这里100表示不

Android Bitmap 开源图片框架分析(精华三)

主要介绍这三个框架,都挺有名的,其他的框架估计也差不多了 Android-Universal-Image-Loaderhttps://github.com/nostra13/Android-Universal-Image-Loader ImageLoaderhttps://github.com/novoda/ImageLoader Volley(综合框架,包含图片部分)https://github.com/mcxiaoke/android-volley 扯淡时间,可以跳过这段这些开源框架的源码还

Android Bitmap 开源图片框架分析(精华四)

disk缓存主要难点在于内存缓存,disk缓存其实比较简单,就是图片加载完成后把图片文件存到本地方便下次使用 同样,先贴一下官方主页的介绍(主页地址见文章最开始处)和内存缓存差不多,根据算法不同提供了几种类别,可以自行通过ImageLoaderConfiguration.discCache(..)设置<ignore_js_op> 硬盘缓存,保存是以文件的形式框架提供了4种类型,具体算法规则不同,看名字我们大概也能知道对应意思 UnlimitedDiscCache                

Android Bitmap 开源图片框架分析(精华五)

本帖最后由 boredream 于 2014-5-27 09:07 编辑 ImageLoader和Volley图片部分还包括其他大部分图片框架,基本上图片处理都差不多,区别仅在于部分优化了,而优化方面UIL即Universal-Image-Loader框架做的最好,所以这部分章节算是温习一下图片处理以及寻找下其他框架里面一些不一样的图片处理方式(只关注图片方面) 首先是ImageLoaderhttps://github.com/novoda/ImageLoader主要还是分析图片加载的核心代码部

[Android算法] bitmap 将图片压缩到指定的大小

Bitmap压缩到指定大小: private void imageZoom() {//图片允许最大空间 单位:KBdouble maxSize =400.00;//将bitmap放至数组中,意在bitmap的大小(与实际读取的原文件要大)ByteArrayOutputStream baos = new ByteArrayOutputStream();bitMap.compress(Bitmap.CompressFormat.JPEG, 100, baos);byte[] b = baos.toB

Android中的图片压缩

1.android中计算图片占用堆内存的kB大小跟图片本身的kB大小无关,而是根据图片的尺寸来计算的. 比如一张 480*320大小的图片占用的堆内存大小为: 480*320*4/1024=600kB  之所以要乘以4,是因为在android中使用的ARGB图片,图片一个像素占用四个字节. 2.手机出厂时 堆内存(Heap)是固定的,所以为了不造成OOM,我们就需要生成bitmap时对图片进行压缩处理. 实际使用中我们压缩图片的标准是手机屏幕大小作为参照的,这个主要是因为,即便是图片尺寸跟屏幕尺

有关Option.inSamplSize 和 Compress 图片压缩

1.图片占用内存:占用的内存 = 图片长度 * 图片宽度 * 单位像素占用的字节数 注: 图片长度和图片宽度就是图片在行列上的像素数量. 图片格式: ALPHA_8:表示8位Alpha位图,即A=8,一个像素点占用1个字节,它没有颜色,只有透明度 ARGB_4444:表示16位ARGB位图,即A=4,R=4,G=4,B=4,一个像素点占4+4+4+4=16位,2个字节 ARGB_8888:表示32位ARGB位图,即A=8,R=8,G=8,B=8,一个像素点占8+8+8+8=32位,4个字节 RG

Android学习之图片压缩,压缩程度高且失真度小

曾经在做手机上传图片的时候.直接获取相机拍摄的原图上传,原图大小一般1~2M.因此上传一张都比較浪费资源,有些场景还须要图片多张上传,所以近期查看了好多前辈写的关于图片处理的资料.然后试着改了一个图片压缩的方法.经測试用着还不错.压缩效果非常理想,主要是失真不明显.在这里分享一下. 以下直接上代码: /** * 上传server时把图片调用以下方法压缩后 保存到暂时目录 图片压缩后小于200KB.失真度不明显 * * @param path * @return * @throws IOExcep

android bitmap compress

android的照相功能随着手机硬件的发展,变得越来越强大,能够找出很高分辨率的图片. 有些场景中,需要照相并且上传到服务,但是由于图片的大小太大,那么就上传就会很慢(在有些网络情况下),而且很耗流量,要想速度快,那么就需要减小图片的大小.减少图片的大小有两种方法,1. 照小图片: 2. 压缩大图片. 照相时获取小图片一般不太符合要求,因为,图片的清晰度会很差,但是这种情况有个好处就是应用速度会快些: 压缩图片,就是把大图片压缩小,降低图片的质量,在一定范围内,降低图片的大小,并且满足需求(图片