Example usage for com.google.gwt.canvas.dom.client Context2d getImageData

List of usage examples for com.google.gwt.canvas.dom.client Context2d getImageData

Introduction

In this page you can find the example usage for com.google.gwt.canvas.dom.client Context2d getImageData.

Prototype

public final native ImageData getImageData(double sx, double sy, double sw, double sh) ;

Source Link

Document

Returns an image data object for the screen area denoted by sx, sy, sw and sh.

Usage

From source file:anagram.client.Lens.java

License:Apache License

public void draw(Context2d back, Context2d front) {
    front.drawImage(back.getCanvas(), 0, 0);

    if (!GWT.isScript()) {
        // in devmode this effect is slow so we disable it here
    } else {//from www. jav a  2  s .  com
        ImageData frontData = front.getImageData((int) (pos.x - radius), (int) (pos.y - radius), 2 * radius,
                2 * radius);
        CanvasPixelArray frontPixels = frontData.getData();
        ImageData backData = back.getImageData((int) (pos.x - radius), (int) (pos.y - radius), 2 * radius,
                2 * radius);
        CanvasPixelArray backPixels = backData.getData();
        int srcIdx, dstIdx;
        for (int i = lensArray.length - 1; i >= 0; i--) {
            dstIdx = 4 * lensArray[i][0];
            srcIdx = 4 * lensArray[i][1];
            frontPixels.set(dstIdx + 0, backPixels.get(srcIdx + 0));
            frontPixels.set(dstIdx + 1, backPixels.get(srcIdx + 1));
            frontPixels.set(dstIdx + 2, backPixels.get(srcIdx + 2));
        }
        front.putImageData(frontData, (int) (pos.x - radius), (int) (pos.y - radius));
    }

    front.setStrokeStyle(strokeStyle);
    front.beginPath();
    front.arc(pos.x, pos.y, radius, 0, Math.PI * 2, true);
    front.closePath();
    front.stroke();
}

From source file:com.google.gwt.sample.userwatcher.client.ImageUtils.java

public static ImageData cropImage(Image image, double sx, double sy, double sw, double sh) {

    Canvas canvasTmp = Canvas.createIfSupported();
    Context2d context = canvasTmp.getContext2d();

    canvasTmp.setCoordinateSpaceHeight((int) sh + 10);
    canvasTmp.setCoordinateSpaceWidth((int) sw + 10);

    ImageElement imageElement = ImageElement.as(image.getElement());

    double dx = 0;
    double dy = 0;
    double dw = sw;
    double dh = sh;

    // draw image to canvas
    context.drawImage(imageElement, sx, sy, sw, sh, dx, dy, dw, dh);

    // get image data
    double w = sw;
    double h = sh;
    ImageData imageData = context.getImageData(0, 0, w, h);

    canvasTmp.removeFromParent();//from  w w  w .ja  va  2 s . c o  m

    return imageData;
}

From source file:com.google.gwt.sample.userwatcher.client.ImageUtils.java

public static ImageData scaleAndCropImage(Image image, double scaleToRatio, double sx, double sy, double sw,
        double sh) {

    Canvas canvasTmp = Canvas.createIfSupported();
    //RootPanel.get().add(canvasTmp);
    Context2d context = canvasTmp.getContext2d();

    double ch = (image.getHeight() * scaleToRatio) + 100;
    double cw = (image.getWidth() * scaleToRatio) + 100;

    canvasTmp.setCoordinateSpaceHeight((int) ch);
    canvasTmp.setCoordinateSpaceWidth((int) cw);

    ImageElement imageElement = ImageElement.as(image.getElement());

    // tell it to scale image
    context.scale(scaleToRatio, scaleToRatio);

    // draw image to canvas
    // s = source
    // d = destination     
    double dx = 0;
    double dy = 0;
    context.drawImage(imageElement, dx, dy);

    // get image data - if you go greater than the scaled image nothing will show up
    ImageData imageData = context.getImageData(sx, sy, sw, sh);

    return imageData;
}

From source file:com.google.gwt.sample.userwatcher.client.ImageUtils.java

/**
 * image - an ImageElement object//from www  .  j  a  v  a 2  s .  c o m
 * 
  sx - the x coordinate of the upper-left corner of the source rectangle
  sy - the y coordinate of the upper-left corner of the source rectangle
  sw - the width of the source rectangle
  sh - the width of the source rectangle
  dx - the x coordinate of the upper-left corner of the destination rectangle
  dy - the y coordinate of the upper-left corner of the destination rectangle
  dw - the width of the destination rectangle
  dh - the height of the destination rectangle
        
 */
