Here you can find the source of cover(BufferedImage source, File to, Image cover, int width, int height, int sourceTop, int sourceLeft, int sourceWidth, int sourceHeight)
public static void cover(BufferedImage source, File to, Image cover, int width, int height, int sourceTop, int sourceLeft, int sourceWidth, int sourceHeight)
//package com.java2s; //License from project: Open Source License import java.awt.Color; import java.awt.Graphics; import java.awt.Image; import java.awt.image.BufferedImage; import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import javax.imageio.IIOImage; import javax.imageio.ImageIO; import javax.imageio.ImageWriteParam; import javax.imageio.ImageWriter; import javax.imageio.stream.FileImageOutputStream; public class Main { public static void cover(BufferedImage source, File to, Image cover, int width, int height, int sourceTop, int sourceLeft, int sourceWidth, int sourceHeight) { BufferedImage dest = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); Image imageSource = source.getScaledInstance(sourceWidth, sourceHeight, Image.SCALE_SMOOTH); Graphics graphics = dest.getGraphics(); graphics.setColor(new Color(225, 225, 225)); graphics.fillRect(0, 0, width, height); graphics.drawImage(imageSource, sourceTop, sourceLeft, null); graphics.drawImage(cover, 0, 0, null); String mimeType = "image/jpeg"; ImageWriter writer = ImageIO.getImageWritersByMIMEType(mimeType).next(); try {//from w ww . j a va 2 s . c o m FileImageOutputStream toFs = new FileImageOutputStream(to); writer.setOutput(toFs); IIOImage iioImage = new IIOImage(dest, null, null); writer.write(null, iioImage, createImageWriteParam(writer)); toFs.flush(); toFs.close(); } catch (FileNotFoundException e) { System.out.println(e); } catch (IOException e) { System.out.println(e); } } public static ImageWriteParam createImageWriteParam(ImageWriter writer) { ImageWriteParam params = writer.getDefaultWriteParam(); // JPEGImageWriteParam jpegImageWriteParam = new // JPEGImageWriteParam(writer.getLocale()); // jpegImageWriteParam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT); // jpegImageWriteParam.setCompressionQuality(0.8F); params.setCompressionMode(ImageWriteParam.MODE_EXPLICIT); params.setCompressionQuality(0.95F); return params; } }