Replacing the deprecated Java JPEG classes for Java 7

來源:互聯網
上載者:User

標籤:

 [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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.