Introduction to the

The utility class is mainly used to compress the picture to the specified size

The case code is as follows

  • Both input and outputbyteArray, easier to use later
Public class ImageFileCompressUtils {/** * compressed image * @param sourceBytes * @param quality * @param maxWidth * @param maxHeight * @param targetFileSize * @return * @throws Exception */ public static byte[] compressImageFile(byte[] sourceBytes, double quality, Integer maxWidth, Integer maxHeight, Integer targetFileSize) throws Exception{ BufferedImage bim = ImageIO.read(new ByteArrayInputStream(sourceBytes)); int srcWidth = bim.getWidth(); int srcHeight = bim.getHeight(); Thumbnails.Builder = Thumbnails. Of (new ByteArrayInputStream(sourceBytes)).outputFormat(" PNG "); / / specified size (width or height above will be scaling) if (srcWidth > maxWidth | | srcHeight > maxHeight) {builder. The size (maxWidth and maxHeight); }else{builder.size(srcWidth,srcHeight);}else{builder.size(srcWidth,srcHeight); } // Write to memory ByteArrayOutputStream ByteArrayOutputStream = new ByteArrayOutputStream(); // Byte output stream (write to memory) Builder.toOutputStream (byteArrayOutputStream); return compressImageCircle(byteArrayOutputStream.toByteArray(),targetFileSize,quality); } /** * compress images to a specified size * @param sourceBytes source * @param targetFileSize target * @param quality Compression ratio 0-1 preferably less than 0.9 * @return * @throws Exception */ public static byte[] compressImageCircle(byte[] sourceBytes, long targetFileSize, double quality) throws Exception{ long sourceFileSize = sourceBytes.length; If (sourceFileSize <= targetFileSize * 1024) {return sourceBytes; } // Calculate width and width BufferedImage bim = imageio. read(new ByteArrayInputStream(sourceBytes)); int sourceWidth = bim.getWidth(); int sourceHeight = bim.getHeight(); int targetWidth = new BigDecimal(sourceWidth).multiply(new BigDecimal(quality)).intValue(); int targetHeight = new BigDecimal(sourceHeight).multiply(new BigDecimal(quality)).intValue(); ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); Thumbnails.of(new ByteArrayInputStream(sourceBytes)).size(targetWidth, targetHeight).outputQuality(quality).toOutputStream(byteArrayOutputStream); return compressImageCircle(byteArrayOutputStream.toByteArray(), targetFileSize, quality); }}Copy the code

Use the demo

@Test public void tt03() throws Exception{ byte[] source = ImageFileCompressUtils.compressImageCircle(Files.readAllBytes(new File (" C: \ \ Users \ \ 86183 \ \ Pictures \ \ 20200911131241 JPG "). The toPath ()), 200,0.8); getFileByBytes(source,"C:\\Users\\86183\\Pictures","2222.png"); } public static void getFileByBytes(Byte [] bytes, String filePath, String fileName) { BufferedOutputStream bos = null; FileOutputStream fos = null; File file = null; try { File dir = new File(filePath); if (! Dir.exists () && dir.isdirectory ()) {// Determine whether dir.mkdirs() exists in the file directory; } file = new File(filePath + "\\" + fileName); fos = new FileOutputStream(file); bos = new BufferedOutputStream(fos); bos.write(bytes); } catch (Exception e) { e.printStackTrace(); } finally { if (bos ! = null) { try { bos.close(); } catch (IOException e) { e.printStackTrace(); } } if (fos ! = null) { try { fos.close(); } catch (IOException e) { e.printStackTrace(); }}}}Copy the code