android中图片压缩以及图片旋转的方法
2015-11-16 21:42
423 查看
博客源址:android中图片压缩以及图片旋转的方法
博客时间:2014-12-08
14:40
android中图片压缩以及图片旋转的方法
在开发中,如果需要上传图片到服务器中,而且还需要在本地预览,就会用到图片的压缩:
[java] view
plaincopy
/**
* 计算图片的缩放值
*
* @param options
* @param reqWidth
* @param reqHeight
* @return
*/
public static int calculateInSampleSize(BitmapFactory.Options options,
int reqWidth, int reqHeight) {
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);
inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
}
return inSampleSize;
}
/**
* 根据路径获得图片并压缩返回bitmap
*
* @param filePath
* @return
*/
public static Bitmap getSmallBitmap(String filePath, int reqWidth,
int reqHeight) {
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(filePath, options);
options.inSampleSize = calculateInSampleSize(options, reqWidth,
reqHeight);
options.inJustDecodeBounds = false;
return BitmapFactory.decodeFile(filePath, options);
}
有时候显示图片的时候 会发现有些图片的位置不正,这时我们就需要调整一下方向:
[java] view
plaincopy
/**
* 获取图片文件的信息,是否旋转了90度,如果是则反转
* @param bitmap 需要旋转的图片
* @param path 图片的路径
*/
public static Bitmap reviewPicRotate(Bitmap bitmap,String path){
int degree = getPicRotate(path);
if(degree!=0){
Matrix m = new Matrix();
int width = bitmap.getWidth();
int height = bitmap.getHeight();
m.setRotate(degree); // 旋转angle度
bitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height,m, true);// 从新生成图片
}
return bitmap;
}
/**
* 读取图片文件旋转的角度
* @param path 图片绝对路径
* @return 图片旋转的角度
*/
public static int getPicRotate(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;
}
android中图片压缩以及图片旋转的方法
博客时间:2014-12-08
14:40
android中图片压缩以及图片旋转的方法
在开发中,如果需要上传图片到服务器中,而且还需要在本地预览,就会用到图片的压缩:
[java] view
plaincopy
/**
* 计算图片的缩放值
*
* @param options
* @param reqWidth
* @param reqHeight
* @return
*/
public static int calculateInSampleSize(BitmapFactory.Options options,
int reqWidth, int reqHeight) {
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);
inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
}
return inSampleSize;
}
/**
* 根据路径获得图片并压缩返回bitmap
*
* @param filePath
* @return
*/
public static Bitmap getSmallBitmap(String filePath, int reqWidth,
int reqHeight) {
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(filePath, options);
options.inSampleSize = calculateInSampleSize(options, reqWidth,
reqHeight);
options.inJustDecodeBounds = false;
return BitmapFactory.decodeFile(filePath, options);
}
有时候显示图片的时候 会发现有些图片的位置不正,这时我们就需要调整一下方向:
[java] view
plaincopy
/**
* 获取图片文件的信息,是否旋转了90度,如果是则反转
* @param bitmap 需要旋转的图片
* @param path 图片的路径
*/
public static Bitmap reviewPicRotate(Bitmap bitmap,String path){
int degree = getPicRotate(path);
if(degree!=0){
Matrix m = new Matrix();
int width = bitmap.getWidth();
int height = bitmap.getHeight();
m.setRotate(degree); // 旋转angle度
bitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height,m, true);// 从新生成图片
}
return bitmap;
}
/**
* 读取图片文件旋转的角度
* @param path 图片绝对路径
* @return 图片旋转的角度
*/
public static int getPicRotate(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;
}
android中图片压缩以及图片旋转的方法
相关文章推荐
- Android开发工具全面转向Android Studio
- android100 自定义内容提供者
- Android MVP with Fragment and RecyclerView
- android 设置椭圆组件
- 基于局域网的聊天系统
- Android Studio如何复制代码提示里的内容
- android源码编译记录
- android学生管理系统,利用linerLayout,实现xml的读取和写入,以及按钮的事件处理
- Android EventBus的简单使用
- android屏幕录像
- Xamarin.Android广播接收器
- Android 一些基本组件的使用
- Android Studio 配置 Gradle 直接运行打包(relase)程序 图文详解
- android带返回按钮的自定义标题栏布局文件详解
- Android 实现LazyFragment
- 目前和接下来的android
- Android基础
- Android APK反编译就这么简单 详解(附图)
- Android开发日记(一)
- android菜鸟修炼记