add isometric head renderer
Some checks failed
Deploy App / docker (ubuntu-latest, 2.44.0, 17, 3.8.5) (push) Failing after 30s
Some checks failed
Deploy App / docker (ubuntu-latest, 2.44.0, 17, 3.8.5) (push) Failing after 30s
This commit is contained in:
25
src/main/java/cc.fascinated/common/ImageUtils.java
Normal file
25
src/main/java/cc.fascinated/common/ImageUtils.java
Normal file
@ -0,0 +1,25 @@
|
||||
package cc.fascinated.common;
|
||||
|
||||
import jakarta.validation.constraints.NotNull;
|
||||
|
||||
import java.awt.*;
|
||||
import java.awt.geom.AffineTransform;
|
||||
import java.awt.image.BufferedImage;
|
||||
|
||||
public class ImageUtils {
|
||||
|
||||
/**
|
||||
* Resize an image.
|
||||
*
|
||||
* @param src the source image
|
||||
* @param scale the scale factor
|
||||
* @return the scaled image
|
||||
*/
|
||||
public static BufferedImage resize(@NotNull final BufferedImage src, final double scale) {
|
||||
BufferedImage scaled = new BufferedImage((int) (src.getWidth() * scale), (int) (src.getHeight() * scale), BufferedImage.TYPE_INT_ARGB);
|
||||
Graphics2D graphics = scaled.createGraphics();
|
||||
graphics.drawImage(src, AffineTransform.getScaleInstance(scale, scale), null);
|
||||
graphics.dispose();
|
||||
return scaled;
|
||||
}
|
||||
}
|
@ -2,17 +2,11 @@ package cc.fascinated.common;
|
||||
|
||||
import cc.fascinated.Main;
|
||||
import cc.fascinated.exception.impl.BadRequestException;
|
||||
import cc.fascinated.model.player.Skin;
|
||||
import com.fasterxml.jackson.annotation.JsonIgnore;
|
||||
import lombok.SneakyThrows;
|
||||
import lombok.experimental.UtilityClass;
|
||||
import lombok.extern.log4j.Log4j2;
|
||||
|
||||
import javax.imageio.ImageIO;
|
||||
import java.awt.*;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.net.URI;
|
||||
import java.net.http.HttpRequest;
|
||||
import java.net.http.HttpResponse;
|
||||
@ -53,38 +47,4 @@ public class PlayerUtils {
|
||||
HttpResponse.BodyHandlers.ofByteArray());
|
||||
return response.body();
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the part data from the skin.
|
||||
*
|
||||
* @return the part data
|
||||
*/
|
||||
public static byte[] getSkinPartBytes(Skin skin, Skin.Parts part, int size) {
|
||||
if (size <= 0) {
|
||||
size = part.getDefaultSize();
|
||||
}
|
||||
|
||||
try {
|
||||
BufferedImage image = ImageIO.read(new ByteArrayInputStream(skin.getSkinImage()));
|
||||
if (image == null) {
|
||||
image = ImageIO.read(new ByteArrayInputStream(Skin.DEFAULT_SKIN.getSkinImage())); // Fallback to the default skin
|
||||
}
|
||||
// Get the part of the image (e.g. the head)
|
||||
BufferedImage partImage = image.getSubimage(part.getX(), part.getY(), part.getWidth(), part.getHeight());
|
||||
|
||||
// Scale the image
|
||||
BufferedImage scaledImage = new BufferedImage(size, size, partImage.getType());
|
||||
Graphics2D graphics2D = scaledImage.createGraphics();
|
||||
graphics2D.drawImage(partImage, 0, 0, size, size, null);
|
||||
graphics2D.dispose();
|
||||
partImage = scaledImage;
|
||||
|
||||
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
|
||||
ImageIO.write(partImage, "png", byteArrayOutputStream);
|
||||
return byteArrayOutputStream.toByteArray();
|
||||
} catch (Exception ex) {
|
||||
log.error("Failed to get {} part bytes for {}", part.name(), skin.getUrl(), ex);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user