1. import java.awt.Color;
    import java.awt.Font;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.LinearGradientPaint;
    import java.awt.Paint;
    import java.awt.RenderingHints;
    import java.awt.geom.AffineTransform;
    import java.awt.image.BufferedImage;
    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.OutputStream;
    import java.util.Arrays;
    import java.util.Random;
  2.  
  3. import javax.imageio.ImageIO;
  4.  
  5. public class Test{
  6.  
  7. //使用到Algerian字体,系统里没有的话需要安装字体,字体只显示大写,去掉了1,0,i,o几个容易混淆的字符
    public static final String VERIFY_CODES = "23456789ABCDEFGHJKLMNPQRSTUVWXYZ";
    private static Random random = new Random();
  8.  
  9. /**
    * 使用系统默认字符源生成验证码
    * @param verifySize 验证码长度
    * @return
    */
    public static String generateVerifyCode(int verifySize){
    return generateVerifyCode(verifySize, VERIFY_CODES);
    }
    /**
    * 使用指定源生成验证码
    * @param verifySize 验证码长度
    * @param sources 验证码字符源
    * @return
    */
    public static String generateVerifyCode(int verifySize, String sources){
    if(sources == null || sources.length() == 0){
    sources = VERIFY_CODES;
    }
    int codesLen = sources.length();
    Random rand = new Random(System.currentTimeMillis());
    StringBuilder verifyCode = new StringBuilder(verifySize);
    for(int i = 0; i < verifySize; i++){
    verifyCode.append(sources.charAt(rand.nextInt(codesLen-1)));
    }
    return verifyCode.toString();
    }
  10.  
  11. /**
    * 生成随机验证码文件,并返回验证码值
    * @param w
    * @param h
    * @param outputFile
    * @param verifySize
    * @return
    * @throws IOException
    */
    public static String outputVerifyImage(int w, int h, File outputFile, int verifySize) throws IOException{
    String verifyCode = generateVerifyCode(verifySize);
    outputImage(w, h, outputFile, verifyCode);
    return verifyCode;
    }
  12.  
  13. /**
    * 输出随机验证码图片流,并返回验证码值
    * @param w
    * @param h
    * @param os
    * @param verifySize
    * @return
    * @throws IOException
    */
    public static String outputVerifyImage(int w, int h, OutputStream os, int verifySize) throws IOException{
    String verifyCode = generateVerifyCode(verifySize);
    outputImage(w, h, os, verifyCode);
    return verifyCode;
    }
  14.  
  15. /**
    * 生成指定验证码图像文件
    * @param w
    * @param h
    * @param outputFile
    * @param code
    * @throws IOException
    */
    public static void outputImage(int w, int h, File outputFile, String code) throws IOException{
    if(outputFile == null){
    return;
    }
    File dir = outputFile.getParentFile();
    if(!dir.exists()){
    dir.mkdirs();
    }
    try{
    outputFile.createNewFile();
    FileOutputStream fos = new FileOutputStream(outputFile);
    outputImage(w, h, fos, code);
    fos.close();
    } catch(IOException e){
    throw e;
    }
    }
  16.  
  17. /**
    * 输出指定验证码图片流
    * @param w
    * @param h
    * @param os
    * @param code
    * @throws IOException
    */
    public static void outputImage(int w, int h, OutputStream os, String code) throws IOException{
    int verifySize = code.length();
    BufferedImage image = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
    Random rand = new Random();
    Graphics2D g2 = image.createGraphics();
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
    Color[] colors = new Color[5];
    Color[] colorSpaces = new Color[] { Color.WHITE, Color.CYAN,
    Color.GRAY, Color.LIGHT_GRAY, Color.MAGENTA, Color.ORANGE,
    Color.PINK, Color.YELLOW };
    float[] fractions = new float[colors.length];
    for(int i = 0; i < colors.length; i++){
    colors[i] = colorSpaces[rand.nextInt(colorSpaces.length)];
    fractions[i] = rand.nextFloat();
    }
    Arrays.sort(fractions);
  18.  
  19. g2.setColor(Color.GRAY);// 设置边框色
    g2.fillRect(0, 0, w, h);
  20.  
  21. Color c = getRandColor(200, 250);
    g2.setColor(c);// 设置背景色
    g2.fillRect(0, 2, w, h-4);
  22.  
  23. //绘制干扰线
    Random random = new Random();
    g2.setColor(getRandColor(160, 200));// 设置线条的颜色
    for (int i = 0; i < 20; i++) {
    int x = random.nextInt(w - 1);
    int y = random.nextInt(h - 1);
    int xl = random.nextInt(6) + 1;
    int yl = random.nextInt(12) + 1;
    g2.drawLine(x, y, x + xl + 40, y + yl + 20);
    }
  24.  
  25. // 添加噪点
    float yawpRate = 0.05f;// 噪声率
    int area = (int) (yawpRate * w * h);
    for (int i = 0; i < area; i++) {
    int x = random.nextInt(w);
    int y = random.nextInt(h);
    int rgb = getRandomIntColor();
    image.setRGB(x, y, rgb);
    }
  26.  
  27. shear(g2, w, h, c);// 使图片扭曲
  28.  
  29. g2.setColor(getRandColor(100, 160));
    int fontSize = h-4;
    Font font = new Font("Algerian", Font.ITALIC, fontSize);
    g2.setFont(font);
    char[] chars = code.toCharArray();
    for(int i = 0; i < verifySize; i++){
    AffineTransform affine = new AffineTransform();
    affine.setToRotation(Math.PI / 4 * rand.nextDouble() * (rand.nextBoolean() ? 1 : -1), (w / verifySize) * i + fontSize/2, h/2);
    g2.setTransform(affine);
    g2.drawChars(chars, i, 1, ((w-10) / verifySize) * i + 5, h/2 + fontSize/2 - 10);
    }
  30.  
  31. g2.dispose();
    ImageIO.write(image, "jpg", os);
    }
  32.  
  33. private static Color getRandColor(int fc, int bc) {
    if (fc > 255)
    fc = 255;
    if (bc > 255)
    bc = 255;
    int r = fc + random.nextInt(bc - fc);
    int g = fc + random.nextInt(bc - fc);
    int b = fc + random.nextInt(bc - fc);
    return new Color(r, g, b);
    }
  34.  
  35. private static int getRandomIntColor() {
    int[] rgb = getRandomRgb();
    int color = 0;
    for (int c : rgb) {
    color = color << 8;
    color = color | c;
    }
    return color;
    }
  36.  
  37. private static int[] getRandomRgb() {
    int[] rgb = new int[3];
    for (int i = 0; i < 3; i++) {
    rgb[i] = random.nextInt(255);
    }
    return rgb;
    }
  38.  
  39. private static void shear(Graphics g, int w1, int h1, Color color) {
    shearX(g, w1, h1, color);
    shearY(g, w1, h1, color);
    }
  40.  
  41. private static void shearX(Graphics g, int w1, int h1, Color color) {
  42.  
  43. int period = random.nextInt(2);
  44.  
  45. boolean borderGap = true;
    int frames = 1;
    int phase = random.nextInt(2);
  46.  
  47. for (int i = 0; i < h1; i++) {
    double d = (double) (period >> 1)
    * Math.sin((double) i / (double) period
    + (6.2831853071795862D * (double) phase)
    / (double) frames);
    g.copyArea(0, i, w1, 1, (int) d, 0);
    if (borderGap) {
    g.setColor(color);
    g.drawLine((int) d, i, 0, i);
    g.drawLine((int) d + w1, i, w1, i);
    }
    }
  48.  
  49. }
  50.  
  51. private static void shearY(Graphics g, int w1, int h1, Color color) {
  52.  
  53. int period = random.nextInt(40) + 10; // 50;
  54.  
  55. boolean borderGap = true;
    int frames = 20;
    int phase = 7;
    for (int i = 0; i < w1; i++) {
    double d = (double) (period >> 1)
    * Math.sin((double) i / (double) period
    + (6.2831853071795862D * (double) phase)
    / (double) frames);
    g.copyArea(i, 0, 1, h1, 0, (int) d);
    if (borderGap) {
    g.setColor(color);
    g.drawLine(i, (int) d, i, 0);
    g.drawLine(i, (int) d + h1, i, h1);
    }
    }
    }
    public static void main(String[] args) throws IOException{
    File dir = new File("D:/aaa");
    int w = 250, h = 80;
    String verifyCode = generateVerifyCode(4);
    File file = new File(dir, verifyCode + ".jpg");
    outputImage(w, h, file,verifyCode);
    }
    }

