gpt4 book ai didi

java - 从java中的图像列表制作幻灯片?

转载 作者:行者123 更新时间:2023-11-30 02:17:20 24 4
gpt4 key购买 nike

我正在尝试实现幻灯片、gif 等。我列出了从文件夹中读取的图像,并将它们设置为在 SWT 对话框中显示的序列。现在我在线程访问方面遇到了麻烦。 SWT 中制作幻灯片的方法是什么?感谢您的建议和指正。

这是实现:

    public class ImageShowDialog extends Dialog {

Shell dialog;
private Label labelImage;
private Canvas canvas;
int numberImage = 0;
private volatile boolean running = true;

ImageShowDialog(Shell parent) {
super(parent);
}

public String open() {
Shell parent = getParent();
dialog = new Shell(parent, SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);
dialog.setSize(600, 400);
dialog.setText("Show Begins!!!");
dialog.setLayout(new FillLayout());
this.func();
dialog.open();

Display display = parent.getDisplay();
while (!dialog.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
return "After Dialog";
}

public void func() {
final List<byte[]> imageCollection = new ArrayList<byte[]>();

File path = new File("..\\folder");

File[] files = path.listFiles();
for (int i = 0; i < files.length; i++) {
if (files[i].isFile()) { // this line weeds out other
// directories/folders
try {
imageCollection.add(loadImage(files[i]));
} catch (IOException e1) {
e1.printStackTrace();
}
}
}

new Thread(new Runnable() {
@Override
public void run() {
while (running) {

ImageData imageData = new ImageData(
new ByteArrayInputStream(
imageCollection.get(numberImage)));
final Image image = new Image(Display.getDefault(),
imageData);
canvas = new Canvas(dialog, SWT.NONE);
canvas.addPaintListener(new PaintListener() {
public void paintControl(PaintEvent e) {
e.gc.setAlpha(255);
e.gc.drawImage(image, 0, 0);

}
});

numberImage++;
if (numberImage == imageCollection.size())
try {
running = false;
} catch (Exception e) {
e.printStackTrace();
}

try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}).start();

}

public byte[] loadImage(File file) throws IOException {

BufferedImage image = ImageIO.read(file);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ImageIO.write(image, "jpg", bos);
return bos.toByteArray();
}
<小时/>

和异常(exception):

Exception in thread "Thread-45" org.eclipse.swt.SWTException: Invalid thread access
at org.eclipse.swt.SWT.error(SWT.java:4282)
at org.eclipse.swt.SWT.error(SWT.java:4197)
at org.eclipse.swt.SWT.error(SWT.java:4168)
at org.eclipse.swt.widgets.Widget.error(Widget.java:468)
at org.eclipse.swt.widgets.Widget.checkWidget(Widget.java:359)
at org.eclipse.swt.widgets.Widget.checkParent(Widget.java:279)
at org.eclipse.swt.widgets.Widget.<init>(Widget.java:149)
at org.eclipse.swt.widgets.Control.<init>(Control.java:110)
at org.eclipse.swt.widgets.Scrollable.<init>(Scrollable.java:75)
at org.eclipse.swt.widgets.Composite.<init>(Composite.java:95)
at org.eclipse.swt.widgets.Canvas.<init>(Canvas.java:79)

最佳答案

您只能在主 UI 线程中创建和访问 SWT 控件,任何在其他线程中执行此操作的尝试都会出现“无效线程访问”错误。

您可以在后台线程中使用 DisplayasyncExecsyncExec 方法在主线程中运行代码:

Display.getDefault().asyncExec(() ->
{
... code accessing the UI
});

(使用 lambda 的 Java 8/9 代码,对于旧 Java 使用 Runnable)。

asyncExec 异步运行代码,syncExec 等待 UI 线程运行代码然后返回。

关于java - 从java中的图像列表制作幻灯片?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47922978/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com