B4J Question Error FillImageToView and FitImageToView

ivanomonti

Expert
Licensed User
Longtime User
in both ways it gives me error, what gives results is the classic method but not valid for me.

B4X:
Sub bt_Action
    
    Dim bt As Button  = Sender
    
    If bt.Text = "Close" Then
        Main.openimage_animate
        Main.menusx_enabled
    End If
    
    If bt.Text = "Open" Then
        Dim fc As FileChooser
        fc.Initialize
        fc.SetExtensionFilter("Image document", Array As String("*.jpg"))
        Dim docname As String = fc.ShowOpen(Main.MainForm)
        Dim bmp As B4XBitmap = Main. xui.LoadBitmap(docname,"")
        If bmp = Null Then Return
        Dim rec As B4XRect
        rec.Initialize(0,0,View.Width,View.Height)
        'ViewCanvas.DrawBitmap(bmp,rec)
        FillImageToView(bmp, ViewCanvas)
        'FitImageToView(bmp,ViewCanvas)
    End If
    
End Sub

Sub FitImageToView(bmp As B4XBitmap, ImageView As B4XView)
    Dim scale As Float = 1
    ImageView.SetBitmap(bmp.Resize(ImageView.Width * scale, ImageView.Height * scale, True))
End Sub

Sub FillImageToView(bmp As B4XBitmap, ImageView As B4XView)
    Dim bmpRatio As Float = bmp.Width / bmp.Height
    Dim viewRatio As Float = ImageView.Width / ImageView.Height
    If viewRatio > bmpRatio Then
        Dim NewHeight As Int = bmp.Width / viewRatio
        bmp = bmp.Crop(0, bmp.Height / 2 - NewHeight / 2, bmp.Width, NewHeight)
    Else if viewRatio < bmpRatio Then
        Dim NewWidth As Int = bmp.Height * viewRatio
        bmp = bmp.Crop(bmp.Width / 2 - NewWidth / 2, 0, NewWidth, bmp.Height)
    End If
    Dim scale As Float = 1
    ImageView.SetBitmap(bmp.Resize(ImageView.Width * scale, ImageView.Height * scale, True))
End Sub


FillImageToView:
Waiting for debugger to connect...
Program started.
apr 19, 2020 9:27:34 PM com.sun.javafx.css.parser.CSSParser term
WARNING: CSS Error parsing file:/C:/Users/ivanomonti/Desktop/Poject-b4x/Animazioni/Files/dark.css: Unexpected token ';' at [72,25]
Errore nella linea: 69 (ClassOpenimage)
java.lang.ClassCastException: anywheresoftware.b4a.objects.B4XCanvas cannot be cast to javafx.scene.Node
    at anywheresoftware.b4a.objects.B4XViewWrapper.getNodeObject(B4XViewWrapper.java:103)
    at anywheresoftware.b4a.objects.B4XViewWrapper.asViewWrapper(B4XViewWrapper.java:99)
    at anywheresoftware.b4a.objects.B4XViewWrapper.getWidth(B4XViewWrapper.java:172)
    at b4j.example.classopenimage._fillimagetoview(classopenimage.java:192)
    at b4j.example.classopenimage._bt_action(classopenimage.java:169)
    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:497)
    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.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:497)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:91)
    at anywheresoftware.b4a.shell.ShellBA.raiseEvent2(ShellBA.java:98)
    at anywheresoftware.b4a.BA$1.run(BA.java:216)
    at com.sun.javafx.application.PlatformImpl.lambda$null$174(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$175(PlatformImpl.java:294)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$149(WinApplication.java:191)
    at java.lang.Thread.run(Thread.java:745)


FitImageToView:
Waiting for debugger to connect...
Program started.
apr 19, 2020 9:25:30 PM com.sun.javafx.css.parser.CSSParser term
WARNING: CSS Error parsing file:/C:/Users/ivanomonti/Desktop/Poject-b4x/Animazioni/Files/dark.css: Unexpected token ';' at [72,25]
Errore nella linea: 64 (ClassOpenimage)
java.lang.ClassCastException: anywheresoftware.b4a.objects.B4XCanvas cannot be cast to javafx.scene.Node
    at anywheresoftware.b4a.objects.B4XViewWrapper.getNodeObject(B4XViewWrapper.java:103)
    at anywheresoftware.b4a.objects.B4XViewWrapper.asViewWrapper(B4XViewWrapper.java:99)
    at anywheresoftware.b4a.objects.B4XViewWrapper.getWidth(B4XViewWrapper.java:172)
    at b4j.example.classopenimage._fitimagetoview(classopenimage.java:188)
    at b4j.example.classopenimage._bt_action(classopenimage.java:169)
    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:497)
    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.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:497)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:91)
    at anywheresoftware.b4a.shell.ShellBA.raiseEvent2(ShellBA.java:98)
    at anywheresoftware.b4a.BA$1.run(BA.java:216)
    at com.sun.javafx.application.PlatformImpl.lambda$null$174(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$175(PlatformImpl.java:294)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$149(WinApplication.java:191)
    at java.lang.Thread.run(Thread.java:745)
 

ivanomonti

Expert
Licensed User
Longtime User
that's how I solved it.

viewscale:
Sub viewscale(bmp As B4XBitmap,rec As B4XRect, Views As B4XView)
    Dim bmpRatio As Float = bmp.Width / bmp.Height
    Dim viewRatio As Float = Views.Width / Views.Height
    If viewRatio > bmpRatio Then
        Dim NewHeight As Int = bmp.Width / viewRatio
        bmp = bmp.Crop(0, bmp.Height / 2 - NewHeight / 2, bmp.Width, NewHeight)
    Else if viewRatio < bmpRatio Then
        Dim NewWidth As Int = bmp.Height * viewRatio
        bmp = bmp.Crop(bmp.Width / 2 - NewWidth / 2, 0, NewWidth, bmp.Height)
    End If
    Dim scale As Int = 1
    bmp = bmp.Resize(Views.Width * scale, Views.Height * scale, True)
    ViewCanvas.DrawBitmap(bmp,rec)
End Sub

001254.png
 
Upvote 0
Top