我想顯示一個瀏覽器插件(SWT)內的圖像。這些圖像可以在jar文件中找到(插件開發)。但是:這不是直接可能的,因爲瀏覽器小部件需要某種URL或URI信息。如何SWT圖像轉換成數據URI在瀏覽器的插件使用
我的想法是將SWT圖像轉換爲數據URI值,我可以將其引入到每個給定的img元素的src屬性中。我知道,這是不是從性能的角度來看一個很好的解決辦法,但我不介意的速度上的劣勢。
我想知道如何將SWT圖像轉換爲數據URI值,以便在瀏覽器窗口小部件中使用。
到目前爲止我的代碼:
package editor.plugin.editors.htmlprevieweditor;
import editor.plugin.Activator;
import org.eclipse.swt.browser.Browser;
import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Composite;
public class HtmlPreview extends Composite implements DisposeListener {
private final Browser content;
public HtmlPreview(final Composite parent, final int style) {
super(parent, style);
this.setLayout(new FillLayout());
content = new Browser(this, style);
final ImageData imageData = Activator.getImageDescriptor(Activator.IMAGE_ID + Activator.PREVIEW_SMALL_ID).getImageData();
content.setText("<html><body><img src=\"data:image/png;base64," + imageData + "\"/></body></html>"); // need help on changing imageData to a base64-encoded String of bytes?
this.addDisposeListener(this);
}
@Override
public void widgetDisposed(final DisposeEvent e) {
e.widget.dispose();
}
}
任何幫助,非常感謝!:)
編輯1:我已閱讀SWT Image to/from String,但不幸的是,它似乎並不能完全覆蓋我的需求。編輯2:我不知道它是否重要,但我試圖加載一個PNG24圖像與每像素alpha透明度。
我讀過,除非指定SWT.MOZILLA並確保xulrunner.exe位於類路徑中,否則我無法在Windows計算機上使用Mozilla-Browser-widget。這意味着它會是相當困難的使用Mozilla的瀏覽器插件 - 儘管我很感激,因爲這支持它。 我會嘗試一下!非常感謝你的提示。只要我想出一種使用Mozilla-Browser-widget的方式,我會將這個答案標記爲正確的答案,除非有人提供了更好的答案。 – Igor
@Igor在我來說,我只是說:System.setProperty( 「org.eclipse.swt.browser.XULRunnerPath」, 「路徑XULRunner的」);並且xul亞軍被複制在「通往xulrunner的道路上」。 – andi