java 改变图片的DPI
2024-10-20 06:31:55
代码如下:
public class test01
{ private static int DPI = 300; public static void main(String[] args)
{
String path = "E:\\002.jpg";
File file = new File(path);
handleDpi(file, DPI, DPI);
} /**
* 改变图片DPI
* @param file
* @param xDensity 水平分辨率
* @param yDensity 垂直分辨率
*/
public static void handleDpi(File file, int xDensity, int yDensity)
{
FileOutputStream fileOutputStream = null;
try
{
BufferedImage image = ImageIO.read(file);
fileOutputStream = new FileOutputStream(file);
JPEGImageEncoder jpegEncoder = JPEGCodec.createJPEGEncoder(fileOutputStream);
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 (Exception e)
{
e.printStackTrace();
}
finally
{
//关流
try
{
if(fileOutputStream!=null)
{
fileOutputStream.close();
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
}
最新文章
- jQuery中的$.extend方法来扩展JSON对象及合并,方便调用对象方法
- iOS dealloc 不被调用的问题
- Logging with Log4net (二)
- ASP.Net MVC3 图片上传详解(form.js,bootstrap)
- 2013-09-22 [随笔]-Roy
- Linux vi编辑器的基本命令
- 第三个Sprint冲刺第五天
- web.xml讲解
- Gdb远程调试Linux内核遇到的Bug
- vue2.0 网页标题更新实现思路
- redis的密码设置(windows与linux相同)
- Linux Mint chrome浏览器提示“需要安装adobe flash player”
- Servlet3.0上传
- springboot打war包后部署到tomcat后访问返回404错误
- 剑指offer:二叉树中和为某一值的路径
- 1、Appium安装
- 【转】Windows Live Writer 代码插件改造
- k-近邻算法(KNN)
- 每日英语:Political Gridlock, Beijing Style
- learning uboot switch to standby system using button