画像のサムネイルは白い辺javaを補って実現します
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を1枚入れます.png図.プログラムを実行するには、対応するフォルダをいくつか作成すればいいです.