Pass image from java applet to html

后端 未结 2 456
清歌不尽
清歌不尽 2020-12-22 01:17

I am using a signed java applet to load an image from the filesystem of the user.
Then I want to display this image in the website where the applet is running.

I

2条回答
  •  自闭症患者
    2020-12-22 01:38

    You might encode the image as Base 64, pass it to JS as a String, and use the data:image/gif; form URL to display it in the web page. You'll need to 'roll your own' base 64 encoder or find an API, since the J2SE has no inbuilt method for the conversion.1

    It might look something like this in the HTML.

    embedded folder icon
    

    E.G. taken from Inline Images with Data URLs.

    1. On later JREs (once JAXB was introduced) look to use DatatypeConverter.printBase64Binary(byte[]) something like this:
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try {
        ImageIO.write(image, "png", baos);
    } catch (IOException e) {
        showError(e);
        e.printStackTrace();
    }
    String imageString = "data:image/png;base64," +
        DatatypeConverter.printBase64Binary(baos.toByteArray());
    

提交回复
热议问题