Skip to content

Instantly share code, notes, and snippets.

@garcia-jj
Created September 13, 2017 00:00
Show Gist options
  • Save garcia-jj/13ead357f87504666158e290e7e46c71 to your computer and use it in GitHub Desktop.
Save garcia-jj/13ead357f87504666158e290e7e46c71 to your computer and use it in GitHub Desktop.
package siscom.model.service.helper;
import static java.awt.Transparency.OPAQUE;
import static java.awt.image.BufferedImage.TYPE_INT_ARGB;
import static java.awt.image.BufferedImage.TYPE_INT_RGB;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import javax.enterprise.context.Dependent;
import javax.imageio.ImageIO;
@Dependent
public class DefaultImageTool implements ImageTool {
@Override
public byte[] resizeCenteredPNG(final byte[] src, final int size) throws IOException {
BufferedImage image = loadImage(src);
if (image.getWidth() < size || image.getHeight() < size) {
throw new IllegalStateException(String.format("Imagem não pode ser menor que %d pixels.", size));
}
if (image.getWidth() != image.getHeight()) {
image = crop(image);
}
if (image.getWidth() != size || image.getHeight() != size) {
image = resize(image, size);
}
return write(image);
}
protected BufferedImage crop(final BufferedImage srcImage) {
final int newSize = Math.min(srcImage.getWidth(), srcImage.getHeight());
final int x = (srcImage.getWidth() - newSize) / 2;
final int y = (srcImage.getHeight() - newSize) / 2;
return crop(srcImage, x, y, newSize, newSize);
}
protected BufferedImage crop(final BufferedImage src, final int x, final int y, final int width, final int height) {
final BufferedImage result = createOptimalImage(src, width, height);
final Graphics resultGraphics = result.getGraphics();
resultGraphics.drawImage(src, 0, 0, width, height, x, y, x + width, y + height, null);
resultGraphics.dispose();
return result;
}
protected BufferedImage resize(final BufferedImage src, final int size) {
final BufferedImage result = createOptimalImage(src, size, size);
final Graphics2D resultGraphics = result.createGraphics();
final Map<Object, Object> hints = new HashMap<>();
hints.put(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
hints.put(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
resultGraphics.setRenderingHints(hints);
resultGraphics.drawImage(src, 0, 0, size, size, null);
resultGraphics.dispose();
return result;
}
protected byte[] write(final BufferedImage image) throws IOException {
final ByteArrayOutputStream out = new ByteArrayOutputStream();
ImageIO.write(image, "PNG", out);
return out.toByteArray();
}
protected BufferedImage loadImage(final byte[] src) throws IOException {
return ImageIO.read(new ByteArrayInputStream(src));
}
private BufferedImage createOptimalImage(final BufferedImage src, final int width, final int height) {
return new BufferedImage(width, height, getImageType(src));
}
private int getImageType(final BufferedImage src) {
return src.getTransparency() == OPAQUE ? TYPE_INT_RGB : TYPE_INT_ARGB;
}
}
package siscom.model.service.helper;
import java.io.IOException;
public interface ImageTool {
byte[] resizeCenteredPNG(byte[] src, int size) throws IOException;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment