[英]Pass image from JSF bean to Applet
I meant to post this as a comment at this question but I don't have enough rep and I saw no other way but to ask a new question (although it seems a bit redundant). 我打算将其发布为对此问题的评论,但是我没有足够的代表,除了提出一个新问题外,我没有其他办法(尽管似乎有点多余)。
Anyway, I tried the solution skuntsel wrote, but backwards: I encoded the image and sent it from the bean to a javascript method (I'm using icefaces so I called it like this JavascriptContext.addJavascriptCall(FacesContext.getCurrentInstance(), functionCall)
). 无论如何,我尝试了skuntsel编写的解决方案,但是却倒退了:我对图像进行了编码并将其从Bean发送到javascript方法(我使用的是icefaces,因此我将其像这样JavascriptContext.addJavascriptCall(FacesContext.getCurrentInstance(), functionCall)
)。 I get the encoded string in the Applet just fine, but when I try to decode it, nothing happens, the code following it is unreachable. 我在Applet中得到的编码字符串很好,但是当我尝试对其进行解码时,什么也没发生,跟随它的代码是无法访问的。
Am I missing something? 我想念什么吗? Thanks in advance! 提前致谢!
EDIT: Here's the code I'm using. 编辑:这是我正在使用的代码。
In the bean: (method triggered by a button click) 在bean中:(通过单击按钮触发的方法)
BufferedImage originalImage = acquireImage();
byte[] imageInByte = null;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
ImageIO.write( originalImage, "png", baos );
baos.flush();
imageInByte = baos.toByteArray();
baos.close();
} catch (IOException e) {
e.printStackTrace();
}
String imageAsString = Base64.encodeBase64String(imageInByte);
JavascriptContext.addJavascriptCall(FacesContext.getCurrentInstance(), functionCall);
In Javascript: 用Javascript:
function getEncodedImage(image){
var applet = document.getElementById("Applet");
applet.decodeImage(image);
}
In the applet: 在小程序中:
public void decodeImage(String image) {
System.out.println(image); //works
byte[] imageByteArray = Base64.decodeBase64(image);
System.out.println("something"); //doesn't print anything
InputStream is = new ByteArrayInputStream(imageByteArray);
try {
BufferedImage img = ImageIO.read(is);
ImageIO.write(img, "png", new File("D:/image.png"));
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
The only suspicious part is JavascriptContext.addJavascriptCall(FacesContext.getCurrentInstance(), functionCall);
唯一可疑的部分是JavascriptContext.addJavascriptCall(FacesContext.getCurrentInstance(), functionCall);
: I don't see any functionCall
definition. :我看不到任何functionCall
定义。 Despite, replace functionCall
by "getEncodedImage(" + image + ")"
and it should work. 尽管如此,用"getEncodedImage(" + image + ")"
替换functionCall
,它应该可以工作。
I haven't worked with ICEfaces to say anything new about how JavascriptContext#addJavascriptCall()
works, but there is a standard JSF solution: have a hidden input that contains the encoded string set in an action method and add a JS callback via AJAX. 我没有与ICEfaces一起谈论JavascriptContext#addJavascriptCall()
工作原理,但是有一个标准的JSF解决方案:隐藏的输入包含在操作方法中设置的编码字符串,并通过AJAX添加JS回调。
Namely, the bean part: 即,bean部分:
private String imageAsString;//getter + setter
public void encodeImage() {
...
this.imageAsString = Base64.encodeBase64String(imageInByte);
}
And the view: 和视图:
<script>
function update(data){
if(data.status == "success") {
var applet = document.getElementById("Applet");
var image = document.getElementById("form:image").value;
applet.decodeImage(image);
}
}
</script>
...
<h:form id="form">
...
<h:inputHidden id="image" value="#{bean.imageAsString}" />
<h:commandButton value="Submit" action="#{bean.encodeImage}">
<f:ajax render="image" onevent="update" />
</h:commandButton>
</h:form>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.