package de.oaa.xxx.aufgaben;

import org.slf4j.LoggerFactory;

import javax.imageio.ImageIO;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;

public class ImageScaler {

    public byte[] scale(byte[] origByte) {
        try (ByteArrayInputStream bais = new ByteArrayInputStream(origByte)) {
            BufferedImage orig = ImageIO.read(bais);
            BufferedImage scaled = (BufferedImage) orig.getScaledInstance(128, 128, Image.SCALE_DEFAULT);
            try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
                ImageIO.write(scaled, "png", baos);
                return baos.toByteArray();
            } catch (IOException exception) {
                LoggerFactory.getLogger(getClass()).error("Fehler beim Skalieren des Bildes", exception);
            }
        } catch (IOException exception) {
            LoggerFactory.getLogger(getClass()).error("Fehler beim Skalieren des Bildes", exception);
        }
        return new byte[0];
    }
}
