嗨,我正在开发一个应用程序,我们在其中截屏并通过gc绘制图像来显示图像。在升级到macOS mojave之后,相同的代码无法正常工作。我在下面的代码段中对此进行了描述。在paint listiner中,而不是从paint事件中获取gc,我创建了另一个我正在对其进行一些操作的gc,并通过该gc我正在绘制图像。此解决方案可在Windows和Mac OS上运行,直至最高分辨率(10.13)。使用Mojave更新创建另一个gc会给出白色图像,当我直接尝试使用paint事件中的gc进行绘制时,它的工作没有太大问题。 / p>
import org.eclipse.jface.window.Window;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.ScrolledComposite;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.graphics.ImageLoader;
import org.eclipse.swt.graphics.PaletteData;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
public class ScreenShotWithGC {
public static void main(String[] args) {
final Display display = new Display();
final Shell shell = new Shell(display);
shell.setLayout(new FillLayout());
Button button = new Button(shell, SWT.PUSH);
button.setText("Capture");
Button save = new Button(shell, SWT.PUSH);
save.setText("save");
button.addListener(SWT.Selection, new Listener() {
public void handleEvent(Event event) {
/* Take the screen shot */
GC gc = new GC(display);
final Image image = new Image(display, display.getBounds());
gc.copyArea(image, 0, 0);
gc.dispose();
ImageData imageData = image.getImageData();
image.dispose();
Image imagetemp=new Image(display, imageData);
Shell popup = new Shell(shell, SWT.SHELL_TRIM);
popup.setLayout(new FillLayout());
popup.setText("Image");
popup.setBounds(50, 50, 200, 200);
popup.addListener(SWT.Close, new Listener() {
public void handleEvent(Event e) {
image.dispose();
}
});
ScrolledComposite sc = new ScrolledComposite(popup, SWT.V_SCROLL | SWT.H_SCROLL);
Canvas canvas = new Canvas(sc, SWT.NONE);
sc.setContent(canvas);
canvas.setBounds(display.getBounds());
canvas.addPaintListener(new PaintListener() {
public void paintControl(PaintEvent e) {
GC gc=e.gc;
PaletteData offscreenImagePalette = new PaletteData(0x0000FF00, 0x00FF0000, 0xFF000000);
ImageData offscreenImageData = new ImageData(1000, 768, 32, offscreenImagePalette);
offscreenImageData.type = SWT.BITMAP;
Image gdim = new Image(null, offscreenImageData);
GC ggc = new GC(gdim, Window.getDefaultOrientation());
//gc=ggc;
gc.fillRectangle(0, 0, 1000, 768);
gc.drawImage(imagetemp, 0, 0);
if(gc==ggc)
{
e.gc.drawImage(gdim, 0, 0);
}
}
});
popup.open();
save.addSelectionListener(new SelectionListener() {
@Override
public void widgetSelected(SelectionEvent arg0) {
// TODO Auto-generated method stub
ImageLoader saver = new ImageLoader();
saver.data = new ImageData[] { imageData };
saver.save("/Users/prabhu_n/Desktop/Image/output.png", SWT.IMAGE_PNG);
}
@Override
public void widgetDefaultSelected(SelectionEvent arg0) {
// TODO Auto-generated method stub
}
});
}
});
shell.pack();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}
关于此的任何线索都将有所帮助 更新:此代码适用于旧版swt,但适用于新版swt,getimagedata()无法返回正确的值。