今天在网上找了一下参考,得出把图片压缩至KB

其他不想多说、直接上代码

拍完照后调用下面代码

BitmapUtils.compressBitmap(photoPath, photoPath, 640);  //压缩

这是个工具类。

package com.continuouscamera;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException; import android.app.Activity;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.provider.MediaStore.MediaColumns; /**
* Tools for handler picture
*/
public final class BitmapUtils { private static final String TAG = BitmapUtils.class.getSimpleName(); public static void compressBitmap(String sourcePath, String targetPath, float maxSize) { BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true; BitmapFactory.decodeFile(sourcePath, options); final float originalWidth = options.outWidth;
final float originalHeight = options.outHeight; float convertedWidth;
float convertedHeight; if (originalWidth > originalHeight) {
convertedWidth = maxSize;
convertedHeight = maxSize / originalWidth * originalHeight;
} else {
convertedHeight = maxSize;
convertedWidth = maxSize / originalHeight * originalWidth;
} final float ratio = originalWidth / convertedWidth; options.inSampleSize = (int) ratio;
options.inJustDecodeBounds = false; Bitmap convertedBitmap = BitmapFactory.decodeFile(sourcePath, options);
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
convertedBitmap.compress(Bitmap.CompressFormat.JPEG, 100, byteArrayOutputStream);
FileOutputStream fileOutputStream;
try {
fileOutputStream = new FileOutputStream(new File(targetPath));
fileOutputStream.write(byteArrayOutputStream.toByteArray());
fileOutputStream.flush();
fileOutputStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
} /**
* 根据URI获取图片物理路径
*/
public static String getAbsoluteImagePath(Uri uri, Activity activity) { String[] proj = {
MediaColumns.DATA
};
Cursor cursor = activity.managedQuery(uri, proj, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaColumns.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
} /**
*
* @param path
* @param maxSize
* @return
*/
public static Bitmap decodeBitmap(String path, int maxSize) {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true; BitmapFactory.decodeFile(path, options); final int originalWidth = options.outWidth;
final int originalHeight = options.outHeight; int convertedWidth;
int convertedHeight; if (originalWidth > originalHeight) {
convertedWidth = maxSize;
convertedHeight = maxSize / originalWidth * originalHeight;
} else {
convertedHeight = maxSize;
convertedWidth = maxSize / originalHeight * originalWidth;
} options = new BitmapFactory.Options();
options.inJustDecodeBounds = false;
options.inSampleSize = computeSampleSize(options, maxSize, convertedWidth * convertedHeight); Bitmap convertedBitmap = BitmapFactory.decodeFile(path, options); if (convertedBitmap != null) {
final int realWidth = convertedBitmap.getWidth();
final int realHeight = convertedBitmap.getHeight(); } return convertedBitmap;
} /**
*
* @param path
* @param maxWidth
* @param maxHeight
* @return
*/
public static Bitmap decodeBitmap(String path, int maxWidth, int maxHeight) {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true; BitmapFactory.decodeFile(path, options); final int originalWidth = options.outWidth;
final int originalHeight = options.outHeight; options = new BitmapFactory.Options();
options.inJustDecodeBounds = false;
options.inSampleSize = computeSampleSize(options, maxWidth, maxWidth * maxHeight); Bitmap convertedBitmap = BitmapFactory.decodeFile(path, options); if (convertedBitmap != null) {
final int realWidth = convertedBitmap.getWidth();
final int realHeight = convertedBitmap.getHeight(); } return convertedBitmap;
} private static int computeInitialSampleSize(BitmapFactory.Options options, int minSideLength, int maxNumOfPixels) { double w = options.outWidth;
double h = options.outHeight;
int lowerBound = (maxNumOfPixels == -1) ? 1 : (int) Math.ceil(Math.sqrt(w * h / maxNumOfPixels));
int upperBound = (minSideLength == -1) ? minSideLength
: (int) Math.min(Math.floor(w / minSideLength), Math.floor(h / minSideLength));
if (upperBound < lowerBound) {
return lowerBound;
}
if ((maxNumOfPixels == -1) && (minSideLength == -1)) {
return 1;
} else if (minSideLength == -1) {
return lowerBound;
} else {
return upperBound;
}
} private static int computeSampleSize(BitmapFactory.Options options, int minSideLength, int maxNumOfPixels) { int initialSize = computeInitialSampleSize(options, minSideLength, maxNumOfPixels);
int roundedSize;
if (initialSize <= 8) {
roundedSize = 1;
while (roundedSize < initialSize) {
roundedSize <<= 1;
}
} else {
roundedSize = (initialSize + 7) / 8 * 8;
}
return roundedSize;
} /**
* 生成8位16进制的缓存因子:规则的8位哈希码,不足前面补零
* @param string
* @return
*/
public static String toRegularHashCode(String string) {
final String hexHashCode = Integer.toHexString(string.hashCode());
final StringBuilder stringBuilder = new StringBuilder(hexHashCode);
while(stringBuilder.length() < 8){
stringBuilder.insert(0, '0');
}
return stringBuilder.toString();
}
}

  

Android 拍照后保证保证图片不失真,进行压缩的更多相关文章

