2

我正在尝试捕获当前场景的屏幕截图并将其保存为 png/jpg 和 pdf。保存屏幕截图时,这两个选项都成功,但是图像显示不正确。如图所示,屏幕截图非常糟糕,我似乎无法让它工作。由于某种原因保存时,图像也很可爱。

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));
 }

真的需要帮助解决这个问题。谢谢

当前状态:

在此处输入图像描述

期望状态:

在此处输入图像描述

4

2 回答 2

2

当我使用 ImageIO 将图像转换为 PNG 时效果很好

下面是我的实现代码

        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();
        }
于 2013-11-06T11:54:18.403 回答
0

Jpeg 图像是有损的,并且可以具有可变的压缩级别,因此您可能需要调整压缩。(PNG 图像是无损的,因此如果您将文件类型更改为 PNG 并且图像以所需的质量保存,这将证实我的怀疑)。

要调整 Jpeg 编码器的压缩级别,您应该能够遵循本指南; http://www.universalwebservices.net/web-programming-resources/java/adjust-jpeg-image-compression-quality-when-saving-images-in-java

从指南中,我希望您需要做的就是调用此代码;

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();

代替;

ImageIO.write(image, "jpg", fa);
于 2013-08-08T18:08:05.230 回答