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

ThRuST

Well-Known Member
Licensed User
Longtime 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



Example icon that was generated


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

Anywhere Software


Microsoft



[SOLVED]
 

Attachments

  • Favicon.zip
    33.7 KB · Views: 251
  • Favicon v1.1.zip
    44.7 KB · Views: 226
  • Favicon v1.2.zip
    44.4 KB · Views: 240
  • Favicon v1.3.zip
    154 KB · Views: 234
Last edited:

Daestrum

Expert
Licensed User
Longtime 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)))
 
Upvote 0

ThRuST

Well-Known Member
Licensed User
Longtime 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
 
Upvote 0

Cableguy

Expert
Licensed User
Longtime User
Please add the [SOLVED] prefix to your post!
 
Upvote 0

ThRuST

Well-Known Member
Licensed User
Longtime User
Thanks master Paulo

[SOLVED]

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

Cableguy

Expert
Licensed User
Longtime User
nop, you need to edit the post title! dropdown menu "threadTools" on top right of the page
 
Upvote 0

ThRuST

Well-Known Member
Licensed User
Longtime 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)


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

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


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)
 
Upvote 0

ThRuST

Well-Known Member
Licensed User
Longtime 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



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.

 
Last edited:
Upvote 0

ThRuST

Well-Known Member
Licensed User
Longtime 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.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…