public static ImageData scaleImage(Image image, double scaleToRatio) {

    //System.out.println("PanoTiler.scaleImag()e: scaleToRatio=" + scaleToRatio + " width=" + width + " x height=" + height);

    Canvas canvasTmp = Canvas.createIfSupported();
    Context2d context = canvasTmp.getContext2d();

    double ch = (image.getHeight() * scaleToRatio) + 100; // 100 is offset so it doesn't throw
    double cw = (image.getWidth() * scaleToRatio) + 100;

    canvasTmp.setCoordinateSpaceHeight((int) ch);
    canvasTmp.setCoordinateSpaceWidth((int) cw);

    ImageElement imageElement = ImageElement.as(image.getElement());

    // s = source
    // d = destination 
    double sx = 0;
    double sy = 0;
    double sw = imageElement.getWidth();
    double sh = imageElement.getHeight();

    double dx = 0;
    double dy = 0;
    double dw = imageElement.getWidth();
    double dh = imageElement.getHeight();

    // tell it to scale image
    context.scale(scaleToRatio, scaleToRatio);

    // draw image to canvas
    context.drawImage(imageElement, sx, sy, sw, sh, dx, dy, dw, dh);

    // get image data
    double w = dw * scaleToRatio;
    double h = dh * scaleToRatio;
    ImageData imageData = context.getImageData(0, 0, w, h); // this won't get the extra 100

    return imageData;
}

From source file:com.google.gwt.sample.userwatcher.client.ImageUtils.java

public static ImageData cropImage(ImageData imageData, double sx, double sy, double sw, double sh) {

    Canvas canvasTmp = Canvas.createIfSupported();
    canvasTmp.setStyleName("mainCanvas");
    Context2d context = canvasTmp.getContext2d();

    canvasTmp.setCoordinateSpaceHeight((int) imageData.getHeight() + 10);
    canvasTmp.setCoordinateSpaceWidth((int) imageData.getWidth() + 10);

    // draw image to canvas
    context.putImageData(imageData, 0, 0);

    // get image data
    //imageData = context.getImageData(0, 0, imageData.getWidth(), imageData.getHeight());
    ImageData newImageData = context.getImageData(sx, sy, sw, sh);

    return newImageData;
}

From source file:com.googlecode.mgwt.image.client.ImageConverter.java

License:Apache License

public void convert(final ImageResource resource, String color,
        final ImageConverterCallback imageConverterCallback) {

    if (color == null) {
        throw new IllegalArgumentException();
    }//from  w  ww .j a  v a 2  s .  c o  m

    if (!color.startsWith("#")) {
        throw new IllegalArgumentException();
    }

    color = maybeExpandColor(color);

    final int hexColor = Integer.parseInt(color.substring(1), 16);

    final int red = hexColor >> 16 & 0xFF;
    final int green = hexColor >> 8 & 0xFF;
    final int blue = hexColor & 0xFF;

    final int height = resource.getHeight();
    final int width = resource.getWidth();

    loadImage(resource.getSafeUri().asString(), width, height, new LoadImageCallback() {

        @Override
        public void onSuccess(ImageElement imageElement) {

            Canvas canvas = Canvas.createIfSupported();
            canvas.getElement().setPropertyInt("height", height);
            canvas.getElement().setPropertyInt("width", width);

            Context2d context = canvas.getContext2d();
            context.drawImage(imageElement, 0, 0);
            ImageData imageData = context.getImageData(0, 0, width, height);

            CanvasPixelArray canvasPixelArray = imageData.getData();

            for (int i = 0; i < canvasPixelArray.getLength(); i += 4) {
                canvasPixelArray.set(i, red);
                canvasPixelArray.set(i + 1, green);
                canvasPixelArray.set(i + 2, blue);
                canvasPixelArray.set(i + 3, canvasPixelArray.get(i + 3));
            }
            context.putImageData(imageData, 0, 0);
            imageConverterCallback.onSuccess(new ConvertedImageResource(canvas.toDataUrl("image/png"),
                    resource.getWidth(), resource.getHeight()));
        }
    });
}

From source file:jetbrains.jetpad.projectional.domUtil.TextMetricsCalculator.java

License:Apache License

private static int measureHeight(Font font, String text) {
    Canvas canvas = canvas();//from  ww w.  java  2  s.c  o  m
    Context2d ctx = canvas.getContext2d();

    ctx.setFont(getFontString(font));
    ctx.setFillStyle("rgb(255, 0, 0)");

    int width = (int) ctx.measureText(text).getWidth();
    int canvasHeight = font.getSize() * 2;
    canvas.setHeight(canvasHeight + "px");
    canvas.setHeight(font.getSize() * 2 + "px");
    canvas.setWidth(width + "px");

    ctx.fillText(text, 0, font.getSize());
    ImageData data = ctx.getImageData(0, 0, width, canvasHeight);
    int firstY = canvasHeight - 1;
    int lastY = 0;
    for (int x = 0; x < width; x++) {
        for (int y = 0; y < canvasHeight; y++) {
            int red = data.getRedAt(x, y);
            if (red != 0) {
                if (firstY > y) {
                    firstY = y;
                }
                if (lastY < y) {
                    lastY = y;
                }
            }
        }
    }
    return lastY - firstY;
}

