2


I'm working on a Java/Eclipse SWT application that displays and edits map data captured by a special device in a stacked fashion, i.e. there are different layers of "geospatial features" that can be shown/hidden or modified. It was found to be helpful to have an aerial imagery layer which could be easily retrieved e.g. from google maps.

I thought of using the SWT Browser Widget to retrieve and render this satellite view, which actually works like a charm. The Problem is that I need to have a hidden Browser Widget which would work in the background and return me a swt.graphics.Image etc. of the rendered content or even better directly use a given GC for drawing.

I also thought about simpler solutions but there are two restrictions:

  1. I can't just use static maps from Google because the map tile I need would have to be larger than they allow and the partial reloading that they provide (e.g. when moving the map view port) would also be very handy.
  2. I can't simply feed my data into Google Maps for several reasons.

So in general: How do I have a (hidden) instance of a Browser Widget render its output to an Image/GC instead of the screen. Is there something else except from the Browser Widget which could do the job?

4

2 回答 2

1

我认为您可以使用该org.eclipse.swt.widgets.Control.print(GC) 方法将控件打印到图像上。我还没有尝试过浏览器。

于 2012-10-28T21:12:29.967 回答
1

这是开始使用SWT Browser控件的示例

  public static void main(String[] args) {
    final Display display = new Display();
    final Shell shell = new Shell(display);
    shell.setText("Browser Test");
    shell.setSize(500, 500);
    shell.setLayout(new GridLayout(1,false));

    final Browser browser = new Browser(shell, SWT.NONE);

    browser.setUrl("https://maps.google.com/maps?hl=en&tab=wl");
    //browser.setVisible(false);
    browser.setLayoutData(new GridData(GridData.FILL_BOTH));

    Button b = new Button(shell, SWT.NONE);
    b.setText("Show");
    b.addListener(SWT.Selection, new Listener() {

        @Override
        public void handleEvent(Event event) {


            Image img = new Image(display, 500, 500);
            GC gc = new GC(img);
            browser.print(gc);
            gc.dispose();

            showImage(img);

        }
    });

    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch())
        display.sleep();
    }
    display.dispose();
  }


private static class ImageDialog extends Dialog
{

    private Image img;

    protected ImageDialog(Shell parentShell,Image img) {
        super(parentShell);
        this.img = img;
    }
    @Override
    protected Control createDialogArea(Composite parent) {

        Composite comp =  (Composite) super.createDialogArea(parent);

        Label lbl = new Label(comp,SWT.NONE);

        lbl.setImage(img);

        return comp;
    }

    @Override
    protected void okPressed() {
        img.dispose();
        super.okPressed();
    }

}
protected static void showImage(Image img) {

    ImageDialog dialog = new ImageDialog(Display.getDefault().getActiveShell(), img);
    dialog.open();

}

我们可以想到的另一种方法

用户将 div 捕获为图像并保存到计算机

执行 java script intoSWT Browser Browser.execute(java script)以将 div 捕获到图像中。

于 2012-10-29T03:53:25.370 回答