Identicons are images that are generated for users of websites and programs that are unique for the given username or e-mail o whatever string you want. According to Wikipedia an identicon is
a visual representation of a hash value, usually of an IP address, that serves to identify a user of a computer system as a form of avatar while protecting the users' privacy.
There are various generators for Identicons out there and when building a powerful program with Java you may want to add a bit of an extra for your users. So instead of having one basic image for every user after signup you could use an Identicon. According to JDenticon it is a
free Javascript library for generating highly recognizable identicons.
And the Identicons are very nicely generated and can be generated as .svg-files. Unfortunately JDenticon is written in javascript and can't be used in Java without some other code.
And that's exactly the point where Jdenticon Java API is useful. It let's you easily import the few classes and use the methods to
- generate hashcodes from a given String
- generate a .svg file from a hashcode
- save the file on a given location on your pc
JDJA uses Apache batik and the jdenticon.js to work and generates with just these 2 things images in various formats.
Simply download the latest file as .jar (e.g. "JDJA version 0.5.jar") and add it to your Java Build Path. After that you have to get Apache Batik and add it to your Java Build Path as well. (Link here - use Batik 1.7!!)
If you want to install JDJA in Eclipse follow these steps after downloading the .jar:
- Create a new project:
File -> New -> Java project
(only if you want to start a new project) - go to your Java Build Path:
left-click on your project -> Properties -> Java Build Path
- add JDJA:
Libraries -> Add External Jars -> select your downloaded .jar
- After that download the jdenticon.js (Link) and put it into your src folder.
- you can now use JDJA
First you have to import everything needed for this program to run. Do this by putting this to you imports:
import de.jdenticon.main.HashGen;
import de.jdenticon.main.JDenticon;
import de.jdenticon.utilities.Hash;
To generate a jpg File use this code:
JDenticon jd = new JDenticon();
File jpg = jd.generateJPG("test", 1000); //"test" -> String that is converted to Hash
// 1000 -> size of the image
// the next 3 lines are just for saving the generated file on your computer
BufferedImage img = ImageIO.read(jpg);
File out = new File("C:/outFile.jpg"); //"C:/output.jpg" is the file that java writes to
ImageIO.write(img, "jpg", out); //writes the file
The code above is also used to generate PNG (just replace "jpg" with "png".
If you now want to generate a .svg file the code for saving it on you computer is a little bit different. You don't have to use ImageIO
in this case. Just write the data to a File like C:/output.svg
line by line (normally its just 1 line).
Version 0.5: MEGA
Version 0.7: MEGA
- added support for .svg files
- added export of .svg
- implemented the generation of hash-codes from given strings
- minor changes (first implementation of JPG / PNG export)
- made the code look better
- added support for generating JPG
- added support for generating PNG
- added own "Hash" data type (for better handling, to minimize failure)
- added HashGen class to generate hashcodes
- changed data format (now type "File" is getting returned)
- add svg support
- add canvas support
- add image export for .png and .jpg
- rework code for better understanding
- add support for .pdf