B4J Question B4J server, display dynamic images

Mostez

Well-Known Member
Licensed User
Longtime User
I want to display an image (generated QR code) dynamically on http page, I tried OK to display a test image with this code, but the problem is, I had to store the image in www folder and I don't know how to write files to this folder! or is it possible to do that with another method?

TIA

B4X:
Sub Handle(req As ServletRequest, resp As ServletResponse)
    resp.ContentType = "text/html"
    resp.Write($"<img src="/test.jpg" alt="Test" title="Test "style="width: 128px; height: 118px;" />"$)
 

udg

Expert
Licensed User
Longtime User
Maybe this can help you.
It was a specific solution for a case similar to what you need, altough built differently.
 
Upvote 0

Mostez

Well-Known Member
Licensed User
Longtime User
I started with this code to generate QR using QRGenerator Class, but I've got this error

Waiting for debugger to connect...
Program started.
2023-02-21 16:39:28.473:INFO ejs.Server:main: jetty-11.0.9; built: 2022-03-30T17:44:47.085Z; git: 243a48a658a183130a8c8de353178d154ca04f04; jvm 11.0.1+13
2023-02-21 16:39:28.583:INFO ejss.DefaultSessionIdManager:main: Session workerName=node0
2023-02-21 16:39:28.607:INFO ejsh.ContextHandler:main: Started o.e.j.s.ServletContextHandler@cb0755b{/,file:///D:/Mostafa/Programs/B4J/ESP_SERVER/Objects/www/,AVAILABLE}
2023-02-21 16:39:28.616:INFO ejs.RequestLogWriter:main: Opened D:\Mostafa\Programs\B4J\ESP_SERVER\Objects\logs\b4j-2023_02_21.request.log
2023-02-21 16:39:28.842:INFO ejs.AbstractConnector:main: Started ServerConnector@59d016c9{HTTP/1.1, (http/1.1)}{0.0.0.0:51042}
2023-02-21 16:39:28.865:INFO ejs.Server:main: Started Server@385c9627{STARTING}[11.0.9,sto=0] @1154ms
Emulated network latency: 100ms
Error occurred on line: 3 (QRGenerator)
java.lang.reflect.InvocationTargetException
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at ESP.SERVER.qrgenerator.innerInitialize(qrgenerator.java:21)
at ESP.SERVER.qrgenerator._initialize(qrgenerator.java:106)
at ESP.SERVER.testhandler._handle(testhandler.java:65)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:629)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:237)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:167)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:111)
at anywheresoftware.b4a.shell.ShellBA.raiseEvent2(ShellBA.java:100)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:98)
at anywheresoftware.b4j.object.JServlet$Handle.run(JServlet.java:146)
at anywheresoftware.b4a.keywords.SimpleMessageLoop.runMessageLoop(SimpleMessageLoop.java:47)
at anywheresoftware.b4a.StandardBA.startMessageLoop(StandardBA.java:43)
at anywheresoftware.b4a.shell.ShellBA.startMessageLoop(ShellBA.java:121)
at anywheresoftware.b4a.keywords.Common.StartMessageLoop(Common.java:180)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:309)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:167)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:111)
at anywheresoftware.b4a.shell.ShellBA.raiseEvent2(ShellBA.java:100)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:98)
at ESP.SERVER.main.main(main.java:29)
Caused by: java.lang.NoClassDefFoundError: javafx/scene/Node
at ESP.SERVER.qrgenerator._class_globals(qrgenerator.java:1354)
... 36 more
Caused by: java.lang.ClassNotFoundException: javafx.scene.Node
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:583)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
... 37 more

B4X:
Sub Handle(req As ServletRequest, resp As ServletResponse)
    Dim qr As QRGenerator
    qr.Initialize(400)
    Dim b As B4XBitmap = qr.Create("Hello world")
    Dim out As OutputStream = File.OpenOutput(File.DirApp & "\www\", "test.png", False)  
    b.WriteToStream(out, 100, "PNG")
    out.Close
 
Upvote 0

Quandalle

Member
Licensed User
I had to store the image in www folder and I don't know how to write files to this folder! or is it possible to do that with another method?
to access the www directory from the server code, use as directory name : srvr.StaticFilesFolder
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
I just made a quick attempt
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…