Git Product home page Git Product logo

android-html2bitmap's Introduction

Generate a bitmap from html on Android

Generates a bitmap of a defined width from html by rendering the content inside an off screen webview.

Does not block main thread.

Uses the webView available on the system so it's possibilities and limitations will be mirrored by the library.

Height will be calculated using WebView.getContentHeight()

Usage:

dependencies {
    implementation 'com.izettle:html2bitmap:1.9'
}
Bitmap bitmap = new Html2Bitmap.Builder(context: Context, content: WebViewContent).build().getBitmap();
  • context Used to create webview - application context is fine.
  • html The html that should be rendered to a bitmap.
  • width The requested width of the generated bitmap.
  • timeout Seconds before the job will terminate - if timeout is reached will call stopLoading() on the webview and return null.

Calls are blocking and hence recommended to be moved into a background thread.

Under the hood

Will wait for remote and local resources to load before generating bitmap i.e. "http://www.sample.com/image.jpg" will be downloaded before the screenshot is taken - take this into account before defining a timeout for the whole process.

Can be used anywhere you have access to a context. The purpose of the library was to perform its job inside a background thread and not to be tied to an activity.

Calls are synchronous to simplify usage in a background thread.

new AsyncTask<Void, Void, Bitmap>() {
    @Override
    protected Bitmap doInBackground(Void... voids) {
        String html = "<html><body><p>Hello world!</p><br/>Html bitmap</body><html>";
        
        return new Html2Bitmap.Builder().setContext(context).setContent(WebViewContent.html(html)).build().getBitmap();   
    }

    @Override
    protected void onPostExecute(Bitmap bitmap) {
        if (bitmap != null) {
            imageView.setImageBitmap(bitmap);
        }
    }
}.execute();

Note: Some of the methods on the webview are required to be run on the mainThread of the application - so some operations will be performed on that thread.

screenshot_1523608598

android-html2bitmap's People

Contributors

ambreensh avatar erikeelde avatar erikeeldeiz avatar jeppeman avatar kikju avatar marijangazica avatar warting avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.