python怎么过滤字符串中的英文字母
293
2023-01-18
Java实战之用Spring开发条形码和验证码
一、条形码
代码如下:
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);//规定画笔的一个范围
http:// g.setColor(Color.black);//这个是设置线条的颜色
for(int i=0;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 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 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) { Shttp://ystem.out.println(textFile.getText().equals(obj[0])); } }); return jButton; } } 效果如下: 验证码这里是因为没有设置好字符编码的原因,让中文字符无法在窗口内不显示 验证码就比条形码难以点点,但是基本的编写思想都是差不多的, 但最难的还是在二维码上,编写二维码就需要要求编写者的算法能力足够的扎实,而且还要有足够丰富的Java功底
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 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 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) { Shttp://ystem.out.println(textFile.getText().equals(obj[0])); } }); return jButton; } } 效果如下: 验证码这里是因为没有设置好字符编码的原因,让中文字符无法在窗口内不显示 验证码就比条形码难以点点,但是基本的编写思想都是差不多的, 但最难的还是在二维码上,编写二维码就需要要求编写者的算法能力足够的扎实,而且还要有足够丰富的Java功底
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 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) { Shttp://ystem.out.println(textFile.getText().equals(obj[0])); } }); return jButton; } } 效果如下: 验证码这里是因为没有设置好字符编码的原因,让中文字符无法在窗口内不显示 验证码就比条形码难以点点,但是基本的编写思想都是差不多的, 但最难的还是在二维码上,编写二维码就需要要求编写者的算法能力足够的扎实,而且还要有足够丰富的Java功底
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) {
Shttp://ystem.out.println(textFile.getText().equals(obj[0]));
}
});
return jButton;
}
}
效果如下:
验证码这里是因为没有设置好字符编码的原因,让中文字符无法在窗口内不显示
验证码就比条形码难以点点,但是基本的编写思想都是差不多的,
但最难的还是在二维码上,编写二维码就需要要求编写者的算法能力足够的扎实,而且还要有足够丰富的Java功底
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~