  1. 彻底解决android拍照后无法显示的问题

    这是对上篇"android 图片拍照,相册选图,剪切并显示"的文章之后的 改进 上一篇文章虽然能解决图片的拍照剪切以及显示,但是发现他有一个缺点, 如果该程序单独运行,貌似没有任何 ...

  2. Android拍照得到全尺寸图片并进行压缩/拍照或者图库选择 压缩后 图片 上传

    http://www.jb51.net/article/77223.htm https://www.cnblogs.com/breeze1988/p/4019510.html

  3. Android拍照后更新相册

    方法一: Uri updateUri = Uri.fromFile(file); Intent updateIntent = new Intent(Intent.ACTION_MEDIA_SCANNE ...

  4. Android 拍照或者从相册获取图片的实现

    我们常常会用到上传头像,或者发帖子的时候选择本地图片上传的功能.这个很常见 今天因为app的需求我研究了下.现在分享下. 其实不论是通过拍照还是从相册选取都会用到Intent 这是系统提供给我们用来调 ...

  5. 部分Android或IOS手机拍照后照片被旋转的问题

    1.我们平时手机拍的照片,传到电脑后,使用Photoshop或者其它图片浏览工具打开时,发现图片是被转过的.可是Windows上预览却是正的.其实原因是部分Android或IOS手机拍照后,将图片角度 ...

  6. Android 拍照图片选取与图片剪裁

    最近从以前的项目中扒下来一个常用的模块,在这里有必要记录一下的,就是android上获取图片以及裁剪图片,怎么样?这个功能是不是很常用啊,你随便打开一个App,只要它有注册功能都会有设置人物头像的功能 ...

  7. android拍照获得图片及获得图片后剪切设置到ImageView

    ok,这次的项目需要用到设置头像功能,所以做了个总结,直接进入主题吧. 先说说怎么 使用android内置的相机拍照然后获取到这张照片吧 直接上代码: Intent intentFromCapture ...

  8. Android在有存储卡和无存储卡情况下拍照后固定尺寸和压缩大小

    我最近工作挺忙,距离上一次写博客转眼已经过了一个多月,每次学到和用到点新东西,其实都有分享的欲望,但奈何文笔太差,而一篇文章包括构思,排版,修改发布的时间最少要花费2个小时(这其中还不包括写完后未保存 ...

  9. 【转】Android开发之如何保证Service不被杀掉(broadcast+system/app)

    Service简介 1.Service 每个Service必须在manifest中 通过<service>来声明. 可以通过contect.startservice和contect.bin ...

随机推荐

  1. Android沉浸式通知栏设计

    转载博客:http://www.2cto.com/kf/201503/381348.html Android4.4新特性,系统状态栏一体化. 实现的步骤主要有以下几点: 1.android4.4 以上 ...

  2. Javascript对象的方法赋值

    Javascript对象编程学习中,一直不能很好的掌握对象的属性(property)和方法(method).今天在写代码过程中,又犯了一个低级错误. <!DOCTYPE html> < ...

  3. make things simple

    以前看过一篇文章,具体内容不记得了,只记得它的结论了:懒是人类进步的源动力.当时觉得结论有点新颖,文中列举了大量的实例证明这个结论,其中重点强调了计算机学科.我本身从事算是计算机相关的工作,对文中的部 ...

  4. jar包依赖性查询

    项目中碰到jar包冲突,需要排除一些jar包时先要了解jar的依赖关系,maven提供了命令行来查询: mvn dependency:tree 返回依赖的属性结构

  5. js 对闭包的理解

    <!DOCTYPE html> <html> <body> <p>局部变量计数.</p> <button type="but ...

  6. 原生JS实现jquery的链式编程。

    这是我根据之前遇到的一个面试题,题目:用原生JS实现$("#ct").on("click",fn).attr("id"). 然后看了篇jqu ...

  7. Spring配置c3p0数据源时出错报:java.lang.NoClassDefFoundError: com/mchange/v2/ser/Indirector

    今天在使用Spring配置c3p0数据源时,使用的数据库是mysql,服务器是tomcat,运行时报了一个 java.lang.NoClassDefFoundError: com/mchange/v2 ...

  8. centos7 mysql数据库安装和配置

    一.系统环境 yum update升级以后的系统版本为 [root@yl-web yl]# cat /etc/redhat-release CentOS Linux release 7.1.1503 ...

  9. 使用webstom或者idea上传代码到github或coding

    鉴于github网络速度太慢,建议用coding.先介绍github上传方式,因为webstom或idea集成了github,方法简单. git是一个版本控制器,他的作用是管理代码.比如你修改了代码, ...

  10. Sql Server函数全解(五)之系统函数

     系统信息包括当前使用的数据库名称,主机名,系统错误消息以及用户名称等内容.使用SQL SERVER中的系统函数可以在需要的时候获取这些信息.下面介绍系统函数的作用和使用方法. 1.返回表中指定字段的 ...