开发者社区> 问答> 正文

Android 从相册里面获取图片如何压缩上传服务器?

screenshot
我是从相册里面获取到图片,转换成File类型上传到服务器。
我现在想把图片压缩一下再上传,有没有哪位大神有个demo给我看下,或者教教我

展开
收起
蛮大人123 2016-03-09 18:46:32 4985 0
1 条回答
写回答
取消 提交回答
  • 我说我不帅他们就打我,还说我虚伪

    缩小图片尺寸:

    /**
     * 放大缩小图片(基本不会出现锯齿,比{@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(ios的safari就不支持),所以webp格式图片上传到服务器,ios是加载不出来的(当然可以加一下第三方库让ios加载webp)

    2019-07-17 18:56:36
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
58同城Android客户端Walle框架演进与实践之路 立即下载
Android组件化实现 立即下载
蚂蚁聚宝Android秒级编译——Freeline 立即下载