【问题标题】:Resize image before sending to server在发送到服务器之前调整图像大小
【发布时间】:2016-04-27 05:19:56
【问题描述】:

我的设备上有一张非常大的图像(不是用设备相机拍摄的)我需要在将图像发送到服务器之前调整其大小。所以说它最初是 14mb,我想把它减少到 2mb。我想在不损失质量的情况下调整图像大小。我的意思是服务器将允许放大照片。所以我认为 inDensity 很重要。除了我不明白 inDensity 在这方面是如何工作的。有人能解释一下我如何将照片调整为 2mb,但保持如此高的密度以使图像可以高质量地缩放吗?或者那是不可能的。

我已经知道如何调整图片大小:

public static Bitmap resizeImage(String file, int reqHeight, int reqWidth) {
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(file, options);

    options.inSampleSize = calculateInSampleSize(options, reqHeight, reqWidth);
    options.inJustDecodeBounds = false;
    return BitmapFactory.decodeFile(file, options);
}

【问题讨论】:

    标签: android image bitmap network-programming


    【解决方案1】:

    在你的代码中试试这个:

    Bitmap bmp = BitmapFactory.decodeFile(myImage)
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    bmp.compress(CompressFormat.JPEG, 70, bos);
    
    InputStream in = new ByteArrayInputStream(bos.toByteArray());
    
    ContentBody image = new InputStreamBody(in, "image/jpeg", "filename");
    

    它对我有用!

    【讨论】:

    • 谢谢。它有什么作用?你能解释为什么它有效吗?
    • 方法 compress 接受下一个变量: compress(Bitmap.CompressFormat format, int quality, OutputStream stream) 一旦你有 bmp 压缩,你可以附加到请求的内容正文中。在您的情况下,这不是必需的,因为您只需要 bmp。
    猜你喜欢
    • 2013-08-05
    • 1970-01-01
    • 2019-04-04
    • 2017-02-26
    • 1970-01-01
    • 1970-01-01
    • 2015-09-22
    • 2013-06-17
    • 1970-01-01
    相关资源
    最近更新 更多