@Test
  public void lossyWrite() throws Exception {
    File f = File.createTempFile("test-jpeg2000-lossy", ".jp2");
    f.deleteOnExit();
    Iterator<ImageWriter> writers = ImageIO.getImageWritersBySuffix("jp2");
    assertTrue(writers.hasNext());
    ImageWriter writer = writers.next();
    J2KImageWriteParam writeParams = (J2KImageWriteParam) writer.getDefaultWriteParam();
    writeParams.setLossless(false);
    writeParams.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
    writeParams.setCompressionType("JPEG2000");
    //        writeParams.setFilter(J2KImageWriteParam.FILTER_97);

    writeParams.setCompressionQuality(0.0f);
    writeParams.setEncodingRate(0.5f);
    ImageOutputStream ios = ImageIO.createImageOutputStream(f);
    writer.setOutput(ios);
    writer.write(null, new IIOImage(image, null, null), writeParams);
    writer.dispose();
    ios.close();
    assertTrue("Expected file size < 1MB", f.length() < 128 * 1024);
    // System.out.println(f.length());
    BufferedImage read = ImageIO.read(f);
    assertEquals(SIZE, read.getWidth());
  }
예제 #2
0
 /** Remember to dispose of the ImageWriter when you're finished with it. */
 private static ImageWriter getPngImageWriter() {
   Iterator<ImageWriter> writers = ImageIO.getImageWritersBySuffix("png");
   if (writers.hasNext()) {
     return (writers.next());
   } else {
     throw (new IllegalStateException("Missing png Image Writer"));
   }
 }
 private static void write(File f, boolean raw, BufferedImage image) throws IOException {
   Iterator<ImageWriter> writers = ImageIO.getImageWritersBySuffix("ppm");
   assertTrue(writers.hasNext());
   ImageWriter writer = writers.next();
   PNMImageWriteParam params = (PNMImageWriteParam) writer.getDefaultWriteParam();
   params.setRaw(raw);
   ImageOutputStream ios = ImageIO.createImageOutputStream(f);
   writer.setOutput(ios);
   writer.write(null, new IIOImage(image, null, null), params);
   writer.dispose();
   ios.close();
 }
