All Downloads are FREE. Search and download functionalities are using the official Maven repository.

annis.gui.components.ScreenshotMaker Maven / Gradle / Ivy

There is a newer version: 4.0.0-beta.4
Show newest version
/*
 * Copyright 2013 Corpuslinguistic working group Humboldt University Berlin.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package annis.gui.components;

import com.vaadin.annotations.JavaScript;
import com.vaadin.server.AbstractJavaScriptExtension;
import com.vaadin.server.ClientConnector;
import com.vaadin.ui.JavaScriptFunction;
import com.vaadin.ui.UI;
import elemental.json.JsonArray;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang3.Validate;
import org.json.JSONException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * A component that can make pure JavaScript based "screenshots".
 * 
 * Uses the http://html2canvas.hertzen.com/ library.
 * @author Thomas Krause 
 */
@JavaScript({"vaadin://jquery.js", "html2canvas.js", "screenshotmaker.js"})
public class ScreenshotMaker extends AbstractJavaScriptExtension
{
  private static final Logger log = LoggerFactory.getLogger(ScreenshotMaker.class);
  
  public ScreenshotMaker(final ScreenshotCallback callback)
  {
    Validate.notNull(callback);
    
    addFunction("finishedScreenshot", new JavaScriptFunction() 
    {
      @Override
      public void call(JsonArray arguments) throws JSONException
      {
        parseAndCallback(arguments.getString(0), callback);
      }
    });
  }

  @Override
  protected Class getSupportedParentType()
  {
    return UI.class;
  }
  
  /**
   * Takes a raw string representing the result of the toDataURL() function
   * of the HTML5 canvas and calls the callback with a proper mime type 
   * and the bytes of the image.
   * 
   * @see  http://www.whatwg.org/specs/web-apps/current-work/multipage/the-canvas-element.html#dom-canvas-todataurl




© 2015 - 2025 Weber Informatics LLC | Privacy Policy