Migrating from JDK 1.5.0_06 to 1.8.0_66 - Replacing the deprecated Java JPEG classes -
i migrating application jdk 1.5.0_06 1.8.0_66. in application, there multiple files use com.sun.image.codec.jpeg classes. when try create jar i'm getting below error.
error: package com.sun.image.codec.jpeg not exist import com.sun.image.codec.jpeg.*;
i referred blog on replacing deprecated java jpeg classes java 7 , errors cleared. i'm getting warning below:
warning: jpegimagewriter internal proprietary api , may removed in future release import com.sun.imageio.plugins.jpeg.jpegimagewriter;
the problematic code blog:
public static void saveasjpeg(string jpgflag, bufferedimage image_to_save, float jpegcompression, fileoutputstream fos) throws ioexception { // 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(); } //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) { // new compression jpegimagewriteparam jpegparams = (jpegimagewriteparam) imagewriter.getdefaultwriteparam(); jpegparams.setcompressionmode(jpegimagewriteparam.mode_explicit); jpegparams.setcompressionquality(jpegcompression); } //new write , clean imagewriter.write(imagemetadata, new iioimage(image_to_save, null, null), null); ios.close(); imagewriter.dispose(); }
the problematic line in code is:
jpegimagewriter imagewriter = (jpegimagewriter) imageio.getimagewritersbysuffix("jpeg").next();
there's no need cast , usage of specific subclass (jpegimagewriter
). it's practice not rely on specific plugin subclasses. in case, use imagewriter
base class.
imagewriter imagewriter = imageio.getimagewritersbysuffix("jpeg").next();
everything else work , run before.
note: jpegimagewriteparam
class on other hand, is part of public api (javax.imageio.plugins.jpeg
package) , needed access jpeg specific writing options. usage ok.
Comments
Post a Comment