可怕的截图JavaFx [英] Terrible Screenshot JavaFx

查看:1264
本文介绍了可怕的截图JavaFx的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

当前状态:

所需状态:

推荐答案

使用ImageIO将图像转换为PNG时工作正常

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

以下是我的实现代码

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

这篇关于可怕的截图JavaFx的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