Skip to content

Commit

Permalink
Ajustes para desacoplar mais o metodo default createSticker
Browse files Browse the repository at this point in the history
  • Loading branch information
durvalprintes committed Jul 22, 2022
1 parent 04f4d87 commit 2a3a7f7
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 67 deletions.
23 changes: 8 additions & 15 deletions src/main/java/com/alura/App.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.alura;

import java.io.IOException;
import java.util.TreeMap;

import com.alura.model.Endpoint;
import com.alura.model.Sticker;
Expand All @@ -13,22 +12,16 @@ public class App {
public static void main(String[] args) {
try {
ImdbService api = new ImdbService(Endpoint.IMDB_MOST_POPULAR_TV, System.getProperty("key"));
// api.updateMoviesWithMyRating(new FileInputStream(new
// File("data/rating.json")));
TreeMap<Integer, String> range = new TreeMap<>();
range.put(0, "AVALIAR");
range.put(1, "PESSIMO");
range.put(4, "RUIM");
range.put(6, "PASSATEMPO");
range.put(8, "TOP");

api.shrinkListMovies(50);
api.generateStickerPosterImage(
api.generateStickerImage(
Sticker.builder()
.output("data/image/sticker/")
.font("Impact")
.size(111)
.rangeText(range).build());
.outputPath("data/image/sticker/")
.targetWidth(1000)
.targetHeight(1500)
.fontName("Impact")
.fontSize(128)
.topImage("data/image/sticker/joinha.png")
.build());
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
Expand Down
29 changes: 19 additions & 10 deletions src/main/java/com/alura/model/Sticker.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,45 @@

import java.awt.Color;
import java.awt.Font;
import java.util.TreeMap;
import java.io.InputStream;

import lombok.Builder;
import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
@Builder
public class Sticker {

private String output;
private TreeMap<Integer, String> rangeText;
private String font;
private Integer size;
private InputStream image;
private String topImage;
private String text;
private String outputPath;
private String outputName;
private String fontName;
private Integer fontSize;
private int targetWidth;
private int targetHeight;

@Builder.Default
private int style = Font.BOLD;
private int fontStyle = Font.BOLD;

@Builder.Default
private Color colorText = Color.BLACK;
private Color textColor = Color.BLACK;

@Builder.Default
private Float stroke = 5F;
private Float strokeNumber = 5F;

@Builder.Default
private Color colorStroke = Color.YELLOW;
private Color strokeColor = Color.YELLOW;

@Builder.Default
private Integer heightPlus = 200;

@Builder.Default
private String format = "png";
private String outputFormat = "png";

@Builder.Default
private boolean isTop = false;
}
66 changes: 30 additions & 36 deletions src/main/java/com/alura/service/WebService.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,17 @@
import static java.awt.Transparency.TRANSLUCENT;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.Shape;
import java.awt.font.TextLayout;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse.BodyHandlers;
import java.util.Optional;

import javax.imageio.ImageIO;

Expand All @@ -36,56 +33,53 @@ default void printField(String format, String field) {
System.out.println(format + field + "\033[0m");
}

default void createSticker(InputStream inputImage, String title, Double rating, Sticker param) throws IOException {
public void print();

BufferedImage movieImage = ImageIO.read(inputImage);
int width = movieImage.getWidth();
int height = movieImage.getHeight();
public void generateStickerImage(Sticker param);

if (width > 1000 || height > 1500) {
default void createSticker(Sticker param) throws IOException {

BufferedImage original = ImageIO.read(param.getImage());
int width = original.getWidth();
int height = original.getHeight();

if (width > param.getTargetWidth() || height > param.getTargetHeight()) {
Double scale = Math.min(
width > 1000 ? 1000 / Double.valueOf(width) : 1,
height > 1500 ? 1500 / Double.valueOf(height) : 1);
width = (int) (movieImage.getWidth() * scale);
height = (int) (movieImage.getHeight() * scale);
width > param.getTargetWidth() ? param.getTargetWidth() / Double.valueOf(width) : 1,
height > param.getTargetHeight() ? param.getTargetHeight() / Double.valueOf(height) : 1);
width = (int) (original.getWidth() * scale);
height = (int) (original.getHeight() * scale);
}

BufferedImage sticker = new BufferedImage(width, height + param.getHeightPlus(),
TRANSLUCENT);

Graphics2D graphic = (Graphics2D) sticker.getGraphics();
graphic.drawImage(movieImage, 0, 0, width, height, null);

Optional.ofNullable(rating).ifPresent(value -> {
if (param.getRangeText().floorEntry((int) Math.round(value)).equals(param.getRangeText().lastEntry())) {
try {
BufferedImage joinha = ImageIO.read(new File("data/image/sticker/joinha.png"));
graphic.drawImage(
joinha, sticker.getWidth() - joinha.getWidth() + 35,
sticker.getHeight() - joinha.getHeight(),
null);
} catch (IOException e) {
e.printStackTrace();
}
}
});

var font = new Font("Impact", Font.PLAIN, 128);
graphic.drawImage(original, 0, 0, width, height, null);

if (param.isTop()) {
BufferedImage topImage = ImageIO.read(new File(param.getTopImage()));
graphic.drawImage(
topImage, sticker.getWidth() - topImage.getWidth() + 35,
sticker.getHeight() - topImage.getHeight(),
null);
}

var font = new Font(param.getFontName(), param.getFontStyle(), param.getFontSize());
Shape textShape = new TextLayout(
param.getRangeText().floorEntry((int) Math.round(Optional.ofNullable(rating).isPresent() ? rating : 0.0))
.getValue(),
font, graphic.getFontRenderContext()).getOutline(null);
param.getText(), font, graphic.getFontRenderContext()).getOutline(null);

graphic.translate((sticker.getWidth() - textShape.getBounds().width) / 2,
((param.getHeightPlus() - textShape.getBounds().height) / 2) + textShape.getBounds().height
+ height);
graphic.setColor(Color.BLACK);
graphic.setColor(param.getTextColor());
graphic.fill(textShape);
graphic.setStroke(new BasicStroke(5));
graphic.setColor(Color.YELLOW);
graphic.setStroke(new BasicStroke(param.getStrokeNumber()));
graphic.setColor(param.getStrokeColor());
graphic.draw(textShape);

ImageIO.write(sticker, param.getFormat(), new File(param.getOutput() + title + "." + param.getFormat()));
ImageIO.write(sticker, param.getOutputFormat(),
new File(param.getOutputPath() + param.getOutputName() + "." + param.getOutputFormat()));
}

}
29 changes: 23 additions & 6 deletions src/main/java/com/alura/service/impl/ImdbService.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import java.net.URL;
import java.util.List;
import java.util.Optional;
import java.util.regex.Pattern;
import java.util.TreeMap;
import java.util.stream.Collectors;

import com.alura.model.Endpoint;
Expand All @@ -27,6 +27,7 @@ public class ImdbService implements WebService {

private String key;
private List<Movie> movies;
private final TreeMap<Integer, String> rangeRatingText = new TreeMap<>();

public ImdbService(Endpoint endpoint, String key) throws IOException, InterruptedException {
this.key = key;
Expand All @@ -42,7 +43,8 @@ private List<Movie> getListImdb(Endpoint endpoint) throws IOException, Interrupt
Movie.class));
}

public void printMovies() {
@Override
public void print() {
for (Movie movie : this.movies) {
printField("\033[1;37m", "\nTitulo: " + movie.getTitle());
printField("\033[1;37m", "Image: " + movie.getImage());
Expand Down Expand Up @@ -76,15 +78,30 @@ public void updateMoviesWithMyRating(InputStream inputRating) throws IOException
}
}

public void generateStickerPosterImage(Sticker param) {
@Override
public void generateStickerImage(Sticker param) {
rangeRatingText.put(0, "AVALIAR");
rangeRatingText.put(1, "PESSIMO");
rangeRatingText.put(4, "RUIM");
rangeRatingText.put(6, "PASSATEMPO");
rangeRatingText.put(8, "TOP");

System.out.println("Iniciando geração...");
this.movies.stream().forEach(
movie -> {
try {
System.out.print(movie.getTitle() + "... ");
createSticker(
new URL(Pattern.compile("\\._(.+).jpg$").matcher(movie.getImage()).replaceAll(".jpg")).openStream(),
movie.getTitle(), movie.getImDbRating(), param);
Double rating = Optional.ofNullable(movie.getImDbRating()).isPresent() ? movie.getImDbRating() : 0;
String text = rangeRatingText.floorEntry((int) Math.round(rating)).getValue();

param.setImage(new URL(movie.getImage().replaceAll("\\._(.+).jpg$", ".jpg")).openStream());
param.setText(text);
param.setTop(false);
if (rangeRatingText.lastEntry().getValue().equals(text)) {
param.setTop(true);
}
param.setOutputName(movie.getTitle());
createSticker(param);
System.out.println("OK!");
} catch (IOException e) {
System.out.println("Fail!");
Expand Down

0 comments on commit 2a3a7f7

Please sign in to comment.