forked from TCGdex/java-sdk
Ease fetching images
This commit is contained in:
parent
a91d254080
commit
79692ec3ad
@ -1,5 +1,6 @@
|
|||||||
package net.tcgdex;
|
package net.tcgdex;
|
||||||
|
|
||||||
|
import java.awt.image.BufferedImage;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@ -17,6 +18,14 @@ public class TCGDexAPI {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public enum ImageResolution {
|
||||||
|
HIGH, LOW;
|
||||||
|
|
||||||
|
public String forUseInURL() {
|
||||||
|
return this.name().toLowerCase();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private static final String API_URL = "https://api.tcgdex.net/v2/%s/%s";
|
private static final String API_URL = "https://api.tcgdex.net/v2/%s/%s";
|
||||||
|
|
||||||
private Language language;
|
private Language language;
|
||||||
@ -42,6 +51,18 @@ public class TCGDexAPI {
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Loads the image of a card
|
||||||
|
* @param card Card to get image for
|
||||||
|
* @param resolution High is recommended if the card is supposed to be readable and more than a thumbnail
|
||||||
|
* @return BufferedImage loaded, possibly null if no image of this card exists for this language
|
||||||
|
* @throws IOException Thrown in response to any kind of networking error
|
||||||
|
*/
|
||||||
|
public BufferedImage getImage(CardResume card, ImageResolution resolution) throws IOException {
|
||||||
|
String url = card.getImage() + "/" + resolution.forUseInURL() + ".png";
|
||||||
|
return Utils.getImage(url);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets a list containing the core information for every core
|
* Gets a list containing the core information for every core
|
||||||
*
|
*
|
||||||
|
@ -1,8 +1,14 @@
|
|||||||
package net.tcgdex;
|
package net.tcgdex;
|
||||||
|
|
||||||
|
import java.awt.image.BufferedImage;
|
||||||
import java.io.BufferedReader;
|
import java.io.BufferedReader;
|
||||||
|
import java.io.ByteArrayOutputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
import java.io.InputStreamReader;
|
import java.io.InputStreamReader;
|
||||||
|
import java.net.URL;
|
||||||
|
|
||||||
|
import javax.imageio.ImageIO;
|
||||||
|
|
||||||
import org.apache.http.client.methods.CloseableHttpResponse;
|
import org.apache.http.client.methods.CloseableHttpResponse;
|
||||||
import org.apache.http.client.methods.HttpGet;
|
import org.apache.http.client.methods.HttpGet;
|
||||||
@ -31,23 +37,26 @@ class Utils {
|
|||||||
return output;
|
return output;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static BufferedImage getImage(String imageUrl) throws IOException {
|
||||||
|
return ImageIO.read(new URL(imageUrl));
|
||||||
|
}
|
||||||
|
|
||||||
public static String prettifyEnumName(Enum<?> enumInstance) {
|
public static String prettifyEnumName(Enum<?> enumInstance) {
|
||||||
boolean space = true;
|
boolean space = true;
|
||||||
String rawRepresentation = enumInstance.name();
|
String rawRepresentation = enumInstance.name();
|
||||||
StringBuilder output = new StringBuilder(rawRepresentation.length());
|
StringBuilder output = new StringBuilder(rawRepresentation.length());
|
||||||
for(int i = 0; i < rawRepresentation.length(); i++) {
|
for (int i = 0; i < rawRepresentation.length(); i++) {
|
||||||
String character = rawRepresentation.substring(i, i+1);
|
String character = rawRepresentation.substring(i, i + 1);
|
||||||
if (character.equals("_")) {
|
if (character.equals("_")) {
|
||||||
output.append(" ");
|
output.append(" ");
|
||||||
space = true;
|
space = true;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if (space) {
|
if (space) {
|
||||||
//keep upper case
|
// keep upper case
|
||||||
space = false;
|
space = false;
|
||||||
output.append(character);
|
output.append(character);
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
output.append(character.toLowerCase());
|
output.append(character.toLowerCase());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -6,6 +6,7 @@ import static org.junit.Assert.assertFalse;
|
|||||||
import static org.junit.Assert.assertNotNull;
|
import static org.junit.Assert.assertNotNull;
|
||||||
import static org.junit.Assert.assertTrue;
|
import static org.junit.Assert.assertTrue;
|
||||||
|
|
||||||
|
import java.awt.image.BufferedImage;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.time.Month;
|
import java.time.Month;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
@ -14,17 +15,7 @@ import java.util.List;
|
|||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import net.tcgdex.Ability;
|
import net.tcgdex.TCGDexAPI.ImageResolution;
|
||||||
import net.tcgdex.Attack;
|
|
||||||
import net.tcgdex.CardInfo;
|
|
||||||
import net.tcgdex.Categories;
|
|
||||||
import net.tcgdex.Rarities;
|
|
||||||
import net.tcgdex.SeriesInfo;
|
|
||||||
import net.tcgdex.SetInfo;
|
|
||||||
import net.tcgdex.SetResume;
|
|
||||||
import net.tcgdex.TCGDexAPI;
|
|
||||||
import net.tcgdex.Types;
|
|
||||||
import net.tcgdex.Weakness;
|
|
||||||
import net.tcgdex.TCGDexAPI.Language;
|
import net.tcgdex.TCGDexAPI.Language;
|
||||||
|
|
||||||
public class TestAPI {
|
public class TestAPI {
|
||||||
@ -163,8 +154,16 @@ public class TestAPI {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testImage() {
|
public void testImage() throws IOException {
|
||||||
|
CardInfo info = api.getCardInfo("base4", "1");
|
||||||
|
BufferedImage high = api.getImage(info, ImageResolution.HIGH);
|
||||||
|
assertNotNull(high);
|
||||||
|
assertEquals(825, high.getHeight());
|
||||||
|
assertEquals(600, high.getWidth());
|
||||||
|
BufferedImage low = api.getImage(info, ImageResolution.LOW);
|
||||||
|
assertNotNull(low);
|
||||||
|
assertEquals(337, low.getHeight());
|
||||||
|
assertEquals(245, low.getWidth());
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user