Android Question how to get Canvas bitmap from JavaScript ?

peacemaker

Expert
Licensed User
Longtime User
HI, All

If web-page makes the image by drawing on Canvas - how to get it ?
Source:
B4X:
<canvas id="canvas" width="1024" height="500" class="chartjs-render-monitor" style="display: block;"></canvas>

B4X:
    Dim Javascript As String = "B4A.CallSub('Page_finished', true, document.getElementById('canvas'))"
    WebViewExtras1.executeJavascript(Javascript)
End Sub

Sub Page_finished(HTML As String)
    Log(HTML)
....

Here the result is "undefined". How correctly to do ?
 

peacemaker

Expert
Licensed User
Longtime User
SOLVED:

B4X:
Sub Process_Start
  Dim Javascript As String = "B4A.CallSub('Page_finished', true, document.getElementById('canvas').toDataURL())"
    WebViewExtras1.executeJavascript(Javascript)
End Sub


Sub wvc_PageFinished(URL As String)
    Sleep(1000)    'required: time for drawing on canvas
    Process_Start
End Sub


Sub Page_finished(HTML As String)
    Dim a As String = HTML.Replace("data:image/png;base64,", "")
    Dim su As StringUtils
 
    Dim FileBytes() As Byte = su.DecodeBase64(a)
    Dim InStream As InputStream
    InStream.InitializeFromBytesArray(FileBytes, 0, FileBytes.Length)
    Dim b As Bitmap
    b.Initialize2(InStream)
    InStream.Close
    ImageView1.Bitmap = b
 
Upvote 0
Top