@SuppressWarnings("unused") private boolean saveJpeg(int[] byteArray, int width, int height, int dpi, String file) { BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); WritableRaster wr = bufferedImage.getRaster(); wr.setPixels(0, 0, width, height, byteArray); try { // Image writer JPEGImageWriter imageWriter = (JPEGImageWriter) ImageIO.getImageWritersBySuffix("jpeg").next(); ImageOutputStream ios = ImageIO.createImageOutputStream(new File(file)); imageWriter.setOutput(ios); // Compression JPEGImageWriteParam jpegParams = (JPEGImageWriteParam) imageWriter.getDefaultWriteParam(); jpegParams.setCompressionMode(JPEGImageWriteParam.MODE_EXPLICIT); jpegParams.setCompressionQuality(0.85f); // Metadata (dpi) IIOMetadata data = imageWriter.getDefaultImageMetadata(new ImageTypeSpecifier(bufferedImage), jpegParams); Element tree = (Element) data.getAsTree("javax_imageio_jpeg_image_1.0"); Element jfif = (Element) tree.getElementsByTagName("app0JFIF").item(0); jfif.setAttribute("Xdensity", Integer.toString(dpi)); jfif.setAttribute("Ydensity", Integer.toString(dpi)); jfif.setAttribute("resUnits", "1"); // density is dots per inch // Write and clean up imageWriter.write(data, new IIOImage(bufferedImage, null, null), jpegParams); ios.close(); imageWriter.dispose(); } catch (Exception e) { return false; } return true; }