zoukankan      html  css  js  c++  java
  • android 图片处理

    一、缩放 指宽、高缩放

    (1)按比例缩放

    在开发图片浏览器等软件是,很多时候要显示图片的缩略图,而一般情况下,我们要将图片按照固定大小取缩略图,一般取缩略图的方法是使用BitmapFactory的decodeFile方法,然后通过传递进去 BitmapFactory.Option类型的参数进行取缩略图,在Option中,属性值inSampleSize表示缩略图大小为原始图片大小的几分之一,即如果这个值为2,则取出的缩略图的宽和高都是原始图片的1/2,图片大小就为原始大小的1/4。

    然而,如果我们想取固定大小的缩略图就比较困难了,比如,我们想将不同大小的图片去出来的缩略图高度都为200px,而且要保证图片不失真,那怎么办?我们总不能将原始图片加载到内存中再进行缩放处理吧,要知道在移动开发中,内存是相当宝贵的,而且一张100K的图片,加载完所占用的内存何止 100K?

      经过研究,发现,Options中有个属性inJustDecodeBounds,研究了一下,终于明白是什么意思了,SDK中的E文是这么说的

      If set to true, the decoder will return null (no bitmap), but the out... fields will still be set, allowing the caller to query the bitmap without having to allocate the memory for its pixels.

      意思就是说如果该值设为true那么将不返回实际的bitmap不给其分配内存空间而里面只包括一些解码边界信息即图片大小信息,那么相应的方法也就出来了,通过设置inJustDecodeBounds为true,获取到outHeight(图片原始高度)和 outWidth(图片的原始宽度),然后计算一个inSampleSize(缩放值),然后就可以取图片了,这里要注意的是,inSampleSize 可能小于0,必须做判断。也就是说先将Options的属性inJustDecodeBounds设为true,先获取图片的基本大小信息数据(信息没有保存在bitmap里面,而是保存在options里面),通过options.outHeight和 options. outWidth获取的大小信息以及自己想要到得图片大小计算出来缩放比例inSampleSize,然后紧接着将inJustDecodeBounds设为false,就可以根据已经得到的缩放比例得到自己想要的图片缩放图了。

     示例代码:

       BitmapFactory.Options options = new BitmapFactory.Options(); 
         options.inJustDecodeBounds = true;    //只取边界值信息
         Bitmap bitmap = BitmapFactory.decodeFile("/sdcard/test.jpg", options); //此时返回bm为空  
         options.inJustDecodeBounds = false;   //取图片实体
         //缩放比。由于是固定比例缩放,只用高或者宽其中一个数据进行计算即可  

     int sampleSize = 1;        //

     if(options.outWidth > deviceWidth){   //deviceWidth 为屏幕宽,这个值可以自己设定

      sampleSize = (int)options.outWidth / deviceWidth; 

     }

     options.inSampleSize = sampleSize; 
         //重新读入图片,注意此时已经把options.inJustDecodeBounds 设回false了  
         bitmap=BitmapFactory.decodeFile("/sdcard/test.jpg",options); 

    (2)按指定图片宽高缩放

            int bmpWidth  = bitmap.getWidth();  
        int bmpHeight  = bitmap.getHeight();  
          //缩放图片的尺寸   
          float scaleWidth  = (float) sWidth / bmpWidth;     //按固定大小缩放  sWidth 写多大就多大  
          float scaleHeight = (float) sHeight / bmpHeight;  //  
          Matrix matrix = new Matrix();  
          matrix.postScale(scaleWidth, scaleHeight);//产生缩放后的Bitmap对象   
          Bitmap resizeBitmap = Bitmap.createBitmap(bitmap, 0, 0, bmpWidth, bmpHeight, matrix, false);  
          bitmap.recycle();    //回收

    二、图片大小压缩 compress

        

    private Bitmap compressImage(Bitmap image,int size) {
            try {
              ByteArrayOutputStream baos = new ByteArrayOutputStream();
              image.compress(Bitmap.CompressFormat.JPEG, 100, baos);// 质量压缩方法,这里100表示不压缩,把压缩后的数据存放到baos中
                int options = 100;
              while (baos.toByteArray().length/1024 > size) { // 循环判断如果压缩后图片是否大于size,大于继续压缩
                     baos.reset();// 重置baos即清空baos
                  image.compress(Bitmap.CompressFormat.JPEG, options, baos);// 这里压缩比options=50,把压缩后的数据存放到baos中              
                     options -= 10;// 每次都减少10
              }
              ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray());// 把压缩后的数据baos存放到ByteArrayInputStream中
                Bitmap bitmap = BitmapFactory.decodeStream(isBm, null, null);// 把ByteArrayInputStream数据生成图片
              return bitmap;
            } catch (Exception e) {
                e.printStackTrace();
                return null;
            } 
     }
     
  • 相关阅读:
    Java进阶之并发初探
    Java进阶之HashMap剖析
    Java进阶之反射
    Linux常用命令
    海量数据处理算法与面试题
    一些刷题时总结的重要知识点
    一些Java刷题时的小知识点
    九章算法知识点与题目总结(不定时更新...)
    c++设计模式之状态模式
    c++设计模式之抽象工厂模式
  • 原文地址:https://www.cnblogs.com/lianghui66/p/3435923.html
Copyright © 2011-2022 走看看