Replacing the deprecated Java JPEG classes for Java 7
[src: https://blog.idrsolutions.com/2012/05/replacing-the-deprecated-java-jpeg-classes-for-java-7/]
In the early days of Java, Sun produced a really handy set of classes to handle JPEG images. These included some really nifty little features like the ability to easily set the amount of compression and the resolution. When ImageIO came along, the class was deprecated. This means that it is still in Java but not guaranteed to be in any later releases. ImageIO was more complicated to use for JPEG images and we felt the earlier code produced better results so we continued to use it.
I have been checking our code against the new Java 7 (release 4) build for the Mac and it now appears that the old JPEG classes have finally been removed. So I have updated my code to use ImageIO. Here is my updated version with both old and new versions so you can see the changes if you are still using these classes.
public static void saveAsJPEG(String jpgFlag,BufferedImage image_to_save, float JPEGcompression, FileOutputStream fos) throws IOException {
//useful documentation at http://docs.oracle.com/javase/7/docs/api/javax/imageio/metadata/doc-files/jpeg_metadata.html
//useful example program at http://johnbokma.com/java/obtaining-image-metadata.html to output JPEG data
//old jpeg class
//com.sun.image.codec.jpeg.JPEGImageEncoder jpegEncoder = com.sun.image.codec.jpeg.JPEGCodec.createJPEGEncoder(fos);
//com.sun.image.codec.jpeg.JPEGEncodeParam jpegEncodeParam = jpegEncoder.getDefaultJPEGEncodeParam(image_to_save);
// Image writer
JPEGImageWriter imageWriter = (JPEGImageWriter) ImageIO.getImageWritersBySuffix(“jpeg”).next();
ImageOutputStream ios = ImageIO.createImageOutputStream(fos);
imageWriter.setOutput(ios);
//and metadata
IIOMetadata imageMetaData = imageWriter.getDefaultImageMetadata(new ImageTypeSpecifier(image_to_save), null);
if (jpgFlag != null){
int dpi = 96;
try {
dpi = Integer.parseInt(jpgFlag);
} catch (Exception e) {
e.printStackTrace();
}
//old metadata
//jpegEncodeParam.setDensityUnit(com.sun.image.codec.jpeg.JPEGEncodeParam.DENSITY_UNIT_DOTS_INCH);
//jpegEncodeParam.setXDensity(dpi);
//jpegEncodeParam.setYDensity(dpi);
//new metadata
Element tree = (Element) imageMetaData.getAsTree(“javax_imageio_jpeg_image_1.0?);
Element jfif = (Element)tree.getElementsByTagName(“app0JFIF”).item(0);
jfif.setAttribute(“Xdensity”, Integer.toString(dpi));
jfif.setAttribute(“Ydensity”, Integer.toString(dpi));
}
if(JPEGcompression>=0 && JPEGcompression<=1f){
//old compression
//jpegEncodeParam.setQuality(JPEGcompression,false);
// new Compression
JPEGImageWriteParam jpegParams = (JPEGImageWriteParam) imageWriter.getDefaultWriteParam();
jpegParams.setCompressionMode(JPEGImageWriteParam.MODE_EXPLICIT);
jpegParams.setCompressionQuality(JPEGcompression);
}
//old write and clean
//jpegEncoder.encode(image_to_save, jpegEncodeParam);
//new Write and clean up
imageWriter.write(imageMetaData, new IIOImage(image_to_save, null, null), null);
ios.close();
imageWriter.dispose();
}
Otherwise, Java 7 looks to be a definite progression over Java 6. What do you think of it?
Replacing the deprecated Java JPEG classes for Java 7的更多相关文章
- Java Date Classes
References: [1] http://tutorials.jenkov.com/java-date-time/index.html [2] https://docs.oracle.com/ja ...
- Top 15 Java Utility Classes
In Java, a utility class is a class that defines a set of methods that perform common functions. Thi ...
- Java Inner Classes
When thinking about inner classes in java, the first thing that comes to my mind is that, WHY do we ...
- Effective Java Chapter4 Classes and Interface
MInimize the accessibility of classes and members 这个叫做所谓的 information hiding ,这么做在于让程序耦合度更低,增加程序的健壮性 ...
- Java Nested Classes(内部类~第一篇英文技术文档翻译)
鄙人最近尝试着翻译了自己的第一篇英文技术文档.Java Nested Classes Reference From Oracle Documentation 目录 嵌套类-Nested Classes ...
- [Java面经]干货整理, Java面试题(覆盖Java基础,Java高级,JavaEE,数据库,设计模式等)
如若转载请注明出处: http://www.cnblogs.com/wang-meng/p/5898837.html 谢谢.上一篇发了一个找工作的面经, 找工作不宜, 希望这一篇的内容能够帮助到大 ...
- Java多线程编程核心技术---Java多线程技能
基本概念 进程是操作系统结构的基础,是一次程序的执行,是一个程序及其数据结构在处理机上顺序执行时所发生的活动,是程序在一个数据集合上运行的过程,是系统进行资源分配和调度的独立单位.线程可以理解成是在进 ...
- JAVA生成图片缩略图、JAVA截取图片局部内容
package com.ares.image.test; import java.awt.Color; import java.awt.Graphics; import java.awt.Image; ...
- Java Main Differences between Java and C++
转载自:http://www.cnblogs.com/springfor/p/4036739.html C++ supports pointers whereas Java does not. But ...
随机推荐
- CodeForces 1216C(假的计算几何+扫描线)
传送门 •题意 给你三个矩形,依次编号为 1,2,3: 判断 矩形1 是否被 矩形2 和 矩形3 完全覆盖: 如果没有完全覆盖,输出 "YES",反之,输出 "NO&qu ...
- P1099 双连击
题目描述 我们假设一个二位整数 \(N(10 \le N \le 99)\) ,它的十位上的数字是 \(A\) ,个位上的数字是 \(B\) ,如果 \(A\) 和 \(B\) 的比例关系满足 \(A ...
- H3C RIPv2配置任务
- 11584 - Partitioning by Palindromes——[DP]
题意分析: 题目比较容易理解,以d[i]表示前i个字符的最优解,状态转移方程 d[i]=min{d[j]+1| [j+1~i]为回文串} 代码如下: #include <cstdio> # ...
- 解决 el-autocomplete 不显示及没数据时闪一下的问题
项目中用到了elementUI中的远程搜索即 el-autocomplete 组件,估计首次使用的都会遇到一些小问题,只要你能认真看完并且耐心理解,保证能帮到你,效果图如下: 组件代码: <el ...
- 2018-9-30-C#-使用外部别名
title author date CreateTime categories C# 使用外部别名 lindexi 2018-09-30 18:37:23 +0800 2018-07-02 14:31 ...
- LuoguP3521 [POI2011]ROT-Tree Rotations
P3521 [POI2011]ROT-Tree Rotations 题目大意: 给一棵\((1≤n≤200000)\)个叶子的二叉树,可以交换每个点的左右子树,要求前序遍历叶子的逆序对最少. 我们发现 ...
- 牛客多校第3场 J 思维+树状数组+二分
牛客多校第3场 J 思维+树状数组+二分 传送门:https://ac.nowcoder.com/acm/contest/883/J 题意: 给你q个询问,和一个队列容量f 询问有两种操作: 0.访问 ...
- WCF 服务应用程序
1. 创建 WCF 服务程序和客户端程序,参考如下: https://docs.microsoft.com/zh-cn/dotnet/framework/wcf/getting-started-tut ...
- Vsual Studio 2010可用的sqlite驱动程序(实体数据模型使用)
背景 昨天一个旧的项目(.net framework 4 + EF4 +sqlite + edmx db first),数据库结构有变更,要更新实体edmx模型 先是到官网下载最新的驱动,结果不能更新 ...