B4J Question B4XImageView - Extract the Bitmap (?)

mmieher

Active Member
Licensed User
Longtime User
Very new to B4J. Already convinced it is the way to go for new projects. I cannot seem to do this in B4A either though.

I am not using the correct search words in the forum. How to get the newly rotated ImageView/Bitmap/B4Anything out of a B4XImageView?

"Pane is in unnamed module of loader app" - no idea whatsoever what this is telling me.

B4X:
Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
    Private xui As XUI

End Sub

Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    MainForm.RootPane.LoadLayout("Layout1")
    MainForm.Show
    
'    Dim p As B4XView = MainForm.RootPane
'    Log("p.NumberOfViews = " & p.NumberOfViews)        '    "1"

    ''Dim xImgView As B4XImageView = p.GetView(0)        '    type mismatch
    
    
'    Dim xView As B4XView = p.GetView(0)   
'    Dim bm As B4XBitmap = xView.GetBitmap                '    boom line
'    Log(bm.IsInitialized)
'    java.lang.ClassCastException: class anywheresoftware.b4j.objects.PaneWrapper$ConcretePaneWrapper$NonResizePane
'        cannot be cast to class javafx.scene.image.ImageView
'         (anywheresoftware.b4j.objects.PaneWrapper$ConcretePaneWrapper$NonResizePane [B]is in unnamed module[/B]
'        of loader 'app'; javafx.scene.image.ImageView is in module javafx.graphics of loader 'app')


'    Dim iv As ImageView = p.GetView(0)                 '    boom line
'    Log(iv.IsInitialized)                                              
'    same error
    
'    Dim xView As B4XView = p.GetView(0)
'    Dim iv As ImageView = xView                            '    boom line
'    Log(iv.IsInitialized)
'    same error
    
    Dim p As Pane = MainForm.RootPane
    Log(p.NumberOfNodes)                                      '    "1"
    Dim bm As ImageView = p.GetNode(0)              '    boom lines
    Log(bm.IsInitialized)
    '    same error
    
End Sub
 

Attachments

  • SmallProject.zip
    29.9 KB · Views: 159
Solution
This code works:

B4X:
Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    MainForm.RootPane.LoadLayout("Layout1")
    MainForm.Show
    
    B4XImageView1.Load(File.DirAssets, "hbavatar.jpg")
    Private bmp As B4XBitmap
    bmp = B4XImageView1.Bitmap
    
    xui.SetDataFolder("AllCases")
    Dim FileDir As String = xui.DefaultFolder

    bmp.WriteToStream(File.OpenOutput(FileDir, "InverseHunter.png", False), 100, "PNG")
End Sub

Unfortunately, the image added in the Designer is not added to the bitmap of the B4XImageView, you need to load it.

klaus

Expert
Licensed User
Longtime User
Simply that way:

B4X:
Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
    Private xui As XUI
    
    Private B4XImageView1 As B4XImageView
End Sub

Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    MainForm.RootPane.LoadLayout("Layout1")
    MainForm.Show
    
    Private bmp As B4XBitmap
    bmp = B4XImageView1.Bitmap   
End Sub
 
Upvote 1

mmieher

Active Member
Licensed User
Longtime User
I want to save this bitmap somewhere, but ...

B4X:
Waiting for debugger to connect...
Program started.
bmp.IsInitialized = false
B4X:
Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
    Private xui As XUI

    Private B4XImageView1 As B4XImageView
    
End Sub

Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    MainForm.RootPane.LoadLayout("Layout1")
    MainForm.Show
    
    Private bmp As B4XBitmap
    bmp = B4XImageView1.Bitmap
    Sleep(2000)        ' makes no diff
    
    Log("bmp.IsInitialized = " & bmp.IsInitialized)
    
End Sub
 
Upvote 0

mmieher

Active Member
Licensed User
Longtime User
B4X:
Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    MainForm.RootPane.LoadLayout("Layout1")
    MainForm.Show
    
    Private bmp As B4XBitmap
    bmp = B4XImageView1.Bitmap
    'Sleep(2000)        ' makes no diff
    
    Log("bmp.IsInitialized = " & bmp.IsInitialized)
    
    xui.SetDataFolder("AllCases")
    Dim FileDir As String = xui.DefaultFolder

    '  bmp.WriteToStream(File.OpenOutput(FileDir, "InverseHunter.jpg", False), 100, "JPEG")
    bmp.WriteToStream(File.OpenOutput(FileDir, "InverseHunter.png", False), 100, "PNG")
    
End Sub
B4X:
Waiting for debugger to connect...
Program started.
bmp.IsInitialized = false
Error occurred on line: 29 (Main)
java.lang.RuntimeException: Object should first be initialized (B4XBitmap).
    at anywheresoftware.b4a.AbsObjectWrapper.getObject(AbsObjectWrapper.java:49)
    at anywheresoftware.b4a.objects.B4XViewWrapper$B4XBitmapWrapper.WriteToStream(B4XViewWrapper.java:822)
    at b4j.example.main._appstart(main.java:101)
    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 b4j.example.main.start(main.java:38)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:846)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:455)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:428)
    at java.base/java.security.AccessController.doPrivileged(Native Method)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:427)
    at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
    at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:174)
    at java.base/java.lang.Thread.run(Thread.java:834)
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
This code works:

B4X:
Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    MainForm.RootPane.LoadLayout("Layout1")
    MainForm.Show
    
    B4XImageView1.Load(File.DirAssets, "hbavatar.jpg")
    Private bmp As B4XBitmap
    bmp = B4XImageView1.Bitmap
    
    xui.SetDataFolder("AllCases")
    Dim FileDir As String = xui.DefaultFolder

    bmp.WriteToStream(File.OpenOutput(FileDir, "InverseHunter.png", False), 100, "PNG")
End Sub

Unfortunately, the image added in the Designer is not added to the bitmap of the B4XImageView, you need to load it.
 
Upvote 0
Solution

mmieher

Active Member
Licensed User
Longtime User
Thank you, @klaus

B4X:
Waiting for debugger to connect...
Program started.
bmp.IsInitialized = true
Thank God.  There is still no use for an uninitialized object.
B4X:
Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    MainForm.RootPane.LoadLayout("Layout1")
    MainForm.Show
    
    Private bmp As B4XBitmap = xui.LoadBitmapResize(File.DirAssets, "hbavatar.jpg", B4XImageView1.mBase.Width,B4XImageView1.mBase.Height, False)
    Log("bmp.IsInitialized = " & bmp.IsInitialized)
    B4XImageView1.Bitmap = bmp
    
    xui.SetDataFolder("AllCases")
    Dim FileDir As String = xui.DefaultFolder

    bmp.WriteToStream(File.OpenOutput(FileDir, "InverseHunter.png", False), 100, "PNG")

    Log("Thank God.  There is still no use for an uninitialized object.")
    
End Sub
 
Upvote 0
Top