Try something similar to this:
/**
* Redimensiona a imagem.
*
* @return array de bytes com imagem redimensionada
*/
private static byte[] resizeImage(byte[] imageByte, int width, int height) throws IOException {
try (InputStream inputImage = new ByteArrayInputStream(imageByte);) {
BufferedImage image = ImageIO.read(inputImage);
BufferedImage imgResized = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
imgResized.getGraphics().drawImage(image, 0, 0, width, height, null);
return imageToByte(imgResized);
}
}
Where imageToByte is this method:
/**
* Converte BufferedImage para array de bytes no formato JPG.
*
*/
private static byte[] imageToByte(BufferedImage img) throws IOException {
return imageToByte(img, ImageType.JPG);
}
imagesToByte:
/**
* Converte BufferedImage para array de bytes.
*
*/
private static byte[] imageToByte(BufferedImage img, ImageType tipoImagem) throws IOException {
try (ByteArrayOutputStream baos = new ByteArrayOutputStream();) {
ImageIO.write(img, tipoImagem.getValue(), baos);
baos.flush();
return baos.toByteArray();
}
}
The Enum he passes as a parameter is this:
public enum ImageType {
JPG("jpg"), PNG("png"), JPEG("jpeg");
This method works perfectly. Doubts I am available.
I don’t know in Java, but I use this service for this purpose. Even, they have an API that has an excellent compression result. If useful, please follow the link: https://tinypng.com.
– Bruno Rigolon
Doubled his own question: Decrease a Base64
– Homer Simpson