问题
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:
- Create a byte array from your
BufferedImage
that is holding its data; - 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;
- Save the data as a hidden form field via Applet-to-JavaScript communication;
- Send POST request to the server by, for example, triggering
<h:commandButton>
'sonclick
; - Write encoded string to a java bean property in a standard JSF way;
- Decode the string to get the byte array representing the image;
- 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