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);
}
}
Being imageToByte 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 it passes as a parameter is this:
public enum ImageType {
JPG("jpg"), PNG("png"), JPEG("jpeg");
This method works perfectly. Doubts are available.