是否正确替换Sun内部com.sun.image.codec.jpeg包的使用?

时间:2009-08-31 23:44:59

标签: java jpeg encode decode

我们有一些代码在使用这个旧的内部Sun软件包来处理图像,实际上在从输入流读取/解码后将JPEG编码为特定的大小和质量。代码示例如下。我将非常感谢使用适当的java。*或javax。* API的最佳实践替换示例。

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.....

}

2 个答案:

答案 0 :(得分:2)

我没有尝试过这种方法,但您可以查看Java Image I/O APIJPEGImageWriteParam class会特别感兴趣。

答案 1 :(得分:1)

我遇到了同样的问题。我做了这个测试,用于测量编码和使用JpegCodec,ImageIO(不带参数),ImageIO(带参数)写入文件的差异。

    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);

对于上面的代码,我的结果测试在ms下面。

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

我知道我不应该使用com.sun.image.codec.jpeg.JPEGCodec,但是由于这种结果,它使得使用ImageIO编码/编写jpg难以忍受。在我当前的项目中差异平均编码和写入非常好,我必须使用JPEGCODEC,或使用另一个外部库来达到同样的效果。