问题在于:我有几张图片,想在JavaFX WebView
中显示HTML时使用它们。
当前的实现非常明显:有一个文件,链接到HTML内容。我假设WebView
不会从JEditorPane
回退,并且只会执行单个I / O操作,即使图像在整个内容中被引用了10 000次。
但是,拥有一个Image
实例并在遇到相关的<img>
代码时将其提供给WebView会很棒。
我已经看到有一个很好的半解决方案涉及URL
处理,但问题仍然存在:你有一个Image
实例,你转换为存储格式(BMP,PNG与专有扩展等等)并将其保存在内存中。但是,这意味着每次WebView需要图像分辨率时,它必须手动从二进制数据中解析图像。最后,您只需要将文件映射到内存以及内部Image
实例而不是共享Image
实例。
使用JEditorPane
,您可以将Image
推送到其图像缓存并解决此类问题。不幸的是,自Java 7以来,该组件无法使用且无可置疑。
基本上,WebView
/ WebEngine
是否有机会维护这样的缓存/等价物并且有预先填充它的方法吗?
答案 0 :(得分:2)
/**
* Encodes the image as a whole into PNG, then into Base64 and finally into an URI suitable for the HTML {@code <img>} tag.
*
* @param image an image
* @return image as URI (image within the URI)
* @throws IIOException if there is a fault with an image writer
* @throws IOException in case of a general I/O error
*/
public static final String getImageSrcForWebEngine(RenderedImage image) throws IIOException, IOException
{
final ByteArrayOutputStream output = new ByteArrayOutputStream();
ImageIO.write(image, "PNG", output);
return "data:base64," + Base64.getMimeEncoder().encodeToString(output.toByteArray());
}
使用示例:
RenderedImage image = […];
String tag = "<img src=\"" + getImageSrcForWebEngine(image) + "\" border=\"0\" />";
SSCCE:
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.awt.image.RenderedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Base64;
import javax.imageio.IIOException;
import javax.imageio.ImageIO;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.web.WebView;
import javafx.stage.Stage;
public class WebViewWithMemoryImages extends Application
{
private static String IMAGE_IN_MEMORY;
@Override
public void start(Stage primaryStage)
{
WebView webView = new WebView();
webView.getEngine().loadContent("<html><body><img src=\"" + IMAGE_IN_MEMORY + "\"></body></html>");
primaryStage.setScene(new Scene(webView, 420, 420));
primaryStage.show();
}
public static void main(String[] args) throws Exception
{
BufferedImage image = new BufferedImage(400, 400, BufferedImage.TYPE_INT_BGR);
Graphics2D g = image.createGraphics();
try
{
g.setColor(Color.RED);
g.fillRect(0, 0, 400, 400);
g.setColor(Color.WHITE);
g.fillRect(50, 50, 300, 300);
g.setColor(Color.BLACK);
g.fillRect(100, 100, 200, 200);
g.drawString("No image files were used in this WebView.", 90, 70);
}
finally
{
g.dispose();
}
IMAGE_IN_MEMORY = getImageSrcForWebEngine(image);
launch(args);
}
public static String getImageSrcForWebEngine(RenderedImage image) throws IIOException, IOException
{
final ByteArrayOutputStream output = new ByteArrayOutputStream();
ImageIO.write(image, "PNG", output);
return "data:base64," + Base64.getMimeEncoder().encodeToString(output.toByteArray());
}
}