android的照相功能随着手机硬件的发展,变得越来越强大,能够找出很高分辨率的图片。
有些场景中,需要照相并且上传到服务,但是由于图片的大小太大,那么就上传就会很慢(在有些网络情况下),而且很耗流量,要想速度快,那么就需要减小图片的大小。减少图片的大小有两种方法,1. 照小图片; 2. 压缩大图片。 照相时获取小图片一般不太符合要求,因为,图片的清晰度会很差,但是这种情况有个好处就是应用速度会快些; 压缩图片,就是把大图片压缩小,降低图片的质量,在一定范围内,降低图片的大小,并且满足需求(图片仍就清晰)。下面组要是介绍图片的压缩:
 
1. 照相请查看http://blog.csdn.net/luhuajcdd/article/details/8826587 ->想要保存图片到制定目录,启动Camera应用时,需要指定文件
2. 压缩过程:
    2.1 从图片路径中读取图片(图片很大,不能全部加在到内存中处理,要是全部加载到内存中会内存溢出)
  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 处理图片旋转

  1. int degree = readPictureDegree(filePath);
  2. bm = rotateBitmap(bm,degree) ;
  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. }
  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压缩图片

  1. bm.compress(Bitmap.CompressFormat.JPEG, 30, baos);//30 是压缩率,表示压缩70%; 如果不压缩是100,表示压缩率为0
 
完整的方法代码:
  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. }
  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. }

android bitmap compress的更多相关文章

  1. android bitmap compress(图片压缩)

    android bitmap compress android的照相功能随着手机硬件的发展,变得越来越强大,能够找出很高分辨率的图片. 有些场景中,需要照相并且上传到服务,但是由于图片的大小太大,那么 ...

  2. android bitmap的 一些简单操作

    /** 获取 drawable 的图片 可以循环 1.图名 2.drawable 3.包名 **/ int imgid = getResources().getIdentifier("ic_ ...

  3. Android bitmap序列化

    最近在开发中需要用到图片bitmap的序列化并进行传递,发现bitmap是没有序列化的,下面是自己实现的一个序列化方法,分享下. 以下是通过byte[]来进行序列化的,因为bitmap是没有序列化的, ...

  4. android Bitmap(将视图转为bitmap对象)

    1)从android的资源文件夹layout中加载xml布局文件,并把布局文件映射为Bitmap main.xml文件如下: <?xmlversion="1.0"encodi ...

  5. Android Bitmap转换WebP图片导致损坏的分析及解决方案

    背景 作为移动领域所力推的图片格式,WebP图片在商业领域证明了其应有的价值.基于其他格式的横向对比,其在压缩性能表现,及还原度极为优秀,节省大量的带宽开销.基于可观的效益比,团队早前已开始磋商将当前 ...

  6. [翻译]开发文档:android Bitmap的高效使用

    内容概述 本文内容来自开发文档"Traning > Displaying Bitmaps Efficiently",包括大尺寸Bitmap的高效加载,图片的异步加载和数据缓存 ...

  7. Android Bitmap 和 ByteArray的互相转换

    Android Bitmap 和 ByteArray的互相转换 移动平台图像处理,需要将图像传给native处理,如何传递?将bitmap转换成一个 byte[] 方便传递也方便cpp代码直接处理图像 ...

  8. Android Bitmap转换WebPng图片导致损坏的分析及解决方案

    出现问题的code!!! private void saveImage(String uri, String savePath) throws IOException { // 创建连接 HttpUR ...

  9. Android——BitMap(位图)相关知识总结贴

    Android中文API(136) —— Bitmap http://www.apkbus.com/android-54644-1-1.html Android 4.0 r1 API—Bitmap(S ...

随机推荐

  1. Qt 事件处理机制 (下篇)

    继续我们上一篇文章继续介绍,Qt 事件处理机制 (上篇) 介绍了Qt框架的事件处理机制:事件的产生.分发.接受和处理,并以视窗系统鼠标点击QWidget为例,对代码进行了剖析,向大家分析了Qt框架如何 ...

  2. tornado框架介绍

    一.安装tornado 手动安装: 下载 tornado-1.2.1.tar.gz tar xvzf tornado-1.2.1.tar.gz cd tornado-1.2.1 python setu ...

  3. servlet中获得tomcat项目根目录的绝对路径

    public class CreateXmlAction extends HttpServlet { private ServletConfig config; public void init(Se ...

  4. 了解Redis 和 Memcached 的区别

    1.Redis支持服务器端的数据操作:Redis相比Memcached来说,拥有更多的数据结构和并支持更丰富的数据操作,通常在Memcached里,你需要将数据拿到客户端来进行类似的修改再set回去. ...

  5. 【笔记】让360浏览器用chrome 内核渲染你的网页

    学校的项目还处在测试阶段 有一个痛点就是有一些页面在360浏览器中默认以ie 内核渲染 这样很不好 以为部分页面因技术方面的不足导致并不能很好地兼容ie 浏览器,于是在网上找了一下答案 可真还有解决方 ...

  6. SVM 总结

    SVM有一个核心函数SMO,也就是序列最小最优化算法.SMO基本是最快的二次规划优化算法,其核心就是找到最优参数α,计算超平面后进行分类.SMO方法可以将大优化问题分解为多个小优化问题求解,大大简化求 ...

  7. windows获取本机MAC地址并写入文件的bat

    windows获取本机MAC地址并写入文件的bat MAC(Media Access Control)地址,或称为 MAC地址.硬件地址,用来定义网络设备的位置. bat代码例如以下: @echo o ...

  8. 取石子(六)_nyoj_585(博弈-奇异矩阵).java

    取石子(六) 时间限制: 1000 ms  |  内存限制: 65535 KB 难度: 3   描述 最近 TopCoder 的 PIAOYI 和 HRDV 很无聊,于是就想了一个游戏,游戏是这样的: ...

  9. vue - 添加sass(less)处理

    1. 添加less.sass处理 1.1如果是sass,首先在当前目录安装处理插件(sass): npm i -D node-sass sass-loader 1.2如果是less,首先在当前目录安装 ...

  10. 算法笔记_136:交替字符串(Java)

    目录 1 问题描述 2 解决方案   1 问题描述 输入三个字符串s1.s2和s3,判断第三个字符串s3是否由前两个字符串s1和s2交错而成且不改变s1和s2中各个字符原有的相对顺序. 2 解决方案 ...