Skip to content

Commit

Permalink
IMPLEMENT[render random face]
Browse files Browse the repository at this point in the history
  • Loading branch information
shuoros committed Aug 7, 2023
1 parent baafa36 commit b1aafd0
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package io.github.shuoros.peoplify.model.enumeration;

public enum FaceExpression {

HAPPY
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import io.github.shuoros.peoplify.model.enumeration.BodyColor;
import io.github.shuoros.peoplify.model.enumeration.ClothColor;
import io.github.shuoros.peoplify.model.enumeration.FaceExpression;
import org.springframework.util.ResourceUtils;

import javax.imageio.ImageIO;
Expand All @@ -12,6 +13,8 @@
public class AvatarComponentsProvider {

protected static final Map<BodyColor, BufferedImage> body;
protected static final Map<FaceExpression, BufferedImage> face;

protected static final Map<ClothColor, BufferedImage> cloth;


Expand All @@ -24,6 +27,9 @@ BodyColor.NUDE, loadComponent("body-nude"),
BodyColor.PINK, loadComponent("body-pink"),
BodyColor.WHITE, loadComponent("body-white")
);
face = Map.of(
FaceExpression.HAPPY, loadComponent("face-happy")
);
cloth = Map.of(
ClothColor.BLACK, loadComponent("cloth-black"),
ClothColor.BLUE, loadComponent("cloth-blue"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import io.github.shuoros.peoplify.model.enumeration.BackgroundColor;
import io.github.shuoros.peoplify.model.enumeration.BodyColor;
import io.github.shuoros.peoplify.model.enumeration.ClothColor;
import io.github.shuoros.peoplify.model.enumeration.FaceExpression;
import org.springframework.stereotype.Service;

import javax.imageio.ImageIO;
Expand All @@ -23,6 +24,8 @@ public void generateAvatar(OutputStream outputStream) throws IOException {

renderBody(canvas);

renderFace(canvas);

renderCloth(canvas);

writeToOutputStream(canvas, outputStream);
Expand All @@ -39,17 +42,24 @@ private BufferedImage setUpCanvas() {
return canvas;
}

private void renderBody(BufferedImage canvas) {
private void renderBody(final BufferedImage canvas) {
final Graphics2D graphics = (Graphics2D) canvas.getGraphics();
final BufferedImage body = resolveRandomBody();
graphics.drawImage(body, 116, 22, null);
graphics.dispose();
}

private void renderCloth(BufferedImage canvas) {
private void renderFace(final BufferedImage canvas) {
final Graphics2D graphics = (Graphics2D) canvas.getGraphics();
final BufferedImage face = resolveRandomFace();
graphics.drawImage(face, 216, 160, null);
graphics.dispose();
}

private void renderCloth(final BufferedImage canvas) {
final Graphics2D graphics = (Graphics2D) canvas.getGraphics();
final BufferedImage body = resolveRandomCloth();
graphics.drawImage(body, 109, 384, null);
final BufferedImage cloth = resolveRandomCloth();
graphics.drawImage(cloth, 109, 384, null);
graphics.dispose();
}

Expand All @@ -63,13 +73,19 @@ private BufferedImage resolveRandomBody() {
);
}

private BufferedImage resolveRandomFace() {
return AvatarComponentsProvider.face.get(
FaceExpression.values()[RANDOM.nextInt(FaceExpression.values().length)]
);
}

private BufferedImage resolveRandomCloth() {
return AvatarComponentsProvider.cloth.get(
ClothColor.values()[RANDOM.nextInt(ClothColor.values().length)]
);
}

private void writeToOutputStream(BufferedImage canvas, OutputStream outputStream) throws IOException {
private void writeToOutputStream(final BufferedImage canvas, final OutputStream outputStream) throws IOException {
ImageIO.write(canvas, "png", outputStream);
outputStream.close();
}
Expand Down
Binary file added src/main/resources/static/face-happy.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit b1aafd0

Please sign in to comment.