WebView drawn to Android Bitmap from java to Qt C++

446 views Asked by At

I'm trying to generate a webview in Java (Android) and return an Android Bitmap back to Qt C++. So far I am getting an invalid object.

The static java funtion: (in 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;
    }
}

And I'm trying to fetch the Bitmap in QT with:

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");
}

Can anybody tell me what it is I am doing wrong?

Edit 1:

In the meantime I am trying to get the WebView to draw to a Bitmap in an Android Studio project. Here's what I've got so far:

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);
    }

}

Still I am getting a blank bitmap.

Edit 2:

As suggested in comments, I've tried both capturePicture() and getDrawingCache() and neither are working in the Android Studio test project:

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);

The main reason I want to render html to a bitmap is so I only have to transfer simple html strings across the network to devices and not have the server render the images and send them.

0

There are 0 answers