java 压缩图片,文字不失真自定义宽高

/**设置默认值*/
public static void  zoomImg(ZoomImage dto)   {
    try {
        //默认A4纸高宽
        if(ObjectUtils.isEmpty(dto.getHeight())){
            dto.setHeight(1300);
        }
        /**if(ObjectUtils.isEmpty(dto.getWidth())){
         dto.setWidth(1000);
         }
         if(ObjectUtils.isEmpty(dto.getRatio())){
         dto.setRatio(Float.valueOf("1"));
         }
         if(ObjectUtils.isEmpty(dto.getDpi())){
         dto.setDpi(3000);
         }*/
        if(ObjectUtils.isEmpty(dto.getZoom())){
            dto.setZoom(false);
        }
        /**A4纸的尺寸的图像的像素是1240×1754。----这里设置高度1300不限制宽度*/
        Image src  =  ImageIO.read(new File(dto.getInput()));
        if(src.getHeight(null)>1300){
            File infile=new File(dto.getInput());
            File outfile=new File(dto.getOutPut());
            log.info("压缩图片参数{}", JSON.toJSONString(dto));
            reSize(infile,outfile,src.getWidth(null),dto.getHeight(),dto.getZoom());
            log.info("压缩图片成功{}",JSON.toJSONString(dto));
        }else{
            log.info("没压缩");
        }

    }catch (Exception e){
        log.error("压缩图片失败{}{}", JSON.toJSONString(dto), e.toString());
    }
}

/**
 * @param srcImg     原图片
 * @param destImg    目标位置
 * @param width      期望宽
 * @param height     期望高
 * @param equalScale 是否等比例缩放
 */
public static void reSize(File srcImg, File destImg, int width,
                          int height, boolean equalScale) {
    String type = getImageType(srcImg);
    if (type == null) {
        return;
    }
    switch (type) {
        case "jpg":
        case "png":
        case "JPG":
        case "jpeg":
            break;
        default:
            return;
    }
    if (width < 0 || height < 0) {
        return;
    }
    BufferedImage srcImage;
    try {
        srcImage = ImageIO.read(srcImg);
        if (srcImage.getWidth() < 1000 && srcImage.getHeight() < 1000) {
            return;
        }
        System.out.println("srcImg size=" + srcImage.getWidth() + "X" + srcImage.getHeight());
    } catch (IOException e) {
        log.error("压缩图片失败{}",e.toString());
        return;
    }
    // targetW,targetH分别表示目标长和宽
    BufferedImage target;
    /**width = (int) (0.3 * srcImage.getWidth());
     height = (int) (0.3 * srcImage.getHeight());
     if (width < 1000) {
     width = 1000;
     }
     if (height < 1000) {
     height = 1000;
     }*/
    double sx = (double) width / srcImage.getWidth();
    double sy = (double) height / srcImage.getHeight();
    // 等比缩放
    if (equalScale) {
        if (sx > sy) {
            sx = sy;
            width = (int) (sx * srcImage.getWidth());
        } else {
            sy = sx;
            height = (int) (sy * srcImage.getHeight());
        }
    }
    System.out.println("destImg size=" + width + "X" + height);
    ColorModel cm = srcImage.getColorModel();
    WritableRaster raster = cm.createCompatibleWritableRaster(width, height);
    boolean alphaPremultiplied = cm.isAlphaPremultiplied();

    target = new BufferedImage(cm, raster, alphaPremultiplied, null);
    Graphics2D g = target.createGraphics();
    // smoother than exlax:
    g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
    g.drawRenderedImage(srcImage, AffineTransform.getScaleInstance(sx, sy));
    g.dispose();
    // 将转换后的图片保存
    try {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ImageIO.write(target, type, baos);
        FileOutputStream fos = new FileOutputStream(destImg);
        fos.write(baos.toByteArray());
        fos.flush();
        fos.close();
    } catch (IOException e) {
        log.error("压缩图片失败{}",e.toString());
    }
}

/**
 * 获取文件后缀不带.
 *
 * @param file 文件后缀名
 */
private static String getImageType(File file) {
    if (file != null && file.exists() && file.isFile()) {
        String fileName = file.getName();
        int index = fileName.lastIndexOf(".");
        if (index != -1 && index < fileName.length() - 1) {
            return fileName.substring(index + 1);
        }
    }
    return null;
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

rose_marry

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值