今天没事想要研究一下图片压缩的技术,研究完了之后才发现原来压缩图片这么简单,好了,闲话不多说了,进入我们今天的正题--图片压缩与解压!
首先我们要在layout文件中加入如下布局:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MainActivity">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/ddd"/>
</RelativeLayout>
<span style="font-size:14px;">其实就是一个图片的容器,用来显示我们压缩之前与压缩之后的效果的。</span>
MainActivity我也先上代码:
<pre name="code" class="java">package com.example.wjhan.imagecompress;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.OutputStream;
import android.app.Activity;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Handler.Callback;
import android.os.Message;
import android.widget.ImageView;
public class MainActivity extends Activity {
private ImageView ddd;
private Bitmap b,cc;
private static String a;
Handler handler = new Handler(new Callback() {
@Override
public boolean handleMessage(Message msg) {
// TODO Auto-generated method stub
switch (msg.what) {
case 0:
b= ImageScale.decodeSampledBitmapFromResource(a);
ddd.setImageBitmap(b);
break;
default:
break;
}
return false;
}
});
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ddd = (ImageView) findViewById(R.id.ddd);
new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
long start = System.currentTimeMillis();
// cc = ImageScale.decodeSampledBitmapFromResource(Environment.getExternalStorageDirectory().getAbsolutePath()+"/jin.jpg");//这句话是文件在指定的路径下使用 下边的两句是获取到drawable中图片
Resources res = getResources();
cc= BitmapFactory.decodeResource(res, R.drawable.capture);
long end = System.currentTimeMillis();
System.out.println("压缩时间---->"+(end-start));
handler.sendEmptyMessage(0);
saveBitmap2file(cc, "jin.jpg");
}
}).start();
}
/**
* 保存压缩之后的文件的方法
* @param bmp
* @param filename
* @return
*/
static boolean saveBitmap2file(Bitmap bmp, String filename) {
CompressFormat format = Bitmap.CompressFormat.JPEG;
int quality = 100;
OutputStream stream = null;
try {
a= Environment.getExternalStorageDirectory().getAbsolutePath() +"/"+ filename;
stream = new FileOutputStream(a);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
// Generated by Foxit PDF Creator © Foxit Software
// http://www.foxitsoftware.com For evaluation only.
e.printStackTrace();
}
return bmp.compress(format, quality, stream);
}
}
由于压缩文件是一个耗时的工作,所以上述代码我用了异步,在线程中进行压缩操作,再用回调方法获取到压缩状态,并在主线程中进行解压并显示的操作。
解压方法如下:
public static Bitmap decodeSampledBitmapFromResource(String path) {
if (path == null || path == "") {
// 文件不存在
return null;
}
File file = new File(path);
int scale = 1;
System.out.println("--->" + file.length() / 1024);
if (file.exists()
&& (file.getName().indexOf(".jpg") > 0
|| file.getName().indexOf(".JPG") > 0
|| file.getName().indexOf(".png") > 0
|| file.getName().indexOf(".PNG") > 0
|| file.getName().indexOf(".JPEG") > 0 || file
.getName().indexOf(".jpeg") > 0)) {
if (file.length() / 1024 < 200) {
return BitmapFactory.decodeFile(path);
} else {
scale = (int) ((file.length() / 1024) / 200);
}
} else {
return null;
}
// 首先不加载图片,仅获取图片尺寸
final BitmapFactory.Options options = new BitmapFactory.Options();
// 当inJustDecodeBounds设为true时,不会加载图片仅获取图片尺寸信息
options.inJustDecodeBounds = true;
// 此时仅会将图片信息会保存至options对象内,decode方法不会返回bitmap对象
BitmapFactory.decodeFile(path, options);
// 计算压缩比例,如inSampleSize=4时,图片会压缩成原图的1/4
options.inSampleSize = scale;
// 当inJustDecodeBounds设为false时,BitmapFactory.decode...就会返回图片对象了
options.inJustDecodeBounds = false;
// 利用计算的比例值获取压缩后的图片对象
return BitmapFactory.decodeFile(path, options);
}
上面的代码注释的已经很详细了,我就不多解释了,也到了我们检验结果的时候了。。
运行结果如下图:
压缩之后的文件在该文件夹下如图所示
到这里图片压缩、解压、储存、显示的功能就都实现了,没弄明白的同学欢迎给我留言~
源码下载地址:点击打开链接