今天在网上找了一下参考,得出把图片压缩至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. 支持向量机原理(四)SMO算法原理

    支持向量机原理(一) 线性支持向量机 支持向量机原理(二) 线性支持向量机的软间隔最大化模型 支持向量机原理(三)线性不可分支持向量机与核函数 支持向量机原理(四)SMO算法原理 支持向量机原理(五) ...

  2. 如何完全卸载OneDrive (Windows 10 64bit)

    原文参考 http://lifehacker.com/how-to-completely-uninstall-onedrive-in-windows-10-1725363532 To complete ...

  3. easyui-datagrid 列单击事件

    首先要注意的就是,先添加一个js方法,名字可以自定义,但是必须得与下面的option里面的onClickRow:后面的一致即可      <script type="text/java ...

  4. 基础知识javascript--事件

    群里有一个小伙伴在处理事件监听函数的时候,遇到了一点问题,正好我比较空闲,于是帮他指出了代码中的问题,顺便整理一下,方便以后遇到类似问题的伙伴们有一个参考. 这是一个很简单的问题,对于基础知识比较杂实 ...

  5. NAS与SAN

    1. NAS(Network Attached Storage,网络附加存储服务器) 简单地说,NAS就是一台File Server,只要将NAS连接上网络,那么在网络上面的其他主机就能够访问NAS上 ...

  6. 初试Code First(附Demo)

    写在前面 新建项目 安装EntityFramework程序包 创建模型 创建上下文DbContext 创建数据库.读/写数据 配置连接字符串 Code First 迁移 示例Demo下载 后记 以前逛 ...

  7. es6新特性分享

    1.字符串查找es5使用是indexOf() 返回字符第一次出现的位置int值es6新增了3个方法:includes()/startsWith()/endWith()返回bool值includes = ...

  8. svn 几个常用命令(持续更新)

    1:获取某个版本号(3583)下的代码                                               svn co http://tech.yoai.com:8300/c ...

  9. 几个步骤轻松搞定ASP.NET 依赖注入。

    http://www.it165.net/pro/html/201407/17685.html 我在网上看到了这篇文章,这边文章主要说的方法就是通过读取配置文件来解决依赖注入的问题.但是每次新建一个依 ...

  10. Android应用底部导航栏(选项卡)实例

    现在很多android的应用都采用底部导航栏的功能,这样可以使得用户在使用过程中随意切换不同的页面,现在我采用TabHost组件来自定义一个底部的导航栏的功能. 我们先看下该demo实例的框架图: 其 ...