您的位置:首页 > 编程语言 > Java开发

JAVA 图片压缩 20M->188K

2017-08-23 09:53 441 查看
说明:

本人为什么要用这个,由于服务器尚未搭建完成,图片暂时存于数据库,导致查询奇慢无比,故使用压缩图片。可以解决一部分问题

因为数据库的存取类型是BLOB  所以此方法是从byte[] 压缩到 byte[] 存入数据库 已实验19.6M的世界地图可以压缩至188k ,但是发现小图片貌似不会改变。

代码:压缩工具类

package com.hzcominfo.application.devcenter.core.web.controller.common.util;

import java.awt.Color;

import java.awt.Graphics;

import java.awt.Image;

import java.awt.image.BufferedImage;

import java.io.ByteArrayInputStream;

import java.io.ByteArrayOutputStream;

import java.io.IOException;

import java.io.InputStream;

import javax.imageio.ImageIO;

 

public class ImgTools {

 

    /**

     * 按照 宽高 比例压缩

     * @param b  byte[]

     * @param width

     * @param height

     * @param suf

     * @return  byte[]

     * @throws IOException

     */

    public static byte[] getImg(byte[] b, int width, int height,String suf) throws IOException {

        InputStream input = new ByteArrayInputStream(b);

        BufferedImage bi = ImageIO.read(input);

        double srcWidth = bi.getWidth(); // 源图宽度

        double srcHeight = bi.getHeight(); // 源图高度

 

        double scale = 1;

 

        if (width > 0) {

            scale = width / srcWidth;

        }

        if (height > 0) {

            scale = height / srcHeight;

        }

        if (width > 0 && height > 0) {

            scale = height / srcHeight < width / srcWidth ? height / srcHeight

                    : width / srcWidth;

        }

        

        int w = (int) (srcWidth * (scale >= 1 ? 0.9 : scale));

        int h = (int) (srcHeight * (scale >= 1 ? 0.9 : scale));

        Image image = bi.getScaledInstance(w, h, Image.SCALE_SMOOTH);

        BufferedImage result = new BufferedImage(w, h,BufferedImage.TYPE_INT_RGB);

        Graphics g = result.getGraphics();

        g.setColor(Color.RED);

        g.drawImage(image, 0, 0, null); // 绘制处理后的图

        g.dispose();

        ByteArrayOutputStream out = new ByteArrayOutputStream();

        try {

            ImageIO.write(result, suf, out);

        } catch (IOException e) {

            e.printStackTrace();

        }

        return out.toByteArray();

    }

}

具体使用:

 @ResponseBody

    @RequestMapping("importPic")

    @Transactional(readOnly = false)

    // 需要事务操作必须加入此注解

    @SystemLog(module = "服务中心", methods = "接口管理-导入接口申请审批凭证")

    // 凡需要处理业务逻辑的.都需要记录操作日志

    public String importPic(@RequestParam(value = "file", required = false) MultipartFile file,String ids) throws IOException {

        InterfaceApplyFormMap param = new InterfaceApplyFormMap();

        if (null != file) {

            String fileName =file.getOriginalFilename();

            String[] ss = fileName.split("\\.");

            param.put("certificate",ImgTools.getImg(file.getBytes(), 1024, 960,ss[1]));//第一个参数原始图片byte[]数组,第二个长,第三个宽,第四个目标图片的文件格式(jpg...)

            param.put("fileName",fileName);

        }

        String[] idss = ids.split(",");

        try {

            for (String iaid : idss) {

                param.put("id", iaid);

                applyMapper.interFileByPicFlag(param);

            }

            return AttrConstants.SUCCESS;

        } catch (Exception e) {

            return AttrConstants.FAIL;

        }

    }

    
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: