Java實(shí)戰(zhàn)之用Spring開發(fā)條形碼和驗(yàn)證碼
一、條形碼
代碼如下:
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;//內(nèi)部的線條數(shù)量
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);//創(chuàng)建一個(gè)圖片畫板
Graphics g = bufferedImage.getGraphics();//得到畫筆
g.setColor(Color.white);//設(shè)置畫筆顏色
g.fillRect(0, 0, WIDTH, 80);//規(guī)定畫筆的一個(gè)范圍
g.setColor(Color.black);//這個(gè)是設(shè)置線條的顏色
for(int i=0;i<LINES;i++){
int row=random.nextInt(WIDTH)+SPACE;
g.drawLine(row,0,row,HEIGHT);
}
return bufferedImage;
}
}
效果如下:
二、驗(yàn)證碼
代碼如下:
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("檢測(cè)");
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;
}
}
效果如下:

驗(yàn)證碼這里是因?yàn)闆]有設(shè)置好字符編碼的原因,讓中文字符無法在窗口內(nèi)不顯示
驗(yàn)證碼就比條形碼難以點(diǎn)點(diǎn),但是基本的編寫思想都是差不多的,
但最難的還是在二維碼上,編寫二維碼就需要要求編寫者的算法能力足夠的扎實(shí),而且還要有足夠豐富的Java功底
到此這篇關(guān)于Java實(shí)戰(zhàn)之用Spring開發(fā)條形碼和驗(yàn)證碼的文章就介紹到這了,更多相關(guān)Java Spring開發(fā)條形碼和驗(yàn)證碼內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java中字符數(shù)組和字符串與StringBuilder和字符串轉(zhuǎn)換的講解
今天小編就為大家分享一篇關(guān)于Java中字符數(shù)組和字符串與StringBuilder和字符串轉(zhuǎn)換的講解,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2019-03-03
java用兩個(gè)例子充分闡述多態(tài)的可拓展性介紹
下面小編就為大家?guī)硪黄猨ava用兩個(gè)例子充分闡述多態(tài)的可拓展性介紹。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-06-06
Layui前后臺(tái)交互數(shù)據(jù)獲取java實(shí)例
下面小編就為大家分享一篇Layui前后臺(tái)交互數(shù)據(jù)獲取java實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-01-01
spring security數(shù)據(jù)庫表結(jié)構(gòu)實(shí)例代碼
這篇文章主要介紹了spring security數(shù)據(jù)庫表結(jié)構(gòu)實(shí)例代碼,需要的朋友可以參考下2017-09-09
mybatis insert foreach循環(huán)插入方式
這篇文章主要介紹了mybatis insert foreach循環(huán)插入方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-07-07
基于springboot實(shí)現(xiàn)redis分布式鎖的方法
這篇文章主要介紹了基于springboot實(shí)現(xiàn)redis分布式鎖的方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11
使用System.exit()來優(yōu)雅地終止SpringBoot項(xiàng)目的代碼示例
System.exit() 方法是 Java 中用于退出程序的方法,它接受一個(gè)整數(shù)參數(shù),通常被用來指示程序的退出狀態(tài),本文給大家介紹了如何使用System.exit()來優(yōu)雅地終止SpringBoot項(xiàng)目,需要的朋友可以參考下2024-08-08


