Android图片处理-图片压缩处理
2016-03-24 16:37
357 查看
这里先重复温习一下上一篇,调用相册获取图片:
获取选择的图片:
压缩处理类:
/*** * 这个是调用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 HAL想到的
- Android的RelativeLayout的view的layout_marginBottom不起作用
- 设计模式 —— 观察者
- android 的分发、拦截、处理机制
- (4.1.36.11)Android 一张图理解getWidth和getMeasuredWidth
- (4.1.36.10)[Android]Android字体高度的研究
- Android调试驱动抓log的方法
- Android中Volley框架Get,POST封装使用及自动解析JSON
- 通常在使用Android的getActionBar()的时候报空指针异常,通常有5种原因。
- Android系统关机或重启的几种实现方式
- android 数据传递——实例化对象调用方法
- Android图片处理-相机、相处简单调用
- ListView和GridView与ScrollView冲突只显示一行的问题
- Android中退出多个Activity应用
- android activity onWindowFocusChanged函数的使用场景
- 自定义ViewGroup,流式布局FlowLayout
- android 高效加载大图
- Android 特色开发,基于位置的服务
- Android异常和工具使用笔记