图片压缩

来源:互联网 发布:Tensorflow 字符识别 编辑:程序博客网 时间:2024/06/06 14:32
    /**     * 根据路径,二次采样并且压缩     * @param filePath 路径     * @param destWidth 压缩到的宽度     * @param destHeight 压缩到的高度     * @return     */    public Bitmap convertToBitmap(String filePath, int destWidth, int destHeight) {        //第一采样        BitmapFactory.Options options = new BitmapFactory.Options();        options.inJustDecodeBounds = true;        BitmapFactory.decodeFile(filePath, options);        int outWidth = options.outWidth;        int outHeight = options.outHeight;        int sampleSize = 1;        while ((outWidth / sampleSize > destWidth) || (outHeight / sampleSize > destHeight)) {            sampleSize *= 2;        }        //第二次采样        options.inJustDecodeBounds = false;        options.inSampleSize = sampleSize;        options.inPreferredConfig = Bitmap.Config.RGB_565;        return BitmapFactory.decodeFile(filePath, options);    }

0 0
原创粉丝点击