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

Discussion in 'B4J Questions' started by ThRuST, Apr 21, 2019.

  1. ThRuST

    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

    [SOLVED]
     

    Attached Files:

    Last edited: Apr 26, 2019
    AnandGupta likes this.
  2. ThRuST

    ThRuST Well-Known Member Licensed User

    Last edited: Apr 21, 2019
  3. Daestrum

    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
    Code:
    #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)))
     
    Erel, ThRuST and DonManfred like this.
  4. ThRuST

    ThRuST Well-Known Member Licensed User

    @Daestrum Exceptional magnificient sorcery :)
     
  5. ThRuST

    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 :)
     
  6. Cableguy

    Cableguy Expert Licensed User

    Please add the [SOLVED] prefix to your post!
     
    ThRuST likes this.
  7. ThRuST

    ThRuST Well-Known Member Licensed User

    Thanks master Paulo :)

    [SOLVED]

    I was thinking that the thread was supposed to be locked lol
     
  8. ThRuST

    ThRuST Well-Known Member Licensed User

    Is it working ? lol
     
  9. Cableguy

    Cableguy Expert Licensed User

    nop, you need to edit the post title! dropdown menu "threadTools" on top right of the page
     
    ThRuST likes this.
  10. ThRuST

    ThRuST Well-Known Member Licensed User

  11. ThRuST

    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 :)

    Code:
    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)
     
  12. ThRuST

    ThRuST Well-Known Member Licensed User

    And here we have a final working solution for the icon generator code


    Code:
    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: Apr 26, 2019
  13. ThRuST

    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.
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice