/** Make (scale down) the thumbnail */
  private Image makeThumbnail(Image imageOriginal) {

    double ratio = (double) imageOriginal.getWidth(null) / (double) imageOriginal.getHeight(null);
    int b = THUMBNAIL.width;
    int h = THUMBNAIL.height;
    if (ratio > 0) {
      b = (int) (h * ratio);
    } else {
      h = (int) (b * ratio);
    }

    if (b == 0 || h == 0) {
      System.out.println(
          "ERROR makeThumbnail: width = " + b + ", height = " + h + ", imageRation = " + ratio);
      System.out.println(
          "  Workaround: width = " + THUMBNAIL.width + ", height = " + THUMBNAIL.height);
      b = THUMBNAIL.width;
      h = THUMBNAIL.height;
    }

    return getScaledImage(imageOriginal, b, h);
  }
 private void writeToFile(File selectedFile, ImageWriterSpiFileFilter ff) {
   try {
     ImageOutputStream ios = ImageIO.createImageOutputStream(selectedFile);
     ImageWriter iw = ff.getImageWriterSpi().createWriterInstance();
     iw.setOutput(ios);
     ImageWriteParam iwp = iw.getDefaultWriteParam();
     if (iwp.canWriteCompressed()) {
       iwp.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
       // set maximum image quality
       iwp.setCompressionQuality(1.f);
     }
     Image image = viewerPanel.getImage();
     BufferedImage bufferedImage;
     if (viewerPanel.getImage() instanceof BufferedImage)
       bufferedImage = (BufferedImage) viewerPanel.getImage();
     else {
       bufferedImage =
           new BufferedImage(
               image.getWidth(null), image.getHeight(null), BufferedImage.TYPE_INT_RGB);
       bufferedImage.createGraphics().drawImage(image, 0, 0, null);
     }
     iw.write(null, new IIOImage(bufferedImage, null, null), iwp);
     iw.dispose();
     ios.close();
   } catch (IOException ioe) {
     JOptionPane.showMessageDialog(
         viewerPanel,
         messagesBundle.getString(
             "ImageViewerPanelSaveAction." + "Error_during_image_saving_message_7"),
         //$NON-NLS-1$
         messagesBundle.getString("ImageViewerPanelSaveAction." + "Error_dialog_title_8"),
         //$NON-NLS-1$
         JOptionPane.ERROR_MESSAGE);
     ioe.printStackTrace();
   }
 }
Пример #3
0
 /**
  * calls drawImage(Image,int,int,int,int,Color,ImageObserver)
  *
  * @see #drawImage(Image,int,int,int,int,Color,ImageObserver)
  */
 public boolean drawImage(Image img, int x, int y, Color bgcolor, ImageObserver observer) {
   return drawImage(img, x, y, img.getWidth(observer), img.getHeight(observer), bgcolor, observer);
 }