验证码图片生成器
验证码图片生成器:http://pan.baidu.com/s/1kUL25Cz
生成算法Algorithm.java
package PictureVerification; import java.awt.Color; import java.awt.Font; import java.awt.Graphics2D; import java.awt.Image; import java.awt.image.BufferedImage; public class Algorithm{ private static final int size=6; private static final int WIDTH=30*size; private static final int HEIGHT=40; private static final int lineNum=6; private char[] cstr=new char[size]; public Algorithm() {} public Image generate(){ int width=WIDTH/size; int height=HEIGHT; BufferedImage imagebuf=new BufferedImage(width*size,height, BufferedImage.TYPE_INT_RGB); Graphics2D g=imagebuf.createGraphics(); int base=0; for(int i=1;i<size-1;i++){ base=width*i; Font font =new Font("Tahoma",Font.ITALIC,(int)((Math.random()*(30-10))+10)); g.setFont(font); double redix = Math.PI*Math.random()/2-Math.PI/4; g.rotate(redix,base+width/2, height/2); String str=""+(int)(Math.random()*10); cstr[i]=str.charAt(0); g.drawString(str,(int)(base*(1.2)),(int)(height*(0.8))); g.rotate(-redix,base+width/2, height/2); } //加线条 for(int i=0;i<lineNum;i++){ g.setColor(new Color((int)(Math.random()*255),(int)(Math.random()*255),(int)(Math.random()*255))); int a=(int)(Math.random()*WIDTH); int b=(int)(Math.random()*HEIGHT); int c=(int)(Math.random()*WIDTH); int d=(int)(Math.random()*HEIGHT); g.drawLine(a, b,c,d); } return imagebuf; } public String getStr(){ return new String(cstr, 1, cstr.length-2); } }
View Code
MenuView.java(展示类)
package PictureVerification; import java.awt.AWTException; import java.awt.BorderLayout; import java.awt.EventQueue; import java.awt.Graphics2D; import java.awt.Robot; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.image.BufferedImage; import java.io.File; import javax.imageio.ImageIO; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.border.EmptyBorder; public class MenuView extends JFrame { /** * */ private static final long serialVersionUID = 1L; private JPanel contentPane; private JFrame f=this; private JPanel panel; private JLabel label; private ImageIcon image; private BufferedImage imagebuf; private JButton btnNewButton_1; private JLabel lblNewLabel; private Algorithm algo=new Algorithm(); /** * Launch the application. */ public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { try { MenuView frame = new MenuView(); //MyCanvas m=new MyCanvas(); JFrame f=frame; //f.getContentPane().add(m); //f.setLayout(null); f.setVisible(true); frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); } /** * Create the frame. */ public MenuView() { setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(100, 100, 301, 256); contentPane = new JPanel(); contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); setContentPane(contentPane); contentPane.setLayout(null); panel = new JPanel(new BorderLayout()); image=new ImageIcon(""); label=new JLabel("",image,JLabel.CENTER); panel.add(label,BorderLayout.CENTER); panel.setBounds(31, 30, 221, 97); contentPane.add(panel); JButton btnNewButton = new JButton("generate"); btnNewButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { loadImage(); } }); btnNewButton.setBounds(56, 174, 93, 23); contentPane.add(btnNewButton); btnNewButton_1 = new JButton("save"); btnNewButton_1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { saveImage(); } }); btnNewButton_1.setBounds(159, 174, 93, 23); contentPane.add(btnNewButton_1); lblNewLabel = new JLabel(""); lblNewLabel.setBounds(120, 137, 54, 15); contentPane.add(lblNewLabel); } private void loadImage(){ image=new ImageIcon(algo.generate()); label.setIcon(image); lblNewLabel.setText(algo.getStr()); } private void saveImage(){ BufferedImage imagebuf=null;; try { imagebuf = new Robot().createScreenCapture(panel.bounds()); } catch (AWTException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } Graphics2D graphics2D = imagebuf.createGraphics(); panel.paint(graphics2D); try { ImageIO.write(imagebuf,"png", new File(algo.getStr()+".png")); } catch (Exception e) { // TODO Auto-generated catch block System.out.println("error"); } } }
View Code
下面是一我做这个随机码图片生成器的一些些技术细节:
需求:
1、展示:一个界面,点击生成按钮就能够生成一张验证码图片;点击保存按钮能够把图片保存到当前文件夹;
在一个JPanel中显示图片:
JPanel panel = new JPanel(new BorderLayout()); ImageIcon image=new ImageIcon("1.png"); JLabel label=new JLabel("",image,JLabel.CENTER); panel.add(label,BorderLayout.CENTER); panel.setBounds(31, 30, 221, 121);
能够把JFrame存为图片:
package PictureVerification; import java.awt.AWTException; import java.awt.BorderLayout; import java.awt.Canvas; import java.awt.Color; import java.awt.EventQueue; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Image; import java.awt.Rectangle; import java.awt.Robot; import java.awt.Toolkit; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.image.BufferedImage; import java.io.File; import javax.imageio.ImageIO; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.border.EmptyBorder; class MyCanvas extends Canvas{ public void paint(Graphics g) { Toolkit t=Toolkit.getDefaultToolkit(); Image i=t.getImage("1.png"); g.drawImage(i, 120,100,this); } } class DisplayGraphics extends Canvas{ public void paint(Graphics g) { int num=(int)(Math.random()*10); g.drawString(""+num, 10, 10); //g.drawString("Hello",40,40); setBackground(Color.WHITE); g.fillRect(130, 30,100, 80); g.drawOval(30,130,50, 60); setForeground(Color.RED); g.fillOval(130,130,50, 60); g.drawArc(30, 200, 40,50,90,60); g.fillArc(30, 130, 40,50,180,40); } } public class Mine extends JFrame { private JPanel contentPane; private JFrame f=this; private JPanel panel; private JLabel label; private ImageIcon image; private BufferedImage imagebuf; private JButton btnNewButton_1; /** * Launch the application. */ public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { try { Mine frame = new Mine(); //MyCanvas m=new MyCanvas(); JFrame f=frame; //f.getContentPane().add(m); //f.setLayout(null); f.setVisible(true); frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); } /** * Create the frame. */ public Mine() { setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(100, 100, 301, 256); contentPane = new JPanel(); contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); setContentPane(contentPane); contentPane.setLayout(null); JPanel panel = new JPanel(new BorderLayout()); ImageIcon image=new ImageIcon(""); label=new JLabel("",image,JLabel.CENTER); panel.add(label,BorderLayout.CENTER); panel.setBounds(31, 30, 221, 121); contentPane.add(panel); JButton btnNewButton = new JButton("generate"); btnNewButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { loadImage(); } }); btnNewButton.setBounds(56, 174, 93, 23); contentPane.add(btnNewButton); btnNewButton_1 = new JButton("save"); btnNewButton_1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { saveImage(); } }); btnNewButton_1.setBounds(159, 174, 93, 23); contentPane.add(btnNewButton_1); } private void loadImage(){ image=new ImageIcon("1.png"); label.setIcon(image); } private void saveImage(){ //imagebuf = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_RGB); BufferedImage imagebuf=null;; try { imagebuf = new Robot().createScreenCapture( new Rectangle( getX(), getY(), getWidth(), getHeight() ) ); } catch (AWTException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } Graphics2D graphics2D = imagebuf.createGraphics(); paint(graphics2D); System.out.println("before save"); try { ImageIO.write(imagebuf,"jpeg", new File("save1.jpeg")); } catch (Exception e) { // TODO Auto-generated catch block System.out.println("error"); } } }
View Code
保存单个JPanel:
private void saveImage(){ BufferedImage imagebuf=null;; try { imagebuf = new Robot().createScreenCapture(panel.bounds()); } catch (AWTException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } Graphics2D graphics2D = imagebuf.createGraphics(); panel.paint(graphics2D); try { ImageIO.write(imagebuf,"jpeg", new File("save1.jpeg")); } catch (Exception e) { // TODO Auto-generated catch block System.out.println("error"); } }
View Code
2、生成算法:返回值为Image
(1)直接的纯文本
可以设置字体,大小,颜色
(2)纯文本加噪声
加线条
(3)调整大小方位
旋转
(4)使用矩阵变换进行扭曲
仿射变化
java中的robot类
Image转BufferedImage
BufferedImage image = ImageIO.read(new File(filename));
public static BufferedImage toBufferedImage(Image img) { if (img instanceof BufferedImage) { return (BufferedImage) img; } // Create a buffered image with transparency BufferedImage bimage = new BufferedImage(img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_INT_ARGB); // Draw the image on to the buffered image Graphics2D bGr = bimage.createGraphics(); bGr.drawImage(img, 0, 0, null); bGr.dispose(); // Return the buffered image return bimage; }
旋转绘图:
BufferedImage imagebuf=ImageIO.read(new File("test2.png")); Graphics2D g=imagebuf.createGraphics(); g.rotate(0.5, 50, 25); g.drawString("hello", 100,50); ImageIO.write(imagebuf,"png", new File("test22.png"));
import java.awt.Graphics2D; import java.awt.geom.AffineTransform; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import javax.imageio.ImageIO; public class Test { public static void main(String[] args) throws IOException { // TODO Auto-generated method stub BufferedImage imagebuf=new BufferedImage(200, 200, BufferedImage.TYPE_INT_RGB); Graphics2D g=imagebuf.createGraphics(); g.drawString("hello", 80,120); AffineTransform transtate=new AffineTransform(); transtate.translate(18,10); AffineTransform scale=AffineTransform.getScaleInstance(300/100, 350/50); AffineTransform tx=g.getTransform(); tx.concatenate(scale); tx.concatenate(transtate); g.setTransform(tx); g.drawString("hello", 10,5); g.rotate(Math.PI, 10, 5); g.drawString("hello", 10,5); ImageIO.write(imagebuf,"png", new File("test22.png")); } }
View Code
图片的仿射变化:http://docs.geotools.org/stable/userguide/tutorial/affinetransform.html#
可以翻转,移动缩放图片。
关键技术就是这些,最完后就是上面的样子。
最后介绍一个开源的验证码项目:http://jcaptcha.sourceforge.net/