public void initUI(){
gbl = new GridBagLayout();
gbc = new GridBagConstraints();
mainf = new JFrame("二维码生成器");
pannel = new JPanel(gbl);
input = new JTextArea();
output = new ImagePannel();
generat = new JButton();
generat.setText("生成二维码");
generat.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
if(!"".equals(input.getText().trim()) && !(input.getText() == null)){
bi = new BufferedImage(275, 275, BufferedImage.TYPE_INT_RGB);
for(int i = 0 ; i < 275 ; i++)
for(int j = 0 ; j < 275 ; j++)
bi.setRGB(j, i, Color.WHITE.getRGB());
gs = bi.createGraphics();
gs.setColor(Color.BLACK);
Qrcode qrcode = new Qrcode();
qrcode.setQrcodeEncodeMode('B');//N A ...
qrcode.setQrcodeErrorCorrect('M');//L M Q H
qrcode.setQrcodeVersion(7);
if((input.getText().trim().getBytes()).length > 0 && (input.getText().trim().getBytes()).length < 123){
boolean[][] rest = qrcode.calQrcode(input.getText().trim().getBytes());
for(int i = 0 ; i< rest.length ; i++){
for(int j = 0 ; j < rest.length ; j++){
if(rest[j][i])
gs.fillRect(j * 6, i * 6, 6, 6);
}
}
output.setBufferedImage(bi);
output.repaint();
File f = new File("c:\\qrcode.png");
try {
ImageIO.write(bi, "png", f);
JOptionPane.showMessageDialog(mainf, new String("恭喜您,2维码生成成功咯!"), "结果提示", JOptionPane.INFORMATION_MESSAGE);
} catch (IOException e) {
JOptionPane.showMessageDialog(mainf, new String("很遗憾,2维码生成失败了唉!"), "结果提示", JOptionPane.ERROR_MESSAGE);
e.printStackTrace();
}
}else{
JOptionPane.showMessageDialog(mainf, new String("编码内容长度不能超过123个字符哦,亲!"), "输入提示", JOptionPane.WARNING_MESSAGE);
}
}else
JOptionPane.showMessageDialog(mainf, new String("编码内容不能为空哦,亲!"), "输入提示", JOptionPane.WARNING_MESSAGE);
}
});