不改变图片尺寸压缩图片,减少页面图片加载时间。

    当做到上传图片的功能时,用户或许会上传一些超清的,很大的图片。那样显示到首页会耗费更多的时间来加载图片。这里写一篇关于保存图片尺寸并压缩图片的博客。不啰嗦了,直接上代码。

    首先将图片转为byte[ ]数组。

    这里是将图片转为byte[ ]数组的方法:

    //参数path为图片路径

  public static byte[] image2byte(String path) {
        byte[] data = null;
        FileImageInputStream input = null;
        try {
            input = new FileImageInputStream(new File(path));
            ByteArrayOutputStream output = new ByteArrayOutputStream();
            byte[] buf = new byte[1024];
            int numBytesRead = 0;
            while ((numBytesRead = input.read(buf)) != -1) {
                output.write(buf, 0, numBytesRead);
            }
            data = output.toByteArray();
            output.close();
            input.close();
        } catch (FileNotFoundException ex1) {
            ex1.printStackTrace();
        } catch (IOException ex1) {
            ex1.printStackTrace();
        }
        return data;
    }

    然后减少图片的分辨率来达到压缩图片的功能

    //参数imageByte为byte[ ]图片,可用上面的方法获取

    //参数quality为将图片压缩到什么程度,quality越大图片占空间越大,越清晰。quality越小图片越模糊,占空间越小。(0.001~0.999之间),具体合适的区间自己可以测试下。

public static byte[] compressPic(byte[] imageByte, float quality) {
        byte[] inByte = null;
        try {
            ByteArrayInputStream byteInput = new ByteArrayInputStream(imageByte);
            Image img = ImageIO.read(byteInput);
            float newWidth = img.getWidth(null);
            float newHeight = img.getHeight(null);

            Image image = img.getScaledInstance((int) newWidth, (int) newHeight, Image.SCALE_SMOOTH);

            // 缩放图像
            BufferedImage tag = new BufferedImage((int) newWidth, (int) newHeight, BufferedImage.TYPE_INT_RGB);
            Graphics2D g = tag.createGraphics();
            g.drawImage(image, 0, 0, null); // 绘制缩小后的图
            g.dispose();
            ByteArrayOutputStream out = new ByteArrayOutputStream(imageByte.length);
            JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
            JPEGEncodeParam jep = JPEGCodec.getDefaultJPEGEncodeParam(tag);
            /* 压缩质量 */
            jep.setQuality(quality, true);
            encoder.encode(tag, jep);
            inByte = out.toByteArray();
            out.close();

        } catch (IOException ex) {
            ex.printStackTrace();
        }
        return inByte;
    }

上述方法返回了一个压缩后的byte[ ]数组,这里将数组转回图片文件并保存。

//data参数为要转型的byte[ ]数组。

//path参数为转型后将图片保存到的位置,这里为路径。

public static void byte2image(byte[] data, String path) {
        if (data.length < 3 || path.equals(""))
            return;
        try {
            FileImageOutputStream imageOutput = new FileImageOutputStream(new File(path));
            imageOutput.write(data, 0, data.length);
            imageOutput.close();
            System.out.println("Make Picture success,Please find image in " + path);
        } catch (Exception ex) {
            System.out.println("Exception: " + ex);
            ex.printStackTrace();
        }
    }

    这样既可实现压缩功能,可以对比一下压缩前后所占空间。

180729_rKLx_3648730.png

    这里的quality貌似不是百分比压缩。

    目前此功能可能会繁琐些,我正在继续跟进学习,如有更好的方法会写到博客中,请继续跟进。若有更好的方法请留言给我。

转载于:https://my.oschina.net/ImagineMeWithoutYou/blog/1519500

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值