/**
   * @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();
      }
    }
  }
Exemple #2
0
 public void compressJpegFile(
     File infile, File outfile, float compressionQuality, int widthCanvas, int heightCanvas)
     throws IOException {
   // Retrieve jpg image to be compressed
   BufferedImage rendImage = ImageIO.read(infile);
   widthFile = rendImage.getWidth();
   heightFile = rendImage.getHeight();
   double aspectRatioCanvas = widthCanvas / heightCanvas;
   double aspectRatioFile = widthFile / heightFile;
   widthFile = widthCanvas;
   heightFile = widthCanvas / aspectRatioFile;
   if (heightFile > heightCanvas) {
     heightFile = heightCanvas;
     widthFile = heightCanvas * aspectRatioFile;
   }
   this.width = (int) widthFile;
   this.height = (int) heightFile;
   ImageTypeSpecifier its = ImageTypeSpecifier.createFromRenderedImage(rendImage);
   BufferedImage outImage = its.createBufferedImage(width, height);
   Graphics2D graphics2D = outImage.createGraphics();
   graphics2D.setRenderingHint(
       RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
   //         graphics2D.drawRenderedImage(rendImage, new AffineTransform());
   graphics2D.drawImage(rendImage, 0, 0, width, height, null);
   // Find a jpeg writer
   ImageWriter writer = null;
   Iterator iter = ImageIO.getImageWritersByFormatName("jpg");
   if (iter.hasNext()) {
     writer = (ImageWriter) iter.next();
   }
   // Prepare output file
   ImageOutputStream ios = ImageIO.createImageOutputStream(outfile);
   writer.setOutput(ios);
   // Set the compression quality
   ImageWriteParam iwparam = new JPEGImageWriteParam(Locale.getDefault());
   iwparam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
   iwparam.setCompressionQuality(compressionQuality);
   // Write the image
   writer.write(null, new IIOImage(outImage, null, null), iwparam);
   // infile.delete();
   // Cleanup
   ios.flush();
   writer.dispose();
   ios.close();
   // return
 }
  public static List<File> createTiffFiles(List<IIOImage> imageList, int index, int dpiX, int dpiY)
      throws IOException {
    List<File> tiffFiles = new ArrayList<File>();

    // Set up the writeParam
    TIFFImageWriteParam tiffWriteParam = new TIFFImageWriteParam(Locale.US);
    tiffWriteParam.setCompressionMode(ImageWriteParam.MODE_DISABLED);

    // Get tif writer and set output to file
    Iterator<ImageWriter> writers = ImageIO.getImageWritersByFormatName(TIFF_FORMAT);
    ImageWriter writer = writers.next();

    if (writer == null) {
      throw new RuntimeException(
          "Need to install JAI Image I/O package.\nhttps://jai-imageio.dev.java.net");
    }

    // Get the stream metadata
    IIOMetadata streamMetadata = writer.getDefaultStreamMetadata(tiffWriteParam);

    // all if index == -1; otherwise, only index-th
    for (IIOImage oimage : (index == -1 ? imageList : imageList.subList(index, index + 1))) {
      if (dpiX != 0 && dpiY != 0) {
        // Get the default image metadata.
        ImageTypeSpecifier imageType =
            ImageTypeSpecifier.createFromRenderedImage(oimage.getRenderedImage());
        IIOMetadata imageMetadata = writer.getDefaultImageMetadata(imageType, null);
        imageMetadata = setDPIViaAPI(imageMetadata, dpiX, dpiY);
        oimage.setMetadata(imageMetadata);
      }

      File tiffFile = File.createTempFile(OUTPUT_FILE_NAME, TIFF_EXT);
      ImageOutputStream ios = ImageIO.createImageOutputStream(tiffFile);
      writer.setOutput(ios);
      writer.write(streamMetadata, oimage, tiffWriteParam);
      ios.close();
      tiffFiles.add(tiffFile);
    }
    writer.dispose();

    return tiffFiles;
  }
 /**
  * Returns <code>true</code> if the <code>ImageWriter</code> implementation associated with this
  * service provider is able to encode the given <code>RenderedImage</code> instance. Note that
  * this includes instances of <code>java.awt.image.BufferedImage</code>.
  *
  * <p>See the discussion for <code>canEncodeImage(ImageTypeSpecifier)</code> for information on
  * the semantics of this method.
  *
  * @param im an instance of <code>RenderedImage</code> to be encoded.
  * @return <code>true</code> if this writer is likely to be able to encode this image.
  * @exception IllegalArgumentException if <code>im</code> is <code>null</code>.
  */
 public boolean canEncodeImage(RenderedImage im) {
   return canEncodeImage(ImageTypeSpecifier.createFromRenderedImage(im));
 }