The project uses GWT, but in this case it serves only as a wrapper for JavaScript code that works with canvas
. Therefore, I will be equally happy to address both the GWT and JavaScript.
The task is as follows: we have an input image that is uploaded and drawn to the canvas, and this canvas is attached
. By clicking we need to apply a graphic filter. To do this, take the input image inputImage
, apply a filter to it, and then output the result to another outputImage
image. I wrote this code:
// ΠΊΠ°Π½Π²Π°Ρ Π΄Π»Ρ Π²ΡΡ
ΠΎΠ΄Π½ΠΎΠ³ΠΎ ΠΈΠ·ΠΎΠ±ΡΠ°ΠΆΠ΅Π½ΠΈΡ Canvas canvas = new Canvas(Document.get().createCanvasElement()); Context2d context2d = canvas.getContext2d(); // ΡΠ»Π΅ΠΌΠ΅Π½Ρ Π²Ρ
ΠΎΠ΄Π½ΠΎΠ³ΠΎ ΠΈΠ·ΠΎΠ±ΡΠ°ΠΆΠ΅Π½ΠΈΡ ImageElement imageElement = ImageElement.as(inputImage.getElement()); context2d.drawImage(imageElement, 0, 0, width, height, 0, 0, width, height); ImageData data = context2d.getImageData(0, 0, width, height); CanvasPixelArray array = data.getData(); for (int i = 0; i < array.getLength(); i = i + 4) { // ΠΏΡΠΈΠΌΠ΅Π½ΡΠ΅ΠΌ ΡΠΈΠ»ΡΡΡ, Π΄Π»Ρ ΠΏΡΠΈΠΌΠ΅ΡΠ° Ρ ΠΏΠ΅ΡΠ΅Π²ΠΎΠΆΡ ΠΈΠ·ΠΎΠ±ΡΠ°ΠΆΠ΅Π½ΠΈΠ΅ Π² ΠΎΡΡΠ΅Π½ΠΊΠΈ ΡΠ΅ΡΠΎΠ³ΠΎ int silver = (array.get(i) + array.get(i + 1) + array.get(i + 2)) / 3; array.set(i, silver); array.set(i + 1, silver); array.set(i + 2, silver); } context2d.putImageData(data, 0, 0); outputImage.setUrl(canvas.toDataUrl("image/png"));
It works great in Chrome and in Safari, but does not work in Mozille and in IE9. At the same time in IE9, an exception falls:
com.google.gwt.core.client.JavaScriptException: (Error): Unknown failure at com.google.gwt.dev.shell.BrowserChannelServer.invokeJavascript (BrowserChannelServer.java:237) at com.google.gwt.dev.shell. ModuleSpaceOOPHM.doInvoke (ModuleSpaceOOPHM.java:132) at com.google.gwt.dev.shell.ModuleSpace.invokeNative (ModuleSpace.javament61) at com.google.gwt.dev.shell.ModuleSpace.invokeNativeObject (ModuleSpace.java: 269) at com.google.gwt.dev.shell.JavaScriptHost.invokeNativeObject (JavaScriptHost.java:91) at com.google.gwt.canvas.dom.client.ImageData $ .getData $ (ImageData.java)
That is, Internet Explorer 9
swears at the line
CanvasPixelArray array = data.getData();
When you try to modify the code, for example, replacing the CanvasPixelArray
pass with a pass directly through ImageData
exception in IE does not fall, but the application does not output anything to outputImage
.
And in Mozilla:
com.google.gwt.core.clients ) at com.google.gwt.dev.shell.ModuleSpaceOOPHM.doInvoke (ModuleSpaceOOPHM.java:132) at com.google.gwt.dev.shell.ModuleSpace.invokeNative (ModuleSpace.java partner61) at com.google.gwt. dev.shell.ModuleSpace.invokeNativeVoid (ModuleSpace.java:289) at com.google.gwt.dev.shell.JavaScriptHost.invokeNativeVoid (JavaScriptHost.java:107) at com.google.gwt.canvas.dom.client.Context2d $ .drawImage $ (Context2d.java)
Mozilla swears at the attempt to output outputImage
to the main canvas using the drawImage
method.
How can I make this code work?