[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的更多相关文章

  1. Java Date Classes

    References: [1] http://tutorials.jenkov.com/java-date-time/index.html [2] https://docs.oracle.com/ja ...

  2. Top 15 Java Utility Classes

    In Java, a utility class is a class that defines a set of methods that perform common functions. Thi ...

  3. Java Inner Classes

    When thinking about inner classes in java, the first thing that comes to my mind is that, WHY do we ...

  4. Effective Java Chapter4 Classes and Interface

    MInimize the accessibility of classes and members 这个叫做所谓的 information hiding ,这么做在于让程序耦合度更低,增加程序的健壮性 ...

  5. Java Nested Classes(内部类~第一篇英文技术文档翻译)

    鄙人最近尝试着翻译了自己的第一篇英文技术文档.Java Nested Classes Reference From Oracle Documentation 目录 嵌套类-Nested Classes ...

  6. [Java面经]干货整理, Java面试题(覆盖Java基础,Java高级,JavaEE,数据库,设计模式等)

    如若转载请注明出处: http://www.cnblogs.com/wang-meng/p/5898837.html   谢谢.上一篇发了一个找工作的面经, 找工作不宜, 希望这一篇的内容能够帮助到大 ...

  7. Java多线程编程核心技术---Java多线程技能

    基本概念 进程是操作系统结构的基础,是一次程序的执行,是一个程序及其数据结构在处理机上顺序执行时所发生的活动,是程序在一个数据集合上运行的过程,是系统进行资源分配和调度的独立单位.线程可以理解成是在进 ...

  8. JAVA生成图片缩略图、JAVA截取图片局部内容

    package com.ares.image.test; import java.awt.Color; import java.awt.Graphics; import java.awt.Image; ...

  9. Java Main Differences between Java and C++

    转载自:http://www.cnblogs.com/springfor/p/4036739.html C++ supports pointers whereas Java does not. But ...

随机推荐

  1. H5 新增表单属性和事件

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  2. java 基本数据类型的自动拆箱与装箱

    ——>  -128~127之间的特殊性.为什么要这样设计,好处? ——>  享元模式(Flyweight Pattern):享元模式的特点是,复用我们内存中已存在的对象,降低系统创建对象实 ...

  3. java 实现类似spring的可配置的AOP框架

    一.工厂类BeanFactory: 1.工厂类BeanFactory负责创建目标类或代理类的实例对象,并通过配置文件实现切换. 2.getBean方法根据参数字符串返回一个相应的实例对象,如果参数字符 ...

  4. Python3使用过程中需要注意的点

    命名规则 变量 变量名只能是数字.字母或下划线的任意组合 变量名的第一个字符不能是数字 不能使用关键字作为变量名 变量的定义要具有可描述性 变量名不宜过长.不宜使用中文.拼音 常量(常用在配置文件中) ...

  5. 2018-9-14-win10-UWP-标题栏后退

    title author date CreateTime categories win10 UWP 标题栏后退 lindexi 2018-9-14 20:22:8 +0800 2018-2-13 17 ...

  6. hdu 2454 Degree Sequence of Graph G(可简单图化判定)

    传送门 •Havel-Hakimi定理: 给定一个非负整数序列{d1,d2,...dn},若存在一个无向图使得图中各点的度与此序列一一对应,则称此序列可图化. 进一步,若图为简单图,则称此序列可简单图 ...

  7. codeforces 1194F (组合数学)

    Codeforces 11194F (组合数学) 传送门:https://codeforces.com/contest/1194/problem/F 题意: 你有n个事件,你需要按照1~n的顺序完成这 ...

  8. 使用FluentEmail发送outlook邮件

    一,邮箱账号相关设置 1,创建outLook邮箱. 2,进入邮箱设置->同步电子邮件->允许设备和应用使用pop 3,设置microsoft账号的应用程序密码->进入安全性页面-&g ...

  9. Struts2注解详解(转)

    一,引入支持Struts2支持注解开发jar包: struts2-convention-plugin-2.1.8.1.jar(支持Struts2框架注解开发的jar包) 二,Struts2使用注解开发 ...

  10. 14.python函数

    原文:https://www.cnblogs.com/linhaifeng/articles/6113086.html 1.python中函数定义:函数是逻辑结构化和过程化的一种编程方法. pytho ...