javaimgscalr

Base64 trimming my thumbnail image


I have some issues that I am having a hard time solving.

I made a short code snippet :

BufferedImage image = ImageIO.read(new ByteArrayInputStream(payload));
BufferedImage thumbImg = Scalr.resize(image, Method.QUALITY,
    Mode.AUTOMATIC, WIDTH, HEIGHT, Scalr.OP_ANTIALIAS);

ByteArrayOutputStream baos = new ByteArrayOutputStream();
Base64OutputStream b64s = new Base64OutputStream(baos);
ImageIO.write(thumbImg, DATA_TYPE, b64s);
return baos.toByteArray();

The returned thumbnail/byte is trimmed down. It deletes the bottom part and shows just a transparent area.

What I want is to have a scaled down image without removing some parts of it.

The purpose for this is to return a base64 to my html project.


Solution

  • Yea.. I just changed my logic for creating a base64 output.

    Instead of having it write in Base64OutputStream of Apache Commons Framework.

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    Base64OutputStream b64s = new Base64OutputStream(baos);
    ImageIO.write(thumbImg, DATA_TYPE, b64s);
    return new ThumbnailPayload(baos.toByteArray()));
    

    I did this instead

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ImageIO.write(thumbImg, DATA_TYPE, baos);
    return new ThumbnailPayload(Base64.encodeBase64(baos.toByteArray()));
    

    Currently it is working. But if you guys can suggest another way with an explanation before the day ends, that would be awesome and helpful.