我正在尝试在 Java (Android) 中生成一个 webview,并将一个 Android 位图返回给 Qt C++。到目前为止,我得到了一个无效的对象。
静态java函数:(在android/src/com/parity/jni/JniExtras.java中)
package com.parity.jni;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.webkit.WebView;
public class JniExtras {
public static Bitmap htmlToBitmap(String html, float scale, int w, int h, int r, int g, int b, int a) {
int color = (r << 24) | (g << 16) | (b << 8) | a;
WebView web = new WebView(null);
web.loadData(html, "text/html", "UTF-8");
web.layout(0, 0, (int)((float)w / scale), (int)((float)h / scale));
Bitmap bitmap = Bitmap.createBitmap(w, h, Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
Paint paint = new Paint();
paint.setColor(color);
canvas.drawPaint(paint);
canvas.scale(scale, scale);
web.draw(canvas);
return bitmap;
}
}
我正在尝试通过以下方式获取 QT 中的位图:
QAndroidJniObject jni_html = QAndroidJniObject::fromString(html);
QAndroidJniObject bitmap = QAndroidJniObject::callStaticObjectMethod(
"com/parity/jni/JniExtras",
"htmlToBitmap",
"(Ljava/lang/String;FIIIIII)Ljava/lang/Object;",
jni_html.object<jstring>(),
scale,
w, h,
r, g, b, a);
if (!bitmap.isValid()) {
qDebug("Java call failed to get android bitmap");
}
谁能告诉我我做错了什么?
编辑1:
与此同时,我试图让 WebView 绘制到 Android Studio 项目中的位图。这是我到目前为止所得到的:
package com.parity.webviewtest;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.webkit.WebView;
import android.widget.ImageView;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
String tag = "WLGfx";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
int wid = 1000;
int hgt = 1000;
String html = "<div align=\"center\"><font face=\"FreeMono\"><b><font color=\"#FF0000\"><font size=\"6\">Title of report</font></font></b></font><br></div><font face=\"FreeSerif\" color=\"#0000FF\">Now as we enter a new era of stuff to do some really amazing and weird things, we notice that our distractions are seemingly taking our focus away from the smaller and more important things that we should be thoroughly immersed in. Either way, this will test out a formatted paragraph.</font><font face=\"FreeSerif\"><br></font><ol><li><font face=\"FreeSerif\" color=\"#008000\">Throw away old bins</font></li><li><font face=\"FreeSerif\" color=\"#008000\">Buy new bins</font></li><li><font face=\"FreeSerif\" color=\"#008000\">Fill the new bins</font></li></ol><div align=\"center\"><font face=\"FreeSerif\" color=\"#993300\"><i>The quick brown fox does stuff</i></font><br></div>";
//String html = "Some line of text to test out...";
Log.d(tag, html);
Context context = this.getApplicationContext();
WebView web = new WebView(context);
web.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
web.layout(0,0,wid,hgt);
web.loadData(html, "text/html", "UTF-8");
web.buildDrawingCache();
Paint paint = new Paint();
paint.setColor(0xffffffff);
Bitmap bitmap = Bitmap.createBitmap(wid, hgt, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
web.draw(canvas);
ImageView img = (ImageView)findViewById(R.id.imageView);
img.setImageBitmap(bitmap);
}
}
我仍然得到一个空白位图。
编辑2:
正如评论中所建议的,我已经尝试了 capturePicture() 和 getDrawingCache() 并且都没有在 Android Studio 测试项目中工作:
Context context = getApplicationContext();
WebView web = new WebView(context);
web.setEnabled(true);
//web.loadUrl(html);
web.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
web.layout(0,0,wid,hgt);
web.loadData(html, "text/html", "UTF-8");
web.setDrawingCacheEnabled(true);
web.buildDrawingCache();
Picture picture = web.capturePicture();
Bitmap bitmap = Bitmap.createBitmap(picture.getWidth(), picture.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
//picture.draw(canvas);
canvas.drawPicture(picture);
Log.d(tag, "Picture: " + picture.getWidth() + "x" + picture.getHeight());
//Bitmap bitmap = Bitmap.createBitmap(web.getDrawingCache());
//web.setDrawingCacheEnabled(false);
//Paint paint = new Paint();
//paint.setColor(0xffffffff);
//Bitmap bitmap = Bitmap.createBitmap(wid, hgt, Bitmap.Config.ARGB_8888);
//Canvas canvas = new Canvas(bitmap);
//web.draw(canvas);
ImageView img = (ImageView)findViewById(R.id.imageView);
img.setImageBitmap(bitmap);
我想将 html 渲染为位图的主要原因是,我只需要将简单的 html 字符串通过网络传输到设备,而无需让服务器渲染图像并发送它们。