我打算将此作为对此问题的评论发布,但我没有足够的代表,除了提出一个新问题之外我没有其他办法(尽管它似乎有点多余)。
无论如何,我尝试了skuntsel写的解决方案,但反过来:我对图像进行编码并将其从 bean 发送到 javascript 方法(我使用的是 icefaces,所以我这样称呼它JavascriptContext.addJavascriptCall(FacesContext.getCurrentInstance(), functionCall)
)。我在 Applet 中得到了编码的字符串,但是当我尝试解码它时,什么也没有发生,它后面的代码无法访问。
我错过了什么吗?提前致谢!
编辑:这是我正在使用的代码。
在 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);
在 Javascript 中:
function getEncodedImage(image){
var applet = document.getElementById("Applet");
applet.decodeImage(image);
}
在小程序中:
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();
}
}