Android图片处理-图片压缩处理
这里先重复温习一下上一篇,调用相册获取图片:
/***
* 这个是调用android内置的intent,来过滤图片文件 ,同时也可以过滤其他的
*/
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(intent, 1);
获取选择的图片:
if (resultCode == Activity.RESULT_OK) {
Uri uri = data.getData();
try {
String[] pojo = { MediaStore.Images.Media.DATA }; Cursor cursor = managedQuery(uri, pojo, null, null, null);
if (cursor != null) {
ContentResolver cr = this.getContentResolver();
int colunm_index = cursor
.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
String path = cursor.getString(colunm_index);
/***
* 这里加这样一个判断主要是为了第三方的软件选择,比如:使用第三方的文件管理器的话,你选择的文件就不一定是图片了,
* 这样的话,我们判断文件的后缀名 如果是图片格式的话,那么才可以
*/
if (path.endsWith("jpg") || path.endsWith("png")) {
picPath = path;
Bitmap bitmap = PictureUtil.getSmallBitmap(picPath,480,320,50);
//
imageShow.setImageBitmap(bitmap);
//这里更新发布分享按键的可点出状态
btnSubmit.setEnabled(true); } else {
alert();
}
} else {
alert();
}
} catch (Exception e) {
}
}
压缩处理类:
/**
* @Description 调用系统拍照或进入图库中选择照片,再进行裁剪,压缩.
* @author chq
*/
public class PictureUtil {
//加载并显示一副图像对内存使用情况有显著的影响,Android提供了一个名为BitmapFactory 的实用程序类,该程序提供了一系列的静态方法,允许通过各种来源加载Bitmap图像。针对我们的需求,将从文件加载图像,并在最初的活动中显示它。幸运的是,BitmapFactory中的可用方法将会调用BitmapFactory.Options类,这使得我们能够定义如何将Bitmap读入内存。具体而言,当加载图像时,可以设置BitmapFactory应该使用的采样大小。在BitmapFactory.Options中指定inSampleSize参数。例如,将inSampleSize
//= 8时,产生一幅图的大小是原始大小的1/8。要注意的是首先应将BitmapFactoryOptions.inJustDecodeBounds变量设置为true,这将通知BitmapFactory类只需返回该图像的范围,而无需尝试解码图像本身。最后将BitmapFactory.Options.inJustDecodeBounds设置为false,最后对其进行真正的解码。
/**
*
* @param picPath
* @param reqWidth
* @param reqHeight
* @param compress
* @return
*/
public static Bitmap getSmallBitmap(String picPath,int reqWidth,int reqHeight,int compress) {
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(picPath, options); //options中将获得图片一些信息
options.inSampleSize = calulateInSampleSize(options, reqWidth, reqHeight);
options.inJustDecodeBounds = false; Bitmap bitmap = BitmapFactory.decodeFile(picPath, options);
if (bitmap == null) {
return null;
}
int degree = readPictureDegree(picPath);
bitmap = rotateBitmap(bitmap, degree);
ByteArrayOutputStream baos = null;
try {
baos = new ByteArrayOutputStream();
//压缩图片质量
bitmap.compress(Bitmap.CompressFormat.JPEG,compress, baos);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if(baos != null) {
baos.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return bitmap;
} private static int calulateInSampleSize(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) {
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;
} /**
* 读取图片旋转处理
* @param path
* @return
*/
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;
default:
break;
}
} catch (IOException e) {
e.printStackTrace();
}
return degree;
}
/**
* 图片旋转处理
* @param bitmap
* @param rotate
* @return
*/
private static Bitmap rotateBitmap(Bitmap bitmap,int rotate) {
if(bitmap == null) {
return null;
}
int w = bitmap.getWidth();
int h = bitmap.getHeight();
Matrix mtx = new Matrix();
mtx.postRotate(rotate);
return Bitmap.createBitmap(bitmap,0,0,w,h,mtx,true);
}
}
Android图片处理-图片压缩处理的更多相关文章
- Android中的图片压缩
1.android中计算图片占用堆内存的kB大小跟图片本身的kB大小无关,而是根据图片的尺寸来计算的. 比如一张 480*320大小的图片占用的堆内存大小为: 480*320*4/1024=600kB ...
- Android仿微信高效压缩图片(libjpeg)
用过ios手机的同学应该很明显感觉到,ios拍照1M的图片要比安卓拍照排出来的5M的图片还要清晰.这是为什么呢? 这得了解android底层是如何对图片进行处理的. 当时谷歌开发Android的时候, ...
- Android 编程下图片的内存优化
1. 对图片本身进行操作 尽量不要使用 setImageBitmap.setImageResource. BitmapFactory.decodeResource 来设置一张大图,因为这些方法在完成 ...
- 转-android图片降低图片大小保持图片清晰的方法
http://i.cnblogs.com/EditPosts.aspx?opt=1 android里面对于图片的处理一直是个比较烦人的问题,烦人之处在于一个不小心,就有可能造成OOM. 最近碰到一个关 ...
- Android加载图片OOM错误解决方式
前几天做项目的时候,甲方要求是PAD (SAMSUNG P600 10.1寸 2560*1600)的PAD上显示高分辨率的大图片. SQLITE採用BOLD方式存储图片,这个存取过程就不说了哈,网上一 ...
- android 加载图片oom若干方案小结
本文根据网上提供的一些技术方案加上自己实际开发中遇到的情况小结. 众所周知,每个Android应用程序在运行时都有一定的内存限制,限制大小一般为16MB或24MB(视手机而定).一般我们可以通过获取当 ...
- Android开发笔记——图片缓存、手势及OOM分析
把图片缓存.手势及OOM三个主题放在一起,是因为在Android应用开发过程中,这三个问题经常是联系在一起的.首先,预览大图需要支持手势缩放,旋转,平移等操作:其次,图片在本地需要进行缓存,避免频繁访 ...
- Android高效异步图片加载框架
概述 Android高效异步图片加载框架:一个高效的异步加载显示的图片加载框架,同时具备图片压缩,缓存机制等特性. 详细 代码下载:http://www.demodashi.com/demo/1214 ...
- 【Android】内存卡图片读取器,图库app
上一篇<[Android]读取sdcard卡上的全部图片而且显示,读取的过程有进度条显示>(点击打开链接)在真机上測试非常有问题.常常遇到内存溢出.卡死的情况.由于如今真机上的内存上,2G ...
- iOS学习-压缩图片(改变图片的宽高)
压缩图片,图片的大小与我们期望的宽高不一致时,我们可以将其处理为我们想要的宽高. 传入想要修改的图片,以及新的尺寸 -(UIImage*)imageWithImage:(UIImage*)image ...
随机推荐
- python学习笔记-Day7
class Province: # 静态字段(类变量/属性) country = '中国' def __init__(self, name): # 普通字段(实例变量/属性) self.name = ...
- 10个你必须掌握的Linux超酷VI命令技巧
大部分Linux开发者对vi命令相当熟悉,可是遗憾的是,大部分开发者都只能掌握一些最常用的Linux vi命令,下面介绍的10个vi命令虽然很多不为人知,但是在实际应用中又能让你大大提高效率. 在使用 ...
- hibernate多表查询,结果封装在自己定义的一个实体类当中(在自己定义的类中增加构造函数)
hibernate的hql查询直接返回java对象时出现问题3 向大家请教一个问题,现在有三张表,表之间没有关联,我需要将三张表里面的所有东西查询出来存储到一个新的对象中,该如何实现,使用hibern ...
- 何时使用Swift Structs和Classes
Swift 圈中有一个被反复讨论的话题是:何时使用struct,何时使用class.我觉得今天我也要给出我的个人观点. 值 VS 引用 答案真的很简单了:当你需要用值语义的时候使用class,需要用引 ...
- 基本套接字编程(5) -- epoll篇
1. epoll技术 epoll是Linux内核为处理大批量文件描述符而作了改进的poll,是Linux下多路复用IO接口select/poll的增强版本,它能显著提高程序在大量并发连接中只有少量活跃 ...
- EditText 焦点
让EditText不自动获取焦点,只需要在EditText的父控件加上 android:focusable="true"android:focusableInTouchMode=& ...
- ansible 2.2的源码编译安装
ansible代码下载地址:http://releases.ansible.com/ansible/ # git clone git://github.com/ansible/ansible.git ...
- GitLab的Gravatar头像服务不可用
由于www.gravatar.com在国内不可正常使用,导致我们搭建的GitLab在网页上会阻塞大量时间,并最终无法显示头像.我们可以将其替换成"多说"的头像服务.我使用的是CE ...
- mysql.sock
Mysql有两种连接方式: (1)TCP/IP (2)socket 对mysql.sock来说,其作用是程序与mysqlserver处于同一台机器,发起本地连接时可用. 例如你无须定义连接host的具 ...
- Java里this的作用和用法
this, 一个官方的说法是,this首先是一个对象,它代表调用这个函数的对象. 根据面向对象的基本语法,每当调用变量或者函数的时候,都要按照类名.变量(函数)的格式来调用,意即每个变量或函数都必须属 ...