android bitmap compress(图片压缩)
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 将图片压缩到指定的大小
Bitmap压缩到指定大小: private void imageZoom() {//图片允许最大空间 单位:KBdouble maxSize =400.00;//将bitmap放至数组中,意在bit ...
- Bitmap之compress图片压缩
package com.loaderman.customviewdemo; import android.app.Activity; import android.graphics.Bitmap; i ...
- Android中的图片压缩
1.android中计算图片占用堆内存的kB大小跟图片本身的kB大小无关,而是根据图片的尺寸来计算的. 比如一张 480*320大小的图片占用的堆内存大小为: 480*320*4/1024=600kB ...
- 有关Option.inSamplSize 和 Compress 图片压缩
1.图片占用内存:占用的内存 = 图片长度 * 图片宽度 * 单位像素占用的字节数 注: 图片长度和图片宽度就是图片在行列上的像素数量. 图片格式: ALPHA_8:表示8位Alpha位图,即A=8, ...
- Android笔记(七十五) Android中的图片压缩
这几天在做图记的时候遇第一次遇到了OOM,好激动~~ 追究原因,是因为在ListView中加载的图片太大造成的,因为我使用的都是手机相机直接拍摄的照片,图片都比较大,所以在加载的时候会出现内存溢出,那 ...
- Android学习之图片压缩,压缩程度高且失真度小
曾经在做手机上传图片的时候.直接获取相机拍摄的原图上传,原图大小一般1~2M.因此上传一张都比較浪费资源,有些场景还须要图片多张上传,所以近期查看了好多前辈写的关于图片处理的资料.然后试着改了一个图片 ...
- android bitmap compress
android的照相功能随着手机硬件的发展,变得越来越强大,能够找出很高分辨率的图片. 有些场景中,需要照相并且上传到服务,但是由于图片的大小太大,那么就上传就会很慢(在有些网络情况下),而且很耗流量 ...
- Android 简单介绍图片压缩和图片内存缓存
转载请注明出处:http://blog.csdn.net/guolin_blog/article/details/9316683 本篇文章主要内容来自于Android Doc,我翻译之后又做了些加工, ...
- Android 中图片压缩分析(上)
作者: shawnzhao,QQ音乐技术团队一员 一.前言 在 Android 中进行图片压缩是非常常见的开发场景,主要的压缩方法有两种:其一是质量压缩,其二是下采样压缩. 前者是在不改变图片尺寸的情 ...
随机推荐
- flash跨域策略文件crossdomain.xml配置详解
来源:http://www.2cto.com/Article/201108/100008.html 0x01 简介 flash在跨域时唯一的限制策略就是crossdomain.xml文件,该文件限制了 ...
- 二分三角形的时候尤其需要注意!!! HDU 5115 二分+模拟
题目大意:http://blog.csdn.net/snowy_smile/article/details/49535301 思路:分类讨论,分别在[1,2].(2,3).[3,4).[4,1]相遇, ...
- 编程实现Windows瞬间关机
我们先来看看Windows正常的关机流程:①关机指令通知Windows子系统csrss.exe,csrss.exe收到通知后会和Winlogon.exe做一个数据交换,再由Winlogon.exe通知 ...
- request.getparam()与request.getAttibute()的区别
request.getparam()是用来获取已get或post提交的参数的值,而request.getAttibute()是获取request中存放的值
- zf-关于交换工具配置文件,交换的“列名字段”前面加个“0,1,2”的意思。
- cocos2d-x 不规则形状按钮的点击判定
cocos2d-x 不规则形状按钮的点击判定 原理: 1.OpeGL ES提供了glReadPixels[^footnote]函数,来获取当前framebuffer上的像素数据 2.cocos2d-x ...
- php 环信 接口的例子
<?php class Hxcall{ private $app_key = 'yunjiankang#medical'; private $client_id = 'YXA6ARjBgDnxE ...
- Android手机图片适配问题
需求:今天在做ListView的时候遇到一个问题,就是ListView中加载图片的时候.有些图片的大小比较大,所以会出现图片显示不充分的问题. 首先,再不做任何处理的情况下,大小是这样的.宽度是Wra ...
- Android如何使用API
转自:http://www.cnblogs.com/vanezkw/archive/2012/07/03/2574559.html 本文针对Android开发如何使用API文档进行一些经验分享. 1. ...
- mybatis generator Date类型时间丢失
开篇 本次项目换用了Oracle数据库,讲真,真的不好用,不太喜欢,比较喜欢轻量级的MySQL但是不得不说从Oracle的严谨中学到不少好东西. <resultMap id="Base ...