public static final String toDataURL(ImageElement element, DataURLType mimetype, double quality) { if (null == mimetype) { mimetype = DataURLType.PNG; } ScratchCanvas canvas = new ScratchCanvas(element.getWidth(), element.getHeight()); canvas.getContext().drawImage(element, 0, 0, element.getWidth(), element.getHeight()); return canvas.toDataURL(mimetype, quality); }
public static final String toDataURL(final ImageElement element) { final ScratchCanvas canvas = new ScratchCanvas(element.getWidth(), element.getHeight()); canvas.getContext().drawImage(element, 0, 0); return canvas.toDataURL(); }
@Override public void getRgb( int startX, int startY, int width, int height, int[] rgbArray, int offset, int scanSize) { Asserts.checkState(isReady(), "Cannot getRgb() a non-ready image"); if (canvas == null) { canvas = img.getOwnerDocument().createCanvasElement(); canvas.setHeight(img.getHeight()); canvas.setWidth(img.getWidth()); canvas.getContext2d().drawImage(img, 0, 0); // img.getOwnerDocument().getBody().appendChild(canvas); } Context2d ctx = canvas.getContext2d(); ImageData imageData = ctx.getImageData(startX, startY, width, height); CanvasPixelArray pixelData = imageData.getData(); int i = 0; int dst = offset; for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { int r = pixelData.get(i++); int g = pixelData.get(i++); int b = pixelData.get(i++); int a = pixelData.get(i++); rgbArray[dst + x] = a << 24 | r << 16 | g << 8 | b; } dst += scanSize; } }
/** * Draws an input image at a given position on the canvas. Resizes image according to specified * width and height. * * <p>We recommend that the pixel and coordinate spaces be the same to provide consistent * positioning and scaling results * * @param img The image to be drawn * @param offsetX x coord of the top left corner in the destination space * @param offsetY y coord of the top left corner in the destination space * @param width the size of the image in the destination space * @param height the size of the image in the destination space */ public void drawImage( ImageElement img, double offsetX, double offsetY, double width, double height) { impl.drawImage(img, 0, 0, img.getWidth(), img.getHeight(), offsetX, offsetY, width, height); }
/** * Draws an input image to a specified position on the canvas. Size defaults to the default * dimensions of the image. * * @param img the image to be drawn * @param offsetX x coord of the top left corner in the destination space * @param offsetY y coord of the top left corner in the destination space */ public void drawImage(ImageElement img, double offsetX, double offsetY) { drawImage(img, offsetX, offsetY, img.getWidth(), img.getHeight()); }
@Override public float height() { return img == null ? 0 : scale.invScaled(img.getHeight()); }