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)闆](méi)有設(shè)置好字符編碼的原因,讓中文字符無(wú)法在窗口內(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)文章
mybatis中字段名與關(guān)鍵字相同問(wèn)題
這篇文章主要介紹了mybatis中字段名與關(guān)鍵字相同問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-02-02Java實(shí)現(xiàn)批量導(dǎo)出導(dǎo)入數(shù)據(jù)及附件文件zip包
這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)批量導(dǎo)出導(dǎo)入數(shù)據(jù)及附件文件zip包的方法,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一2022-09-09Java設(shè)計(jì)模塊系列之書店管理系統(tǒng)單機(jī)版(二)
這篇文章主要為大家詳細(xì)介紹了Java單機(jī)版的書店管理系統(tǒng)設(shè)計(jì)模塊和思想第二章,感興趣的小伙伴們可以參考一下2016-08-08jenkins+maven+svn自動(dòng)部署和發(fā)布的詳細(xì)圖文教程
Jenkins是一個(gè)開源的、可擴(kuò)展的持續(xù)集成、交付、部署的基于web界面的平臺(tái)。這篇文章主要介紹了jenkins+maven+svn自動(dòng)部署和發(fā)布的詳細(xì)圖文教程,需要的朋友可以參考下2020-09-09