[英]how can I capture html5 canvas in Android webview?
I have a question. 我有个问题。 when I got a capture webview, canvas area are not captured in my tablet... but the content is fine.
当我获得捕获Web视图时,平板电脑中没有捕获画布区域...但是内容还不错。 I don't understand... but weird thing is...
我不明白...但是奇怪的是...
If i give option 'webView.setLayerType(WebView.LAYER_TYPE_SOFTWARE, null);' 如果我给选项'webView.setLayerType(WebView.LAYER_TYPE_SOFTWARE,null);' pen data(canvas) appear after save, but the drawing is really really slow...
保存后出现笔数据(画布),但是绘图确实非常慢...
It's my source.. 这是我的资料
@SuppressLint("SetJavaScriptEnabled")
private void setWebviewInit(WebView webview) {
webview.setInitialScale(1);
webview.getSettings().setJavaScriptEnabled(true);
webview.getSettings().setUseWideViewPort(true);
webview.getSettings().setLoadWithOverviewMode(true);
webview.getSettings().setSupportZoom(true);
webview.getSettings().setBuiltInZoomControls(true);
webview.getSettings().setDisplayZoomControls(false);
webview.setWebViewClient(webviewclient);
webview.setWebChromeClient(new WebviewAlert());
webview.setWebChromeClient(new ChromeClient(this));
webview.addJavascriptInterface(new JIFace(), "ezandroid");
}
// capture //捕获
WebView webview = arrWebView.get(0);
webview.measure(MeasureSpec.makeMeasureSpec(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED),MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
webview.layout(0, 0, webview.getMeasuredWidth(), webview.getMeasuredHeight());
webview.setDrawingCacheEnabled(true);
webview.buildDrawingCache();
Bitmap bitmap = Bitmap.createBitmap(webview.getMeasuredWidth(), webview.getMeasuredHeight(),Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
Paint paint = new Paint();
int height = bitmap.getHeight();
canvas.drawBitmap(bitmap, 0, height, paint);
webview.draw(canvas);
if(penBitmap != null){
bitmap = combineBitmaps(bitmap, penBitmap, OVERLAY);
}
int fullHeight = webview.getMeasuredHeight();
int pageHeight = (int) (1122*WEBVIEW_SCALE_RATE);
int croppedStart = 0;
int cnt;
if(fullHeight == pageHeight){
cnt =1;
}else{
cnt = (fullHeight / pageHeight) + 1;
}
ArrayList<String> localFilePath = new ArrayList<String>();
for (int i = 0; i < cnt + 1; i++) { // 0: full Img, else: cropped
Bitmap croppedBitmap = null;
if (i!=cnt) {
if ((croppedStart + pageHeight) > webview.getMeasuredHeight()) {
int lastHeight = webview.getMeasuredHeight() - croppedStart;
croppedBitmap = Bitmap.createBitmap(bitmap, 0, croppedStart, webview.getMeasuredWidth(),lastHeight-1);
} else {
try{
croppedBitmap = Bitmap.createBitmap(bitmap, 0, croppedStart, webview.getMeasuredWidth(), pageHeight);
}catch(Exception e){
e.printStackTrace();
}
}
croppedStart += pageHeight;
}
if (bitmap != null) {
try {
String filePath = Environment.getExternalStorageDirectory().toString();
OutputStream out = null;
File file = new File(filePath, "test");
if(!file.exists()){
file.mkdir();
}
out = new FileOutputStream(filePath + "/test/consentImg" + i + ".jpeg");
localFilePath.add(filePath + "/test/consentImg" + i + ".jpeg");
if(i==cnt){
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
}else{
croppedBitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
}
if(croppedBitmap != null){
croppedBitmap.recycle();
croppedBitmap = null;
}
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
bitmap.recycle();
You need to call WebView.enableSlowWholeDocumentDraw() before creating any WebViews. 创建任何WebView之前,您需要调用WebView.enableSlowWholeDocumentDraw()。
@Override
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
WebView.enableSlowWholeDocumentDraw();
}
...
setContentView(layout);
...
...
}
Simply adding WebView.enableSlowWholeDocumentDraw() is not enough, I still got blank images saved to my sd card. 仅仅添加WebView.enableSlowWholeDocumentDraw()是不够的,我仍然将空白图像保存到我的SD卡中。 After a good amount of googling I found the solution.
经过大量的谷歌搜索后,我找到了解决方案。 You need to disable hardware acceleration for the webview, like this:
您需要为Webview禁用硬件加速,如下所示:
mWebview.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
UnSolved. 无法解决。 when saving, I make an image then I put it on the background of webview...
保存时,我制作图像,然后将其放在webview的背景上...
the problem is with hardware acceleration. 问题在于硬件加速。
turning it off should work. 将其关闭应该可以。
<application android:hardwareAccelerated="false">
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.