B4J Library [Source code] ImageScaler

Introduction:
I don't know if there is any library exist to resize image in B4J non-UI app.
I thought such library will be useful to process images for web server in building REST API for e-commerce or e-learning web apps.
I found an example on web and chose the third method, from an old project call ImgScalr.
I done some experiments and manage to get it work in B4J using Inline Java.
Then I tried to compile the project as a library. boop! Done. It is so easy and I have my .jar and .xml files ready to use.

Supported file types:
  • JPG
  • JPEG
  • PNG
  • BMP
  • WBMP
Note: GIF is not supported

Additional Jar: imgscalr-lib-4.2.jar



ImageScaler
Author:
Aeric Poon
Version: 1.00
  • ImageScaler
    • Functions:
      • Initialize As String
      • IsInitialized As Boolean
      • Resizeimage (InputDir As String, OutputDir As String, Filename As String, Width As Int, Height As Int, Mode As String) As String
        Mode: AUTOMATIC, FIT_EXACT, FIT_TO_HEIGHT and FIT_TO_WIDTH
        Reference: https://www.baeldung.com/java-resize-image

        Example: Private img As ImageScaler
        img.Initialize
        img.ResizeImage(File.DirApp, File.DirApp, "sonic.jpg", 200, 200, "AUTOMATIC")
 

Attachments

  • ImageScaler.jar
    2.8 KB · Views: 281
  • ImageScaler.xml
    1.8 KB · Views: 298
  • imgscalr-lib-4.2.jar
    27.2 KB · Views: 306
  • Source.zip
    1.9 KB · Views: 277
Last edited:

Theera

Expert
Licensed User
Longtime User
Source code attached in post #1
I think the code has something wroing. It has no reference from file.DirAsset, has it? Or you need to test someone have tried to test the app.
 

aeric

Expert
Licensed User
Longtime User
I think the code has something wroing. It has no reference from file.DirAsset, has it? Or you need to test someone have tried to test the app.
There is nothing wrong.
Just use your own image to test.
You can change the path to your need.
Don't take it as it is.
 

Theera

Expert
Licensed User
Longtime User
There is nothing wrong.
Just use your own image to test.
You can change the path to your need.
Don't take it as it is.
I've test, it has error 'file not found', although I have input my image in file tabs, the log shows 'never used the file'
I think that it should have the line
File.copy(File.DirAsset, "xx.png", File.DirApp, "xx.png") before.
 

aeric

Expert
Licensed User
Longtime User
I've test, it has error 'file not found', although I have input my image in file tabs, the log shows 'never used the file'
I think that it should have the line
File.copy(File.DirAsset, "xx.png", File.DirApp, "xx.png") before.
DirApp mean Objects folder. Is the file exist there?
 

Theera

Expert
Licensed User
Longtime User
Your sourcecode has no reference the file from file.DirAsset. I don't know how to test app.
 

Theera

Expert
Licensed User
Longtime User
I don't understand about why do you create non-ui app, how to get the lastest image to use?
 

aeric

Expert
Licensed User
Longtime User
Your sourcecode has no reference the file from file.DirAsset. I don't know how to test app.
Because it doesn't need to rely on file in File.DirAssets.

Your problem is you don't understand how to work with B4J, B4A or B4i on the file system.

I already provided an answer that you need to put the file in Objects folder.
 

aeric

Expert
Licensed User
Longtime User
I don't understand about why do you create non-ui app, how to get the lastest image to use?
The library can work not only on B4J non-UI app, you can make it work on B4J UI app and B4A by changing the path.
Reason I used non-UI app is for web server which is lack of UI support, in B4J case JavaFX or XUI.
 

Theera

Expert
Licensed User
Longtime User
Your problem is you don't understand how to work with B4J, B4A or B4i on the file system.
It means that copying the image file into objects folder directly,doesn't it?
 
Last edited:

aeric

Expert
Licensed User
Longtime User
I think that it should have the line
File.copy(File.DirAsset, "xx.png", File.DirApp, "xx.png") before.
You can put the file in Files folder and load to the tab and use the above code to copy to File.DirApp which means it will copied to Objects folder.
This is extra steps which is not needed.

I want to clarify, this class initially is intended to use in server. In real world use case, user will upload images to server and saved into a static files folder (www) inside Objects folder.
So there is no point to put images into assets folder.
For example purpose, you can use your own images for testing. Just drop it to Objects folder.
 
Top