Android下压缩图片的方法:

大概能将3M左右的图片压缩到100K左右, 几乎不失真。 代码如下:

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.OutputStream; import android.graphics.Bitmap;
import android.graphics.BitmapFactory; public class BitmapUtil {
/**
* 压缩图片之后保存为文件
*
* @param filePath
* 原始图片的完整路径
* @param storeImgPath
* 压缩之后要存储的图片的完整路径
* @return boolean
* @author Doraemon
* @time 2014年6月27日下午5:10:19
*/
public static boolean saveCompressImg(String filePath, String storeImgPath) {
Bitmap bm = getSmallBitmap(filePath);
OutputStream stream = null;
try {
stream = new FileOutputStream(storeImgPath);
} catch (FileNotFoundException e) {
e.printStackTrace();
} return bm.compress(Bitmap.CompressFormat.JPEG, 40, stream);
} /**
* 根据路径获得突破并压缩返回bitmap用于显示
*
* @param imagesrc
* @return
*/
private static Bitmap getSmallBitmap(String filePath) {
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(filePath, options); // Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, 480, 800); // Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false; return BitmapFactory.decodeFile(filePath, options);
} /**
* 计算图片的缩放值
*
* @param options
* @param reqWidth
* @param reqHeight
* @return
*/
private static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1; if (height > reqHeight || width > reqWidth) { // Calculate ratios of height and width to requested height and
// width
final int heightRatio = Math.round((float) height / (float) reqHeight);
final int widthRatio = Math.round((float) width / (float) reqWidth); // Choose the smallest ratio as inSampleSize value, this will
// guarantee
// a final image with both dimensions larger than or equal to the
// requested height and width.
inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
} return inSampleSize;
}
}

Android 下压缩图片—微弱失真的更多相关文章

  1. Android LruCache 压缩图片 有效避免程序OOM

    转载请注明出处:http://blog.csdn.net/guolin_blog/article/details/9316683 本篇文章主要内容来自于Android Doc,我翻译之后又做了些加工, ...

  2. Android下将图片载入到内存中

    Android的系统的标准默认每一个应用程序分配的内存是16M.所以来说是很宝贵的,在创建应用的时候要尽可能的去节省内存,可是在载入一些大的文件的时候,比方图片是相当耗内存的,一个1.3M的图片,分辨 ...

  3. Android 开发压缩图片

    private Bitmap imageZoom(int position , Bitmap bitMap) {        //图片允许最大空间         double maxSize =2 ...

  4. Android 下实现图片的移动

    网上看到的demo,感觉很有趣,但是 实用性不是太强,记录一下. 源码下载地址:请戳这里---------------->

  5. Android 调用jepg库进行图片压缩,保持图片不失真

    1. 浅谈为什么Android和iOS图片质量差距那么大? 首先来说,作为一个安卓狗,机器当然用的是安卓的手机.现在的安卓手机大多数都会以高清拍照,动不动就几千万柔光相机来吸引各种买家.买来后,拍照发 ...

  6. Android图片压缩,不失真,上线项目

    当然了,图片压缩是利用了libjpeg库的基础上,牛逼的同学可以自行生成so.jar.在此给出一个链接: http://www.cnblogs.com/hrlnw/p/4403334.html 在生成 ...

  7. 上传图片转为base64格式预览并压缩图片(不兼容IE9以下浏览器,兼容移动端ios,android)

    前些天公司要求在微信移动端做上传图片并预览的功能,要求能够调用摄像头拍照并立即预览. 在网上搜了一些方法,开始自己写了个简单的功能实现代码.结果发现移动端拍照出来的图片动不动就2M+,又因为要批量上传 ...

  8. Android压缩图片到100K以下并保持不失真的高效方法

    前言:目前一般手机的相机都能达到800万像素,像我的Galaxy Nexus才500万像素,拍摄的照片也有1.5M左右.这么大的照片上传到服务器,不仅浪费流量,同时还浪费时间. 在开发Android企 ...

  9. Android—将Bitmap图片保存到SD卡目录下或者指定目录

    直接上代码就不废话啦 一:保存到SD卡下 File file = new File(Environment.getExternalStorageDirectory(), System.currentT ...

随机推荐

  1. js 判断是否为chrome浏览器

    var isChrome =navigator.userAgent.indexOf("Chrome") !== -1 用 navigator.appVersion 不好使,因为al ...

  2. 【WCF--初入江湖】07 分布式事务

    07 分布式事务 一.前言 [1]理解事务特性 [2]掌握TransactionFlow 特性 [3]掌握WCF中的事务属性 TransactionAutoCompleteOnSessionClose ...

  3. Root resource classes

    Overview A root resource class is the entry point into a JAX-RS implemented RESTful Web service. It ...

  4. Oracle的学习三:java连接Oracle、事务、内置函数、日期函数、转换函数、系统函数

    1.java程序操作Oracle java连接Oracle JDBC_ODBC桥连接 1.加载驱动: Class.forName("sun.jdbc.odbc.JdbcodbcDriver& ...

  5. 原版win7镜像IE主页被篡改?

    装了几次系统,镜像是从“MSDN我告诉你”上下载的,但是每次装完,发现IE主页不是microsoft的官方网页,而是www.3456.com. 这就奇了怪了,难道“MSDN我告诉你”提供的不是原版镜像 ...

  6. 257. Binary Tree Paths

    题目: Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree ...

  7. C++:运算符重载函数之友元运算符重载

    5.2.2 友元运算符重载函数 运算符重载函数一般采用两种形式定义: 一是定义为它将要操作的类的成员函数(简称运算符重载函数): 二是定义为类的友元函数(简称为友元运算符重载函数). 1.定义友元运算 ...

  8. Android Handler与多线程

    本文首先解释一下handler是用来干嘛的,然后通过例子介绍其在多线程中的应用. 什么是Handler handler通俗一点讲就是用来在各个进程之间发送数据的处理对象.在任何进程中,只要获得了另一个 ...

  9. UDP丢包和无序 问题的解决方法

    最近在做一个项目,在这之前,做了个验证程序. 发现客户端连续发来1000个1024字节的包,服务器端出现了丢包现象. 纠其原因,是服务端在还未完全处理掉数据,客户端已经数据发送完毕且关闭了. 我用过s ...

  10. 关于ssh的一篇很好的文章

    源地址:http://www.w3hacker.com/?p=156   ssh-agent的manual写得倒是挺详细,可看了好几次都没怎么搞明白.08年在网上找到了非常好的一篇文章,An Illu ...