Hoje de tarde perguntei para a secretária da minha esposa que estava rodeando pela casa : ” Ae vai querer umas fotos 3×4 ? to fazendo umas e vou mandar imprimir … quer ? ” a resposta foi ótima !!! “pra que ? é tudo digital hoje … ” pronto eu com cara de tacho, maledito condomínio que esta pedindo fotos 3×4 para carteirinha da piscina, não podia ter uma camera por lá ?
Bem choradeira de lado, acabei resgatando um código que tinha feito em 2005 para montar uma folha de impressão de fotos 5×7, porque na época achei um absurdo pagar o preço pedido, ainda acho hehehe, dai fiz as fotos com o ipod mesmo, dei uma arrumada basica no código para suportar mais dimensões do que o 5×7 e pronto !! recebe como input uma imagem jpg e monta uma outra de saida no tamanho 1000×1500 que eh para ser impressa no tamanho 10×15.
clique na imagem para ver como fica o resultado :


o código em si não é complicado, é soh ter alguns cuidados na hora de compor a imagem de resultado e todos ficaram felizes, segue o código
package br.com.thz.photo;
import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.ImageIcon;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGEncodeParam;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
public class ProtoComposer {
private static final int BORDER = 5;
public static void main(String[] args) throws FileNotFoundException, IOException {
createImage3_4("mario.jpg");
createImage3_4("cachorro.jpg");
}
private static void createImage3_4(String fileName) throws FileNotFoundException, IOException {
createImage(fileName, 300, 400);
}
private static void createImage5_7(String fileName) throws FileNotFoundException, IOException {
createImage(fileName, 500, 700);
}
private static void createImage(String fileName, int width, int height) throws FileNotFoundException, IOException {
int x = 0;
int y = 0;
int maxWidth = 1000;
int maxHeight = 1500;
BufferedImage buffer = new BufferedImage(maxWidth, maxHeight, BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = buffer.createGraphics();
ImageIcon image = new ImageIcon(fileName);
g2.setColor(Color.WHITE);
g2.fillRect(0, 0, maxWidth, maxHeight);
// font definition
Font font = new Font("Tahoma", Font.BOLD, 30);
g2.setFont(font);
// draw images with the dimension informed
while ((x + width) <= maxWidth) {
while ((y + height) <= maxHeight) {
drawImageWithTimeStamp(g2, image.getImage(), x, y, width, height);
y += height;
}
x += width;
y = 0;
}
generateJPG(buffer, width +"_" + height + "_" + fileName);
}
private static void drawImageWithTimeStamp(Graphics2D g2, Image image, int x, int y, int w, int h) {
g2.drawImage(image, x + BORDER, y + BORDER, w - BORDER, h - BORDER, null);
String text = getDate();
FontMetrics metrics = g2.getFontMetrics();
Rectangle2D rect = metrics.getStringBounds(text, 0, text.length(), g2);
g2.setColor(Color.BLACK);
int lateral = (int) ((w - rect.getWidth()) / 2.0);
Rectangle toFill = new Rectangle();
toFill.x = x + lateral - BORDER;
toFill.y = (int) (y + h - (2 * BORDER) - rect.getHeight());
toFill.height = (int) rect.getHeight();
toFill.width = (int) rect.getWidth() + BORDER;
g2.fillRect(toFill.x, toFill.y, toFill.width, toFill.height);
g2.setColor(Color.WHITE);
g2.drawString(text, toFill.x, (int) (toFill.y + rect.getHeight() - (2 * BORDER)));
}
public static String getDate() {
SimpleDateFormat sdf = new SimpleDateFormat("dd-MMM-yyyy");
return sdf.format(new Date());
}
private static void generateJPG(BufferedImage buffer, String fileName) throws FileNotFoundException, IOException {
FileOutputStream file = new FileOutputStream(fileName);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(file);
JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(buffer);
param.setQuality(1.0F, true);
encoder.setJPEGEncodeParam(param);
encoder.encode(buffer);
file.close();
}
}