예제 #4
0
 public static boolean writeImageMetadata(IIOMetadata imageMetadata, File target)
     throws IOException {
   Iterator writers =
       ImageIO.getImageWritersBySuffix(StringHelper.getFileExtension(target.getName()));
   if (!writers.hasNext()) {
     return false;
   } else {
     ImageWriter writer = (ImageWriter) writers.next();
     writer.setOutput(new FileImageOutputStream(target));
     BufferedImage image = ImageIO.read(target);
     writer.write(null, new IIOImage(image, null, imageMetadata), null);
     return true;
   }
 }
  /**
   * Takes a screenshot and saves the image to disk. This method will attempt to encode the image
   * according to the file extension of the given output file. If this is not possible (because the
   * encoding type is not supported), then the default encoding type will be used. If the default
   * encoding type is used, an appropriate extension will be added to the filename.
   *
   * @param captureRect Rect to capture in screen coordinates.
   * @param scaleFactor Degree to which the image should be scaled, in percent. A <code>scaleFactor
   *     </code> of <code>100</code> produces an unscaled image. This value must be greater than
   *     <code>0</code> and less than or equal to <code>200</code>.
   * @param outputFile Path and filename for the created image.
   */
  public void takeScreenshot(Rectangle captureRect, double scaleFactor, File outputFile) {
    // Create screenshot
    java.awt.Robot robot;
    File out = outputFile;

    try {
      robot = new java.awt.Robot();
      BufferedImage image = robot.createScreenCapture(captureRect);

      int scaledWidth = (int) Math.floor(image.getWidth() * scaleFactor);
      int scaledHeight = (int) Math.floor(image.getHeight() * scaleFactor);
      BufferedImage imageOut =
          new BufferedImage(scaledWidth, scaledHeight, BufferedImage.TYPE_INT_RGB);
      // Scale it to the new size on-the-fly
      Graphics2D graphics2D = imageOut.createGraphics();
      graphics2D.setRenderingHint(
          RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
      graphics2D.drawImage(image, 0, 0, scaledWidth, scaledHeight, null);

      // Save captured image using given format, if supported.
      String extension = getExtension(out.getName());
      if (extension.length() == 0
          || !ImageIO.getImageWritersBySuffix(extension).hasNext()
          || !ImageIO.write(imageOut, extension, out)) {

        // Otherwise, save using default format
        out = new File(outputFile.getPath() + EXTENSION_SEPARATOR + DEFAULT_IMAGE_FORMAT);
        if (!ImageIO.write(imageOut, DEFAULT_IMAGE_FORMAT, out)) {

          // This should never happen, so log as error if it does.
          // In this situation, the screenshot will not be saved, but
          // the test step will still be marked as successful.
          log.error(
              "Screenshot could not be saved. "
                  + //$NON-NLS-1$
                  "Default image format ("
                  + DEFAULT_IMAGE_FORMAT //$NON-NLS-1$
                  + ") is not supported."); //$NON-NLS-1$
        }
      }

    } catch (AWTException e) {
      throw new RobotException(e);
    } catch (IOException e) {
      throw new StepExecutionException(
          "Screenshot could not be saved", //$NON-NLS-1$
          EventFactory.createActionError(TestErrorEvent.FILE_IO_ERROR));
    }
  }
  @SuppressWarnings("unused")
  private boolean saveJpeg(int[] byteArray, int width, int height, int dpi, String file) {
    BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

    WritableRaster wr = bufferedImage.getRaster();
    wr.setPixels(0, 0, width, height, byteArray);

    try {
      // Image writer
      JPEGImageWriter imageWriter =
          (JPEGImageWriter) ImageIO.getImageWritersBySuffix("jpeg").next();
      ImageOutputStream ios = ImageIO.createImageOutputStream(new File(file));
      imageWriter.setOutput(ios);

      // Compression
      JPEGImageWriteParam jpegParams = (JPEGImageWriteParam) imageWriter.getDefaultWriteParam();
      jpegParams.setCompressionMode(JPEGImageWriteParam.MODE_EXPLICIT);
      jpegParams.setCompressionQuality(0.85f);

      // Metadata (dpi)
      IIOMetadata data =
          imageWriter.getDefaultImageMetadata(new ImageTypeSpecifier(bufferedImage), jpegParams);
      Element tree = (Element) data.getAsTree("javax_imageio_jpeg_image_1.0");
      Element jfif = (Element) tree.getElementsByTagName("app0JFIF").item(0);
      jfif.setAttribute("Xdensity", Integer.toString(dpi));
      jfif.setAttribute("Ydensity", Integer.toString(dpi));
      jfif.setAttribute("resUnits", "1"); // density is dots per inch

      // Write and clean up
      imageWriter.write(data, new IIOImage(bufferedImage, null, null), jpegParams);
      ios.close();
      imageWriter.dispose();
    } catch (Exception e) {
      return false;
    }

    return true;
  }
예제 #7
0
  /**
   * Write the image to a file in uncompressed tiff format.
   *
   * @param image image to write
   * @param file path and file name (extension will be ignored and changed to tiff.
   */
  public static void writeUncompressedImage(BufferedImage image, String file) throws IOException {
    FileImageOutputStream out = null;
    try {
      final File parentFile = new File(file).getParentFile();
      Iterator<ImageWriter> writers = ImageIO.getImageWritersBySuffix("tiff");
      final ImageWriter next = writers.next();

      final ImageWriteParam param = next.getDefaultWriteParam();
      param.setCompressionMode(ImageWriteParam.MODE_DISABLED);

      final File outputFile = new File(parentFile, Files.getNameWithoutExtension(file) + ".tiff");

      out = new FileImageOutputStream(outputFile);
      next.setOutput(out);
      next.write(image);
    } catch (Throwable e) {
      System.err.println("Error writing the image generated by the test:" + file + "\n\t");
      e.printStackTrace();
    } finally {
      if (out != null) {
        out.close();
      }
    }
  }