B4J Question ImageView file formats

oldeast

Active Member
Licensed User
Longtime User
Hi,
Is there any method to display .tif/.tiff files, I read that ImageView has no native support for these formats.

Thank You
 
You can load tiff images into an imageview but ... you need to use a bit of inline java to do it. Also uses javaobject library.
Example:
Code: Copy
Sub AppStart (Form1 As Form, Args() As String)
 MainForm = Form1
 MainForm.Show
 Dim iv As ImageView
 iv.Initialize("")
 iv.SetImage(asJO(Me).RunMethod("loadImage",Array("file:///c:/temp/marbles.tif")))
 MainForm.RootPane.AddNode(iv,10,10,300,300)
End Sub
Sub asJO(o As JavaObject)As JavaObject
 Return o
End Sub
'Return true to allow the default exceptions handler to handle the uncaught exception.
Sub Application_Error (Error As Exception, StackTrace As String) As Boolean
 Return True
End Sub
#if java
import javafx.embed.swing.SwingFXUtils;
import javafx.scene.image.Image;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import java.net.*; 
import java.io.*;
public static Image loadImage(String file)throws MalformedURLException,IOException{
 BufferedImage bufferedImage = ImageIO.read(new URL(file));
 Image image = SwingFXUtils.toFXImage(bufferedImage, null);
 return image;
}
#End If
 
Upvote 0
Thank you very much.
I wanted to include the .tif file format as it is considered the best for image archiving in the museum sector, so this will be a big help.
Graham
 
Upvote 0
I'm getting this error:

Code: Copy
Fehler in Zeile: 42
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.b4j.object.JavaObject.RunMethod(JavaObject.java:131)
    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:613)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:228)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:159)
    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:90)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:93)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:77)
    at b4j.example.main.start(main.java:44)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$161(LauncherImpl.java:863)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$174(PlatformImpl.java:326)
    at com.sun.javafx.application.PlatformImpl.lambda$null$172(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
 
Upvote 0
i remember that a tiff can also contain multiple images
 
Upvote 0
Where are you trying to read the image from ?
(I just tried the original code and it works as expected)
 
Upvote 0
Here's my code:

Code: Copy
...
Dim iv As ImageView
    iv.Initialize("")
    Log(asJO(Me))
    iv.SetImage(asJO(Me).RunMethod("loadImage",Array("file:///C:/test/CCF100420160009.tif")))
    RechnungenForm.RootPane.AddNode(iv,10,10,300,300)
  
End Sub

Sub asJO(o As JavaObject)As JavaObject
    Return o
End Sub

#if java
import javafx.embed.swing.SwingFXUtils;
import javafx.scene.image.Image;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import java.net.*;
import java.io.*;
public static Image loadImage(String file)throws MalformedURLException,IOException{
 BufferedImage bufferedImage = ImageIO.read(new URL(file));
 Image image = SwingFXUtils.toFXImage(bufferedImage, null);
 return image;
}
#End If

I tried different folders and files. No success. I'm jusing the latest Java SDK.

I've uploaded the image. Must be something stupid.
 

Attachments

Last edited:
Upvote 0
I tried it with the file you uploaded and again it worked as expected.

CORRECTION**
When I ran it under java 8 I got the same error as you.
Having a look now to see why.
 
Last edited:
Upvote 0
Here is a version written using JavaObject I find it easier to debug:

Code: Copy
Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    MainForm.RootPane.LoadLayout("1") 'Load the layout file.
    MainForm.Show
  
    File.Copy(File.DirAssets,"CCF100420160009.tif",File.DirApp,"CCF100420160009.tif")
  
    ImageView1.SetImage(LoadTiff(File.DirApp,"CCF100420160009.tif"))
  
End Sub

Public Sub LoadTiff(FileDir As String,FileName As String) As Image
   
    Dim BuffImg As JavaObject
    Dim ImageIO As JavaObject
    Dim SwingFXUtils As JavaObject
   
    ImageIO.InitializeStatic("javax.imageio.ImageIO")
    SwingFXUtils.InitializeStatic("javafx.embed.swing.SwingFXUtils")

    BuffImg = ImageIO.RunMethod("read",Array(File.Openinput(FileDir,FileName)))
    Log(BuffImg)
   
    Return SwingFXUtils.RunMethod("toFXImage",Array(BuffImg,Null))
  
End Sub

This works in Java9, but in java8 the buffered image is not returned. Having a quick look on the internet it may be that the image format is not recognised by java8.
https://stackoverflow.com/questions/36568214/imageio-readinputstream-gives-null-in-bufferedimage.

You may need to see if it's possible to add a codec to read that specific files format.
 
Upvote 0
If you find this jar 'jai-imageio-core-1.3.1.jar' you can just add it to #AdditionalJar and the original code can read TIFF.
 
Upvote 0
Back
Top