Android 拍照后保证保证图片不失真,进行压缩
今天在网上找了一下参考,得出把图片压缩至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 拍照后保证保证图片不失真,进行压缩的更多相关文章
- 彻底解决android拍照后无法显示的问题
这是对上篇"android 图片拍照,相册选图,剪切并显示"的文章之后的 改进 上一篇文章虽然能解决图片的拍照剪切以及显示,但是发现他有一个缺点, 如果该程序单独运行,貌似没有任何 ...
- Android拍照得到全尺寸图片并进行压缩/拍照或者图库选择 压缩后 图片 上传
http://www.jb51.net/article/77223.htm https://www.cnblogs.com/breeze1988/p/4019510.html
- Android拍照后更新相册
方法一: Uri updateUri = Uri.fromFile(file); Intent updateIntent = new Intent(Intent.ACTION_MEDIA_SCANNE ...
- Android 拍照或者从相册获取图片的实现
我们常常会用到上传头像,或者发帖子的时候选择本地图片上传的功能.这个很常见 今天因为app的需求我研究了下.现在分享下. 其实不论是通过拍照还是从相册选取都会用到Intent 这是系统提供给我们用来调 ...
- 部分Android或IOS手机拍照后照片被旋转的问题
1.我们平时手机拍的照片,传到电脑后,使用Photoshop或者其它图片浏览工具打开时,发现图片是被转过的.可是Windows上预览却是正的.其实原因是部分Android或IOS手机拍照后,将图片角度 ...
- Android 拍照图片选取与图片剪裁
最近从以前的项目中扒下来一个常用的模块,在这里有必要记录一下的,就是android上获取图片以及裁剪图片,怎么样?这个功能是不是很常用啊,你随便打开一个App,只要它有注册功能都会有设置人物头像的功能 ...
- android拍照获得图片及获得图片后剪切设置到ImageView
ok,这次的项目需要用到设置头像功能,所以做了个总结,直接进入主题吧. 先说说怎么 使用android内置的相机拍照然后获取到这张照片吧 直接上代码: Intent intentFromCapture ...
- Android在有存储卡和无存储卡情况下拍照后固定尺寸和压缩大小
我最近工作挺忙,距离上一次写博客转眼已经过了一个多月,每次学到和用到点新东西,其实都有分享的欲望,但奈何文笔太差,而一篇文章包括构思,排版,修改发布的时间最少要花费2个小时(这其中还不包括写完后未保存 ...
- 【转】Android开发之如何保证Service不被杀掉(broadcast+system/app)
Service简介 1.Service 每个Service必须在manifest中 通过<service>来声明. 可以通过contect.startservice和contect.bin ...
随机推荐
- Eclipse开发工具组合键介绍
1. 编辑类 Ctrl + Q 跳到最后一次的编辑处 Ctrl + 1 快速修复提示 Alt + ↓ ...
- 跟我学Angular2(1-初体验)
0.导言 Angular1作为最流行的前端MV*框架,给前端开发带来了极大的便利性.但是,仍然有许多不好的地方已经很难再改变了.Angular团队根据WEB发展的趋势和Angular1中积累的经验来开 ...
- 分享一个 @user 前端插件
开源地址:https://github.com/yuezhongxin/Mention.js 插件效果:类似于微博或 github 中 @user 列表效果. 这是个二次开发的插件,花了几天时间,对 ...
- DDD 领域驱动设计-Value Object(值对象)如何使用 EF 进行正确映射
写在前面 首先,这篇博文是用博客园新发布的 MarkDown编辑器 编写的,这也是我第一次使用,语法也不是很熟悉,但我觉得应该会很爽,博文后面再记录下用过的感受,这边就不多说. 阅读目录: 上一篇回顾 ...
- jQuery的extend方法
jq中的extend在面试中经常会被问道,今天我总结一个下有关于extend的用法三种进行对比,可能不全,希望大家指点, 用法一: $.extend({}) ,为jQuery类添加方法,可以理解为扩 ...
- 走进vue.js(一)
走进vue.js(一) vue.js作为目前最具前景的前端框架之一,既然如此,博主贯彻以往的作风,肯定要捣鼓一番,经过为期半个多月的研究,算是对vue有了深刻的理解与认识,下面分三个章节解开vue.j ...
- Tools - Docker与Vagrant
Docker Docker - HomePage Wiki - Docker Docker简介 Overview Docker 是一个开源的应用容器引擎,基于 Go 语言并遵从 Apache2.0 协 ...
- jquery自定义滚动条 鼠标移入或滚轮时显示 鼠标离开或悬停超时时隐藏
一.需求: 我需要做一个多媒体播放页面,左侧为播放列表,右侧为播放器.为了避免系统滚动条把列表和播放器隔断开,左侧列表的滚动条需要自定义,并且滚动停止和鼠标离开时要隐藏掉. 二.他山之石: 案例来自h ...
- Ajax跨域访问wcf服务中所遇到的问题总结。
工具说明:vs2012,sql server 2008R2 1.首先,通过vs2012建立一个wcf服务项目,建立好之后.再新开一个vs2012 建立web项目,通过jQuery的ajax方法访问服务 ...
- 2.Java基础之Runtime对象
毕向东老师Java基础学习笔记——Runtime对象 今天学习Java中的Runtime对象后,感觉这个对象对我们主要有以下几点用处. 1.使用java代码打开本地可执行文件,比如打开一个计算器. 2 ...