From 79692ec3ad22514fe69fa043fffd7958ff971186 Mon Sep 17 00:00:00 2001 From: Maxopoly Date: Wed, 10 Nov 2021 13:58:37 +0100 Subject: [PATCH] Ease fetching images --- src/main/java/net/tcgdex/TCGDexAPI.java | 21 +++++++++++++++++++++ src/main/java/net/tcgdex/Utils.java | 21 +++++++++++++++------ src/test/java/net/tcgdex/TestAPI.java | 25 ++++++++++++------------- 3 files changed, 48 insertions(+), 19 deletions(-) diff --git a/src/main/java/net/tcgdex/TCGDexAPI.java b/src/main/java/net/tcgdex/TCGDexAPI.java index e32eef4..ad463d6 100644 --- a/src/main/java/net/tcgdex/TCGDexAPI.java +++ b/src/main/java/net/tcgdex/TCGDexAPI.java @@ -1,5 +1,6 @@ package net.tcgdex; +import java.awt.image.BufferedImage; import java.io.IOException; import java.util.ArrayList; import java.util.List; @@ -16,6 +17,14 @@ public class TCGDexAPI { return toString().toLowerCase(); } } + + 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"; @@ -41,6 +50,18 @@ public class TCGDexAPI { } 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 diff --git a/src/main/java/net/tcgdex/Utils.java b/src/main/java/net/tcgdex/Utils.java index e191dca..e6d93fc 100644 --- a/src/main/java/net/tcgdex/Utils.java +++ b/src/main/java/net/tcgdex/Utils.java @@ -1,8 +1,14 @@ package net.tcgdex; +import java.awt.image.BufferedImage; import java.io.BufferedReader; +import java.io.ByteArrayOutputStream; import java.io.IOException; +import java.io.InputStream; 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.HttpGet; @@ -30,24 +36,27 @@ class Utils { output = builder.toString(); return output; } - + + public static BufferedImage getImage(String imageUrl) throws IOException { + return ImageIO.read(new URL(imageUrl)); + } + public static String prettifyEnumName(Enum enumInstance) { boolean space = true; String rawRepresentation = enumInstance.name(); StringBuilder output = new StringBuilder(rawRepresentation.length()); - for(int i = 0; i < rawRepresentation.length(); i++) { - String character = rawRepresentation.substring(i, i+1); + for (int i = 0; i < rawRepresentation.length(); i++) { + String character = rawRepresentation.substring(i, i + 1); if (character.equals("_")) { output.append(" "); space = true; continue; } if (space) { - //keep upper case + // keep upper case space = false; output.append(character); - } - else { + } else { output.append(character.toLowerCase()); } } diff --git a/src/test/java/net/tcgdex/TestAPI.java b/src/test/java/net/tcgdex/TestAPI.java index ab2cfe6..6e2ec82 100644 --- a/src/test/java/net/tcgdex/TestAPI.java +++ b/src/test/java/net/tcgdex/TestAPI.java @@ -6,6 +6,7 @@ import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; +import java.awt.image.BufferedImage; import java.io.IOException; import java.time.Month; import java.util.Arrays; @@ -14,17 +15,7 @@ import java.util.List; import org.junit.Before; import org.junit.Test; -import net.tcgdex.Ability; -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.ImageResolution; import net.tcgdex.TCGDexAPI.Language; public class TestAPI { @@ -163,8 +154,16 @@ public class TestAPI { } @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()); } }