[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 Resoluti Mnl When ImageIO came along, the class is deprecated. This means, it is still in Java and not guaranteed to being in any later releases. ImageIO is more complicated to use for JPEG images and we felt the earlier code produced better results so we continued t o use it.
I have been checking we code against the new Java 7 (Release 4) build for the MAC and it now appears that the old JPEG cl Asses has finally been removed. So I has updated my code to use ImageIO. Here's my updated version with both, and new versions so 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 @ 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.jpe G.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_DOT S_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.setq Uality (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 is a definite progression over Java 6. What does you think of it?
Replacing the deprecated Java JPEG classes for Java 7