使用java修改图片DPI
修改以后可以直接用PS打开看效果
全部使用rt下的类,无需下载其他jar包
import com.sun.image.codec.jpeg.JPEGCodec; import com.sun.image.codec.jpeg.JPEGEncodeParam; import com.sun.image.codec.jpeg.JPEGImageEncoder; import javax.imageio.ImageIO; import java.awt.image.BufferedImage; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; public class DPIHandleHelper { private static int DPI = 300; public static void main(String[] args) { String path = "C:\\test.jpg"; File file = new File(path); handleDpi(file, 300, 300); } /** * 改变图片DPI * * @param file * @param xDensity * @param yDensity */ public static void handleDpi(File file, int xDensity, int yDensity) { try { BufferedImage image = ImageIO.read(file); JPEGImageEncoder jpegEncoder = JPEGCodec.createJPEGEncoder(new FileOutputStream(file)); 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); image.flush(); } catch (IOException e) { e.printStackTrace(); } } }
相关推荐
89236831 2020-01-08
纯屌丝程序猿 2014-04-09
scdn 2015-06-12
87286336 2017-04-04
88266839 2017-01-09
wanveedn 2016-08-18
88226330 2015-11-01
85286135 2015-10-15
88296231 2015-08-16
87216131 2015-08-05
赵耀 2015-08-04
迈克揉索芙特 2015-05-03
iseeip 2014-07-05
mahara 2016-08-18
Palingenesis 2019-06-27
ljbblog 2015-12-01
scdn 2015-06-12
sweetga 2012-12-31
maimu 2017-08-18