使用人行横道显示网页时,如何将整个视图捕获到位图中?

时间:2015-04-07 03:05:55

标签: android android-webview crosswalk-runtime

我使用CrossWalk显示网页,效果很好。我有一个功能,将整个内容捕获为位图以保存到SDCard,但我找不到解决方案。使用TextureView只能捕获屏幕大小的位图,任何人都有同样的问题吗?顺便说一句,我可以使用WebView.draw()来捕获整个内容。

3 个答案:

答案 0 :(得分:3)

这里有一些可能有用的伪代码。您可以从webview获取内容高度和宽度,然后您可以利用webview.draw()...如下所示。

Bitmap  bitmap = Bitmap.createBitmap( webView.getWidth(), webView.getContentHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
webView.draw(canvas);

然后,您可以使用bitmap.compress输出位图文件并将其输出到文件输出流

//fos is a FileOutputStream, you can use something else if needed. bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);

我预见到的唯一问题是你的位图可能会水平剪辑内容,因为WebView没有获取内容宽度的方法。否则我认为这应该有效。

答案 1 :(得分:3)

我之前从未使用过人行横道,但我猜测Crosswalk WebView也是android.view.View的后代。如果是这样,那么你可以使用android视图绘图缓存。它看起来像这样

yourWebView.setDrawingCacheEnabled(true);
Bitmap bmp = yourWebView.getDrawingCache();

然后保罗说,通过FileOutputStream保存它

bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);

答案 2 :(得分:2)

You can't capture XWalkView image by standard ways as described in answers above because Crosswalk XWalkView works with hardware layer (i.e. SurfaceView or TextureView). But you can use getBitmap() method of TextureView class to get it.

In short, you should enable using of TextureView in Crosswalk before XWalkView will be initialized (in Application subclass for example) with:

XWalkPreferences.setValue(XWalkPreferences.ANIMATABLE_XWALK_VIEW, true);

And find TextureView in views tree to call getBitmap() on it.

Something like that:

/**
    * Returns TextureView which is used in XWalkView
    *
    * @param group
    * @return
    */
private TextureView findXWalkTextureView(ViewGroup group) {
    int childCount = group.getChildCount();
    for (int i = 0; i < childCount; i++) {
        View child = group.getChildAt(i);
        if (child instanceof TextureView) {
            String parentClassName = child.getParent().getClass().toString();
            boolean isRightKindOfParent = (parentClassName.contains("XWalk"));
            if (isRightKindOfParent) {
                return (TextureView) child;
            }
        } else if (child instanceof ViewGroup) {
            TextureView textureView = findXWalkTextureView((ViewGroup) child);
            if (textureView != null) {
                return textureView;
            }
        }
    }

    return null;
}

/**
    * Example of capturing image from XWalkView based on TextureView
    *
    * @return
    */
public Bitmap captureImage() {
    if (mXWalkView != null) {
        Bitmap bitmap = null;

        boolean isCrosswalk = false;
        try {
            Class.forName("org.xwalk.core.XWalkView");
            isCrosswalk = true;
        } catch (Exception e) {
            e.printStackTrace();
        }

        if (isCrosswalk) {
            try {
                TextureView textureView = findXWalkTextureView(mXWalkView);
                bitmap = textureView.getBitmap();
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {
            bitmap = Bitmap.createBitmap(mXWalkView.getWidth(), mXWalkView.getHeight(), Bitmap.Config.ARGB_8888);
            Canvas c = new Canvas(bitmap);
            mXWalkView.draw(c);
        }

        return bitmap;
    } else {
        return null;
    }
}

Check this code: https://github.com/gitawego/cordova-screenshot/blob/master/src/android/Screenshot.java for more details.