package com.hh.javaapp.app1; import java.awt.Color; import java.awt.Font; import java.awt.Graphics2D; import java.awt.RenderingHints; import java.awt.image.BufferedImage; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.List; import java.util.Random; import javax.imageio.ImageIO; /** * @Title: ImgGenerator.java * @Package com.hh.javaapp.app1 * @project javaapp * @Description: 将用户的姓名生成图片 * @author [email protected] * @date 2013年10月14日 下午2:31:13 * @version V1.0 */ public class CreateNamePicture { /** * @throws IOException * @throws * **/ public static void main(String[] args) throws IOException { String fileName="D:/name.txt"; List<String> nameList = readFileByLine(fileName); for (int i = 0; i < nameList.size(); i++) { generateImg(nameList.get(i)); } } /** * * readFileByLine(将文件中的文字一行一行读取出来并存放在List中返回) * TODO(这里描述这个方法适用条件 – 不允许是图片等特殊文件) * @Title: readFileByLine * @Description: TODO * @param @param filename * @param @return * @param @throws IOException * @param @throws FileNotFoundException 设定文件 * @return List<String> 返回类型 * @throws */ public static List<String> readFileByLine(String filename) throws IOException, FileNotFoundException{ List<String> nameList=new ArrayList<String>(); File file=new File(filename); InputStreamReader isr=new InputStreamReader(new FileInputStream(file),"UTF-8"); BufferedReader reader=new BufferedReader(isr); String tmp; while((tmp=reader.readLine())!=null){ nameList.add(tmp); } reader.close(); return nameList; } /** * @Title: generateImg(生成图片) * @Description: TODO * @param @param name * @param @throws IOException 设定文件 * @return void 返回类型 * @throws */ public static void generateImg(String name) throws IOException{ int width=100; int height=100; String first=null,second=null,third=null,fourth=null; first=name.substring(0, 1); second=name.substring(1,2); if(name.length()==2){ third="印"; fourth=null; }else if(name.length()==3){ third=name.substring(2, 3); fourth="印"; }else{ third=name.substring(2,3); fourth=name.substring(3,4); } String filename="D:/name/"+name+".jpg"; System.out.println(filename); File file=new File(filename); Font font=new Font("黑体",Font.PLAIN,30); BufferedImage bi =new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); Graphics2D g2=(Graphics2D) bi.getGraphics(); g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON); g2.setBackground(getRandomColor()); g2.clearRect(0, 0, width, height); g2.setFont(font); g2.setPaint(Color.WHITE); g2.drawString(first, 55, 40); g2.drawString(second, 55, 80); g2.drawString(third, 15, 40); if(fourth==null){ drawStar(g2); }else{ g2.drawString(fourth, 15, 80); } ImageIO.write(bi,"jpg", file); } /** * drawStar(如果名字为三个字,调用这个方法画五角星) * @Title: drawStar * @Description: TODO * @param @param g2 设定文件 * @return void 返回类型 * @throws */ private static void drawStar(Graphics2D g2) { //定义外切圆和内切圆的半径 int R=16; int r=(int)(R*Math.sin(Math.PI/10)/Math.sin(3*Math.PI/10)); //定义两个数组,分别存放10个顶点的X,Y坐标 int [] x=new int[10]; int [] y=new int[10]; for(int i=0;i<10;i++){ if(i%2==0){ x[i]=30+(int)(R * Math.cos(Math.PI / 10 + (i - 1) * Math.PI / 5)); y[i] = 70 + (int) (R * Math.sin(Math.PI / 10 + (i - 1) * Math.PI / 5)); }else{ x[i] = 30 + (int) (r * Math.cos(Math.PI / 10 + (i - 1) * Math.PI / 5)); y[i] = 70 + (int) (r * Math.sin(Math.PI / 10 + (i - 1) * Math.PI / 5)); } } g2.setPaint(Color.white); // 调用fillPolygon方法绘制 g2.fillPolygon(x, y, 10); } /** * getRandomColor(随机产生颜色) * @Title: getRandomColor * @Description: TODO * @param @return 设定文件 * @return Color 返回类型 * @throws */ private static Color getRandomColor() { Random random=new Random(); return new Color(random.nextInt(255),random.nextInt(255),random.nextInt(255)); } }
以上代码会将存储在name.txt中的姓名名单生成对应的图片,调用下面的工具类会将图片做圆角处理或者缩小处理
package com.hh.javaapp.app1; import java.awt.AlphaComposite; import java.awt.Color; import java.awt.Graphics2D; import java.awt.Image; import java.awt.RenderingHints; import java.awt.geom.RoundRectangle2D; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import javax.imageio.ImageIO; /** * 改变图片的工具类 * @ClassName: ChangeImage * @Description: TODO * @author [email protected] * @date 2013年10月14日 下午3:34:56 * */ public class ChangeImageUtil { /** * makeRoundedCorner(图片做圆角处理) * @Title: makeRoundedCorner * @Description: TODO * @param @param image * @param @param cornerRadius * @param @return 设定文件 * @return BufferedImage 返回类型 * @throws */ public static BufferedImage makeRoundedCorner(BufferedImage image, int cornerRadius) { int w = image.getWidth(); int h = image.getHeight(); BufferedImage output = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB); Graphics2D g2 = output.createGraphics(); g2.setComposite(AlphaComposite.Src); g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON); g2.setColor(Color.WHITE); g2.fill(new RoundRectangle2D.Float(0, 0, w, h, cornerRadius, cornerRadius)); g2.setComposite(AlphaComposite.SrcAtop); g2.drawImage(image, 0, 0, null); g2.dispose(); return output; } /** * createResizedCopy(图片缩放处理) * @Title: createResizedCopy * @Description: TODO * @param @param originalImage * @param @param scaledWidth * @param @param scaledHeight * @param @param preserveAlpha * @param @return 设定文件 * @return BufferedImage 返回类型 * @throws */ public static BufferedImage createResizedCopy(Image originalImage, int scaledWidth, int scaledHeight, boolean preserveAlpha) { int imageType = preserveAlpha ? BufferedImage.TYPE_INT_RGB : BufferedImage.TYPE_INT_ARGB; BufferedImage scaledBI = new BufferedImage(scaledWidth, scaledHeight, imageType); Graphics2D g = scaledBI.createGraphics(); if (preserveAlpha) { g.setComposite(AlphaComposite.Src); } g.drawImage(originalImage, 0, 0, scaledWidth, scaledHeight, null); g.dispose(); return scaledBI; } public static void main(String[] args) throws IOException { BufferedImage icon = ImageIO.read(new File("D:/name/name1.jpg")); BufferedImage rounded = makeRoundedCorner(icon, 99); ImageIO.write(rounded, "png", new File("D:/name/mm_name1.png")); BufferedImage pic = ImageIO.read(new File("D:/name/name2.jpg")); BufferedImage resized = createResizedCopy(pic, pic.getWidth()/2,pic.getHeight()/2, true); ImageIO.write(resized, "jpg", new File("D:/name/name2_small.jpg")); } }
生成的效果如下图: