【No4.】对图片的压缩质量、压缩尺寸、读流压缩、方法介绍
2014-07-29 18:05
423 查看
不多说直接上代码,代码注释写的很详细,都是封装好的方法,直接拿来调用就行了:
通过降低图片的质量来压缩图片:
/** * 通过降低图片的质量来压缩图片 * * @param bmp * 要压缩的图片 * @param maxSize * 压缩后图片大小的最大值,单位KB * @return 压缩后的图片 */ public static Bitmap compressByQuality(Bitmap bitmap, int maxSize) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); int quality = 100; bitmap.compress(CompressFormat.JPEG, quality, baos); while (baos.toByteArray().length / 1024 > maxSize) { quality -= 10; baos.reset(); bitmap.compress(CompressFormat.JPEG, quality, baos); } bitmap = BitmapFactory.decodeByteArray(baos.toByteArray(), 0, baos.toByteArray().length); return bitmap; }
通过压缩图片的尺寸来压缩图片大小
/** * 通过压缩图片的尺寸来压缩图片大小 * * @param pathName * 图片的完整路径 * @param targetWidth * 缩放的目标宽度 * @param targetHeight * 缩放的目标高度 * @return 缩放后的图片 */ public static Bitmap compressBySize(String pathName, int targetWidth, int targetHeight) { BitmapFactory.Options opts = new BitmapFactory.Options(); opts.inJustDecodeBounds = true;// 不去真的解析图片,只是获取图片的头部信息,包含宽高等; Bitmap bitmap = BitmapFactory.decodeFile(pathName, opts); // 得到图片的宽度、高度; int imgWidth = opts.outWidth; int imgHeight = opts.outHeight; // 分别计算图片宽度、高度与目标宽度、高度的比例;取大于等于该比例的最小整数; int widthRatio = (int) Math.ceil(imgWidth / (float) targetWidth); int heightRatio = (int) Math.ceil(imgHeight / (float) targetHeight); if (widthRatio > 1 || widthRatio > 1) { if (widthRatio > heightRatio) { opts.inSampleSize = widthRatio; } else { opts.inSampleSize = heightRatio; } } // 设置好缩放比例后,加载图片进内容; opts.inJustDecodeBounds = false; bitmap = BitmapFactory.decodeFile(pathName, opts); return bitmap; }
通过压缩图片的尺寸来压缩图片大小
/** * 通过压缩图片的尺寸来压缩图片大小 * * @param bitmap * 要压缩图片 * @param targetWidth * 缩放的目标宽度 * @param targetHeight * 缩放的目标高度 * @return 缩放后的图片 */ public static Bitmap compressBySize(Bitmap bitmap, int targetWidth, int targetHeight) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); bitmap.compress(CompressFormat.JPEG, 100, baos); BitmapFactory.Options opts = new BitmapFactory.Options(); opts.inJustDecodeBounds = true; bitmap = BitmapFactory.decodeByteArray(baos.toByteArray(), 0, baos.toByteArray().length, opts); // 得到图片的宽度、高度; int imgWidth = opts.outWidth; int imgHeight = opts.outHeight; // 分别计算图片宽度、高度与目标宽度、高度的比例;取大于该比例的最小整数; int widthRatio = (int) Math.ceil(imgWidth / (float) targetWidth); int heightRatio = (int) Math.ceil(imgHeight / (float) targetHeight); if (widthRatio > 1 && widthRatio > 1) { if (widthRatio > heightRatio) { opts.inSampleSize = widthRatio; } else { opts.inSampleSize = heightRatio; } } // 设置好缩放比例后,加载图片进内存; opts.inJustDecodeBounds = false; bitmap = BitmapFactory.decodeByteArray(baos.toByteArray(), 0, baos.toByteArray().length, opts); return bitmap; }
通过压缩图片的尺寸来压缩图片大小,通过读入流的方式,可以有效防止网络图片数据流形成位图对象时内存过大的问题;
/** * 通过压缩图片的尺寸来压缩图片大小,通过读入流的方式,可以有效防止网络图片数据流形成位图对象时内存过大的问题; * * @param InputStream * 要压缩图片,以流的形式传入 * @param targetWidth * 缩放的目标宽度 * @param targetHeight * 缩放的目标高度 * @return 缩放后的图片 * @throws IOException * 读输入流的时候发生异常 */ public static Bitmap compressBySize(InputStream is, int targetWidth, int targetHeight) throws IOException { ByteArrayOutputStream baos = new ByteArrayOutputStream(); byte[] buff = new byte[1024]; int len = 0; while ((len = is.read(buff)) != -1) { baos.write(buff, 0, len); } byte[] data = baos.toByteArray(); BitmapFactory.Options opts = new BitmapFactory.Options(); opts.inJustDecodeBounds = true; Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length, opts); // 得到图片的宽度、高度; int imgWidth = opts.outWidth; int imgHeight = opts.outHeight; // 分别计算图片宽度、高度与目标宽度、高度的比例;取大于该比例的最小整数; int widthRatio = (int) Math.ceil(imgWidth / (float) targetWidth); int heightRatio = (int) Math.ceil(imgHeight / (float) targetHeight); if (widthRatio > 1 && widthRatio > 1) { if (widthRatio > heightRatio) { opts.inSampleSize = widthRatio; } else { opts.inSampleSize = heightRatio; } } // 设置好缩放比例后,加载图片进内存; opts.inJustDecodeBounds = false; bitmap = BitmapFactory.decodeByteArray(data, 0, data.length, opts); return bitmap; }
把图片变成圆角
/** * 把图片变成圆角 * * @param bitmap * 需要修改的图片 * @param pixels * 圆角的弧度。数值越大,圆角越大 * @return 带圆角的图片 */ public static Bitmap toRoundCorner(Bitmap bitmap, int pixels) { Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888); Canvas canvas = new Canvas(output); final int color = 0xff424242; final Paint paint = new Paint(); final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight()); final RectF rectF = new RectF(rect); final float roundPx = pixels; paint.setAntiAlias(true); canvas.drawARGB(0, 0, 0, 0); paint.setColor(color); canvas.drawRoundRect(rectF, roundPx, roundPx, paint); paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN)); canvas.drawBitmap(bitmap, rect, rect, paint); return output; }
相关文章推荐
- 一种使用GDI+对图片尺寸和质量的压缩方法
- 一种使用GDI+对图片尺寸和质量的压缩方法
- Linux系统下批量压缩图片尺寸大小的方法
- Android图片压缩(质量压缩和尺寸压缩)、图片保存(本地和sqlite数据库)
- 图片质量压缩,尺寸不变,不需要用到磁盘
- ios 压缩图片尺寸方法
- 传统基础图片压缩:先尺寸压缩后质量压缩
- java 程序实现对图片的压缩生成缩略图并可设定长宽、尺寸压缩率、图片质量
- Android图片质量压缩和尺寸压缩处理
- iOS开发中压缩图片的质量以及缩小图片尺寸
- Golang 编写的图片压缩程序,质量、尺寸压缩,批量、单张压缩
- 性能优化之图片压缩(质量压缩、尺寸压缩)
- java 程序实现对图片的压缩生成缩略图并可设定长宽、尺寸压缩率、图片质量
- Android图片压缩,质量压缩和尺寸压缩两种
- 同等尺寸下为何android压缩出来的图片质量不如iphone
- Android图片压缩尺寸及质量
- Android中图片压缩(质量压缩和尺寸压缩)
- 图片按尺寸和质量压缩
- 最新图片质量和按大小压缩方法
- java 程序实现对图片的压缩生成缩略图并可设定长宽、尺寸压缩率、图片质量