Java 生产图片验证码的更多相关文章

  1. 用Java实现图片验证码功能

    一.什么是图片验证码? 可以参考下面这张图: 我们在一些网站注册的时候,经常需要填写以上图片的信息. 1.图片生成实体类: package com.hexianwei.graphic; import ...

  2. java实现图片验证码

    一.验证码生成类 package hbi.tech.utils; import javax.imageio.ImageIO; import java.awt.*; import java.awt.im ...

  3. [Java] 识别图片验证码

    现在大多数网站都采用了验证码来防止暴力破解或恶意提交.但验证码真的就很安全吗?真的就不能被机器识别?? 我先讲讲我是怎么实现站外提交留言到一个网站的程序. 这个网站的留言版大致如下: 我一看这种简单的 ...

  4. 自己封装的一个java图片验证码

    验证码生成器: package com.lz.Tools; import java.awt.Color; import java.awt.Font; import java.awt.Graphics; ...

  5. java分别通过httpclient和HttpURLConnection获取图片验证码内容

    前面的文章,介绍了如何通过selenium+Tesseract-OCR来识别图片验证码,如果用接口来访问的话,再用selenium就闲的笨重,下面就介绍一下分别通过httpclient和HttpURL ...

  6. 图片验证码的JAVA工具类

    我们平时开发时经常会遇到需要图片验证码,基础的验证码包括了数字.字母.甚至可能有汉字.下面我给出一个简单的工具类. package com..ankang.tony.util; import java ...

  7. 用Java制作一个简单的图片验证码

    //Java实现简单验证码功能 package project; import java.awt.Color; import java.awt.Font;import java.awt.Graphic ...

  8. Java如何获取图片验证码保存

    举例网站:https://my.1hai.cn/Login/?url=http://www.1hai.cn/ 一.场景:出于安全性考虑,越来越多的Web平台登录都会增加图形验证码(图片),或者短信验证 ...

  9. 怎样用Java自制优秀的图片验证码?这样!

    Completely Automated Public Turing test to tell Computers and Humans Apart 全自动区分计算机和人类的图灵测试 简称CAPTCH ...

