How to pass image from Applet to JSF backing bean

╄→гoц情女王★ 提交于 2019-12-24 00:35:22

问题


I am working with a web application in which there is a Java Applet that captures an image from a wacom device into a RenderedImage object. The applet itself is embedded into a JSF 2.0 page.

I need to pass the created RenderedImage from Applet to a JSF backing bean so that it would be a part of a User object. My backing bean is view scoped.

I'm really lost with this. I've been searching for a good example on how this goal can be achieved. Should I use JSObject, or should I send an image to a servlet?

Can you offer some advice on how to solve this problem?


回答1:


You problem can be divided into the following sub-steps:

  1. Create a byte array from your BufferedImage that is holding its data;
  2. Encode the data properly so that it won't be damaged/modified while it is being sent to the server as a string, for example by using Apache Commons Base64 codec;
  3. Save the data as a hidden form field via Applet-to-JavaScript communication;
  4. Send POST request to the server by, for example, triggering <h:commandButton>'s onclick;
  5. Write encoded string to a java bean property in a standard JSF way;
  6. Decode the string to get the byte array representing the image;
  7. Recreate the image from the byte array and inject it in your view scoped bean.

That said, let's move on to implementing that agenda.

In your applet you'll have a method that will do points (1) - (4). Call it in a usual way, after you obtain the image:

Java Applet method:

public void processImage() throws IOException, JSException {
    BufferedImage image = createBufferedImage();//the way you get the image
    /* point 1 */
    ByteArrayOutputStream bs = new ByteArrayOutputStream();
    ImageIO.write(image, "png", bs);
    bs.flush();
    byte[] imageByteArray = bs.toByteArray();
    bs.close();
    /* point 1 */
    String imageAsString = Base64.encodeBase64String(imageByteArray);//point 2
    /* points 3-4 */
    JSObject window = JSObject.getWindow(this);
    window.call("writeImageValue", new Object[] {imageAsString});
    /* points 3-4 */
}

JSF page (form and JavaScript):

<script>
    function writeImageValue(imageValue) {
        document.getElementById('image').value = imageValue;//point 3
        document.getElementById('image-form:submit').click();//point 4
    }
</script>
<h:form id="image-form">
    <input type="hidden" id="image" name="image" />
    <h:commandButton id="submit" action="#{imageSubmitBean.submitImage}" style="display:none" />
</h:form>

JSF managed bean:

@ManagedBean
@RequestScoped
public class ImageSubmitBean {

    @ManagedProperty("#{param.image}")//point 5
    private String imageAsString;//getter+setter
    @ManagedProperty("#{userBean}")//your view scoped bean
    private UserBean userBean;//getter+setter

    public String submitImage() throws IOException {
        byte[] imageByteArray = Base64.decodeBase64(imageAsString);//point 6
        /* point 7 */
        InputStream is = new ByteArrayInputStream(imageByteArray);
        BufferedImage image = ImageIO.read(is);
        is.close();
        userBean.setUserImage(image);//update your view scoped bean
        /* point 7 */
        return null;
    }

}


来源:https://stackoverflow.com/questions/17552202/how-to-pass-image-from-applet-to-jsf-backing-bean

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!