2

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.

kristianp
  • 5,496
  • 37
  • 56
A. Gesta
  • 68
  • 7
  • What do you see if instead of a Base64OutputStream you write the file to the filesystem and open it in an image viewer? – dnault Mar 08 '16 at 01:03
  • ahh but i dont want to write it to the filesystem as this is just a temporary. user has uploaded the file and i just want to give him the preview/thumbnail of his uploaded file. – A. Gesta Mar 08 '16 at 01:28
  • I understand; just trying to limit the number of factors so it's easier to identify which one is causing the problem. – dnault Mar 08 '16 at 02:02

1 Answers1

1

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.

A. Gesta
  • 68
  • 7
  • With your old code does calling `flush()` or `close()` on the Base64OutputStream after calling `ImageIO.write` fix the problem? – dnault Mar 08 '16 at 02:04
  • I haven't tried that one. But I did this from the following answer: http://stackoverflow.com/questions/13109588/base64-encoding-in-java – A. Gesta Mar 08 '16 at 02:41