B4J Question Get Favicon from a website (Icon-maker) [SOLVED]

ThRuST

Well-Known Member
Licensed User
I am exploring the possibility to download an icon from a website and have it displayed in B4J.
Please share you experiences on this.

A favicon reference can be found here


Image4j library source
http://image4j.sourceforge.net/

Example
https://www.b4x.com/favicon.ico

favicon.PNG


Example icon that was generated
newicon.png


Another example by using a canvas. Add this in from the code posted later in this thread.

Anywhere Software
newicon2.png


Microsoft
newicon3.png


Attached you'll find four versions.

v1.0 only works with image files and not .ico files (for testing purposes).
v1.1 has image4j library implemented. Thanks to Daestrum for wrapping the image4j lib.
v1.2 it can now generate a new icon which is saved and loaded back in an imageview.
v1.3 this version can generate a new icon by drawing on a canvas.
[SOLVED]
 

Attachments

Last edited:

Daestrum

Well-Known Member
Licensed User
Based on your code, just use that library and get a list of bufferedImages then use SwingFXUtils to convert to an FXImage
B4X:
 #AdditionalJar: image4j-0.7.2.jar
...
 Dim decoder As JavaObject
 Dim imageUtils As JavaObject
...
 decoder.InitializeStatic("net.sf.image4j.codec.ico.ICODecoder")
 imageUtils.InitializeStatic("javafx.embed.swing.SwingFXUtils")
...
  Dim ob As List = decoder.RunMethod("read",Array(job.GetInputStream))
  Log("there were "&ob.Size&" icons in the file")
  Log(ob.Get(0)) ' need to choose which icon you want 0 = smallest ob.size-1 = largest icon(probably)
 
  iv.SetImage (imageUtils.RunMethod("toFXImage",Array(ob.Get(0),Null)))
 

ThRuST

Well-Known Member
Licensed User
I have updated my first post with v1.1 which includes image4j. Unless anyone else have anything to add I see this case as closed.
Credits to @Daestrum for wrapping the image4j library for B4J. Excellente :)
 

ThRuST

Well-Known Member
Licensed User
Thanks master Paulo :)

[SOLVED]

I was thinking that the thread was supposed to be locked lol
 

ThRuST

Well-Known Member
Licensed User
Well I was thinking this thread was done but I want to create a complete icon from the favicon, so here's what I came up with.
I need advice how to make the favicon smaller in relation to the background icon design (emptyicon.png) I attach the neccessary dependencies.

emptyicon.png (add this to the project with the files manager)
emptyicon.png


newicon.png (Saved logo, in this case the Apple logo saved from www.apple.com)
newicon.png

newicon2.png (our new styled icon with transparent corners, which is possible thanks to using a canvas)
newicon2.png


and here's were the magic happens (you might want to change drive from D: to something else)
Pane1 is no longer needed with this addition to the source code example in v1.1.

Quick note about the code, it saves the found favicon to the file newicon.png then saves the new generated icon from canvas1. Simple as that :)

B4X:
Sleep(1000)
                  ' Generate new icon
                Private DriveName As String = "d:\"
                Private IconFileName As String = "newicon.png"
                Private IconFileName2 As String = "newicon2.png"
                Private emptyicon,favicon As Image
                emptyicon.Initialize(File.DirAssets,"emptyicon.png")
                
                Canvas1.Initialize("")
                MainForm.RootPane.AddNode(Canvas1,50,50,100,100)
        
                Private out As OutputStream = File.OpenOutput(DriveName, IconFileName, False)
                ImageView1.Snapshot2(fx.Colors.Transparent).WriteToStream(out)
            
                favicon.Initialize(DriveName, IconFileName)
                
                Canvas1.DrawImage(emptyicon,0,0,100,100)
                Canvas1.DrawImage(favicon,0,0,100,100)
        
                Private out As OutputStream = File.OpenOutput(DriveName, IconFileName2, False)
                Canvas1.Snapshot2(fx.Colors.Transparent).WriteToStream(out)
 

ThRuST

Well-Known Member
Licensed User
And here we have a final working solution for the icon generator code


B4X:
Sleep(1000)
                  ' Generate new icon
                Private DriveName As String = "d:\"
                Private IconFileName As String = "newicon.png"
                Private IconFileName2 As String = "newicon2.png"
                Private emptyicon,favicon As Image
                emptyicon.Initialize(File.DirAssets,"emptyicon.png")
              
                Canvas1.Initialize("")
                MainForm.RootPane.AddNode(Canvas1,50,50,100,100)
      
                Private out As OutputStream = File.OpenOutput(DriveName, IconFileName, False)
                ImageView1.Snapshot2(fx.Colors.Transparent).WriteToStream(out)
          
                favicon.Initialize(DriveName, IconFileName)
              
                Canvas1.DrawImage(emptyicon,0,0,100,100)
                Canvas1.DrawImage(favicon,Canvas1.Width / 1.40 - ImageView1.Width,Canvas1.Height / 1.42 - ImageView1.Height,60,60)
      
                Private out As OutputStream = File.OpenOutput(DriveName, IconFileName2, False)
                Canvas1.Snapshot2(fx.Colors.Transparent).WriteToStream(out)
A generated icon looks like this (example taken from Anywhere software at b4x.com) with transparent corners. Not bad, not bad :)

newicon2.png


And here's a version with another icon background. You can easily exchange the background icon.
The icon background was prepared in Photoshop. This opens for many variations.

newicon2.png
 
Last edited:

ThRuST

Well-Known Member
Licensed User
I have updated the example source to v1.3. This version can generate a new icon. It turned out to be an icon-maker I hope you like it. Perhaps you want to post your beautiful custom made icon for us all to see.
 
Top