Beispiel #1
0
  /**
   * @see ImageWriter#writeImage(java.awt.image.RenderedImage, java.io.OutputStream,
   *     ImageWriterParams)
   */
  public void writeImage(RenderedImage image, OutputStream out, ImageWriterParams params)
      throws IOException {
    Iterator iter;
    iter = ImageIO.getImageWritersByMIMEType(getMIMEType());
    javax.imageio.ImageWriter iiowriter = null;
    try {
      iiowriter = (javax.imageio.ImageWriter) iter.next();
      if (iiowriter != null) {
        iiowriter.addIIOWriteWarningListener(this);

        ImageOutputStream imgout = null;
        try {
          imgout = ImageIO.createImageOutputStream(out);
          ImageWriteParam iwParam = getDefaultWriteParam(iiowriter, image, params);

          ImageTypeSpecifier type;
          if (iwParam.getDestinationType() != null) {
            type = iwParam.getDestinationType();
          } else {
            type = ImageTypeSpecifier.createFromRenderedImage(image);
          }

          // Handle metadata
          IIOMetadata meta = iiowriter.getDefaultImageMetadata(type, iwParam);
          // meta might be null for some JAI codecs as they don't support metadata
          if (params != null && meta != null) {
            meta = updateMetadata(meta, params);
          }

          // Write image
          iiowriter.setOutput(imgout);
          IIOImage iioimg = new IIOImage(image, null, meta);
          iiowriter.write(null, iioimg, iwParam);
        } finally {
          if (imgout != null) {
            System.err.println("closing");
            imgout.close();
          }
        }
      } else {
        throw new UnsupportedOperationException(
            "No ImageIO codec for writing " + getMIMEType() + " is available!");
      }
    } finally {
      if (iiowriter != null) {
        System.err.println("disposing");
        iiowriter.dispose();
      }
    }
  }
 public void initialize() {
   if (log.isTraceEnabled()) {
     String s = "Available JPEG image writers:";
     Iterator<ImageWriter> writers = ImageIO.getImageWritersByFormatName("jpeg");
     while (writers.hasNext()) {
       ImageWriter w = writers.next();
       s += "\n\t" + w.getClass().getName();
     }
     log.trace(s);
   }
   jpegImageWriter = ImageIO.getImageWritersByFormatName("jpeg").next();
   if (jpegImageWriter == null)
     throw new NullPointerException("Unable to create a JPEG image writer");
   jpegImageWriter.addIIOWriteWarningListener(ImageWriterWarningListener.INSTANCE);
   log.debug("Used JPEG image writer: " + jpegImageWriter.getClass().getName());
   iwp = jpegImageWriter.getDefaultWriteParam();
   iwp.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
   iwp.setCompressionQuality(jpegCompressionLevel);
 }