从web视图拍摄照片 [英] Capture picture from WebView

查看:111
本文介绍了从web视图拍摄照片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以监听全面完成的WebView页面加载,然后捕获加载页面的截图?

Is it possible to listen for WebView page load full completed and then capture screenshot of loaded page?

我试了两种方式,但他们都没有工作:

I tried two ways, but they both not working:

  • 使用 WebViewClient onPageFinished(web视图查看,字符串URL)
    它不工作(和它在文档描述的),因为照片可能是没有准备好就这一刻

  • using WebViewClient and onPageFinished(WebView view, String url).
    It doesn't work (and it described in docs) because Picture may be not ready it this moment

使用 WebView.PictureListener onNewPicture(web视图来看,画中画)
它也没有解决的问题,因为此方法调用未确定次。起初的时候,画面经常包含页面内容的一部分。我不知道路该怎么确定,目前的调用是最后的页面。

using WebView.PictureListener and onNewPicture(WebView view, Picture picture)
It also doesn't solve problem, because this method calls undetermined times. At first time, picture often contains only part of page content. And I don't know way how determine, that the current call is last for the page.

我要注意,这个问题是要找到合适的时机进行截图,并没有怎么可以做到的。

I want to notice, that the problem is to find right moment to make screenshot, and not how it can be done.

推荐答案

[注意,因为我回答了这个,onNewPicture已经去precated(见的http://developer.android.com/reference/android/webkit/WebView.PictureListener.html和<一href="http://stackoverflow.com/questions/8449210/what-does-this-method-is-de$p$pcated-mean-for-application-developers">What的确&QUOT;这个方法是去precated&意指为应用程序开发的)。不幸的是,是什么取代它,或在什么API级别它不支持任何信息。我想这意味着你使用这个风险自担。]

[Note that since I answered this, onNewPicture has been deprecated (see http://developer.android.com/reference/android/webkit/WebView.PictureListener.html and What does "This method is deprecated" mean for application developers). Unfortunately, there is no information on what replaces it, or at what API levels it was supported. I guess that means that you use this at your own risk.]

我认为你是正确的onNewPicture是正确的地方捕捉到加载WeView页面的截图,也是正确的,这是很难知道什么时候真正做到了拍摄。

I think you’re correct that onNewPicture is the right place to capture a screenshot of a loaded WeView page, but also correct that it is hard to know when to actually do the capture.

看来,WebView功能调用onNewPicture只要有任何图纸。例如,它会调用多次onNewPicture时,搜索栏是键盘输入模式,光标弹。同样,对于一些网页(如www.yelp.com/nyc)也反复onNewPicture调用,即使在页面完成在搜索框中闪烁的光标移动,可能是因为绘图。但在另一个极端,它会调用onNewPicture只有一次(例如,如果用户滴下来的的iGoogle项)。

It appears that WebView calls onNewPicture whenever there has been any drawing. For example, it calls onNewPicture repeatedly when the search bar is in keyboard entry mode and the cursor is flicking. Similarly, for some web pages (eg www.yelp.com/nyc) it calls onNewPicture repeatedly, even after the page has finished drawing, probably because of the flashing cursor in the Search box. But at the other extreme it will call onNewPicture only once (eg if the user drops down an iGoogle item).

因此​​,有没有简单的规则?我们采取的方法是,以

So there’s no easy rule? The approach we’ve taken is to

  • 在监控范围内所涉及/事件影响页面加载 - 如shouldOverrideUrl,onPageFinished,焦点改变,滚动的开始/结束,除了onNewPicture
  • 运行计时器(2secs运作良好)上onNewPicture,由新onNewPicture重置
  • 实施网页加载FSM使用事件和定时器期满作为输入,并移动通过一系列的状态/动作转换的,到了那里它决定是否真正有一个新的图象的点。

不是pretty的,但它的作品,除了极少数情况下,它抓住了同一张图片两次 - 并且没有情况下,它没有拍摄一张照片,它应该

Not pretty, but it works, with very few cases where it captures the same picture twice – and no cases where it fails to capture a picture where it should.

这篇关于从web视图拍摄照片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