From source file:org.catrobat.html5player.client.Scene.java

License:Open Source License

/**
 *
 * @param imageElement/*  w w  w.  j ava  2  s .c o  m*/
 * @param brightness
 * @return Canvas canvas with the adjusted image
 */
private Canvas adjustImageBrightness(ImageElement imageElement, double brightness) {

    int width = imageElement.getWidth();
    int height = imageElement.getHeight();

    Canvas temp = Canvas.createIfSupported();
    temp.setCoordinateSpaceWidth(width);
    temp.setCoordinateSpaceHeight(height);

    Context2d context = temp.getContext2d();

    context.drawImage(imageElement, 0, 0);

    ImageData imageData = context.getImageData(0, 0, width, height);

    CanvasPixelArray pixelsData = imageData.getData();

    int index = 0;

    System.out.println(pixelsData.getLength());

    int brightnessAdj = (int) (255d * brightness) - 255;

    if (brightnessAdj != 0) {
        while (index < pixelsData.getLength()) {
            if ((index + 1) % 4 != 0) {
                int r = checkColorRange(pixelsData.get(index) + brightnessAdj); //red channel
                pixelsData.set(index, r);

                int g = checkColorRange(pixelsData.get(++index) + brightnessAdj); //green channel
                pixelsData.set(index, g);

                int b = checkColorRange(pixelsData.get(++index) + brightnessAdj); //blue channel
                pixelsData.set(index, b);

                index++; //alpha channel
            }
            index++;
        }
    }

    context.putImageData(imageData, 0, 0);

    return temp;
}

From source file:org.catrobat.html5player.client.Sprite.java

License:Open Source License

/**
 *
 * @param relativeX/*from w w w.  j  a v a  2 s  .  co m*/
 * @param relativeY
 * @return
 */
public boolean processOnTouch(int relativeX, int relativeY) {
    //Create temp canvas with the current look only to determine click (alpha) 
    Canvas temp = Canvas.createIfSupported();
    temp.setWidth(Scene.get().getCanvas().getCoordinateSpaceWidth() + "px");
    temp.setHeight(Scene.get().getCanvas().getCoordinateSpaceHeight() + "px");
    temp.setCoordinateSpaceWidth(Scene.get().getCanvas().getCoordinateSpaceWidth());
    temp.setCoordinateSpaceHeight(Scene.get().getCanvas().getCoordinateSpaceHeight());

    LookData lookData = look.getLookData();
    if (lookData == null)
        return false;

    double size = look.getSize();
    double width = (double) lookData.getWidth() * size;
    double height = (double) lookData.getHeight() * size;
    double x = -width / 2;
    double y = -height / 2;

    Context2d context = temp.getContext2d();

    context.translate(look.getXPosition(), look.getYPosition());
    context.rotate((-look.getRotation()) * Math.PI / 180);
    context.drawImage((ImageElement) currentLook.getElement().cast(), x, y, width, height);

    if (context.getImageData(relativeX, relativeY, 1, 1).getAlphaAt(0, 0) == 0) {
        return false;
    }

    if (currentLook == null || !look.isVisible())
        return false;

    double xPosition = look.getXPosition();
    double yPosition = look.getYPosition();
    double newSize = look.getSize();

    LookData newLookData = look.getLookData();

    double widthHalf = ((double) newLookData.getWidth() * newSize) / 2;
    double heightHalf = ((double) newLookData.getHeight() * newSize) / 2;

    if (xPosition + widthHalf > relativeX && xPosition - widthHalf < relativeX
            && yPosition + heightHalf > relativeY && yPosition - heightHalf < relativeY) {

        CatrobatDebug.info("Sprite " + this.name + " got touched");

        return true;
    }

    return false;
}

From source file:org.cruxframework.crux.widgets.client.colorpicker.SaturationLightnessPicker.java

License:Apache License

private String getColorAtPixel(int x, int y) {
    x = Math.max(Math.min(x, 179), 0);
    y = Math.max(Math.min(y, 179), 0);
    Context2d ctx = canvas.getContext2d();
    ImageData imageData = ctx.getImageData(x, y, 1, 1);
    CanvasPixelArray data = imageData.getData();
    return ColorUtils.rgb2hex(data.get(0), data.get(1), data.get(2));
}