Skin Database >> FaceLibrary

Discussion in 'Resources' started by ccrama, May 26, 2014.

Thread Status:
Not open for further replies.
  1. Offline

    ccrama

    Hello Bukkit,
    I developed this code to take a user's face directly from Mojang for a project a while ago, and thought I'd share it here. This code will take the skin from Mojang's skin servers, crops the head out, and allows you to save it to a database or lets you use it with anything dealing with Images. Recommended for use Asynchronously, as any slowdown in downloading the image from Mojang can cause big problems on the main thread!

    Code:java
    1. import java.awt.Image;
    2. import java.awt.image.BufferedImage;
    3. import java.io.File;
    4. import java.io.IOException;
    5. import java.net.MalformedURLException;
    6. import java.net.URL;
    7.  
    8. import javax.imageio.ImageIO;
    9.  
    10. import org.bukkit.entity.Player;
    11.  
    12. public class FaceLibrary {
    13. //Library by ccrama, can be found at gist.github.com/ccrama/759879cace268a99a548
    14. public void saveFace(Player p) {
    15. String name = p.getName();
    16. URL url = null;
    17. if (new File(new Main().getDataFolder() + File.separator
    18. + "FaceLibrary" + File.separator + name + ".png").exists() == false) {
    19. try {
    20. url = new URL("[url]http://skins.minecraft.net/MinecraftSkins/[/url]"
    21. + name + ".png");
    22. } catch (MalformedURLException e1) {
    23. e1.printStackTrace();
    24. }
    25. Image img = null;
    26. try {
    27. img = ImageIO.read(url);
    28. } catch (IOException e1) {
    29. e1.printStackTrace();
    30. }
    31. BufferedImage dest = ((BufferedImage) img).getSubimage(8, 8, 8, 8);
    32. File outputfile = new File(new Main().getDataFolder()
    33. + File.separator + "FaceLibrary" + File.separator + name
    34. + ".png");
    35.  
    36. try {
    37. ImageIO.write(dest, "png", outputfile);
    38. } catch (IOException e1) {
    39. e1.printStackTrace();
    40. }
    41. }
    42. }
    43.  
    44. public BufferedImage getImage(Player p) {
    45. String name = p.getName();
    46. File imageFile = new File(new Main().getDataFolder() + File.separator
    47. + "FaceLibrary" + File.separator + name + ".png");
    48. BufferedImage img = null;
    49. try {
    50. img = ImageIO.read(imageFile);
    51. return img;
    52. } catch (IOException e) {
    53. System.out.println("Image for " + name + " is invalid!");
    54. return null;
    55.  
    56. }
    57.  
    58. }
    59.  
    60. }


    Usage is simple. Use saveFace(player); on join event, for example, and use getImage(player) to retrieve the image at a later time. I used this to display a user's face with bobacadodl 's ImageMessage and with holograms. The reason for saving it to a database is in the case that Mojang's skin servers are down. You can easily edit this to skip the saving and return an image if you don't want this functionality. This also assumes that the directory /plugins/yourplugin/FaceLibrary/ exists.


    Github: http://gist.github.com/ccrama/759879cace268a99a548

    Post any creative ways you used this library!

    Cheers,
    ccrama

    EDIT: Use the github link, formatting is horrible on Bukkit :(
     
    GrandmaJam, Flybelette and bobacadodl like this.
  2. Offline

    Wizehh

    Thanks for this resource! Would you mind posting an example image?
     
  3. Offline

    Bammerbom

    Epic! But as Wizehh sayed images?
     
  4. Offline

    ccrama

    Bammerbom Wizehh Here is a post copied directly from the ImageMessage thread

    My face looks discolored because it's using a bad color detection algorithm. Newest version of bobacadodl 's library will look better.
     
Thread Status:
Not open for further replies.

Share This Page