随机推荐

  1. zookeeper单机模式安装

    zookeeper单机模式安装 更多文章:http://blogxinxiucan.sh1.newtouch.com/2017/07/26/zookeeper单机模式安装/ 下载zookeeper u ...

  2. MySQL Database Command Line Client

    .输入密码:****** 1.1.create datatable Name;创建数据库 1.2.source D://taotao.sql; source后面加sql文件是批量导入sql语句 .ue ...

  3. [补档]暑假集训D1总结

    归来 今天就这样回来了,虽然心里极其不想回来(暑假!@#的只有一天啊喂),但还是回来了,没办法,虽然不喜欢这个地方,但是机房却也是少数能给我安慰的地方,心再累,也没有办法了,不如好好集训= = %da ...

  4. SpringBoot填坑系列---XML方式配置数据库

    本次只是简单的运用SpringBoot搭建框架,对其原理并不做深入的探究 1.POM文件 <?xml version="1.0" encoding="UTF-8&q ...

  5. Red Hat 9.0 Linux 分辨率修改

    Red Hat 9.0 Linux 分辨率修改 我是在VMware Workstation中装了一个红帽的Linux系统,装上之后发现分辨率有点低,是800x600的,看着很不舒服,然后就想着怎么样可 ...

  6. C#语言入门详解(002)

    c# 所編寫的不同應用程序 Console.WriteLine("Hello World!"); ///console textBoxShowHellow.Text = " ...

  7. java 内存模型的理解

    之前一直在实习,博客停写了一段时间,现在秋招开始了,所以辞职回来专心看书,同时将每天的收获以博客的形式记录下来.最近在看jvm相关的书籍,下面对面试中问得最多的部分--java 内存模型. 本篇博客大 ...

  8. HTML 贝塞尔曲线

    1.二次贝塞尔曲线 <canvas id="myCanvas" width="300" height="150" style=&quo ...

  9. .NET并行计算和并发2-Foreground and Background Threads

    后台线程不会使托管执行环境处于运行状态,除此之外,后台线程与前台线程是一样的. 一旦所有前台线程在托管进程(其中 .exe 文件是托管程序集)中被停止,系统将停止所有后台线程并关闭.

  10. 【复制】【编码】MySQL复制中的编码问题

    编码背景知识 Latin-1,全称ISO 8859-1 Latin 1 对ASCII的拉丁语扩展 向下兼容ASCII,其编码范围是0x00-0xFF,0x00-0x7F之间完全和ASCII一致,0x8 ...