【问题标题】:How to take screenshot of webview in Android如何在Android中截取webview的屏幕截图
【发布时间】:2014-06-01 16:55:49
【问题描述】:

我在 webview 中的 html5 画布上画了一些线条,并尝试使用以下代码截取 webview 的屏幕截图...

WebView webView = (WebView) findViewById(R.id.webview);
webView.setDrawingCacheEnabled(true);
Bitmap screenshot = Bitmap.createBitmap(webView.getDrawingCache());
webView.setDrawingCacheEnabled(false);
File myFile = new File(Environment.getExternalStorageDirectory().getPath()+ "/myfolder");
if(!myFile.exists()) {
    myFile.mkdir();
}
imagePath = myFile.getAbsolutePath() + "/myimage001.png";
FileOutputStream fos = null;
try {
    fos = new FileOutputStream(imagePath);
    if ( fos != null ) {
        screenshot.compress(Bitmap.CompressFormat.PNG, 100, fos);
        fos.close();
    }
} catch( Exception e ) {
    e.printStackTrace();
}

但是当我打开该图像时,它看起来是空的。请帮忙。

【问题讨论】:

    标签: android html canvas webview


    【解决方案1】:

    如下操作

    private void TakeScreenshot()
    {
     Picture picture = webview.capturePicture();
                        Bitmap  b = Bitmap.createBitmap( picture.getWidth(),
                        picture.getHeight(), Bitmap.Config.ARGB_8888);
                        Canvas c = new Canvas( b );
    
                        picture.draw( c );
                        FileOutputStream fos = null;
                        try {
    
                            fos = new FileOutputStream( "mnt/sdcard/yahoo.jpg" );
                                if ( fos != null )
                                {
                                    b.compress(Bitmap.CompressFormat.JPEG, 100, fos);
    
                                    fos.close();
                                }
                            }
                       catch( Exception e )
                       {
    
                       }
    }
    

    注意:在WebView 加载完成后截取屏幕截图,否则会出现空白屏幕。

    【讨论】:

    • 这仅将 webview 的背景捕获为白屏。它没有 html5 画布元素内容。 :-(
    • 当我在不使用 html5 画布标签的情况下捕获 webview 时,我的代码运行良好。使用 htm5 canvas 标签后开始产生问题。
    【解决方案2】:

    我也发现直接从 Android 应用程序截取显示 HTML5 画布的 webview 的屏幕截图确实不可靠。但是有一个解决方法-

    您可以使用 window.JSInterface.(params) 从 javascript 调用 android java 函数。您可以使用 canvas.toDataURL() 获取画布图像的 base64 编码字符串,并将其用作传递给函数的参数。在 android 函数中,捕获字符串,对其进行解码并获得图像。

    唯一需要注意的是,仅画布区域将作为图像出现,而不是整个设备屏幕。此外,如果画布没有设置背景颜色,则捕获的 png 图像将具有透明背景。但这些都可以通过应用代码中的图像处理轻松解决。

    例如在这个游戏应用程序中,游戏在 web 视图中显示的 HTML5 画布中运行。游戏结束时,“分享分数”选项使用上述技术捕获要分享的画布图像。 https://play.google.com/store/apps/details?id=com.skipser.flappytrex

    【讨论】:

      【解决方案3】:

      截屏:

      File srcFile= driver.getScreenshotAs(OutputType.FILE);
      String filename = UUID.randomUUID().toString();
      File targetFile = new File("D:\\Appium\\" + filename + ".png");
      FileUtils.copyFile(srcFile, targetFile);
      System.out.println(targetFile);enter code here
      

      【讨论】:

        猜你喜欢
        • 2014-01-22
        • 1970-01-01
        • 2017-10-09
        • 2021-07-12
        • 1970-01-01
        • 2016-03-30
        • 2013-01-11
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多