简体   繁体   中英

Compress image, using imageScalr

I am using imagescalr for resizing images, but i also want to compress the images so that the size stays below 32kb(as ie 8 supports base64 strings upto 32kb). I am resizing the image like this

Map resizeImage(BufferedImage imageData, int width, int height, String imageFormat){

    BufferedImage thumbnail =  Scalr.resize(imageData, Scalr.Method.SPEED, Scalr.Mode.FIT_EXACT ,
    width, height, Scalr.OP_ANTIALIAS);

    ByteArrayOutputStream baos = new ByteArrayOutputStream()
    ImageIO.write(thumbnail, imageFormat, baos)
    baos.flush()
    byte[] imageBytes = baos.toByteArray()
    baos.close()

    return [imageBytes:imageBytes, imageFormat:imageFormat]
}

I want a way so that i can compress with least change to the code.

You could try

ByteArrayOutputStream baos = new ByteArrayOutputStream();
OutputStream gzip = new GZIPOutputStream(baos);
ImageIO.write(thumbnail, imageFormat, baos)
gzip.close();
byte[] zippedImageBytes = baos.toByteArray();

unzipping

InputStream in = new GZIPInputStream(new ByteArrayInputStream(zippedImageBytes));
....

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM