请尊重他人的劳动成果,转载请注明出处:Android图片压缩技巧
http://blog.csdn.net/fengyuzhengfan/article/details/41759835
当需要将Android客户端的图片上传到服务器时,往往需要将图片进行压缩,关于图片的压缩方法,小编分享几种常用的方式:
第一种方式:裁切以达到压缩的目的
我曾在《Android开发之裁剪照片》一文中详细介绍过如何裁切照片,感兴趣的朋友可以去看一下。
第二种方式:将图片进行降质处理(即降低图片的质量)以达到压缩的目的
这种方式也是比较常用的方式,下面就为大家介绍如何对图片进行降质:
将图片降质我们可以使用Bitmap的这个方法:boolean android.graphics.Bitmap.compress(CompressFormat format, int quality, OutputStream stream)
其中,参数format表示压缩后的格式,quality压缩后的图片质量(0表示最低,100表示不压缩),stream表示要将压缩后的图片保存到的输出流。
下面是详细代码:
-
-
-
-
-
-
-
- public static void compressImageByQuality(final Bitmap bitmap,final String imgPath){
- new Thread(new Runnable() {
- @Override
- public void run() {
-
- ByteArrayOutputStream baos = new ByteArrayOutputStream();
- options = 100;
- bitmap.compress(Bitmap.CompressFormat.JPEG, options, baos);
- while (baos.toByteArray().length / 1024 > 100) {
- baos.reset();
- options -= 10;
- if(options<0)options=0;
- bitmap.compress(Bitmap.CompressFormat.JPEG, options, baos);
- if(options==0)break;
- }
- try {
- FileOutputStream fos = new FileOutputStream(new File(imgPath));
- fos.write(baos.toByteArray());
- fos.flush();
- fos.close();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }).start();
- }
方法解析:
由于此方法中包含I/O操作和递归调用比较耗时所以我采用了多线程去处理。
第三种方式:按比例缩小图片的像素以达到压缩的目的
此种方法主要是使用android.graphics.BitmapFactory.Options.Options()方法将图片以指定的采用率加载到内存然后输出到本地以达到压缩像素的目的。
详细代码:
-
-
-
-
-
-
- public static void compressImageByPixel(String imgPath) {
- BitmapFactory.Options newOpts = new BitmapFactory.Options();
- newOpts.inJustDecodeBounds = true;
- Bitmap bitmap = BitmapFactory.decodeFile(imgPath, newOpts);
- newOpts.inJustDecodeBounds = false;
- int width = newOpts.outWidth;
- int height = newOpts.outHeight;
- float maxSize = 1000f;
- int be = 1;
- if (width > height && width > maxSize) {
- be = (int) (newOpts.outWidth / maxSize);
- } else if (width < height && height > maxSize) {
- be = (int) (newOpts.outHeight / maxSize);
- }
- be++;
- newOpts.inSampleSize = be;
- newOpts.inPreferredConfig = Config.ARGB_8888;
- newOpts.inPurgeable = true;
- newOpts.inInputShareable = true;
- bitmap = BitmapFactory.decodeFile(imgPath, newOpts);
-
- ByteArrayOutputStream baos = new ByteArrayOutputStream();
- bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
- try {
- FileOutputStream fos = new FileOutputStream(new File(imgPath));
- fos.write(baos.toByteArray());
- fos.flush();
- fos.close();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
第四种方式:将图片先按比例压缩然后再降质
此种方式主要结合第二种和第三种方法以下是详细代码:
-
-
-
-
-
-
-
- public static void compressImageByQuality(final Bitmap bitmap,final String imgPath){
- new Thread(new Runnable() {
- @Override
- public void run() {
-
- ByteArrayOutputStream baos = new ByteArrayOutputStream();
- options = 100;
- bitmap.compress(Bitmap.CompressFormat.JPEG, options, baos);
- while (baos.toByteArray().length / 1024 > 100) {
- baos.reset();
- options -= 10;
- if(options<0)options=0;
- bitmap.compress(Bitmap.CompressFormat.JPEG, options, baos);
- if(options==0)break;
- }
- try {
- FileOutputStream fos = new FileOutputStream(new File(imgPath));
- fos.write(baos.toByteArray());
- fos.flush();
- fos.close();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }).start();
- }
-
-
-
-
-
-
- public static void compressImageByPixel(String imgPath) {
- BitmapFactory.Options newOpts = new BitmapFactory.Options();
- newOpts.inJustDecodeBounds = true;
- Bitmap bitmap = BitmapFactory.decodeFile(imgPath, newOpts);
- newOpts.inJustDecodeBounds = false;
- int width = newOpts.outWidth;
- int height = newOpts.outHeight;
- float maxSize = 1000f;
- int be = 1;
- if (width > height && width > maxSize) {
- be = (int) (newOpts.outWidth / maxSize);
- } else if (width < height && height > maxSize) {
- be = (int) (newOpts.outHeight / maxSize);
- }
- be++;
- newOpts.inSampleSize = be;
- newOpts.inPreferredConfig = Config.ARGB_8888;
- newOpts.inPurgeable = true;
- newOpts.inInputShareable = true;
- bitmap = BitmapFactory.decodeFile(imgPath, newOpts);
- compressImageByQuality(bitmap,imgPath);
- }