B4J Question Work with Images on a jServer

b4x-de

Active Member
Licensed User
Longtime User
My users upload photos to a jServer WebApp. Before storing a photo, I would like to remove all meta data i.e. GPS location, camera model from EXIF. Therefore, I try to convert JPG to PNG by using this simple code:

B4X:
 Dim insPhoto As InputStream
 Dim imgPhoto As Image
 Dim outPhoto As OutputStream

' Read Source, i.e. JPG
insPhoto = File.OpenInput(strDirSource, strFileSource)
imgPhoto.Initialize2(insPhoto)

' Write as PNG without EXIF metadata
outPhoto = File.OpenOutput(strDirTarget, strFileTarget, False)
imgPhoto.WriteToStream(outPhoto)

The code compiles with jFX but it fails at runtime because jFX can not be used on a jServer:
B4X:
java.lang.NoClassDefFoundError: javafx/scene/image/Image
Caused by: java.lang.ClassNotFoundException: javafx.scene.image.Image

How can I convert a JPG to PNG on a jServer? How can I work with Images in general on a jServer (i.e. resize, rotate)?
 

b4x-de

Active Member
Licensed User
Longtime User
Thank you! I can confirm, it works with jServer. But unfortunately it is very slow. Is there some faster and lightweight way without tons of external libraries?
 
Upvote 0

teddybear

Well-Known Member
Licensed User
My users upload photos to a jServer WebApp. Before storing a photo, I would like to remove all meta data i.e. GPS location, camera model from EXIF. Therefore, I try to convert JPG to PNG by using this simple code:

B4X:
 Dim insPhoto As InputStream
 Dim imgPhoto As Image
 Dim outPhoto As OutputStream

' Read Source, i.e. JPG
insPhoto = File.OpenInput(strDirSource, strFileSource)
imgPhoto.Initialize2(insPhoto)

' Write as PNG without EXIF metadata
outPhoto = File.OpenOutput(strDirTarget, strFileTarget, False)
imgPhoto.WriteToStream(outPhoto)

The code compiles with jFX but it fails at runtime because jFX can not be used on a jServer:
B4X:
java.lang.NoClassDefFoundError: javafx/scene/image/Image
Caused by: java.lang.ClassNotFoundException: javafx.scene.image.Image

How can I convert a JPG to PNG on a jServer? How can I work with Images in general on a jServer (i.e. resize, rotate)?
You can use the library to convert jpg to png
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
B4X:
Sub ConvertImage (InputDir As String, InputFile As String, OutputDir As String, OutputFile As String) As Boolean
    Try
        If InputFile = "" Or File.Exists(InputDir, InputFile) = False Then
            Log("File not found")
            Return False
        End If

        Dim jo As JavaObject = Me
        jo.RunMethod("ConvertJpg2Png", Array(File.Combine(InputDir, InputFile), File.Combine(OutputDir, OutputFile)))
        Return True
    Catch
        Log(LastException)
    End Try
    Return False
End Sub

#If Java
    import java.io.File;
    import java.io.IOException;
    import javax.imageio.ImageIO;
    import java.awt.image.BufferedImage;
    
    // Convert jpg to png
    public static void ConvertJpg2Png (String input, String output) throws Exception
    {
        BufferedImage originalImage = ImageIO.read(new File(input));
        File file = new File(output);
        ImageIO.write(originalImage, "png", file);
    }
#End If

B4X:
Dim success As Boolean = ConvertImage(File.DirApp, "MyPhoto.jpg", File.DirApp, "MyPhoto.png")
Log(success)
 
Upvote 0
Top