我正在尝试使用Java(Android)生成webview并将Android Bitmap返回到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项目中的Bitmap。这是我到目前为止所得到的:
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字符串通过网络传输到设备,而不是让服务器渲染图像并发送它们。