Skip to content

Commit

Permalink
Add method getFaviconImage(); for getting decoded image from Base64 c…
Browse files Browse the repository at this point in the history
…ode of favicon
  • Loading branch information
RockChinQ committed Jun 21, 2021
1 parent 34c9d03 commit 0f92d02
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 6 deletions.
5 changes: 4 additions & 1 deletion BasicInfoAPI/src/main/api/IServerInfo.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package main.api;

import java.awt.image.BufferedImage;

/**
* Defines interfaces to get the basic info of a Minecraft server.
* @author Rock Chin
Expand All @@ -22,5 +24,6 @@ class ExtraDescr{
String getDefaultDescriptionText();
String getDefaultDescriptionColor();
ExtraDescr[] getExtraDescription();
String getFavicon();
String getFaviconBase64();
BufferedImage getFaviconImage();
}
24 changes: 20 additions & 4 deletions BasicInfoAPI/src/main/conn/MinecraftServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@

import com.google.gson.Gson;
import main.api.IServerInfo;
import sun.misc.BASE64Decoder;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.EOFException;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.*;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
Expand Down Expand Up @@ -270,12 +271,27 @@ public ExtraDescr[] getExtraDescription(){
}
}
@Override
public String getFavicon() {
public String getFaviconBase64() {
if (response!=null) {
return response.favicon;
}else {
return null;
}
}
@Override
public BufferedImage getFaviconImage(){
try{
return base64ToBufferedImage(getFaviconBase64().split(",")[1]);
}catch (Exception e){
e.printStackTrace();
}
return null;
}
private static BufferedImage base64ToBufferedImage(String base64)throws IOException {
BASE64Decoder decoder = new sun.misc.BASE64Decoder();
byte[] bytes1 = decoder.decodeBuffer(base64);
ByteArrayInputStream bais = new ByteArrayInputStream(bytes1);
return ImageIO.read(bais);
}

}
22 changes: 21 additions & 1 deletion BasicInfoAPI/src/test/conn/MinecraftServerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
import main.api.IServerInfo;
import main.conn.MinecraftServer;

import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;

public class MinecraftServerTest {
public static void main(String[] args)throws Exception {
MinecraftServer minecraftServer=new MinecraftServer("play.ee-fans.com",25565);
Expand All @@ -18,6 +22,22 @@ public static void main(String[] args)throws Exception {
for (IServerInfo.Player p:players){
System.out.println("player:name:"+p.name+" id:"+p.id);
}
System.out.println("favicon:"+minecraftServer.getFavicon());
System.out.println("favicon:"+minecraftServer.getFaviconBase64());

FaviconDisplay display=new FaviconDisplay(minecraftServer.getFaviconImage());
display.setBounds(200,200,200,200);
display.setVisible(true);
display.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}
static class FaviconDisplay extends JFrame{
BufferedImage bufferedImage;
@Override
public void paint(Graphics graphics){
graphics.drawImage(this.bufferedImage,15,40,this);
}
public FaviconDisplay(BufferedImage img){
this.bufferedImage=img;
}
}
}

0 comments on commit 0f92d02

Please sign in to comment.