在Android中从HTML生成位图
如何在Android中从HTML生成位图 是否可以使用在Android中从HTML生成位图,android,html,bitmap,webview,Android,Html,Bitmap,Webview,如何在Android中从HTML生成位图 是否可以使用WebView进行此操作,或者是否有更好的方法(例如直接使用WebView渲染引擎)?怎么做 我想实现以下方法 public Bitmap toBitmap(Context context, String html, Rect rect); …其中,html是要渲染的html,rect是所需位图的帧。您可以使用绘制方法,让它在您选择的位图中绘制。我举了一个例子,别忘了您清单的internet和外部存储权限: 公共类MainActivity
WebView
进行此操作,或者是否有更好的方法(例如直接使用WebView
渲染引擎)?怎么做
我想实现以下方法
public Bitmap toBitmap(Context context, String html, Rect rect);
…其中,
html
是要渲染的html,rect
是所需位图的帧。您可以使用绘制方法,让它在您选择的位图中绘制。我举了一个例子,别忘了您清单的internet和外部存储权限:
公共类MainActivity扩展了活动{
私有网络视图;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
mWebView=新的WebView(此);
setContentView(mWebView);
mWebView.loadUrl(“http://tea.ch");
}
@凌驾
公共布尔onKeyDown(int-keyCode,KeyEvent事件){
if(keyCode!=KeyEvent.keyCode\u BACK)返回super.onKeyDown(keyCode,event);
位图bm=Bitmap.createBitmap(200300,Bitmap.Config.ARGB_8888);
画布c=新画布(bm);
mWebView.图纸(c);
OutputStream=null;
试一试{
stream=newfileoutputstream(Environment.getExternalStorageDirectory()+“/teach.png”);
压缩(CompressFormat.PNG,80,stream);
如果(stream!=null)stream.close();
}捕获(IOE异常){
}最后{
bm.recycle();
}
返回super.onKeyDown(keyCode,event);
}
}
为什么不使用WebView方法:capturePicture(),该方法返回图片,并且从API级别1开始可用
它返回整个文档的图片
然后,您可以使用矩形裁剪结果并保存位图。一种同步方法,使用WebView从HTML字符串生成位图,可在异步任务中使用:
public Bitmap getBitmap(final WebView w, int containerWidth, int containerHeight, final String baseURL, final String content) {
final CountDownLatch signal = new CountDownLatch(1);
final Bitmap b = Bitmap.createBitmap(containerWidth, containerHeight, Bitmap.Config.ARGB_8888);
final AtomicBoolean ready = new AtomicBoolean(false);
w.post(new Runnable() {
@Override
public void run() {
w.setWebViewClient(new WebViewClient() {
@Override
public void onPageFinished(WebView view, String url) {
ready.set(true);
}
});
w.setPictureListener(new PictureListener() {
@Override
public void onNewPicture(WebView view, Picture picture) {
if (ready.get()) {
final Canvas c = new Canvas(b);
view.draw(c);
w.setPictureListener(null);
signal.countDown();
}
}
});
w.layout(0, 0, rect.width(), rect.height());
w.loadDataWithBaseURL(baseURL, content, "text/html", "UTF-8", null);
}});
try {
signal.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
return b;
}
它有一些限制,但这只是一个开始。此示例演示如何捕获webView内容最后一张图片(它等待webView完成渲染图片),这是使用Android将HTML转换为PNG的示例 活动代码
public class HtmlViewer extends Activity {
private String HTML;
private Context ctx;
private Picture pic = null;
private int i=0; suppose this is the last pic
private int oldi = 0;
private Timer myTimer; // timer for waiting until last picture loaded
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_html_viewer);
Intent intent = getIntent();
HTML = intent.getStringExtra("HTML");
ctx = this;
WebView wv = (WebView)findViewById(R.id.webView1);
wv.setPictureListener(new PictureListener(){
public void onNewPicture(WebView view, Picture picture) {
Log.w("picture", "loading.." + String.valueOf(view.getProgress()));
pic = picture;
i++;
}
});
wv.loadData(HTML, "text/html; charset=utf-8", null);
wv.setWebViewClient(new WebViewClient()
{
public void onPageFinished(WebView wv, String url)
{
Picture p = wv.capturePicture();
myTimer = new Timer();
myTimer.schedule(new TimerTask() {
@Override
public void run() {
if (i > oldi)
oldi = i;
else
if (i != 0)
{
Log.w("picture", "finished");
cancel();
Picture picture = pic;
Log.w("picture", "onNewPicture- Height"+ picture.getHeight());
Log.w("picture", "onNewPicture- Width"+ picture.getWidth());
File sdCard = Environment.getExternalStorageDirectory();
if (picture != null)
{
Log.w("picture", " P OK");
Bitmap image = Bitmap.createBitmap(picture.getWidth(),picture.getHeight(), Config.ARGB_8888);
Canvas canvas = new Canvas(image);
picture.draw(canvas);
Log.w("picture", "C OK");
if (image != null) {
Log.w("picture", "I OK");
ByteArrayOutputStream mByteArrayOS = new ByteArrayOutputStream();
image.compress(Bitmap.CompressFormat.PNG, 90, mByteArrayOS);
try {
File file = new File(sdCard, "AccountView.PNG");
FileOutputStream fos = new FileOutputStream(file);
fos.write(mByteArrayOS.toByteArray());
fos.flush();
fos.close();
Log.w("picture", "F OK " + String.valueOf(mByteArrayOS.size()) + " ? " + String.valueOf(file.length()));
Intent sharingIntent = new Intent(Intent.ACTION_SEND);
Uri screenshotUri = Uri.fromFile(file);
sharingIntent.setType("image/png");
sharingIntent.putExtra(Intent.EXTRA_STREAM, screenshotUri);
startActivity(Intent.createChooser(sharingIntent, getResources().getString(R.string.ACCOUNT_VIEW_TITLE)));
((Activity)ctx).finish();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
}, 0, 1000);
Log.w("picture", "done");
loadcompleted = true;
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_html_viewer, menu);
return true;
}
}
布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".HtmlViewer" >
<WebView
android:id="@+id/webView1"
android:layout_width="match_parent"
android:layout_height="match_parent" />
这是一个很好的库,可用于将任何HTML内容转换为位图 它同时支持URL和HTML字符串
您可以考虑添加几百个单词来解释“从HTML生成动态位图”意味着什么。简而言之,我想从呈现的HTML中生成位图。我删除了“dynamic”并重新措辞,以防混淆。FWIW,以下是我尝试的解决方案:因此,没有办法跳过WebView并直接使用其渲染引擎?它是一个视图,您只能保留它的一个实例作为渲染引擎。您必须自己将渲染的大小设置为Laoyout参数,如:LayoutParams params=new LayoutParams(500500);mWebView.setLayoutParams(参数);mWebView.布局(0,0,500,500);我刚刚测试过,您只需要设置mWebView.layout(0,0500500)+1用于测试。:)如果有效,将尝试接受答案。不幸的是,此解决方案仅在活动中有效。您知道如何在工作线程中实现它吗?请注意,
capturePicture()。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".HtmlViewer" >
<WebView
android:id="@+id/webView1"
android:layout_width="match_parent"
android:layout_height="match_parent" />