Peter
Peter

Reputation: 29857

Proper replacement for usage of the Sun internal com.sun.image.codec.jpeg package?

We have some code kicking around that uses this old internal Sun package for manipulating images, essentially encoding JPEGs to a specific size and quality after reading in / decoding from an inputstream. Code examples below. I would appreciate a best practice replacement example using proper java.* or javax.* APIs.

private void encodeJPEG(BufferedImage bi, BufferedOutputStream out, float quality) throws
  ImageFormatException, IOException {
 JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
 JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(bi);
 param.setQuality(quality, false);
 param.setDensityUnit(DENSITY_UNIT);
 param.setXDensity(DENSITY);
 param.setYDensity(DENSITY);
 encoder.setJPEGEncodeParam(param);
 encoder.encode(bi);
}


private void initJPEG(File jpegFile) throws FileNotFoundException, IOException,
  IOException {
FileInputStream inputStream = new FileInputStream(jpegFile);
try {
  JPEGImageDecoder decoder = JPEGCodec.createJPEGDecoder(inputStream);
  buffImage = decoder.decodeAsBufferedImage();
}
.....error handling.....

}

Upvotes: 4

Views: 4191

Answers (2)

sja
sja

Reputation: 81

I had the same problem, sort of.I did this tests , for measuring the differences in encoding and write to file with JpegCodec,ImageIO(without parameters),ImageIO(with parameters).

    int numTest = 200;
    Robot robot = new Robot();
    BufferedImage image = robot.createScreenCapture(new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()));

    //JPEGCODEC
    long t1 = System.currentTimeMillis();
    ByteArrayOutputStream ba = new ByteArrayOutputStream();
    JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(ba);
    JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(image);
    param.setQuality(0.75f, false);
    encoder.setJPEGEncodeParam(param);

    File f = new File("TestJPEGCODEC");
    f.mkdir();
    for (int i = 0; i < numTest; i++) {
        encoder.encode(image);
        f = new File("TestJPEGCODEC\\test" + i + ".jpg");
        FileOutputStream fo = new FileOutputStream(f);
        fo.write(ba.toByteArray());
        fo.flush();
        ba.reset();

    }
    long t2 = System.currentTimeMillis();
    System.out.println("JPEGCODEC");
    System.out.println("Total time:: " + (t2 - t1) + " average time:: " + (t2 - t1) / numTest);

    //NORMAL IMAGEIO
    t1 = System.currentTimeMillis();
    f = new File("TestImageIO");
    f.mkdir();
    for (int i = 0; i < numTest; i++) {
        f = new File("TestImageIO\\test" + i + ".jpg");
        ImageIO.write(image, "jpg", f);
    }
    t2 = System.currentTimeMillis();
    System.out.println("ImageIO");
    System.out.println("Total time:: " + (t2 - t1) + " average time:: " + (t2 - t1) / numTest);

    Iterator<ImageWriter> it = ImageIO.getImageWritersByFormatName("jpg");
    ImageWriter writer = null;
    while (it.hasNext()) {
        writer = it.next();
    }
    //IMAGEIO EXPLICIT MODE
    t1 = System.currentTimeMillis();
    ImageWriteParam par = writer.getDefaultWriteParam();
    par.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
    par.setCompressionQuality(0.75f);

    f = new File("TestImageIOExplicity");
    f.mkdir();
    for (int i = 0; i < numTest; i++) {
        f = new File("TestImageIOExplicity\\test" + i + ".jpg");
        FileImageOutputStream output = new FileImageOutputStream(f);
        writer.setOutput(output);
        IIOImage img = new IIOImage(image, null, null);
        writer.write(null, img, par);
        output.close();
    }

    t2 = System.currentTimeMillis();
    writer.dispose();
    System.out.println("IMAGEIOPAR");
    System.out.println("Total time:: " + (t2 - t1) + " average time:: " + (t2 - t1) / numTest);

For the code above my result test are below in ms.

JPEGCODEC 
Total time:: 13750 average time:: 68
ImageIO
Total time:: 38906 average time:: 194
IMAGEIOPAR
Total time:: 43078 average time:: 215

I know that i should´t use the com.sun.image.codec.jpeg.JPEGCodec , but with this kind of results, it makes the use of ImageIO for encoding/writing jpg unbearable.In my current project the difference in average encoding and writing is so great that i must use JPEGCODEC, or use another external library for the same effect.

Upvotes: 1

Vineet Reynolds
Vineet Reynolds

Reputation: 76709

I haven't tried this approach, but you can take a look the Java Image I/O API. The JPEGImageWriteParam class would be of particular interest.

Upvotes: 2

Related Questions