java - Android 从相册里面获取图片如何压缩?再上传到服务器
迷茫
迷茫 2017-04-17 16:31:09
[Java讨论组]
迷茫
迷茫

业精于勤,荒于嬉;行成于思,毁于随。

全部回复(4)
天蓬老师

缩小图片尺寸:

/**
 * 放大缩小图片(基本不会出现锯齿,比{@linkplain #zoomBitmapByScale}耗多一点点时间)
 *
 * @param bitmap
 * @param reqWidth 要求的宽度
 * @param reqHeight 要求的高度
 * @return
 */
public static Bitmap zoomBitmap(Bitmap bitmap, int reqWidth, int reqHeight) {
    ByteArrayOutputStream bout = new ByteArrayOutputStream();
    bitmap.compress(CompressFormat.JPEG, 95, bout);// 可以是CompressFormat.PNG

    // 图片原始数据
    byte[] byteArr = bout.toByteArray();

    // 计算sampleSize
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;

    // 调用方法后,option已经有图片宽高信息
    BitmapFactory.decodeByteArray(byteArr, 0, byteArr.length, options);

    // 计算最相近缩放比例
    options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
    options.inJustDecodeBounds = false;

    // 这个Bitmap out只有宽高
    Bitmap out = BitmapFactory.decodeByteArray(byteArr, 0, byteArr.length, options);

    return bitmap;
}

/** 计算图片的缩放值 */
public static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
    final int height       = options.outHeight;
    final int width        = options.outWidth;
    int       inSampleSize = 1;

    if (height > reqHeight || width > reqWidth) {
        final int heightRatio = Math.round((float) height / (float) reqHeight);
        final int widthRatio = Math.round((float) width / (float) reqWidth);
        inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
    }
    return inSampleSize;
}

上面方法,是让一张按一定比例缩放图片(2、4、8...)。

android大部分手机屏幕都是1080P 720P,所以上传到服务器的图片尺寸即使很大,在手机显示最多也就1080px(1080P手机显示一张width=2400px,跟显示width=1080px效果是没区别的)。当然,如果你的APP有很多平板用户使用,大尺寸图片还是可以考虑的。

下面是质量压缩:

/**
 * 压缩图片到指定位置(默认JPG格式)
 *
 * @param bitmap       需要压缩的图片
 * @param compressPath 生成文件路径(例如: /storage/imageCache/1.jpg)
 * @param quality      图片质量,0~100
 * @return if true,保存成功
*/
public static boolean compressBiamp(Bitmap bitmap, String compressPath, int quality) {
    FileOutputStream stream = null;
    try {
        stream = new FileOutputStream(new File(compressPath));

        bitmap.compress(Bitmap.CompressFormat.JPEG, quality, stream);// (0-100)压缩文件

        return true;

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } finally {
        try {
            stream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    return false;
}

quality 90~95之间就行。

Bitmap.CompressFormat.JPEG可以改为Bitmap.CompressFormat.PNG(原图是透明背景的,用PNG)。其实还可以用Bitmap.CompressFormat.WEBP,不过只有部分浏览器支持webp(iossafari就不支持),所以webp格式图片上传到服务器,ios是加载不出来的(当然可以加一下第三方库让ios加载webp

阿神

图片压缩有质量压缩和尺寸压缩之分,选择则看你的具体需求了,Bitmap, BitmapFactory, BitmapFactory.Options, 这些是图片压缩相关类工具,思路就是这些了

PHP中文网

一般拿到图片bitmap,然后进行色彩度的压缩即质量压缩,关于bitmap的compress相关可以找到很多相关的Util,这个不难

阿神

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号