- 資訊首頁(yè) > 開(kāi)發(fā)技術(shù) > 編程語(yǔ)言 >
- Java實(shí)戰之用Spring開(kāi)發(fā)條形碼和驗證碼
代碼如下:
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;//內部的線(xiàn)條數量 private static final int SPACE=10;//線(xiàn)條與線(xiàn)條之間的間距 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è)圖片畫(huà)板 Graphics g = bufferedImage.getGraphics();//得到畫(huà)筆 g.setColor(Color.white);//設置畫(huà)筆顏色 g.fillRect(0, 0, WIDTH, 80);//規定畫(huà)筆的一個(gè)范圍 g.setColor(Color.black);//這個(gè)是設置線(xiàn)條的顏色 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("楷書(shū)",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; } }
效果如下:
驗證碼這里是因為沒(méi)有設置好字符編碼的原因,讓中文字符無(wú)法在窗口內不顯示
驗證碼就比條形碼難以點(diǎn)點(diǎn),但是基本的編寫(xiě)思想都是差不多的,
但最難的還是在二維碼上,編寫(xiě)二維碼就需要要求編寫(xiě)者的算法能力足夠的扎實(shí),而且還要有足夠豐富的Java功底
到此這篇關(guān)于Java實(shí)戰之用Spring開(kāi)發(fā)條形碼和驗證碼的文章就介紹到這了,更多相關(guān)Java Spring開(kāi)發(fā)條形碼和驗證碼內容請搜索腳本之家以前的文章或繼續瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng )、來(lái)自互聯(lián)網(wǎng)轉載和分享為主,文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權請聯(lián)系站長(cháng)郵箱:ts@56dr.com進(jìn)行舉報,并提供相關(guān)證據,一經(jīng)查實(shí),將立刻刪除涉嫌侵權內容。
Copyright ? 2009-2021 56dr.com. All Rights Reserved. 特網(wǎng)科技 版權所有 珠海市特網(wǎng)科技有限公司 粵ICP備16109289號
域名注冊服務(wù)機構:阿里云計算有限公司(萬(wàn)網(wǎng)) 域名服務(wù)機構:煙臺帝思普網(wǎng)絡(luò )科技有限公司(DNSPod) CDN服務(wù):阿里云計算有限公司 中國互聯(lián)網(wǎng)舉報中心 增值電信業(yè)務(wù)經(jīng)營(yíng)許可證B2