Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I capture the whole view into a Bitmap when using crosswalk to display webpage?

I use CrossWalk to display webpage, it works well. I have a feature to capture the whole content as bitmap to save into SDCard, but I cannot found a solution. Using TextureView can only capture a screen size bitmap, anyone has the same issue? BTW, I can capture the whole content using WebView.draw().

like image 622
handrenliang Avatar asked Apr 07 '15 03:04

handrenliang


2 Answers

Here's some pseudo code that may help. You can get the content height and width from the webview and then you can leverage webview.draw()...so something like the following.

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

Then you can output your bitmap file using bitmap.compress and output it to a file output stream

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

The only issue I foresee with this is that your bitmap may clip the content horizontally since WebView doesn't have a method to get the content width. Otherwise I think this should work.

like image 77
Developer Paul Avatar answered Oct 08 '22 00:10

Developer Paul


I never using crosswalk before, but I'm guessing the Crosswalk WebView is also descendant from android.view.View. If so, then you could use android view drawing cache. It look like this

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

Then as Paul said, save it through FileOutputStream

bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
like image 25
fchristysen Avatar answered Oct 07 '22 23:10

fchristysen