B4J Question Draw a picture on another picture in Non UI envirement

johnerikson

Active Member
Licensed User
Longtime User
I wish to run this attached routine below in a Non-UI environment( without form).
What do I need to change and add to create a picture on a another picture?
Imageview1 as B4XView does not work in Non UI environment.

It is not necessary to see the image in a window, the important thing is to generate the image file!

XUI Views:
#Region Project Attributes
    #MainFormWidth: 600
    #MainFormHeight: 600
#End Region

Sub Process_Globals
    Private MainForm As Form
    Private Dialog As B4XDialog
    Private XUI As XUI
    Private Base As B4XView
    Private ImageView1 As B4XView  
    Type vec2(left As Float, top As Float, width As Float, height As Float)

End Sub

Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    MainForm.RootPane.LoadLayout("image") 'Load the layout file.
    MainForm.Show
    Base = MainForm.RootPane
    Dialog.Initialize (Base)
    Dialog.Title = "XUI Views"

    ImageOnImage
End Sub


Sub ImageOnImage

    Dim XUI As XUI
    Dim SrcPic As B4XBitmap = XUI.LoadBitmap(File.DirAssets,"DJI_0001.JPG")

    ImageView1.Left = 0
    ImageView1.Top = 0
    ImageView1.Width = SrcPic.Width
    ImageView1.Height = SrcPic.Height

    Dim logo As B4XBitmap = XUI.LoadBitmap(File.DirAssets,"edit.png")

    Dim v As vec2
    v.Initialize
    v.left = (ImageView1.Width/2)-(logo.Width/2)
    v.top = (ImageView1.Height/2)-(logo.Height/2)
    v.width = ImageView1.Width '*0.3
    v.height = ImageView1.Height'*0.3'    v.left = (Imageview1.Width/2)-(logo.Width/2)

    Dim rect As B4XRect
    rect.Initialize(v.left, v.top, v.left + logo.width, v.top + logo.height)
    ImageView1.SetBitmap(combine2bitmaps(SrcPic,logo,rect))
End Sub

Sub combine2bitmaps(firstBmp As B4XBitmap, secondBmp As B4XBitmap, SecondImageRect As B4XRect) As B4XBitmap
    Dim XUI As XUI
    Dim p As B4XView = XUI.CreatePanel("")
    p.SetLayoutAnimated(0,0,0,firstBmp.Width,firstBmp.Height)
  
    Dim c As B4XCanvas
    c.Initialize(p)
    c.DrawBitmap(firstBmp, c.TargetRect)
    c.DrawBitmap(secondBmp, SecondImageRect )
 
    Dim res As B4XBitmap = c.CreateBitmap 
    Dim Out As OutputStream
    Out = File.OpenOutput(File.DirApp,"ResCamper2.jpg", False)
    res.WriteToStream(Out, 100, "PNG")
    Out.Close
    c.Release
    Return  res
End Sub
 
Last edited:

TILogistic

Expert
Licensed User
Longtime User
save to file


B4X:
Private Sub Button1_Click
    Dim bmp As B4XBitmap = ImageOnImage
    B4XImageView1.Bitmap = bmp 'only view image
 
 
    'Save image to file
    XUI.SetDataFolder("image")
 
    Dim Out As OutputStream
    Out = File.OpenOutput(XUI.DefaultFolder, "Test.png", False)
    bmp.WriteToStream(Out, 100, "PNG")
    Out.Close

'    see dir image
    Log(XUI.DefaultFolder)

End Sub

1632176647943.png
1632176813562.png
 

Attachments

  • image.zip
    90.5 KB · Views: 117
Upvote 0

TILogistic

Expert
Licensed User
Longtime User
Note:
you can change the size of the image if you want

save to file resize

B4X:
Private Sub Button1_Click
    Dim bmp As B4XBitmap = ImageOnImage
    B4XImageView1.Bitmap = bmp 'only view image
    
    
    'Save image to file
    XUI.SetDataFolder("image")
    
    Dim Out As OutputStream
    Out = File.OpenOutput(XUI.DefaultFolder, "Test.png", False)
'    bmp.WriteToStream(Out, 100, "PNG")
    bmp.Resize(100, 100, True).WriteToStream(Out, 100, "PNG")
    
    Out.Close

'    see dir image
    Log(XUI.DefaultFolder)

End Sub
 
Upvote 0

EnriqueGonzalez

Well-Known Member
Licensed User
Longtime User
you could, but this has proven a difficult task. your best bet is to have another code that is called whenever you want to manipulate the image.

this is so, because you cant add the XUI library as proposed by oparra because it is a non ui enviroment.
 
Upvote 0

TILogistic

Expert
Licensed User
Longtime User
you could, but this has proven a difficult task. your best bet is to have another code that is called whenever you want to manipulate the image.

this is so, because you cant add the XUI library as proposed by oparra because it is a non ui enviroment.
sorry:

Just modify the published code.
 
Upvote 0

johnerikson

Active Member
Licensed User
Longtime User
After implementing the attached code from you, I get the following error when running it:
So, the last question about Canvas img from oparra is relevant.

