代码如下:

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();
}
}
}
}

  

最新文章

  1. jQuery中的$.extend方法来扩展JSON对象及合并,方便调用对象方法
  2. iOS dealloc 不被调用的问题
  3. Logging with Log4net (二)
  4. ASP.Net MVC3 图片上传详解(form.js,bootstrap)
  5. 2013-09-22 [随笔]-Roy
  6. Linux vi编辑器的基本命令
  7. 第三个Sprint冲刺第五天
  8. web.xml讲解
  9. Gdb远程调试Linux内核遇到的Bug
  10. vue2.0 网页标题更新实现思路
  11. redis的密码设置(windows与linux相同)
  12. Linux Mint chrome浏览器提示“需要安装adobe flash player”
  13. Servlet3.0上传
  14. springboot打war包后部署到tomcat后访问返回404错误
  15. 剑指offer:二叉树中和为某一值的路径
  16. 1、Appium安装
  17. 【转】Windows Live Writer 代码插件改造
  18. k-近邻算法(KNN)
  19. 每日英语:Political Gridlock, Beijing Style
  20. learning uboot switch to standby system using button

热门文章

  1. 共读《redis设计与实现》-数据结构篇
  2. Vim 中进行文本替换
  3. Kubernetes 的这些原理,你一定要了解
  4. 【DIY】【CSAPP-LAB】深入理解计算机系统--datalab笔记
  5. jQuery前端第三方框架
  6. 使用VUE+SpringBoot+EasyExcel 整合导入导出数据
  7. CSAPP 之 AttackLab 详解
  8. java_简单学习笔记
  9. 记 iTextSharp 提取中文的问题
  10. Ajax前后端交互——后端接收前端页面变量