图片二次采样和按质量压缩
来源:互联网 发布:gta5捏脸数据女黑寡妇 编辑:程序博客网 时间:2024/05/30 23:04
图片上传的时候往往需要压缩图片和调整大小
1.二次采样
/* * * 按比例缩放图片大小 * * @param imageStream * @compress * **/ public static Bitmap compressImageProportion(String srcPath, int height, int width) { if (null == srcPath || "".equals(srcPath.trim())) { return null; } BitmapFactory.Options newOpts = new BitmapFactory.Options(); //开始读入图片,此时把options.inJustDecodeBounds 设回true了 newOpts.inJustDecodeBounds = true; Bitmap bitmap = BitmapFactory.decodeFile(srcPath, newOpts);//此时返回bm为空 int w = newOpts.outWidth; int h = newOpts.outHeight; //现在主流手机比较多是800*480分辨率,所以高和宽我们设置为 float hh = height;//这里设置高度为800f float ww = width;//这里设置宽度为480f //缩放比。由于是固定比例缩放,只用高或者宽其中一个数据进行计算即可 int be = 1;//be=1表示不缩放 if ( w > ww) {//如果宽度大的话根据宽度固定大小缩放 be = (int) (newOpts.outWidth / ww); } if (be <= 0) be = 1; newOpts.inSampleSize = be;//设置缩放比例 newOpts.inJustDecodeBounds = false; //重新读入图片,注意此时已经把options.inJustDecodeBounds 设回false了 bitmap = BitmapFactory.decodeFile(srcPath, newOpts); return bitmap; }
按照宽度的比例进行缩放
2.质量压缩
/* * * 按质量压缩图片大小 * * @param image * @compress * **/ public static InputStream compressImage(Bitmap image, int compress) { if (image != null) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); image.compress(Bitmap.CompressFormat.JPEG, 100, baos);//质量压缩方法,这里100表示不压缩,把压缩后的数据存放到baos中 int options = 100; Log.e("compressImage: ", baos.toByteArray().length / 1024 + "," + compress); while (baos.toByteArray().length / 1024 > compress) { //循环判断如果压缩后图片是否大于compressValue kb,大于继续压缩 baos.reset();//重置baos即清空baos image.compress(Bitmap.CompressFormat.JPEG, options, baos);//这里压缩options%,把压缩后的数据存放到baos中 options -= 10;//每次都减少10 if (options < 0) { break; } } ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray());//把压缩后的数据baos存放到ByteArrayInputStream中 return isBm; } return null; } //压缩图片大小返回图片 public static Bitmap compressImagebp(Bitmap image, int compress) { return BitmapFactory.decodeStream(compressImage(image,compress), null, null); }3.以上两种方法的使用
bt1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Bitmap bitmap = BitmapUtils.getbitmap("/sdcard/img.jpg"); int compress = seekBar1.getProgress(); Bitmap bitmap1 = BitmapUtils.compressImagebp(bitmap, compress); Log.e( "onClick: ",bitmap.getByteCount()+","+bitmap1.getByteCount() ); iv.setImageBitmap(bitmap1); } }); bt2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Bitmap bitmap = BitmapUtils.getbitmap("/sdcard/img.jpg"); int width = bitmap.getWidth(); int height= bitmap.getHeight(); float compress = seekBar2.getProgress()/100f; Log.e( "onClick: ",compress+"" ); /* Bitmap bitmap1 = bitmap.copy(Bitmap.Config.ARGB_8888, true); bitmap1.setWidth(((int) (compress * width))); bitmap1.setHeight(((int) (compress * height)));*/ Bitmap bitmap1 = BitmapUtils.compressImageProportion("/sdcard/img.jpg", ((int) (compress * width)), ((int) (compress * height))); iv.setImageBitmap(bitmap1); } });
阅读全文
0 0
- 图片二次采样和按质量压缩
- 图片的质量压缩和二次采样
- Android 图片二次采样、质量压缩
- Android 图片二次采样、质量压缩
- Android 图片二次采样、质量压缩
- Android 图片二次采样、质量压缩
- Android 图片二次采样、质量压缩
- Android 图片二次采样、质量压缩
- 二次采样质量压缩
- Android---------二次采样和质量压缩
- 图片压缩 二次采样
- 二次采样及质量压缩
- 图片的二次采样和压缩_lemonen
- 二次采样和压缩
- 图片压缩大全及二次采样
- 图片的二次采样
- 图片的二次采样
- android图片二次采样
- 3.12squid
- 二叉树和为某一值的路径
- 013--swift计算型属性和懒加载的区别
- 脑洞大开 [c++][重启病毒]
- 第二课,数据类型与变量,常量
- 图片二次采样和按质量压缩
- 顺时针打印矩阵
- web.xml文件详解
- mysql如何处理无效的数据值
- webService开发
- 始终因消费者而进步——极米无屏电视的成功之道
- Java对redis的基本操作
- Ubuntu 16.04 无法安装.deb解决方案
- php调用js