mirror of
https://github.com/tcgdex/java-sdk.git
synced 2025-04-22 02:42:15 +00:00
Ease fetching images
This commit is contained in:
parent
a91d254080
commit
79692ec3ad
@ -1,5 +1,6 @@
|
||||
package net.tcgdex;
|
||||
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
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 Language language;
|
||||
@ -42,6 +51,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
|
||||
*
|
||||
|
@ -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;
|
||||
@ -31,6 +37,10 @@ class Utils {
|
||||
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();
|
||||
@ -46,8 +56,7 @@ class Utils {
|
||||
// keep upper case
|
||||
space = false;
|
||||
output.append(character);
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
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.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());
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user