B4X:
Sub combine2bitmaps(firstBmp As B4XBitmap, secondBmp As B4XBitmap, SecondImageRect As B4XRect) As B4XBitmap
    Dim XUI As XUI

    Dim p As B4XView = XUI.CreatePanel("")
    p.SetLayoutAnimated(0,0,0,firstBmp.Width,firstBmp.Height)
    
    Dim c As B4XCanvas
    c.Initialize(p)----------------------------------------------------------->  [B]This line caused error, see below[/B]
    c.DrawBitmap(firstBmp, c.TargetRect)
    c.DrawBitmap(secondBmp, SecondImageRect )
    
    Dim res As B4XBitmap = c.CreateBitmap   
    Dim Out As OutputStream
    Out = File.OpenOutput("C:\D\program\B4j\Server\ABMServer\EasyMedia\Objects\www\easymedia\images","ResCamper2.jpg", False)
    res.WriteToStream(Out, 100, "PNG")
    Out.Close
    c.Release
    Return  res
End Sub


Error occurs at line; 799
java.lang.RuntimeException: Internal graphics not initialized yet
at com.sun.glass.ui.Screen.getScreens(Screen.java:70)
at com.sun.glass.ui.Screen.getMainScreen(Screen.java:61)
at com.sun.javafx.font.PrismFontFactory.getSystemFontSize(PrismFontFactory.java:1911)
at com.sun.javafx.font.PrismFontLoader.getSystemFontSize(PrismFontLoader.java:241)
at javafx.scene.text.Font.getDefaultSystemFontSize(Font.java:71)
at javafx.scene.text.Font.getDefault(Font.java:86)
at javafx.scene.canvas.GraphicsContext$State.init(GraphicsContext.java:540)
at javafx.scene.canvas.GraphicsContext$State.<init>(GraphicsContext.java:530)
at javafx.scene.canvas.GraphicsContext.<init>(GraphicsContext.java:504)
at javafx.scene.canvas.Canvas.getGraphicsContext2D(Canvas.java:136)
at anywheresoftware.b4a.objects.B4XCanvas.Initialize(B4XCanvas.java:66)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at anywheresoftware.b4a.shell.Shell.runVoidMethod(Shell.java:676)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:240)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:167)
at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:108)
at anywheresoftware.b4a.shell.ShellBA.raiseEvent2(ShellBA.java:98)
at anywheresoftware.b4a.debug.Debug.delegate(Debug.java:64)
at GISIQON.EasyMedia.easypres._imageonimage(easypres.java:832)
at GISIQON.EasyMedia.easypres._mycheck_clicked(easypres.java:1098)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at anywheresoftware.b4a.debug.Debug.CallSub4(Debug.java:115)
at anywheresoftware.b4a.debug.Debug.CallSubNew2(Debug.java:81)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at anywheresoftware.b4a.keywords.Common.CallSubDebug2(Common.java:477)
at GISIQON.EasyMedia.easypres._page_parseevent(easypres.java:1351)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:632)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:237)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:167)
at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:108)
at anywheresoftware.b4a.shell.ShellBA.raiseEvent2(ShellBA.java:98)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:95)
at anywheresoftware.b4j.object.WebSocketModule$Adapter$1.run(WebSocketModule.java:126)
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:119)
at anywheresoftware.b4a.keywords.Common.StartMessageLoop(Common.java:170)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:309)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:167)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:108)
at anywheresoftware.b4a.shell.ShellBA.raiseEvent2(ShellBA.java:98)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:95)
at anywheresoftware.b4j.object.JServlet.createInstance(JServlet.java:65)
at anywheresoftware.b4j.object.BackgroundWorkersManager$1.run(BackgroundWorkersManager.java:21)
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:119)
at anywheresoftware.b4a.keywords.Common.StartMessageLoop(Common.java:170)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:309)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:167)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:108)
at anywheresoftware.b4a.shell.ShellBA.raiseEvent2(ShellBA.java:98)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:95)
at GISIQON.EasyMedia.main.main(main.java:29)
Fel uppstod vid linje; 452 (EasyPres)
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at anywheresoftware.b4a.keywords.Common.CallSubDebug2(Common.java:477)
at GISIQON.EasyMedia.easypres._page_parseevent(easypres.java:1351)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:632)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:237)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:167)
at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:108)
at anywheresoftware.b4a.shell.ShellBA.raiseEvent2(ShellBA.java:98)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:95)
at anywheresoftware.b4j.object.WebSocketModule$Adapter$1.run(WebSocketModule.java:126)
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:119)
at anywheresoftware.b4a.keywords.Common.StartMessageLoop(Common.java:170)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:309)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:167)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:108)
at anywheresoftware.b4a.shell.ShellBA.raiseEvent2(ShellBA.java:98)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:95)
at anywheresoftware.b4j.object.JServlet.createInstance(JServlet.java:65)
at anywheresoftware.b4j.object.BackgroundWorkersManager$1.run(BackgroundWorkersManager.java:21)
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:119)
at anywheresoftware.b4a.keywords.Common.StartMessageLoop(Common.java:170)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:309)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:167)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:108)
at anywheresoftware.b4a.shell.ShellBA.raiseEvent2(ShellBA.java:98)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:95)
at GISIQON.EasyMedia.main.main(main.java:29)
Caused by: java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
at anywheresoftware.b4a.debug.Debug.CallSub4(Debug.java:134)
at anywheresoftware.b4a.debug.Debug.CallSubNew2(Debug.java:81)
... 49 more
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at anywheresoftware.b4a.debug.Debug.CallSub4(Debug.java:115)
... 50 more
Caused by: java.lang.RuntimeException: Class instance was not initialized (b4ximageview)
at anywheresoftware.b4a.debug.Debug.shouldDelegate(Debug.java:45)
at GISIQON.EasyMedia.b4ximageview._setbitmap(b4ximageview.java:61)
at GISIQON.EasyMedia.easypres._mycheck_clicked(easypres.java:1101)
... 55 more
 
Upvote 0
Top