图片缩放 inJustDecodeBounds inSampleSize matrix

来源:互联网 发布:手机淘宝怎样发链接 编辑:程序博客网 时间:2024/05/05 21:38
安卓发中应用到图片的处理时候,我们通常会怎么缩放操作呢,来看下面的两种做法:

  方法1:按固定比例进行缩放

  在开发一些软件,如新闻客户端,很多时候要显示图片的缩略图,由于手机屏幕限制,一般情况下,我们取图片的缩略图,取缩略图的方法我们可以使用BitmapFactory的decodeFile方法,然后通过传递进去BitmapFactory.Option类型的参数进行取缩略图,在Option中,属性值inSampleSize表示缩略图大小为原始图片大小的几分之一,即如果这个值为2,则取出的缩略图的宽和高都是原始图片的1/2,图片大小就为原始大小的1/4。但是,如果我们想取固定大小的缩略图就比较困难了,比如,我们想将不同大小的图片取出来的缩略图高度都为200px,同时要保证图片不失真,那我们该怎么办?总不能将原始图片加载到内存中再进行缩放处理吧,要知道在移动开发中,内存是相当宝贵的,一张100K的图片,加载完所占用的内存何止是100K。

  经过阅读文档发现,Options中有个属性inJustDecodeBounds,文档中的是这么说的:

  If set to true, the decoder will return null (no bitmap), butthe out... fields will still be set, allowing the caller to querythe bitmap without having to allocate the memory for itspixels.  意思就是说如果该值设为true那么将不返回实际的bitmap对象,不给其分配内存空间但是可以得到一些解码边界信息即图片大小等信息。因此我们可以通过设置inJustDecodeBounds为true,获取到outHeight(图片原始高度)和outWidth(图片的原始宽度),然后计算一个inSampleSize(缩放值),就可以取图片了,这里要注意的是,inSampleSize可能等于0,必须做判断。也就是说先将Options的属性inJustDecodeBounds设为true,先获取图片的基本大小信息数据(信息没有保存在bitmap里面,而是保存在options里面),通过options.outHeight和options.outWidth获取的大小信息以及自己想要到得图片大小计算出来缩放比例inSampleSize,然后紧接着将inJustDecodeBounds设为false,就可以根据已经得到的缩放比例得到自己想要的图片缩放图了。

  实现代码如下:

  BitmapFactory.Options options = newBitmapFactory.Options();

  options.inJustDecodeBounds = true;

  // 记得把assets目录下的图片拷贝到SD卡中

  // 由于设置inJustDecodeBounds为true,因此执行下面代码后bitmap为空

  mBitmap = BitmapFactory.decodeFile("/sdcard/image.jpg",options);

  // 计算缩放比例,由于是固定比例缩放,只用高或者宽其中一个数据进行计算即可

  int scale = (int) (options.outHeight / (float) 200);

  // 因为结果为int型,如果相除后值为0.n,则最终结果将是0

  if (scale <= 0)

  {

  scale = 1;

  }

  System.out.println("Scale=" + scale);

  options.inSampleSize = scale;

  options.inJustDecodeBounds = false;

  // 重新读入图片,注意此时已经把options.inJustDecodeBounds设回false

  mBitmap = BitmapFactory.decodeFile("/sdcard/image.jpg",options);

  int width = mBitmap.getWidth();

  int height = mBitmap.getHeight();

  System.out.println(width + " " + height);

  ImageView image = (ImageView) findViewById(R.id.image);

  image.setImageBitmap(mBitmap);

  这样我们就可以读取较大的图片而不会出现内存溢出问题了。

  如果你想把压缩后的图片保存在sdcard上的话,通过如下代码就可以了:

  File file = new File("/sdcard/ruoshui.png");

  try

  {

  //记得添加sdcard读写权限

  FileOutputStream out = new FileOutputStream(file);

  if (mBitmap.compress(Bitmap.CompressFormat.PNG, 100, out))

  {

  out.flush();

  out.close();

  Toast.makeText(MainActivity.this, "保存成功!",Toast.LENGTH_LONG).show();

  }

  }catch (Exception e)

  {

  e.printStackTrace();

  Toast.makeText(MainActivity.this, "保存失败!",Toast.LENGTH_LONG).show();

  }

  方法2:按长宽各自比例进行缩放

  上面的方法缩放保存是按长宽比例的,我们当然也可以按固定大小进行缩放:

  BitmapFactory.Options options = newBitmapFactory.Options();

  options.inJustDecodeBounds = false;

  // 记得把assets目录下的图片拷贝到SD卡中

  // 由于设置inJustDecodeBounds为true,因此执行下面代码后bitmap为空

  mBitmap = BitmapFactory.decodeFile("/sdcard/image.jpg",options);

  int bmpWidth = mBitmap.getWidth();

  int bmpHeight = mBitmap.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(mBitmap, 0, 0,bmpWidth, bmpHeight, matrix, false);

  mBitmap.recycle();

  ImageView image = (ImageView) findViewById(R.id.image);

  image.setImageBitmap(resizeBitmap);

0 0