开发者

Convert HTML to image (any format) on Android [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.

Want to improve this question? Add details and clarify the problem by editing this post.

开发者_如何学运维

Closed 2 years ago.

Improve this question

Does anyone know how to convert html code (with images within it) to image on Android? I know how to make it on Java using JLabel/JEditorPane and BufferedImage, but now should make the same with Android.


The answer of @Neo1975 is worked for me, but to avoid using deprecated method WebView.capturePicture() you could use the following method to capture content of WebView.

/**
* WevView screenshot
*
* @param webView
* @return
*/
private static Bitmap screenshot(WebView webView) {
  webView.measure(View.MeasureSpec.makeMeasureSpec(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED),View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
    webView.layout(0, 0, webView.getMeasuredWidth(), 
    webView.getMeasuredHeight());
    webView.setDrawingCacheEnabled(true);
    webView.buildDrawingCache();
    Bitmap bitmap = Bitmap.createBitmap(webView.getMeasuredWidth(), 
    webView.getMeasuredHeight(), Bitmap.Config.ARGB_8888);

    Canvas canvas = new Canvas(bitmap);
    Paint paint = new Paint();
    int iHeight = bitmap.getHeight();
    canvas.drawBitmap(bitmap, 0, iHeight, paint);
    webView.draw(canvas);
    return bitmap;
}

The idea behind it is using WebView.draw(Canvas) method.


For this task I use the follow trick: I used a webview to parse HTML and call the method capturePicture on WevView object to extract a Picture of HTML, so I can suggest you the follow sniplet:

WebView wv = new WebView(this);
wv.loadData("<html><body><p>Hello World</p></body></html>");
Picture p = wv.capturePicture();

I hope this can help, but if you foun a different way to solve it please post it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