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 ...
随机推荐
- 【CSS进阶】box-shadow 与 filter:drop-shadow 详解及奇技淫巧
box-shadow 在前端的 CSS 编写工作想必十分常见.但是 box-shadow 除去它的常规用法,其实还存在许多不为人知的奇技淫巧. 喜欢 markdown 版本的可以戳这里. box-sh ...
- 【记录】xUnit for vs2012/vs2013
关于 NUint 以及单元测试的相关内容,可以参考:[单元测试]NUint使用详解及Visual Studio配置. xUnit 是 NUint 的进化版本,使用方法和 NUint 类似,首先下载安装 ...
- java中的list时间排序
最初设想使用:时间long型 private void testTimes() throws InterruptedException{ Calendar cal=Calendar.getInstan ...
- 介绍一个很爽的 php 字符串特定检索函数---strpos()
大家在用 php 开发的时候 是否 有遇到过,对于一个获取的字符串,如果想要特定检测它是否 含有某个特定的字符或者子字符串,总是找不到好方法,或者根本做不到,迫于无奈而使用foreach. 函数: s ...
- 【NET MVC】View
通过阅读一些书籍,结合源代码,稍微深入的学习了Asp.Net MVC中的视图View 任何类型的响应都可以利用当前HttpResponse来响应,MVC可以通过Controller的Response属 ...
- Cesium应用篇:3控件(2)BaseLayerPicker
所有范例均在github中搜索:ExamplesforCesium BaseLayerPicker控件非常简单,似乎没什么可说的,确实非常的简单,但作为一个底图选择控件,可以说是最基本的一个控件. C ...
- 【JUC】JDK1.8源码分析之CyclicBarrier(四)
一.前言 有了前面分析的基础,现在,接着分析CyclicBarrier源码,CyclicBarrier类在进行多线程编程时使用很多,比如,你希望创建一组任务,它们并行执行工作,然后在进行下一个步骤之前 ...
- MongoDB性能优化
一.索引 MongoDB 提供了多样性的索引支持,索引信息被保存在system.indexes 中,且默认总是为_id创建索引,它的索引使用基本和MySQL 等关系型数据库一样.其实可以这样说说,索引 ...
- Sql Server之使用T_SQL创建,修改,查看数据库信息
一.使用Transact_SQL创建数据库 Transact_SQL语法如下: create database database_name [ on [primary] [<fi ...
- 在SQL Serve里停用行和页层级锁
今天我想谈下SQL Server里另一个非常有趣的话题:在SQL Server里停用行和页层级锁.在SQL Server里,每次你重建一个索引,你可以使用ALLOW_ROW_LOCKS 和ALLOW_ ...