import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Image; import java.awt.RenderingHints; import java.awt.geom.AffineTransform; import java.awt.image.BufferedImage; import java.awt.image.ColorModel; import java.awt.image.WritableRaster; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import javax.imageio.ImageIO; import javax.swing.JFileChooser; import com.sun.image.codec.jpeg.JPEGCodec; import com.sun.image.codec.jpeg.JPEGImageEncoder;/*** @author hongliang* */ public class Grip { public static void main(String[] args) throws Throwable {
createNewPic("F:\\img\\img2", "F:\\img\\back\\kong.png", "F:\\img\\tar\\");第一个参数是要处理图片所在的文件夹,第二个就是上面说的用ps搞的一张空白图,第三个参数是处理玩的图片放的文件夹。 } public static BufferedImage resize(BufferedImage source, int targetW, int targetH) { // targetW,targetH分别表示目标长和宽 int type = source.getType(); BufferedImage target = null; double sx = (double) targetW / source.getWidth(); double sy = (double) targetH / source.getHeight(); //这里想实现在targetW,targetH范围内实现等比缩放。如果不需要等比缩放 //则将下面的if else语句注释即可 if(sx>sy) { sx = sy; targetW = (int)(sx * source.getWidth()); }else{ sy = sx; targetH = (int)(sy * source.getHeight()); } if (type == BufferedImage.TYPE_CUSTOM) { //handmade ColorModel cm = source.getColorModel(); WritableRaster raster = cm.createCompatibleWritableRaster(targetW, targetH); boolean alphaPremultiplied = cm.isAlphaPremultiplied(); target = new BufferedImage(cm, raster, alphaPremultiplied, null); } else target = new BufferedImage(targetW, targetH, type); Graphics2D g = target.createGraphics(); //smoother than exlax: g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY ); g.drawRenderedImage(source, AffineTransform.getScaleInstance(sx, sy)); g.dispose(); return target; } public static boolean isPic(File file){ String imgeArray []= {"bmp","dib","gif","jfif","jpe","jpeg","jpg","png","tif","tiff","ico"}; String fileName = file.getName(); fileName=fileName.substring(fileName.lastIndexOf(".")+1); for(String suffix:imgeArray){ if(fileName.equals(suffix)){ return true; } } return false; } public static void createNewPic(String dir,String backGroundPicPath,String targetPicPath) throws IOException{ File ML = new File(dir); File[] files = ML.listFiles(); for(File f:files){ if(f.isFile()){ if(isPic(f)){ File file = new File(backGroundPicPath); Image img = ImageIO.read(file); int width = img.getWidth(null); int height = img.getHeight(null); //create target image BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);//get a graphics pen Graphics g = image.createGraphics();//draw source image g.drawImage(img, 0, 0, width, height, null); //draw target logo BufferedImage doPic = ImageIO.read(f); if(doPic.getWidth()>width||doPic.getHeight()>height){ BufferedImage didPic=resize(doPic,width,height); } BufferedImage didPic=doPic; int lw = didPic.getWidth(null); int lh = didPic.getHeight(null); int px_x=(width-lw)/2; int px_y=(height-lh)/2; g.drawImage(didPic, px_x, px_y, lw, lh, null); //这里是添加水印文字// String str = "http://www.hongliang.net"; // g.setColor(Color.red); // g.setFont(new Font("Courier", Font.PLAIN, 36)); //水印文字对准// g.drawString(str, width-400, height-60); g.dispose(); FileOutputStream os = new FileOutputStream(targetPicPath+f.getName()); JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(os); encoder.encode(image); System.out.println("完成:"+f.getName()); } } } } }
附件 放上一张kong。png图。 程序要运行只要建立好几个相应的文件夹就可以了。