2

I'm trying to capture a screenshot of the current scene and save it as either a png/jpg and pdf. Both options are successful when it comes to saving the screenshot however, the image doesn't come out right. As the image shows, the screenshot is completely awful and I can't seem to make it work. The image is also cute off when saved for some reason.

public void sceneCapture() throws IOException, InterruptedException, Exception
{ 
    File fa = new File("test.jpg");
    snapshot = quotes.getScene().snapshot(null);
    RenderedImage renderedImage = SwingFXUtils.fromFXImage(snapshot, null);
    BufferedImage image = new BufferedImage(600, 750, BufferedImage.TYPE_INT_RGB); 
    image.setData(renderedImage.getData());
    ImageIO.write(image, "jpg", fa);


     int[] RGB_MASKS = {0xFF0000, 0xFF00, 0xFF};
     ColorModel RGB_OPAQUE = new DirectColorModel(32, RGB_MASKS[0], RGB_MASKS[1], RGB_MASKS[2]);

    java.awt.Image img = Toolkit.getDefaultToolkit().createImage("test.jpg");
    PixelGrabber pg = new PixelGrabber(img, 0, 0, -1, -1, true);
    pg.grabPixels();
    int width = pg.getWidth(), height = pg.getHeight();

    DataBuffer buffer = new DataBufferInt((int[]) pg.getPixels(), pg.getWidth() * pg.getHeight());
    WritableRaster raster = Raster.createPackedRaster(buffer, width, height, width, RGB_MASKS, null);
    BufferedImage bi = new BufferedImage(RGB_OPAQUE, raster, false, null);

    String to = "test.jpg";
    ImageIO.write(bi, "jpg", new File(to));
 }

Really need help with this problem. Thank you

Current State:

enter image description here

Desired State:

enter image description here

Morelka
  • 177
  • 4
  • 16
  • 1
    Can you attach images of what you e pect the snapshot to look like and what it actually looks like? – jewelsea Aug 08 '13 at 19:30
  • The buffered Image docs say for the method `setData` - "Sets a rectangular region of the image to the contents of the specified Raster r, which is assumed to be in the same coordinate space as the BufferedImage." Is it possible the sizes are different? SwingFXUtils.fromFXImage returns a buffered image, so skip the 2 lines after it and directly write output that image, see how that changes the output. http://docs.oracle.com/javase/1.4.2/docs/api/java/awt/image/BufferedImage.html#setData(java.awt.image.Raster) – Robadob Aug 10 '13 at 21:15

2 Answers2

2

It worked fine when I converted the Image to PNG using ImageIO

Below is the code of my implementation

        try {
            SnapshotParameters param = new SnapshotParameters();
            param.setDepthBuffer(true);
            param.setFill(Color.CORNSILK);
            WritableImage snapshot = node.snapshot(param, null);
            BufferedImage tempImg = SwingFXUtils.fromFXImage(snapshot, null);

            File outputfile = new File("e:/tempImg.png");

            ImageIO.write(tempImg, "png", outputfile);

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
Kris
  • 133
  • 5
0

Jpeg images are lossy and can have variable levels of compression so it is likely that you need to adjust the compression. (PNG images are lossless, so if you change the filetype to PNG and the image is saved at the desired quality this will confirm my suspicions).

To adjust the compression level of the Jpeg encoder you should be able to follow this guide; http://www.universalwebservices.net/web-programming-resources/java/adjust-jpeg-image-compression-quality-when-saving-images-in-java

From the guide I expect all you need do is call this code;

ImageWriter writer = (ImageWriter)ImageIO.getImageWritersByFormatName("jpeg").next();
ImageWriteParam iwp = writer.getDefaultWriteParam();
iwp.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
iwp.setCompressionQuality(1);   // a float between 0 and 1
// 1 specifies minimum compression and maximum quality
FileImageOutputStream output = new FileImageOutputStream(fa);
writer.setOutput(output);
IIOImage iioimage = new IIOImage(image, null, null);
writer.write(null, iioimage, iwp);
writer.dispose();

In the place of;

ImageIO.write(image, "jpg", fa);
Robadob
  • 5,319
  • 2
  • 23
  • 32
  • I've applied both the steps you've provided and the link and the image still doesn't look all that well. I'm not sure if it is the compression quality since it doesn't work and saving the image as a PNG format also does not remedy the situation. – Morelka Aug 08 '13 at 19:14
  • Hmm, perhaps you could draw the `BufferedImage` that you are saving to a JPanel, this will narrow down whether the quality loss is occurring prior to write or during write. – Robadob Aug 08 '13 at 19:32