用java写图片

登录注册的时候都会有图片验证,这是为了防止暴力破解和恶意注册。写一个思路来实现验证图片的实现,只是一个思路,随机生成文字并没有写。

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import javax.imageio.ImageIO;

public class Image {
public static void main(String[] args) throws FileNotFoundException, IOException{
    //得到图片缓冲区
    BufferedImage bi=new BufferedImage(150,70, BufferedImage.TYPE_INT_RGB);
    //得到画笔
    Graphics2D g2=(Graphics2D) bi.getGraphics();
    //填充背景
    g2.setColor(Color.WHITE);
    g2.fillRect(0, 0, 150, 70);
    //设置边框
    g2.setColor(Color.RED);
    g2.drawRect(0, 0, 149, 69);
    //向图片上写字符串
  g2.setFont(new Font("宋体", Font.BOLD, 10));
g2.setColor(Color.BLACK); g2.drawString("grup", 19, 20); ImageIO.write(bi, "JPEG", new FileOutputStream("f:/g.jpg")); } }

可以做的文章有,第一是随机生成字符,第二随机生成颜色,第三随机生成干扰线,第四随机生成字体,第五随机生成字号等等

网页登录的时候需要图片验证,今天刚好用到,把之前没有做的东西补充上来。

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Random;

import javax.imageio.ImageIO;

public class VerifyCode {
  //设置宽
  private  int w=70;
 //设置高
  private  int h=35;
  private Random r=new Random();
  //字体
  private String[]  fontNames={"宋体","华文楷体","黑体","微软雅黑","楷体_GB2312"};
  //字符
  private String codes="23456789abcdefghigklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
 //背景颜色
  private Color bgColor=new Color(255, 255, 255);
  //图片文字
  private String text;
  //生成随机数
   private Color randomColor(){
       int red=r.nextInt(150);
       int green=r.nextInt(150);
       int blue=r.nextInt(150);
       return new Color(red,green,blue);
   }
   //生成随机字体
   private Font randomFont(){
       int index=r.nextInt(fontNames.length);
       String fontName=fontNames[index];
       int style=r.nextInt(4);
       int size=r.nextInt(5)+24;
       return new Font(fontName, style, size);
   }
   //生成随机字符
   private char randomChar(){
       int index=r.nextInt(codes.length());
       return codes.charAt(index);
   }
   //生成空白图片
   private BufferedImage createImage(){
       BufferedImage image=new BufferedImage(w,h,BufferedImage.TYPE_INT_RGB);
       Graphics2D g2=(Graphics2D)image.getGraphics();
       g2.setColor(this.bgColor);
       g2.fillRect(0, 0, w, h);
       return image;
   }
   //生成随机乱线
   private void drawLine(BufferedImage image){
       
       int num=3;
       Graphics2D g2=(Graphics2D)image.getGraphics();
       for(int i=0;i<3;i++){
           int x1=r.nextInt(w);
           int y1=r.nextInt(h);
           int x2=r.nextInt(w);
           int y2=r.nextInt(h);
           g2.setStroke(new BasicStroke(1.5F));
           g2.setColor(Color.BLUE);
           g2.drawLine(x1,y1,x2,y2);
       }
    
   }
   //将图片输出到某个输出流中
   public static void output(BufferedImage image,OutputStream out) throws IOException{
       ImageIO.write(image, "JPEG",out);
   }
   //得到图片上的文字
   public String getText(){
       return text;
   }
   //得到图片
   public BufferedImage getImage(){
       BufferedImage image=createImage();
       Graphics2D g2=(Graphics2D)image.getGraphics();
       StringBuilder sb=new StringBuilder();
       for(int i=0;i<4;i++){
           String s=randomChar()+"";
           sb.append(s);
           float x=i*1.0F*w/4;
           g2.setFont(randomFont());
           g2.setColor(randomColor());
           g2.drawString(s, x, h-5);
       }
       this.text=sb.toString();
       drawLine(image);
       return image;
   }
}

 

posted @ 2017-10-22 22:06  竹马今安在  阅读(2211)  评论(0编辑  收藏  举报