java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > Java Spring开发条形码和验证码

Java实战之用Spring开发条形码和验证码

作者:风轻仰

这篇文章主要介绍了Java实战之用Spring开发条形码和验证码,文中有非常详细的代码示例,对正在学习java的小伙伴们有非常好的帮助,需要的朋友可以参考下

一、条形码

代码如下:

import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.Random;

public class Text extends JFrame {
    private static final int WIDTH=300;//窗口的宽度
    private static final int HEIGHT=400;//窗口的高度
    private static final int LINES=120;//内部的线条数量
    private static final int SPACE=10;//线条与线条之间的间距
    private static JFrame jFrame=null;

    public static void main(String[] args) {
        initialize();
    }

    private static void initialize(){//初始化窗口
        jFrame=new JFrame("条形码");
        jFrame.setSize(WIDTH,HEIGHT);
        jFrame.setLayout(null);
        JLabel jLabel=new JLabel();
        jLabel.setBounds(0,0,WIDTH,80);
        jLabel.setIcon(new ImageIcon(setCode()));
        jFrame.add(jLabel);
        jFrame.setVisible(true);
        jFrame.setLocationRelativeTo(null);
        jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

    private static BufferedImage setCode() {
        Random random = new Random();
        BufferedImage bufferedImage = new BufferedImage(WIDTH, 80, BufferedImage.TYPE_INT_RGB);//创建一个图片画板
        Graphics g = bufferedImage.getGraphics();//得到画笔
        g.setColor(Color.white);//设置画笔颜色
        g.fillRect(0, 0, WIDTH, 80);//规定画笔的一个范围
        g.setColor(Color.black);//这个是设置线条的颜色
        for(int i=0;i<LINES;i++){
            int row=random.nextInt(WIDTH)+SPACE;
            g.drawLine(row,0,row,HEIGHT);
        }
        return bufferedImage;
    }
}

效果如下:

这里是引用

二、验证码

代码如下:

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.util.Random;

public class Text extends JFrame{
    private final static char[] words=("1234567890" +
            "abcdefghijklmnopqrstuvwxyz" +
            "ABCDEFGHIJKLMNOPQRSTUVWXYZ").toCharArray();

    private final static int WORDLENGTH=4;
    private final static int WIDTH=200;
    private final static int HEIGHT=100;
    private final static int STAR=200;
    private static Text t=null;
    private static TextField textFile=null;
    private static Object[] obj=null;

    private static Object[] drawCode(){
        BufferedImage bufferedImage=new BufferedImage(WIDTH,HEIGHT,BufferedImage.TYPE_INT_RGB);
        Graphics g=bufferedImage.getGraphics();
        char[] selectWord=new char[4];
        g.setColor(Color.LIGHT_GRAY);
        g.fillRect(0,0,WIDTH,HEIGHT);
        Random random=new Random();
        for(int i=0;i<WORDLENGTH;i++){
            int n=random.nextInt(words.length);
            selectWord[i]=words[i];
            g.setFont(new Font("微软雅黑",0,random.nextInt(20)+40));
            g.setColor(setRandomColor());
            g.drawString(words[n]+"",i*WIDTH/WORDLENGTH,HEIGHT/2+10);
        }
        for(int i=0;i<STAR;i++){
            g.setColor(setRandomColor());
            g.setFont(new Font("楷书",0,40));
            g.drawOval(random.nextInt(WIDTH),random.nextInt(HEIGHT),3, 3);
        }
        return new Object[]{selectWord,bufferedImage};
    }

    private static Color setRandomColor(){
        Random colorRandom=new Random();
        return new Color(colorRandom.nextInt(256),colorRandom.nextInt(256),colorRandom.nextInt(256));
    }


    public static void main(String[] args) {
        t=new Text();
        t.setLocationRelativeTo(null);
        t.setSize(WIDTH,200);
        t.setLayout(null);
        t.add(setLabel());
        t.add(setButton());
        t.add(setTextField());
        t.setVisible(true);
        t.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
    
    private static JLabel setLabel(){
        JLabel jLabel=new JLabel();
        obj=drawCode();
        jLabel.setIcon(new ImageIcon((BufferedImage)obj[1]));
        jLabel.setBounds(0,0,WIDTH,HEIGHT);
        jLabel.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                jLabel.setIcon(new ImageIcon((BufferedImage)drawCode()[1]));
            }
        });
        return jLabel;
    }

    private static TextField setTextField(){
        textFile=new TextField();
        textFile.setFont(new Font("华文行楷",0,20));
        textFile.setBounds(5,120, 100,30);
        return textFile;
    }

    private static JButton setButton(){
        JButton jButton=new JButton("检测");
        jButton.setBounds(110,120, 70,30);
        jButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.out.println(textFile.getText().equals(obj[0]));
            }
        });
        return jButton;
    }
}

效果如下:

在这里插入图片描述

验证码这里是因为没有设置好字符编码的原因,让中文字符无法在窗口内不显示

验证码就比条形码难以点点,但是基本的编写思想都是差不多的,
但最难的还是在二维码上,编写二维码就需要要求编写者的算法能力足够的扎实,而且还要有足够丰富的Java功底

到此这篇关于Java实战之用Spring开发条形码和验证码的文章就介绍到这了,更多相关Java Spring开发条形码和验证码内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文