android bitmap compress
- 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;
- Bitmap bm = BitmapFactory.decodeFile(filePath, options);
2.2 处理图片旋转
- int degree = readPictureDegree(filePath);
- bm = rotateBitmap(bm,degree) ;
- private static int readPictureDegree(String path) {
- int degree = 0;
- try {
- ExifInterface exifInterface = new ExifInterface(path);
- int orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
- switch (orientation) {
- case ExifInterface.ORIENTATION_ROTATE_90:
- degree = 90;
- break;
- case ExifInterface.ORIENTATION_ROTATE_180:
- degree = 180;
- break;
- case ExifInterface.ORIENTATION_ROTATE_270:
- degree = 270;
- break;
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- return degree;
- }
- private static Bitmap rotateBitmap(Bitmap bitmap, int rotate){
- if(bitmap == null)
- return null ;
- int w = bitmap.getWidth();
- int h = bitmap.getHeight();
- // Setting post rotate to 90
- Matrix mtx = new Matrix();
- mtx.postRotate(rotate);
- return Bitmap.createBitmap(bitmap, 0, 0, w, h, mtx, true);
- }
2.3压缩图片
- bm.compress(Bitmap.CompressFormat.JPEG, 30, baos);//30 是压缩率,表示压缩70%; 如果不压缩是100,表示压缩率为0
- public 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;
- Bitmap bm = BitmapFactory.decodeFile(filePath, options);
- if(bm == null){
- return null;
- }
- int degree = readPictureDegree(filePath);
- bm = rotateBitmap(bm,degree) ;
- ByteArrayOutputStream baos = null ;
- try{
- baos = new ByteArrayOutputStream();
- bm.compress(Bitmap.CompressFormat.JPEG, 30, baos);
- }finally{
- try {
- if(baos != null)
- baos.close() ;
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- return bm ;
- }
- 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 ? widthRatio : heightRatio;
- }
- return inSampleSize;
- }
android bitmap compress的更多相关文章
- android bitmap compress(图片压缩)
android bitmap compress android的照相功能随着手机硬件的发展,变得越来越强大,能够找出很高分辨率的图片. 有些场景中,需要照相并且上传到服务,但是由于图片的大小太大,那么 ...
- android bitmap的 一些简单操作
/** 获取 drawable 的图片 可以循环 1.图名 2.drawable 3.包名 **/ int imgid = getResources().getIdentifier("ic_ ...
- Android bitmap序列化
最近在开发中需要用到图片bitmap的序列化并进行传递,发现bitmap是没有序列化的,下面是自己实现的一个序列化方法,分享下. 以下是通过byte[]来进行序列化的,因为bitmap是没有序列化的, ...
- android Bitmap(将视图转为bitmap对象)
1)从android的资源文件夹layout中加载xml布局文件,并把布局文件映射为Bitmap main.xml文件如下: <?xmlversion="1.0"encodi ...
- Android Bitmap转换WebP图片导致损坏的分析及解决方案
背景 作为移动领域所力推的图片格式,WebP图片在商业领域证明了其应有的价值.基于其他格式的横向对比,其在压缩性能表现,及还原度极为优秀,节省大量的带宽开销.基于可观的效益比,团队早前已开始磋商将当前 ...
- [翻译]开发文档:android Bitmap的高效使用
内容概述 本文内容来自开发文档"Traning > Displaying Bitmaps Efficiently",包括大尺寸Bitmap的高效加载,图片的异步加载和数据缓存 ...
- Android Bitmap 和 ByteArray的互相转换
Android Bitmap 和 ByteArray的互相转换 移动平台图像处理,需要将图像传给native处理,如何传递?将bitmap转换成一个 byte[] 方便传递也方便cpp代码直接处理图像 ...
- Android Bitmap转换WebPng图片导致损坏的分析及解决方案
出现问题的code!!! private void saveImage(String uri, String savePath) throws IOException { // 创建连接 HttpUR ...
- Android——BitMap(位图)相关知识总结贴
Android中文API(136) —— Bitmap http://www.apkbus.com/android-54644-1-1.html Android 4.0 r1 API—Bitmap(S ...
随机推荐
- HDU 4649 - Professor Tian(2013MUTC5-1007)(概率)
不知道这题算作什么类型的题目,反正很巧妙,队友小杰想了没一会就搞定了 为了学习这种方法,我也搞了搞,其实思路不难想,位运算嘛,只有0和1,而且该位的运算只影响该位,最多20位,一位一位地计算即可,只需 ...
- 通用 CSS 笔记、建议与指导
在参与规模庞大.历时漫长且人手众多的项目时,所有开发者遵守如下规则极为重要: + **保持 CSS 的可维护性** + **保持代码清晰易懂** + **保持代码的可拓展性** 为了实现这一目标,我们 ...
- c# 使用api函数 ShowWindowAsync 控制窗体
1.需要匯入 System.Runtime.InteropServices 命名空間 2.宣告 ShowWindowAsync 函數 [DllImport("user32.dll" ...
- HYSBZ 1012 最大数maxnumber
思路:在单调队列不更新列首.由于查询区间大小不确定,所以不能保证下次是否还用到它 #include <cstdio> #include <cstring> #include & ...
- RSA 公钥加密——私钥解密
作者:刘巍然-学酥链接:http://www.zhihu.com/question/25912483/answer/31653639来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请 ...
- 安装 ibm-java-x86_64-sdk-6.0-9.3.x86_64.rpm 的三步骤
yum install libstdc++-devel.x86_64 yum install compat-libstdc++-33.x86_64 rpm -ivh ibm-java-x86_64-s ...
- centos版本7以上网卡名修改
1.初始状态网卡 2.首先,先编辑网卡的配置文件 vi /etc/sysconfig/network-scripts/ifcfg-eno16777736 将里面的NAME项修改为eth0 3.然后,禁 ...
- QtGui.QGridLayout
The most universal layout class is the grid layout. This layout divides the space into rows and colu ...
- web前端开发文档
- Jquery重新学习之六[操作XML数据]
上一章整理有关Jquery操作JSON格式数据,本章则是整理Jquery与XML的交互,因为XML简单易用及运用在很跨平台上的优点,所以项目运用Jquery操作XML还是比较常见:下面的代码来自Jqu ...