【问题标题】:Capturing android webview image and saving to png/jpeg捕获 android webview 图像并保存为 png/jpeg
【发布时间】:2013-03-12 00:41:06
【问题描述】:

我正在尝试在 android/phonegap 应用程序上实现 css3 页面翻转效果。为此,我需要将当前的 webview 动态保存为 png 或 jpeg,以便可以将其加载到页面翻转 html 中的 div 中。我注意到 android 文档中的 Picture 类,但我不确定它是否可以转换和保存。这可以通过JS完成吗?有什么想法吗?

谢谢

【问题讨论】:

    标签: android html cordova


    【解决方案1】:

    尝试以下代码来捕获 webview 并将 jpg 保存到 sdcard。

    webview.setWebViewClient(new WebViewClient() {
    
        @Override
        public void onPageFinished(WebView view, String url) {
            Picture picture = view.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( "/sdcard/"  + "page.jpg" );
                if ( fos != null ) {
                    b.compress(Bitmap.CompressFormat.JPEG, 90, fos );
                    fos.close();
                }
            } 
            catch( Exception e ) {
                System.out.println("-----error--"+e);
            }
        }
    });
    
    webview.loadUrl("http://stackoverflow.com/questions/15351298/capturing-android-webview-image-and-saving-to-png-jpeg");
    

    【讨论】:

      【解决方案2】:

      View 捕获为图像的最简单方法(据我所知)是创建一个新的Bitmap 和一个新的Canvas。然后,只需让您的 WebView 在您自己的 Canvas 上绘制自己,而不是 Activity 的默认值。

      伪代码:

      Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
      Canvas canvas = new Canvas(bitmap);
      myWebView.draw(canvas);
      //save your bitmap, do whatever you need
      

      【讨论】:

      • 好的,我会试一试。你知道我怎么能在 javascript 中实现它吗?编写一个 java 类并使用 appView.addJavascriptInterface() 工作吗?
      • 没错,您需要回调到 Java 领域才能完成这项工作。
      猜你喜欢
      • 1970-01-01
      • 2013-06-13
      • 2011-06-02
      • 1970-01-01
      • 1970-01-01
      • 2017-04-16
      • 1970-01-01
      • 1970-01-01
      • 2014-01-08
      相关资源
      最近更新 更多