android中图片压缩以及图片旋转的方法
来源:互联网 发布:红蜘蛛软件破解 编辑:程序博客网 时间:2024/06/09 20:40
转载:http://blog.csdn.net/xiaorenwu1206/article/details/41803431
在开发中,如果需要上传图片到服务器中,而且还需要在本地预览,就会用到图片的压缩:
- /**
- * 计算图片的缩放值
- *
- * @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);
- }
有时候显示图片的时候 会发现有些图片的位置不正,这时我们就需要调整一下方向:
- /**
- * 获取图片文件的信息,是否旋转了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;
- }
1 0
- android中图片压缩以及图片旋转的方法
- android中图片压缩以及图片旋转的方法
- android中图片压缩以及图片旋转的方法
- Android 图片压缩的3中方法
- android 图片压缩的方法
- android压缩图片的方法
- android 图片压缩的方法
- Android中图片的压缩
- Android中图片压缩技术以及图片缓存避免OOM
- iOS 中图片的压缩以及保存
- android调用系统相机的方法,并且解决三星手机遇到的问题,图片旋转、压缩
- Android压缩图片并保持不失真的高效方法以及为图片添加水印
- android图片压缩方法
- android图片压缩方法
- android图片压缩方法
- android压缩图片方法
- android 图片压缩方法
- android 图片压缩方法
- webview在红米3上面打开显示错误A WebView method was called on thread 'Thread-9191'. All WebView methods must be
- 杭电ACM2022java做法
- Vim删除不包含指定字符串的行
- nodejs - 如何完全更新
- JQuery的右侧垂直锚点导航
- android中图片压缩以及图片旋转的方法
- 学习linux驱动经典书籍
- python网络编程
- Mybatis联合查询
- mysql中文显示为问号?的解决方法
- 搭建hadoop集群环境
- ios sample code
- 【ASP.NET北大青鸟】-总结(二)
- [OpenCV] How to install opencv by compiling source code