您的位置:首页 > 其它

图片压缩——压尺寸、压大小

2014-08-12 17:07 351 查看
    图片的压缩主要有两种: 压尺寸,即调整平时所说的图片宽高。压大小,即对图片的容量大小进行压缩。

   1、压尺寸

    /**

     * 根据路径获得图片并压缩返回bitmap用于显示(压尺寸)

     * 

     * @param filePath 图片路径

     * @return

     */

    public Bitmap getSmallBitmap(final String filePath) {

        final BitmapFactory.Options options = new BitmapFactory.Options();

        options.inJustDecodeBounds = true;// 当设为true,BitmapFactory.decodeFile(filePath,

                                          // options),不生 成bitmap

        BitmapFactory.decodeFile(filePath, options);

        ImageSize srcSize = new ImageSize(options.outWidth, options.outWidth);

        ImageSize targeSize = new ImageSize(180, 180);

        options.inSampleSize = computeImageSampleSize(srcSize, targeSize);

        // Decode bitmap with inSampleSize set

        options.inJustDecodeBounds = false;

        options.inPurgeable = true;// 如果 inPurgeable

                                   // 设为True的话表示使用BitmapFactory创建的Bitmap

                                   // 用于存储Pixel的内存空间在系统内存不足时可以被回收,

        options.inInputShareable = true;

        Bitmap bitmap = BitmapFactory.decodeFile(filePath, options);

        // 将图片旋转正

        Matrix matrix = new Matrix();

        matrix.postRotate(readPictureDegree(filePath));

        return Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(),

                bitmap.getHeight(), matrix, true);

    }

   

    上面有段代码需要注意,市面有些手机拍完照片后生成的图片是旋转过的,这里需要根据旋转的角度对图片进行调正

    /**

     * 图片大小类,用于记录图片宽度和高度

     **/

    class ImageSize {

        private final int width;

        private final int height;

        public ImageSize(int width, int height) {

            this.width = width;

            this.height = height;

        }

        public int getWidth() {

            return width;

        }

        public int getHeight() {

            return height;

        }

    }

    /**

     * 读取图片属性:旋转的角度

     * 

     * @param path 图片绝对路径

     * @return degree旋转的角度

     */

    public 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;

            }

        } catch (IOException e) {

            e.printStackTrace();

        }

        return degree;

    }

    /**

     * 计算压缩值

     * @param srcSize 根据源图片大小

     * @param targetSize 目标压缩图片大小

     * @return 返回压缩值

     */

    public static int computeImageSampleSize(ImageSize srcSize, ImageSize targetSize) {

        int srcWidth = srcSize.getWidth();

        int srcHeight = srcSize.getHeight();

        int targetWidth = targetSize.getWidth();

        int targetHeight = targetSize.getHeight();

        int scale = 1;

        while (srcWidth / 2 >= targetWidth && srcHeight / 2 >= targetHeight) { // &&

            srcWidth /= 2;

            srcHeight /= 2;

            scale *= 2;

        }

        if (scale < 1) {

            scale = 1;

        }

        return scale;

    }

    // 2、压大小

    /**

     * @param imgPath

     * @param bitmap

     * @param imgFormat 图片格式

     * @return

     */

    public Bitmap compressBitmap(String imgPath, Bitmap bitmap) {

        if (imgPath != null && imgPath.length() > 0) {

            bitmap = getSmallBitmap(imgPath);

        }

        if (bitmap == null) {

            // bitmap not found!!

        }

        ByteArrayOutputStream out = null;

        try {

            out = new ByteArrayOutputStream();

            bitmap.compress(Bitmap.CompressFormat.JPEG, 50, out);//关键代码,根据压缩值50见图片压缩一半

            out.flush();

            out.close();

            byte[] imgBytes = out.toByteArray();

            return BitmapFactory.decodeByteArray(imgBytes, 0, imgBytes.length);

        } catch (Exception e) {

            // TODO Auto-generated catch block

            return null;

        } finally {

            try {

                out.flush();

                out.close();

            } catch (IOException e) {

                // TODO Auto-generated catch block

                e.printStackTrace();

            }

        }

    }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: