如何在Android AsyncTask中将HTML字符串转换为图像?

时间:2016-02-22 20:49:40

标签: java android webview

有没有人知道如何在Android AsyncTask或后台线程中将HTML页面转换为图像?

环境:Android Studio 2.0 Beta 5,Android平板电脑,API版本为18,19

我尝试过使用两种解决方案的WebView控件,但都失败了。

  • 解决方案1:Javascript

它不起作用,getDrawingCache(true)总是返回null。

public void generateImage()
{
    final WebView mWebView = new WebView(this);
    mWebView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
    mWebView.getSettings().setDefaultZoom(WebSettings.ZoomDensity.MEDIUM);
    mWebView.setInitialScale(150);
    mWebView.getSettings().setLoadWithOverviewMode(true);

    mWebView.getSettings().setJavaScriptEnabled(true);
    mWebView.setDrawingCacheEnabled(true);
    mWebView.addJavascriptInterface(new JavaScriptInterface(mWebView), "Android");

    String html = "<html><body onload=\"Android.captureImage()\">hello world<p>bbb</p></body></html>";
    mWebView.loadDataWithBaseURL("", html, "text/html", "utf-8", "");
}

public class JavaScriptInterface 
{
    private WebView mWebView;

    public JavaScriptInterface(WebView webView) {
        mWebView = webView;
    }

    @JavascriptInterface
    public void captureImage()
    {
        Bitmap b = mWebView.getDrawingCache(true);
        if(b==null){
            System.out.println("... b is null");
            return;
        }
        System.out.println("... height "+b.getHeight()+"    width "+b.getWidth());
    }
}
  • 解决方案2:WebViewClient + PictureListener

这个适用于Android API 19.但是在Android API 18上,回调&#34; onNewPicture&#34;永远不会被召唤。

public void generateImage()
{
    final WebView mWebView = new WebView(this);
    mWebView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
    mWebView.getSettings().setDefaultZoom(WebSettings.ZoomDensity.MEDIUM);
    mWebView.setInitialScale(150);
    mWebView.getSettings().setLoadWithOverviewMode(true);

    String html = "<html><body>hello world<p>bbb</p></body></html>";
    mWebView.loadDataWithBaseURL("", html, "text/html", "utf-8", "");

    mWebView.setWebViewClient(new WebViewClient() {
        @Override
        public void onPageFinished(WebView view, String url) {
            System.out.println("............ onPageFinished");
            super.onPageFinished(view, url);
            view.clearCache(true);
        }
    });

    mWebView.setPictureListener(new WebView.PictureListener() {
        @Override
        public void onNewPicture(WebView view, Picture picture)
        {
            if (picture == null) {
                return;
            }

            Picture pic = mWebView.capturePicture();
            if(pic==null){
                System.out.println("............ pic is null");
                return;
            }
            System.out.println("onNewPicture............ width ["+pic.getWidth()+"]  hieght ["+pic.getHeight()+"]");
            final Bitmap bm = Bitmap.createBitmap(pic.getWidth(), pic.getHeight(), Bitmap.Config.ARGB_8888);
            Canvas canvas = new Canvas(bm);
            canvas.drawPicture(pic);
        }
    });
}

如果我按照以下方式创建WebView控件,我可以获得图像

final WebView mWebView = (WebView) findViewById(R.id.webview);

但是我必须在AsyncTask或后台线程中转换html页面字符串。没有用户界面。有人知道如何实施吗?

感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

首先为WebView设置布局参数,然后再调用Webview.measureWebView.layout方法,然后在位图上绘制缓存:

webView.setWebViewClient(new WebViewClient() {
            public boolean shouldOverrideUrlLoading(final WebView view, String url) {
                return false;
            }
            @Override
            public void onPageFinished(final WebView view, String url) {
                Log.i("HTML", "page finished loading " + url); 
                view.measure(View.MeasureSpec.makeMeasureSpec(512, View.MeasureSpec.EXACTLY), View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
                view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
                final Bitmap bmp = Bitmap.createBitmap(view.getMeasuredWidth(), view.getMeasuredHeight(), Bitmap.Config.ARGB_8888);
                final Canvas canvas = new Canvas(bmp);
                canvas.drawBitmap(view.getDrawingCache(), 0, 0, new Paint());
                // bitmap is ready
            }
);

重要提示:确保WebView身高和宽度不为零。