generate Image Code - Java 2D Graphics

Java examples for 2D Graphics:Image Captcha

Description

generate Image Code

Demo Code


//package com.java2s;
import java.util.Random;
import java.awt.image.BufferedImage;
import java.awt.Graphics;
import java.awt.Font;
import java.awt.Color;

public class Main {

    public static final int TYPE_NUM_ONLY = 0;

    public static final int TYPE_LETTER_ONLY = 1;

    public static final int TYPE_ALL_MIXED = 2;

    public static final int TYPE_NUM_UPPER = 3;

    public static final int TYPE_NUM_LOWER = 4;

    public static final int TYPE_UPPER_ONLY = 5;

    public static final int TYPE_LOWER_ONLY = 6;

    public static BufferedImage generateImageCode(int type, int length,
            String exChars, int width, int height, int interLine,
            boolean randomLocation, Color backColor, Color foreColor,
            Color lineColor) {//from  ww  w .ja  va  2s . c o m

        String textCode = generateString(type, length, exChars);
        BufferedImage bim = generateImage(textCode, width, height,
                interLine, randomLocation, backColor, foreColor, lineColor);

        return bim;
    }

    public static String generateString(int type, int length, String exChars) {

        if (length <= 0)
            return "";

        StringBuffer code = new StringBuffer();
        int i = 0;
        Random r = new Random();

        switch (type) {

        // 
        case TYPE_NUM_ONLY:
            while (i < length) {
                int t = r.nextInt(10);
                if (exChars == null || exChars.indexOf(t + "") < 0) {// 
                    code.append(t);
                    i++;
                }
            }
            break;

        // 
        case TYPE_LETTER_ONLY:
            while (i < length) {
                int t = r.nextInt(123);
                if ((t >= 97 || (t >= 65 && t <= 90))
                        && (exChars == null || exChars.indexOf((char) t) < 0)) {
                    code.append((char) t);
                    i++;
                }
            }
            break;

        // 
        case TYPE_ALL_MIXED:
            while (i < length) {
                int t = r.nextInt(123);
                if ((t >= 97 || (t >= 65 && t <= 90) || (t >= 48 && t <= 57))
                        && (exChars == null || exChars.indexOf((char) t) < 0)) {
                    code.append((char) t);
                    i++;
                }
            }
            break;

        // 
        case TYPE_NUM_UPPER:
            while (i < length) {
                int t = r.nextInt(91);
                if ((t >= 65 || (t >= 48 && t <= 57))
                        && (exChars == null || exChars.indexOf((char) t) < 0)) {
                    code.append((char) t);
                    i++;
                }
            }
            break;

        // 
        case TYPE_NUM_LOWER:
            while (i < length) {
                int t = r.nextInt(123);
                if ((t >= 97 || (t >= 48 && t <= 57))
                        && (exChars == null || exChars.indexOf((char) t) < 0)) {
                    code.append((char) t);
                    i++;
                }
            }
            break;

        // 
        case TYPE_UPPER_ONLY:
            while (i < length) {
                int t = r.nextInt(91);
                if ((t >= 65)
                        && (exChars == null || exChars.indexOf((char) t) < 0)) {
                    code.append((char) t);
                    i++;
                }
            }
            break;

        // 
        case TYPE_LOWER_ONLY:
            while (i < length) {
                int t = r.nextInt(123);
                if ((t >= 97)
                        && (exChars == null || exChars.indexOf((char) t) < 0)) {
                    code.append((char) t);
                    i++;
                }
            }
            break;

        }

        return code.toString();
    }

    public static BufferedImage generateImage(String textCode, int width,
            int height, int interLine, boolean randomLocation,
            Color backColor, Color foreColor, Color lineColor) {

        BufferedImage bim = new BufferedImage(width, height,
                BufferedImage.TYPE_INT_RGB);
        Graphics g = bim.getGraphics();

        // 
        g.setColor(backColor == null  getRandomColor() : backColor);
        g.fillRect(0, 0, width, height);

        // 
        Random r = new Random();
        if (interLine > 0) {

            int x = 0, y = 0, x1 = width, y1 = 0;
            for (int i = 0; i < interLine; i++) {
                g.setColor(lineColor == null  getRandomColor() : lineColor);
                y = r.nextInt(height);
                y1 = r.nextInt(height);

                g.drawLine(x, y, x1, y1);
            }
        }

        // 

        // g.setColor(getRandomColor());
        // g.setColor(isSimpleColorColor.BLACK:Color.WHITE);

        // 80%
        int fsize = (int) (height * 0.8);
        int fx = height - fsize;
        int fy = fsize;

        g.setFont(new Font("Default", Font.PLAIN, fsize));

        // 
        for (int i = 0; i < textCode.length(); i++) {
            fy = randomLocation  (int) ((Math.random() * 0.3 + 0.6) * height)
                    : fy;// 
            g.setColor(foreColor == null  getRandomColor() : foreColor);
            g.drawString(textCode.charAt(i) + "", fx, fy);
            fx += fsize * 0.9;
        }

        g.dispose();

        return bim;
    }

    private static Color getRandomColor() {
        Random r = new Random();
        Color c = new Color(r.nextInt(255), r.nextInt(255), r.nextInt(255));
        return c;
    }
}

Related Tutorials