Example usage for com.google.gwt.canvas.client Canvas toDataUrl

List of usage examples for com.google.gwt.canvas.client Canvas toDataUrl

Introduction

In this page you can find the example usage for com.google.gwt.canvas.client Canvas toDataUrl.

Prototype

public String toDataUrl(String type) 

Source Link

Document

Returns a data URL for the current content of the canvas element, with a specified type.

Usage

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

private void upload(final Image image) {

    Canvas canvasTmp = Canvas.createIfSupported();
    canvasTmp.setCoordinateSpaceHeight((int) image.getHeight());
    canvasTmp.setCoordinateSpaceWidth((int) image.getWidth());
    Context2d context = canvasTmp.getContext2d();
    ImageElement imageElement = ImageElement.as(image.getElement());
    context.drawImage(imageElement, 0, 0);

    String contentType = "image/jpeg";

    String fileBase64 = canvasTmp.toDataUrl(contentType);

    UploadImage iu = new UploadImage(cp);
    iu.setFile(1234568759L, fileName, contentType, fileBase64);

    // TODO enable this if your testing it!!!!!!!!!!
    iu.upload();/*  w  w w  .j  ava2  s.  c  o  m*/

    iu.addChangeHandler(new ChangeHandler() {
        public void onChange(ChangeEvent event) {
            UploadImage u = (UploadImage) event.getSource();
            int e = u.getChangeEvent();
            fireChange(e);
        }
    });
}

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  a2 s  .com*/

    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:com.mecatran.otp.gwt.client.view.ItineraryDetailsWidget.java

License:Open Source License

/**
 * Build the background image for the widget, according to the mode. Draw
 * the mode image and a solid line below it with the route color (if in
 * transit mode) or a dotted line (if in road mode). Set the
 * background-image to the generated image for the given widget.
 *///www.  jav a 2 s . c  om
public static void styleComponentWithMode(final Widget widget, TransportMode mode, String color) {
    PlannerResources resources = PlannerResources.INSTANCE;
    ImageResource baseImage = null;
    boolean road = false;
    switch (mode) {
    case WALK:
        road = true;
        color = "#666666";
        baseImage = resources.modeWalkPng();
        break;
    case BICYCLE:
        road = true;
        color = "#23C30B";
        baseImage = resources.modeBicyclePng();
        break;
    case BICYCLE_RENTAL:
        road = true;
        color = "#23C30B";
        baseImage = resources.modeBikeRentalPng();
        break;
    case CAR:
        road = true;
        color = "#333333";
        baseImage = resources.modeCarPng();
        break;
    default:
    case BUS:
        baseImage = resources.modeBusPng();
        break;
    case TRAM:
        baseImage = resources.modeTramPng();
        break;
    case FERRY:
        baseImage = resources.modeFerryPng();
        break;
    case GONDOLA:
        baseImage = resources.modeGondolaPng();
        break;
    case PLANE:
        baseImage = resources.modePlanePng();
        break;
    case RAIL:
        baseImage = resources.modeRailPng();
        break;
    case SUBWAY:
        baseImage = resources.modeSubwayPng();
        break;
    case TROLLEY:
        baseImage = resources.modeTrolleyPng();
        break;
    }
    final String url = baseImage.getSafeUri().asString();
    final Canvas canvas = Canvas.createIfSupported();
    if (canvas != null) {
        int width = baseImage.getWidth();
        int height = 1000;
        canvas.setCoordinateSpaceWidth(width);
        canvas.setCoordinateSpaceHeight(height);
        final Context2d context = canvas.getContext2d();
        context.setLineCap(LineCap.BUTT);
        if (road) {
            context.setStrokeStyle(CssColor.make(color));
            context.setLineWidth(4);
            for (int y = baseImage.getHeight(); y < 1000; y += 7) {
                context.moveTo(width / 2, y);
                context.lineTo(width / 2, y + 5);
            }
            context.stroke();
        } else {
            context.setStrokeStyle(CssColor.make("#000000"));
            context.setLineWidth(5);
            context.moveTo(width / 2, 0);
            context.lineTo(width / 2, height - 1);
            context.stroke();
            context.setStrokeStyle(CssColor.make(color));
            context.setLineWidth(4);
            context.moveTo(width / 2, 0);
            context.lineTo(width / 2, height - 1);
            context.stroke();
        }
        /*
         * HACK ALERT! Image.onLoad event does not fire up when using
         * internal resources (URL is internal data), but using the image
         * immediately does not work (image does not seems to be ready). We
         * defer the processing of the image rendering to a timer delayed a
         * bit.
         */
        Timer timer = new Timer() {
            @Override
            public void run() {
                Image image = new Image(url);
                ImageElement e = ImageElement.as(image.getElement());
                context.drawImage(e, 0, 0);
                String url2 = canvas.toDataUrl("image/png");
                widget.getElement().getStyle().setBackgroundImage("url('" + url2 + "')");
            }
        };
        timer.schedule(500);
    } else {
        widget.getElement().getStyle().setBackgroundImage("url('" + url + "')");
    }
}