有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java在JCEF浏览器中获取网页截图

问题: 我正在一个java项目中使用JCEF(java Chromium Embedded Framework),现在我想在CEF浏览器中获得网页的截图,但我没有找到用于此的API。有什么办法吗?非常感谢


共 (1) 个答案

  1. # 1 楼答案

    据我所知,CefBrowser是基于AWT的。要创建此类组件的屏幕截图,您可以(必须?)创建整个屏幕的捕获,仅限于组件覆盖的区域

    像这样的方法会奏效:

    // Your browser instance.
    org.cef.browser.CefBrowser browser = ... 
    
    // Obtain the component that you want to capture in a screenshot.
    java.awt.Component component = browser.getUIComponent();
    
    // Determine what area of the entire screen is covered by the component.
    java.awt.Point p = new java.awt.Point(0, 0);
    javax.swing.SwingUtilities.convertPointToScreen(p, component);
    java.awt.Rectangle region = component.getBounds();
    region.x = p.x;
    region.y = p.y;
    
    // Store the selected area from the screen in a image buffer.
    java.awt.image.BufferedImage image = new java.awt.Robot().createScreenCapture( region );
    

    要将“保存到缓冲区”保存到文件中,请创建一个File实例(如果希望向用户呈现一个漂亮的“另存为”对话框,请使用JFileChooser),然后使用javax.imageio.ImageIO#write(RenderedImage, String, File)将图像存储到文件中。第二个参数指的是要使用的文件格式(png、bmp等)

    如果有人能提供一个直接存储组件的代码示例,而不将其捕获为大屏幕的一部分(这也将捕获您感兴趣的其他组件),我将非常感兴趣