图片压缩方法
来源:互联网 发布:淘宝转链接 编辑:程序博客网 时间:2024/05/17 00:50
一、图片质量压缩
-
-
-
-
-
-
- public static Bitmap compressImage(Bitmap image) {
-
- ByteArrayOutputStream baos = new ByteArrayOutputStream();
- image.compress(Bitmap.CompressFormat.JPEG, 100, baos);
- int options = 90;
-
- while (baos.toByteArray().length / 1024 > 100) {
- baos.reset();
- image.compress(Bitmap.CompressFormat.JPEG, options, baos);
- options -= 10;
- }
- ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray());
- Bitmap bitmap = BitmapFactory.decodeStream(isBm, null, null);
- return bitmap;
- }
二、按比例大小压缩 (路径获取图片)
-
-
-
-
-
-
- public static Bitmap getimage(String srcPath) {
-
- BitmapFactory.Options newOpts = new BitmapFactory.Options();
-
- newOpts.inJustDecodeBounds = true;
- Bitmap bitmap = BitmapFactory.decodeFile(srcPath, newOpts);
-
- newOpts.inJustDecodeBounds = false;
- int w = newOpts.outWidth;
- int h = newOpts.outHeight;
-
- float hh = 800f;
- float ww = 480f;
-
- int be = 1;
- if (w > h && w > ww) {
- be = (int) (newOpts.outWidth / ww);
- } else if (w < h && h > hh) {
- be = (int) (newOpts.outHeight / hh);
- }
- if (be <= 0)
- be = 1;
- newOpts.inSampleSize = be;
-
- bitmap = BitmapFactory.decodeFile(srcPath, newOpts);
- return compressImage(bitmap);
- }
三、按比例大小压缩 (Bitmap)
-
-
-
-
-
-
- public static Bitmap compressScale(Bitmap image) {
-
- ByteArrayOutputStream baos = new ByteArrayOutputStream();
- image.compress(Bitmap.CompressFormat.JPEG, 100, baos);
-
-
- if (baos.toByteArray().length / 1024 > 1024) {
- baos.reset();
- image.compress(Bitmap.CompressFormat.JPEG, 80, baos);
- }
- ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray());
- BitmapFactory.Options newOpts = new BitmapFactory.Options();
-
- newOpts.inJustDecodeBounds = true;
- Bitmap bitmap = BitmapFactory.decodeStream(isBm, null, newOpts);
- newOpts.inJustDecodeBounds = false;
- int w = newOpts.outWidth;
- int h = newOpts.outHeight;
- Log.i(TAG, w + "---------------" + h);
-
-
-
- float hh = 512f;
- float ww = 512f;
-
- int be = 1;
- if (w > h && w > ww) {
- be = (int) (newOpts.outWidth / ww);
- } else if (w < h && h > hh) {
- be = (int) (newOpts.outHeight / hh);
- }
- if (be <= 0)
- be = 1;
- newOpts.inSampleSize = be;
-
-
-
- isBm = new ByteArrayInputStream(baos.toByteArray());
- bitmap = BitmapFactory.decodeStream(isBm, null, newOpts);
-
- return compressImage(bitmap);
-
-
- }
- http://blog.csdn.net/gao_chun/article/details/45195497
0 0