Java 设置图片的DPI(com.sun.image.codec.jpeg does not exist)

图片DPI:图像每英寸长度内的像素点数。( density is dots per inch)

一开始使用jpeg code包编写,代码如下:

public static String setImgDpi(MultipartFile file, int xDensity, int yDensity) {
        try {
            BufferedImage image = ImageIO.read(file.getInputStream());
            ByteArrayOutputStream bo = new ByteArrayOutputStream();
            JPEGImageEncoder jpegEncoder = JPEGCodec.createJPEGEncoder(bo);
            JPEGEncodeParam jpegEncodeParam = jpegEncoder.getDefaultJPEGEncodeParam(image);
            jpegEncodeParam.setDensityUnit(JPEGEncodeParam.DENSITY_UNIT_DOTS_INCH);
            jpegEncoder.setJPEGEncodeParam(jpegEncodeParam);
            jpegEncodeParam.setQuality(0.75f, false);
            jpegEncodeParam.setXDensity(xDensity);
            jpegEncodeParam.setYDensity(yDensity);
            jpegEncoder.encode(image, jpegEncodeParam);
            return Base64.getEncoder().encodeToString(bo.toByteArray());
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

本地测试没问题,但是在发布时编译提示错误:package com.sun.image.codec.jpeg does not exist
度娘上解释说:因为版权原因,jdk1.7后将不会自动包含sun.*的类库,也不建议我们直接使用sun下的类库。
基于这个原因,我们可以手动的将需要的rt.jar导入到我们的项目中即可解决问题。
1.手动将rt.jar放到项目的lib下
2.maven项目可参考(注意windows和linux下的路径格式不一样)


    maven-compiler-plugin
    
        UTF-8
        
            ${java.home}/lib/rt.jar${path.separator}${java.home}/lib/jce.jar
        
    

但是既然已经不提倡使用sun,所以我没有采用上述方法解决问题,而是找了新的类(ImageIO)代替此功能的实现。

import org.w3c.dom.Element;
import javax.imageio.IIOImage;
import javax.imageio.ImageIO;
import javax.imageio.ImageTypeSpecifier;
import javax.imageio.ImageWriteParam;
import javax.imageio.ImageWriter;
import javax.imageio.metadata.IIOMetadata;
import javax.imageio.stream.ImageOutputStream;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Iterator;

public class ImageUtils {
     /**
     * 设置图片DPI 
     * @param image
     * @param dpi
     * @return
     * @throws IOException
     */
     public static byte[] setImgDpi(BufferedImage image, int dpi) throws IOException {
         for (Iterator iw = ImageIO.getImageWritersByFormatName("jpeg"); iw.hasNext();) {
             ImageWriter writer = iw.next();
             ImageWriteParam writeParams = writer.getDefaultWriteParam();
             writeParams.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
             //调整图片质量
             writeParams.setCompressionQuality(1f);
             IIOMetadata data = writer.getDefaultImageMetadata(new ImageTypeSpecifier(image), writeParams);
             Element tree = (Element) data.getAsTree("javax_imageio_jpeg_image_1.0");
             Element jfif = (Element) tree.getElementsByTagName("app0JFIF").item(0);
             //设置图片dpi
             jfif.setAttribute("resUnits", "1"); // density is dots per inch
             jfif.setAttribute("Xdensity", dpi + "");
             jfif.setAttribute("Ydensity", dpi + "");
             
             ByteArrayOutputStream out = new ByteArrayOutputStream();
             ImageOutputStream stream = null;
         try {
             stream = ImageIO.createImageOutputStream(out);
             writer.setOutput(stream);
             writer.write(data, new IIOImage(image, null, null), writeParams);
         } finally {
            stream.close();
         }
            return out.toByteArray();
         } 
         return null;
     }
}

这里只实现了jpg格式的,png格式的可以参考:
https://www.bbsmax.com/A/Ae5R...

你可能感兴趣的:(java)