(转)android 压缩图片大小 – 热爱改变生活
我的GitHub GitHub |     登录
  • If you can't fly, then run; if you can't run, then walk; if you can't walk, then crawl
  • but whatever you do, you have to keep moving forward。
  • “你骗得了我有什么用,这是你自己的人生”
  • 曾有伤心之地,入梦如听 此歌

(转)android 压缩图片大小

Android工具 sinvader 2512℃ 0评论

package com.img.util;
 
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.OutputStream;
 
import android.content.Context;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Bitmap.CompressFormat;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore.Images;
import android.util.Log;
 
/**
 * 图片压缩工具类
 * 
 * @author Administrator
 * 
 */
public class ImageCompress {
 
    public static final String CONTENT = "content";
    public static final String FILE = "file";
 
    /**
     * 图片压缩参数
     * 
     * @author Administrator
     * 
     */
    public static class CompressOptions {
        public static final int DEFAULT_WIDTH = 400;
        public static final int DEFAULT_HEIGHT = 800;
 
        public int maxWidth = DEFAULT_WIDTH;
        public int maxHeight = DEFAULT_HEIGHT;
        /**
         * 压缩后图片保存的文件
         */
        public File destFile;
        /**
         * 图片压缩格式, 默认为jpg格式
         */
        public CompressFormat imgFormat = CompressFormat.JPEG;
 
        /**
         * 图片压缩比例 默认为30
         */
        public int quality = 30;
 
        public Uri uri;
    }
 
    public Bitmap compressFromUri(Context context,
            CompressOptions compressOptions) {
 
        // uri指向的文件路径
        String filePath = getFilePath(context, compressOptions.uri);
         
        if (null == filePath) {
            return null;
        }
 
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
 
        Bitmap temp = BitmapFactory.decodeFile(filePath, options);
 
        int actualWidth = options.outWidth;
        int actualHeight = options.outHeight;
 
        int desiredWidth = getResizedDimension(compressOptions.maxWidth,
                compressOptions.maxHeight, actualWidth, actualHeight);
        int desiredHeight = getResizedDimension(compressOptions.maxHeight,
                compressOptions.maxWidth, actualHeight, actualWidth);
 
        options.inJustDecodeBounds = false;
        options.inSampleSize = findBestSampleSize(actualWidth, actualHeight,
                desiredWidth, desiredHeight);
 
        Bitmap bitmap = null;
 
        Bitmap destBitmap = BitmapFactory.decodeFile(filePath, options);
 
        // If necessary, scale down to the maximal acceptable size.
        if (destBitmap.getWidth() > desiredWidth
                || destBitmap.getHeight() > desiredHeight) {
            bitmap = Bitmap.createScaledBitmap(destBitmap, desiredWidth,
                    desiredHeight, true);
            destBitmap.recycle();
        } else {
            bitmap = destBitmap;
        }
 
        // compress file if need
        if (null != compressOptions.destFile) {
            compressFile(compressOptions, bitmap);
        }
 
        return bitmap;
    }
 
    /**
     * compress file from bitmap with compressOptions
     * 
     * @param compressOptions
     * @param bitmap
     */
    private void compressFile(CompressOptions compressOptions, Bitmap bitmap) {
        OutputStream stream = null;
        try {
            stream = new FileOutputStream(compressOptions.destFile);
        } catch (FileNotFoundException e) {
            Log.e("ImageCompress", e.getMessage());
        }
 
        bitmap.compress(compressOptions.imgFormat, compressOptions.quality,
                stream);
    }
 
    private static int findBestSampleSize(int actualWidth, int actualHeight,
            int desiredWidth, int desiredHeight) {
        double wr = (double) actualWidth / desiredWidth;
        double hr = (double) actualHeight / desiredHeight;
        double ratio = Math.min(wr, hr);
        float n = 1.0f;
        while ((n * 2) < = ratio) {
            n *= 2;
        }
 
        return (int) n;
    }
 
    private static int getResizedDimension(int maxPrimary, int maxSecondary,
            int actualPrimary, int actualSecondary) {
        // If no dominant value at all, just return the actual.
        if (maxPrimary == 0 && maxSecondary == 0) {
            return actualPrimary;
        }
 
        // If primary is unspecified, scale primary to match secondary's scaling
        // ratio.
        if (maxPrimary == 0) {
            double ratio = (double) maxSecondary / (double) actualSecondary;
            return (int) (actualPrimary * ratio);
        }
 
        if (maxSecondary == 0) {
            return maxPrimary;
        }
 
        double ratio = (double) actualSecondary / (double) actualPrimary;
        int resized = maxPrimary;
        if (resized * ratio > maxSecondary) {
            resized = (int) (maxSecondary / ratio);
        }
        return resized;
    }
 
    /**
     * 获取文件的路径
     * 
     * @param scheme
     * @return
     */
    private String getFilePath(Context context, Uri uri) {
 
        String filePath = null;
 
        if (CONTENT.equalsIgnoreCase(uri.getScheme())) {
 
            Cursor cursor = context.getContentResolver().query(uri,
                    new String[] { Images.Media.DATA }, null, null, null);
 
            if (null == cursor) {
                return null;
            }
 
            try {
                if (cursor.moveToNext()) {
                    filePath = cursor.getString(cursor
                            .getColumnIndex(Images.Media.DATA));
                }
            } finally {
                cursor.close();
            }
        }
 
        // 从文件中选择
        if (FILE.equalsIgnoreCase(uri.getScheme())) {
            filePath = uri.getPath();
        }
 
        return filePath;
    }
}

调用方法:

ImageCompress compress = new ImageCompress();
            ImageCompress.CompressOptions options = new ImageCompress.CompressOptions();
            options.uri = imgUri;
            options.maxWidth=getWindowManager().getDefaultDisplay().getWidth();
            options.maxHeight=getWindowManager().getDefaultDisplay().getHeight();
            Bitmap bitmap = compress.compressFromUri(this, options);
¥ 有帮助么?打赏一下~

转载请注明:热爱改变生活.cn » (转)android 压缩图片大小


本博客只要没有注明“转”,那么均为原创。 转载请注明链接:sumile.cn » (转)android 压缩图片大小

喜欢 (0)
发表我的评论
取消评论
表情

如需邮件形式接收回复,请注册登录

Hi,你需要填写昵称和邮箱~

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址