为了账号安全,请及时绑定邮箱和手机立即绑定

如何在上传到服务器之前减小图像文件大小

如何在上传到服务器之前减小图像文件大小

梵蒂冈之花 2019-08-31 15:21:19
许多应用程序允许共享从图库中挑选的图像。他们上传原始图片文件吗?这是1-3 MB?或者他们处理?在任何情况下,我如何从文件路径中获取图像,通过降低分辨率来减小其大小,并将其保存在其他地方并尝试上传?我试过了:Bitmap photo = decodeSampledBitmapFromFile(filePath, DESIRED_WIDTH,                    DESIRED_HEIGHT);FileOutputStream out = new FileOutputStream(filePath);photo.compress(Bitmap.CompressFormat.JPEG, 100, out);public static Bitmap decodeSampledBitmapFromFile(String path, int reqWidth,        int reqHeight) {    final BitmapFactory.Options options = new BitmapFactory.Options();    options.inJustDecodeBounds = true;    BitmapFactory.decodeFile(path, options);    final int height = options.outHeight;    final int width = options.outWidth;    options.inPreferredConfig = Bitmap.Config.ARGB_8888;    int inSampleSize = 1;    if (height > reqHeight) {        inSampleSize = Math.round((float) height / (float) reqHeight);    }    int expectedWidth = width / inSampleSize;    if (expectedWidth > reqWidth) {        inSampleSize = Math.round((float) width / (float) reqWidth);    }    options.inSampleSize = inSampleSize;    options.inJustDecodeBounds = false;    return BitmapFactory.decodeFile(path, options);}但这是他们正确的方法吗?因为我在这里看到了答案compression operation takes rather big amount of time
查看完整描述

3 回答

?
凤凰求蛊

TA贡献1825条经验 获得超4个赞

我使用此功能在上传之前减小图像的大小,它将图像大小减小到接近200 KB并保持质量相对较好,您可以通过更改REQUIRED_SIZE和inSampleSize来修改它以实现您的目的:


public File saveBitmapToFile(File file){

    try {


        // BitmapFactory options to downsize the image

        BitmapFactory.Options o = new BitmapFactory.Options();

        o.inJustDecodeBounds = true;

        o.inSampleSize = 6;

        // factor of downsizing the image


        FileInputStream inputStream = new FileInputStream(file);

        //Bitmap selectedBitmap = null;

        BitmapFactory.decodeStream(inputStream, null, o);

        inputStream.close();


        // The new size we want to scale to

        final int REQUIRED_SIZE=75;


        // Find the correct scale value. It should be the power of 2.

        int scale = 1;

        while(o.outWidth / scale / 2 >= REQUIRED_SIZE &&

                        o.outHeight / scale / 2 >= REQUIRED_SIZE) {

            scale *= 2;

        }


        BitmapFactory.Options o2 = new BitmapFactory.Options();

        o2.inSampleSize = scale;

        inputStream = new FileInputStream(file);


        Bitmap selectedBitmap = BitmapFactory.decodeStream(inputStream, null, o2);

        inputStream.close();


        // here i override the original image file

        file.createNewFile();

        FileOutputStream outputStream = new FileOutputStream(file);


        selectedBitmap.compress(Bitmap.CompressFormat.JPEG, 100 , outputStream);


        return file;

    } catch (Exception e) {

        return null;

    }

}

注意:我在此函数中调整大小并覆盖原始文件图像,您也可以将其写入另一个文件。


我希望它可以帮助你。


查看完整回答
反对 回复 2019-08-31
?
GCT1015

TA贡献1827条经验 获得超4个赞

这是我的解决方案


/*

* This procedure will replace the original image

* So you need to do a tmp copy to send before reduce

*/

public static boolean reduceImage(String path, long maxSize) {

    File img = new File(path);

    boolean result = false;

    BitmapFactory.Options options = new BitmapFactory.Options();

    Bitmap bitmap = null;

    options.inSampleSize=1;

    while (img.length()>maxSize) {

        options.inSampleSize = options.inSampleSize+1;

        bitmap = BitmapFactory.decodeFile(path, options);

        img.delete();

        try

            {

                FileOutputStream fos = new FileOutputStream(path);

                img.compress(path.toLowerCase().endsWith("png")?

                                Bitmap.CompressFormat.PNG:

                                Bitmap.CompressFormat.JPEG, 100, fos);

                fos.close();

                result = true;

             }catch (Exception errVar) { 

                errVar.printStackTrace(); 

             }

    };

    return result;

}

编辑删除了其他程序调用


查看完整回答
反对 回复 2019-08-31
  • 3 回答
  • 0 关注
  • 487 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信