com.github.qrcode.QrcodeGenerator Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of qrcode-generator Show documentation
Show all versions of qrcode-generator Show documentation
The swing applicationn to generate QR codes
/*
* $Id$
*
* Copyright 2013 Valentyn Kolesnikov
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.github.qrcode;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.ClipboardOwner;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.event.WindowEvent;
import java.awt.image.BufferedImage;
import java.beans.XMLDecoder;
import java.beans.XMLEncoder;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Hashtable;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.JFileChooser;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
/**
* QR code generator.
*
* @author javadev
* @version $Revision$ $Date$
*/
public class QrcodeGenerator extends javax.swing.JFrame {
private static final String ADDRESS_TEMPLATE =
"BEGIN:VCARD\n"
+ "VERSION:3.0\n"
+ "N:{LN};{FN};\n"
+ "FN:{FN} {LN}\n"
+ "TITLE:{TITLE}/{COMPANYNAME}\n"
+ "TEL;TYPE=WORK;VOICE:{PHONE}\n"
+ "EMAIL;TYPE=WORK:{EMAIL}\n"
+ "ADR;TYPE=INTL,POSTAL,WORK:;;{STREET};{CITY};{STATE};{ZIP};{COUNTRY}\n"
+ "URL;TYPE=WORK:{WEBSITE}\n"
+ "END:VCARD";
private BufferedImage image;
private JFileChooser chooser1 = new JFileChooser();
private class QRCodePanel extends javax.swing.JPanel {
@Override
protected void paintComponent(Graphics grphcs) {
super.paintComponent(grphcs);
if (image != null) {
grphcs.drawImage(image, 0, 0, null);
}
}
}
private class FilterPNG extends javax.swing.filechooser.FileFilter {
public String getDescription() {
return "PNG files";
}
public boolean accept(File file) {
if (file.isDirectory()) {
return true;
}
String name = file.getName();
name = name.toLowerCase();
return name.endsWith(".png");
}
}
private class TransferableImage implements Transferable {
private final java.awt.Image image;
public TransferableImage(java.awt.Image image) {
this.image = image;
}
public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException, IOException {
if (flavor.equals( DataFlavor.imageFlavor) && image != null) {
return image;
} else {
throw new UnsupportedFlavorException(flavor);
}
}
public DataFlavor[] getTransferDataFlavors() {
DataFlavor[] flavors = new DataFlavor[1];
flavors[0] = DataFlavor.imageFlavor;
return flavors;
}
public boolean isDataFlavorSupported( DataFlavor flavor) {
DataFlavor[] flavors = getTransferDataFlavors();
for (int i = 0; i < flavors.length; i++) {
if (flavor.equals(flavors[i])) {
return true;
}
}
return false;
}
}
/** Creates new form */
public QrcodeGenerator() {
initComponents();
XMLDecoder d;
String x = null;
String y = null;
String height = null;
String width = null;
String index = null;
try {
d = new XMLDecoder(new BufferedInputStream(new FileInputStream("qrcode.xml")));
jTextField1.setText((String) d.readObject());
jTextField2.setText((String) d.readObject());
jTextField3.setText((String) d.readObject());
jTextField4.setText((String) d.readObject());
jTextField5.setText((String) d.readObject());
jTextField6.setText((String) d.readObject());
jTextField7.setText((String) d.readObject());
jTextField8.setText((String) d.readObject());
jTextField9.setText((String) d.readObject());
jTextField10.setText((String) d.readObject());
jTextField11.setText((String) d.readObject());
jTextField12.setText((String) d.readObject());
jTextField13.setText((String) d.readObject());
jTextField14.setText((String) d.readObject());
jTextField15.setText((String) d.readObject());
jTextArea1.setText((String) d.readObject());
jTextArea2.setText((String) d.readObject());
x = (String) d.readObject();
y = (String) d.readObject();
height = (String) d.readObject();
width = (String) d.readObject();
index = (String) d.readObject();
d.close();
} catch (Exception ex) {
ex.getMessage();
}
addWindowListener(new java.awt.event.WindowAdapter() {
@Override
public void windowClosing(WindowEvent winEvt) {
XMLEncoder e;
try {
e = new XMLEncoder(new BufferedOutputStream(new FileOutputStream("qrcode.xml")));
e.writeObject(jTextField1.getText());
e.writeObject(jTextField2.getText());
e.writeObject(jTextField3.getText());
e.writeObject(jTextField4.getText());
e.writeObject(jTextField5.getText());
e.writeObject(jTextField6.getText());
e.writeObject(jTextField7.getText());
e.writeObject(jTextField8.getText());
e.writeObject(jTextField9.getText());
e.writeObject(jTextField10.getText());
e.writeObject(jTextField11.getText());
e.writeObject(jTextField12.getText());
e.writeObject(jTextField13.getText());
e.writeObject(jTextField14.getText());
e.writeObject(jTextField15.getText());
e.writeObject(jTextArea1.getText());
e.writeObject(jTextArea2.getText());
e.writeObject("" + getLocation().x);
e.writeObject("" + getLocation().y);
e.writeObject("" + getSize().height);
e.writeObject("" + getSize().width);
e.writeObject("" + jTabbedPane6.getSelectedIndex());
e.close();
} catch (FileNotFoundException ex) {
Logger.getLogger(QrcodeGenerator.class.getName()).log(Level.SEVERE, null, ex);
}
System.exit(0);
}
});
chooser1.addChoosableFileFilter(new FilterPNG());
chooser1.setDialogTitle("Select PNG file");
chooser1.setCurrentDirectory(new File("."));
final java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
if (x == null) {
x = "" + ((screenSize.width - getWidth()) / 2);
}
if (y == null) {
y = "" + ((screenSize.height - getHeight()) / 2);
}
if (height == null) {
height = "" + getPreferredSize().height;
}
if (width == null) {
width = "" + getPreferredSize().width;
}
if (index == null) {
index = "" + jTabbedPane6.getSelectedIndex();
}
setLocation(Integer.valueOf(x), Integer.valueOf(y));
setSize(new Dimension(Integer.valueOf(width), Integer.valueOf(height)));
jTabbedPane6.setSelectedIndex(Integer.valueOf(index));
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
// //GEN-BEGIN:initComponents
private void initComponents() {
jPanel1 = new javax.swing.JPanel();
jPanel3 = new QRCodePanel();
jTabbedPane6 = new javax.swing.JTabbedPane();
jPanel4 = new javax.swing.JPanel();
jTextField2 = new javax.swing.JTextField();
jPanel5 = new javax.swing.JPanel();
jScrollPane1 = new javax.swing.JScrollPane();
jTextArea1 = new javax.swing.JTextArea();
jLabel1 = new javax.swing.JLabel();
jPanel6 = new javax.swing.JPanel();
jTextField1 = new javax.swing.JTextField();
jPanel7 = new javax.swing.JPanel();
jLabel2 = new javax.swing.JLabel();
jTextField3 = new javax.swing.JTextField();
jLabel3 = new javax.swing.JLabel();
jScrollPane2 = new javax.swing.JScrollPane();
jTextArea2 = new javax.swing.JTextArea();
jLabel4 = new javax.swing.JLabel();
jScrollPane3 = new javax.swing.JScrollPane();
jPanel8 = new javax.swing.JPanel();
jLabel5 = new javax.swing.JLabel();
jTextField4 = new javax.swing.JTextField();
jLabel6 = new javax.swing.JLabel();
jTextField5 = new javax.swing.JTextField();
jLabel7 = new javax.swing.JLabel();
jLabel8 = new javax.swing.JLabel();
jTextField6 = new javax.swing.JTextField();
jTextField7 = new javax.swing.JTextField();
jLabel9 = new javax.swing.JLabel();
jTextField8 = new javax.swing.JTextField();
jSeparator1 = new javax.swing.JSeparator();
jLabel10 = new javax.swing.JLabel();
jLabel11 = new javax.swing.JLabel();
jLabel12 = new javax.swing.JLabel();
jTextField9 = new javax.swing.JTextField();
jTextField10 = new javax.swing.JTextField();
jSeparator2 = new javax.swing.JSeparator();
jLabel13 = new javax.swing.JLabel();
jLabel14 = new javax.swing.JLabel();
jTextField11 = new javax.swing.JTextField();
jLabel15 = new javax.swing.JLabel();
jTextField12 = new javax.swing.JTextField();
jLabel16 = new javax.swing.JLabel();
jTextField13 = new javax.swing.JTextField();
jLabel17 = new javax.swing.JLabel();
jTextField14 = new javax.swing.JTextField();
jLabel18 = new javax.swing.JLabel();
jTextField15 = new javax.swing.JTextField();
jButton2 = new javax.swing.JButton();
jButton1 = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("QR code generator");
jPanel3.setBorder(javax.swing.BorderFactory.createEtchedBorder());
jPanel3.setPreferredSize(new java.awt.Dimension(212, 212));
org.jdesktop.layout.GroupLayout jPanel3Layout = new org.jdesktop.layout.GroupLayout(jPanel3);
jPanel3.setLayout(jPanel3Layout);
jPanel3Layout.setHorizontalGroup(
jPanel3Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(0, 208, Short.MAX_VALUE)
);
jPanel3Layout.setVerticalGroup(
jPanel3Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(0, 208, Short.MAX_VALUE)
);
jTabbedPane6.addChangeListener(new javax.swing.event.ChangeListener() {
public void stateChanged(javax.swing.event.ChangeEvent evt) {
jTabbedPane6StateChanged(evt);
}
});
jTextField2.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
jTextField2.setText("http://");
jTextField2.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyReleased(java.awt.event.KeyEvent evt) {
jTextField2KeyReleased(evt);
}
});
org.jdesktop.layout.GroupLayout jPanel4Layout = new org.jdesktop.layout.GroupLayout(jPanel4);
jPanel4.setLayout(jPanel4Layout);
jPanel4Layout.setHorizontalGroup(
jPanel4Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(jPanel4Layout.createSequentialGroup()
.addContainerGap()
.add(jTextField2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 524, Short.MAX_VALUE)
.addContainerGap())
);
jPanel4Layout.setVerticalGroup(
jPanel4Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(jPanel4Layout.createSequentialGroup()
.addContainerGap()
.add(jTextField2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.addContainerGap(490, Short.MAX_VALUE))
);
jTabbedPane6.addTab("URL", jPanel4);
jTextArea1.setColumns(20);
jTextArea1.setFont(new java.awt.Font("Monospaced", 0, 18)); // NOI18N
jTextArea1.setRows(5);
jTextArea1.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyReleased(java.awt.event.KeyEvent evt) {
jTextArea1KeyReleased(evt);
}
});
jScrollPane1.setViewportView(jTextArea1);
jLabel1.setText("160 characters");
org.jdesktop.layout.GroupLayout jPanel5Layout = new org.jdesktop.layout.GroupLayout(jPanel5);
jPanel5.setLayout(jPanel5Layout);
jPanel5Layout.setHorizontalGroup(
jPanel5Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(jPanel5Layout.createSequentialGroup()
.addContainerGap()
.add(jPanel5Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 524, Short.MAX_VALUE)
.add(org.jdesktop.layout.GroupLayout.TRAILING, jPanel5Layout.createSequentialGroup()
.add(0, 452, Short.MAX_VALUE)
.add(jLabel1)))
.addContainerGap())
);
jPanel5Layout.setVerticalGroup(
jPanel5Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(jPanel5Layout.createSequentialGroup()
.addContainerGap()
.add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 264, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(jLabel1)
.addContainerGap(234, Short.MAX_VALUE))
);
jTabbedPane6.addTab("Text", jPanel5);
jTextField1.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
jTextField1.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyReleased(java.awt.event.KeyEvent evt) {
jTextField1KeyReleased(evt);
}
});
org.jdesktop.layout.GroupLayout jPanel6Layout = new org.jdesktop.layout.GroupLayout(jPanel6);
jPanel6.setLayout(jPanel6Layout);
jPanel6Layout.setHorizontalGroup(
jPanel6Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(jPanel6Layout.createSequentialGroup()
.addContainerGap()
.add(jTextField1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 524, Short.MAX_VALUE)
.addContainerGap())
);
jPanel6Layout.setVerticalGroup(
jPanel6Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(jPanel6Layout.createSequentialGroup()
.addContainerGap()
.add(jTextField1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.addContainerGap(490, Short.MAX_VALUE))
);
jTabbedPane6.addTab("Phone Number", jPanel6);
jLabel2.setText("Number:");
jTextField3.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
jTextField3.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyTyped(java.awt.event.KeyEvent evt) {
jTextField3KeyTyped(evt);
}
});
jLabel3.setText("Message:");
jTextArea2.setColumns(20);
jTextArea2.setFont(new java.awt.Font("Monospaced", 0, 18)); // NOI18N
jTextArea2.setRows(4);
jTextArea2.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyTyped(java.awt.event.KeyEvent evt) {
jTextArea2KeyTyped(evt);
}
});
jScrollPane2.setViewportView(jTextArea2);
jLabel4.setText("140 characters");
org.jdesktop.layout.GroupLayout jPanel7Layout = new org.jdesktop.layout.GroupLayout(jPanel7);
jPanel7.setLayout(jPanel7Layout);
jPanel7Layout.setHorizontalGroup(
jPanel7Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(jPanel7Layout.createSequentialGroup()
.addContainerGap()
.add(jPanel7Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(org.jdesktop.layout.GroupLayout.TRAILING, jScrollPane2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 524, Short.MAX_VALUE)
.add(jTextField3, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 524, Short.MAX_VALUE)
.add(jPanel7Layout.createSequentialGroup()
.add(jPanel7Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(jLabel2)
.add(jLabel3))
.add(0, 478, Short.MAX_VALUE))
.add(org.jdesktop.layout.GroupLayout.TRAILING, jPanel7Layout.createSequentialGroup()
.add(0, 452, Short.MAX_VALUE)
.add(jLabel4)))
.addContainerGap())
);
jPanel7Layout.setVerticalGroup(
jPanel7Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(jPanel7Layout.createSequentialGroup()
.addContainerGap()
.add(jLabel2)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(jTextField3, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(jLabel3)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(jScrollPane2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 122, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(jLabel4)
.addContainerGap(302, Short.MAX_VALUE))
);
jTabbedPane6.addTab("SMS", jPanel7);
jScrollPane3.setBorder(null);
jScrollPane3.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
jLabel5.setText("First Name");
jTextField4.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
jTextField4.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyReleased(java.awt.event.KeyEvent evt) {
jTextField4KeyReleased(evt);
}
});
jLabel6.setText("Family Name");
jTextField5.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
jTextField5.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyReleased(java.awt.event.KeyEvent evt) {
jTextField4KeyReleased(evt);
}
});
jLabel7.setText("Phone Number");
jLabel8.setText("Email");
jTextField6.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
jTextField6.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyReleased(java.awt.event.KeyEvent evt) {
jTextField4KeyReleased(evt);
}
});
jTextField7.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
jTextField7.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyReleased(java.awt.event.KeyEvent evt) {
jTextField4KeyReleased(evt);
}
});
jLabel9.setText("Website");
jTextField8.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
jTextField8.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyReleased(java.awt.event.KeyEvent evt) {
jTextField4KeyReleased(evt);
}
});
jLabel10.setFont(new java.awt.Font("Tahoma", 0, 14));
jLabel10.setText("Add Organization");
jLabel11.setText("Company Name");
jLabel12.setText("Title within Company");
jTextField9.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
jTextField9.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyReleased(java.awt.event.KeyEvent evt) {
jTextField4KeyReleased(evt);
}
});
jTextField10.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
jTextField10.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyReleased(java.awt.event.KeyEvent evt) {
jTextField4KeyReleased(evt);
}
});
jLabel13.setFont(new java.awt.Font("Tahoma", 0, 14));
jLabel13.setText("Address");
jLabel14.setText("Street");
jTextField11.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
jTextField11.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyReleased(java.awt.event.KeyEvent evt) {
jTextField4KeyReleased(evt);
}
});
jLabel15.setText("City");
jTextField12.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
jTextField12.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyReleased(java.awt.event.KeyEvent evt) {
jTextField4KeyReleased(evt);
}
});
jLabel16.setText("State");
jTextField13.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
jTextField13.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyReleased(java.awt.event.KeyEvent evt) {
jTextField4KeyReleased(evt);
}
});
jLabel17.setText("Zip");
jTextField14.setFont(new java.awt.Font("Tahoma", 0, 18));
jTextField14.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyReleased(java.awt.event.KeyEvent evt) {
jTextField4KeyReleased(evt);
}
});
jLabel18.setText("Country");
jTextField15.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
jTextField15.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyReleased(java.awt.event.KeyEvent evt) {
jTextField4KeyReleased(evt);
}
});
org.jdesktop.layout.GroupLayout jPanel8Layout = new org.jdesktop.layout.GroupLayout(jPanel8);
jPanel8.setLayout(jPanel8Layout);
jPanel8Layout.setHorizontalGroup(
jPanel8Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(jPanel8Layout.createSequentialGroup()
.addContainerGap()
.add(jPanel8Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(jPanel8Layout.createSequentialGroup()
.add(jPanel8Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(jTextField9, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 224, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.add(jLabel11))
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(jPanel8Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(jPanel8Layout.createSequentialGroup()
.add(jLabel12)
.add(0, 195, Short.MAX_VALUE))
.add(jTextField10, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 294, Short.MAX_VALUE)))
.add(jPanel8Layout.createSequentialGroup()
.add(jPanel8Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(jTextField4, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 224, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.add(jLabel5)
.add(jLabel7))
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(jPanel8Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(jTextField5, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 294, Short.MAX_VALUE)
.add(jPanel8Layout.createSequentialGroup()
.add(jPanel8Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(jLabel8)
.add(jLabel6))
.add(0, 234, Short.MAX_VALUE))))
.add(org.jdesktop.layout.GroupLayout.TRAILING, jPanel8Layout.createSequentialGroup()
.add(jTextField6, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 224, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(jTextField7, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 294, Short.MAX_VALUE))
.add(jSeparator1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 524, Short.MAX_VALUE)
.add(jSeparator2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 524, Short.MAX_VALUE)
.add(jPanel8Layout.createSequentialGroup()
.add(jPanel8Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(jTextField13, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 224, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.add(jLabel16))
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(jPanel8Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(jPanel8Layout.createSequentialGroup()
.add(jLabel17)
.add(0, 280, Short.MAX_VALUE))
.add(jTextField14, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 294, Short.MAX_VALUE)))
.add(jPanel8Layout.createSequentialGroup()
.add(jPanel8Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(jPanel8Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(jLabel9)
.add(org.jdesktop.layout.GroupLayout.TRAILING, jTextField8, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 224, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
.add(jLabel10)
.add(jLabel13)
.add(jTextField11, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 224, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.add(jLabel14)
.add(jTextField12, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 224, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.add(jLabel15)
.add(jTextField15, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 224, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.add(jLabel18))
.add(0, 300, Short.MAX_VALUE)))
.addContainerGap())
);
jPanel8Layout.setVerticalGroup(
jPanel8Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(jPanel8Layout.createSequentialGroup()
.addContainerGap()
.add(jPanel8Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
.add(jLabel5)
.add(jLabel6))
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(jPanel8Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
.add(jTextField4, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.add(jTextField5, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(jPanel8Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
.add(jLabel7)
.add(jLabel8))
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(jPanel8Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
.add(jTextField6, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.add(jTextField7, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(jLabel9)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(jTextField8, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(jSeparator1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 10, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(jLabel10)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(jPanel8Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
.add(jLabel12)
.add(jLabel11))
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(jPanel8Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
.add(jTextField9, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.add(jTextField10, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(jSeparator2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 10, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(jPanel8Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
.add(jPanel8Layout.createSequentialGroup()
.add(jLabel13)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(jLabel14)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(jTextField11, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(jLabel15)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(jTextField12, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(jLabel16)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(jTextField13, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
.add(jPanel8Layout.createSequentialGroup()
.add(jLabel17)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(jTextField14, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(jLabel18)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(jTextField15, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.addContainerGap())
);
jScrollPane3.setViewportView(jPanel8);
jTabbedPane6.addTab("Contact", jScrollPane3);
jButton2.setText("Save to disk");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});
jButton1.setText("Copy");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
org.jdesktop.layout.GroupLayout jPanel1Layout = new org.jdesktop.layout.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(jPanel1Layout.createSequentialGroup()
.add(jButton1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 82, Short.MAX_VALUE)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(jButton2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 124, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
.add(jPanel3, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(jTabbedPane6, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 549, Short.MAX_VALUE)
.addContainerGap())
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(jTabbedPane6)
.add(jPanel1Layout.createSequentialGroup()
.add(jPanel3, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.add(11, 11, 11)
.add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
.add(jButton2)
.add(jButton1))
.add(0, 308, Short.MAX_VALUE)))
.addContainerGap())
);
org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(jPanel1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(jPanel1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
);
pack();
}// //GEN-END:initComponents
private void jTextArea1KeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTextArea1KeyReleased
String text = jTextArea1.getText();
int length = text.length();
jLabel1.setText("" + length + " character" + (length <= 1 ? "" : "s"));
generateQrCode(text);
}//GEN-LAST:event_jTextArea1KeyReleased
private void jTextField2KeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTextField2KeyReleased
generateQrCode(jTextField2.getText());
}//GEN-LAST:event_jTextField2KeyReleased
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
if (chooser1.showSaveDialog(this) != JFileChooser.APPROVE_OPTION) {
return;
}
String fileName = chooser1.getSelectedFile().getPath().replaceFirst("(.*?)(\\.\\w{3,4})*$", "$1.png");
try {
ImageIO.write(image, "png", new File(fileName));
} catch (IOException ex) {
Logger.getLogger(QrcodeGenerator.class.getName()).log(Level.SEVERE, null, ex);
}
}//GEN-LAST:event_jButton2ActionPerformed
private void jTabbedPane6StateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_jTabbedPane6StateChanged
javax.swing.JTabbedPane jt = (javax.swing.JTabbedPane) evt.getSource();
switch(jt.getSelectedIndex()) {
case 0:
generateQrCode(jTextField2.getText());
break;
case 1:
String text = jTextArea1.getText();
int length = text.length();
jLabel1.setText("" + length + " character" + (length <= 1 ? "" : "s"));
generateQrCode(text);
break;
case 2:
generateQrCode("tel:" + jTextField1.getText());
break;
case 3:
String text2 = jTextArea2.getText();
int length2 = text2.length();
jLabel4.setText("" + length2 + " character" + (length2 <= 1 ? "" : "s"));
generateQrCode("sms:" + jTextField3.getText() + ":" + text2);
break;
case 4:
generateQrCode(ADDRESS_TEMPLATE.replace("{FN}", jTextField4.getText())
.replace("{LN}", jTextField5.getText())
.replace("{PHONE}", jTextField6.getText())
.replace("{EMAIL}", jTextField7.getText())
.replace("{WEBSITE}", jTextField8.getText())
.replace("{COMPANYNAME}", jTextField9.getText())
.replace("{TITLE}", jTextField10.getText())
.replace("{STREET}", jTextField11.getText())
.replace("{CITY}", jTextField12.getText())
.replace("{STATE}", jTextField13.getText())
.replace("{ZIP}", jTextField14.getText())
.replace("{COUNTRY}", jTextField15.getText())
);
break;
}
}//GEN-LAST:event_jTabbedPane6StateChanged
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
TransferableImage trans = new TransferableImage(image);
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
clipboard.setContents(trans, new ClipboardOwner() {
public void lostOwnership(Clipboard clpbrd, Transferable t) {
// Ignore
}
} );
}//GEN-LAST:event_jButton1ActionPerformed
private void jTextField1KeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTextField1KeyReleased
generateQrCode("tel:" + jTextField1.getText());
}//GEN-LAST:event_jTextField1KeyReleased
private void jTextField3KeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTextField3KeyTyped
generateQrCode("sms:" + jTextField3.getText() + ":" + jTextArea2.getText());
}//GEN-LAST:event_jTextField3KeyTyped
private void jTextArea2KeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTextArea2KeyTyped
String text = jTextArea2.getText();
int length = text.length();
jLabel4.setText("" + length + " character" + (length <= 1 ? "" : "s"));
generateQrCode("sms:" + jTextField3.getText() + ":" + text);
}//GEN-LAST:event_jTextArea2KeyTyped
private void jTextField4KeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTextField4KeyReleased
generateQrCode(ADDRESS_TEMPLATE.replace("{FN}", jTextField4.getText())
.replace("{LN}", jTextField5.getText())
.replace("{PHONE}", jTextField6.getText())
.replace("{EMAIL}", jTextField7.getText())
.replace("{WEBSITE}", jTextField8.getText())
.replace("{COMPANYNAME}", jTextField9.getText())
.replace("{TITLE}", jTextField10.getText())
.replace("{STREET}", jTextField11.getText())
.replace("{CITY}", jTextField12.getText())
.replace("{STATE}", jTextField13.getText())
.replace("{ZIP}", jTextField14.getText())
.replace("{COUNTRY}", jTextField15.getText())
);
}//GEN-LAST:event_jTextField4KeyReleased
private static void setLookAndFeel()
throws ClassNotFoundException, InstantiationException, IllegalAccessException, UnsupportedLookAndFeelException
{
javax.swing.UIManager.LookAndFeelInfo infos[] = UIManager.getInstalledLookAndFeels();
String firstFoundClass = null;
for (javax.swing.UIManager.LookAndFeelInfo info : infos) {
String foundClass = info.getClassName();
if ("Nimbus".equals(info.getName())) {
firstFoundClass = foundClass;
break;
}
if (null == firstFoundClass) {
firstFoundClass = foundClass;
}
}
if(null == firstFoundClass) {
throw new IllegalArgumentException("No suitable Swing looks and feels");
} else {
UIManager.setLookAndFeel(firstFoundClass);
}
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) throws Exception {
setLookAndFeel();
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new QrcodeGenerator().setVisible(true);
}
});
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel10;
private javax.swing.JLabel jLabel11;
private javax.swing.JLabel jLabel12;
private javax.swing.JLabel jLabel13;
private javax.swing.JLabel jLabel14;
private javax.swing.JLabel jLabel15;
private javax.swing.JLabel jLabel16;
private javax.swing.JLabel jLabel17;
private javax.swing.JLabel jLabel18;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JLabel jLabel6;
private javax.swing.JLabel jLabel7;
private javax.swing.JLabel jLabel8;
private javax.swing.JLabel jLabel9;
private javax.swing.JPanel jPanel1;
private javax.swing.JPanel jPanel3;
private javax.swing.JPanel jPanel4;
private javax.swing.JPanel jPanel5;
private javax.swing.JPanel jPanel6;
private javax.swing.JPanel jPanel7;
private javax.swing.JPanel jPanel8;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JScrollPane jScrollPane2;
private javax.swing.JScrollPane jScrollPane3;
private javax.swing.JSeparator jSeparator1;
private javax.swing.JSeparator jSeparator2;
private javax.swing.JTabbedPane jTabbedPane6;
private javax.swing.JTextArea jTextArea1;
private javax.swing.JTextArea jTextArea2;
private javax.swing.JTextField jTextField1;
private javax.swing.JTextField jTextField10;
private javax.swing.JTextField jTextField11;
private javax.swing.JTextField jTextField12;
private javax.swing.JTextField jTextField13;
private javax.swing.JTextField jTextField14;
private javax.swing.JTextField jTextField15;
private javax.swing.JTextField jTextField2;
private javax.swing.JTextField jTextField3;
private javax.swing.JTextField jTextField4;
private javax.swing.JTextField jTextField5;
private javax.swing.JTextField jTextField6;
private javax.swing.JTextField jTextField7;
private javax.swing.JTextField jTextField8;
private javax.swing.JTextField jTextField9;
// End of variables declaration//GEN-END:variables
private void generateQrCode(String messsage) {
Logger.getLogger(QrcodeGenerator.class.getName()).log(Level.INFO, messsage);
if (messsage == null || messsage.isEmpty()) {
image = null;
return;
}
try {
Hashtable hintMap = new Hashtable();
hintMap.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);
hintMap.put(EncodeHintType.CHARACTER_SET, "UTF-8");
QRCodeWriter qrCodeWriter = new QRCodeWriter();
BitMatrix byteMatrix = qrCodeWriter.encode(messsage,
BarcodeFormat.QR_CODE, jPanel3.getPreferredSize().width, jPanel3.getPreferredSize().height, hintMap);
int CrunchifyWidth = byteMatrix.getWidth();
image = new BufferedImage(CrunchifyWidth, CrunchifyWidth,
BufferedImage.TYPE_INT_RGB);
image.createGraphics();
Graphics2D graphics=(Graphics2D)image.getGraphics();
graphics.setColor(Color.WHITE);
graphics.fillRect(0, 0, CrunchifyWidth, CrunchifyWidth);
graphics.setColor(Color.BLACK);
for (int i = 0; i < CrunchifyWidth; i++) {
for (int j = 0; j < CrunchifyWidth; j++) {
if (byteMatrix.get(i, j)) {
graphics.fillRect(i, j, 1, 1);
}
}
}
jPanel3.repaint();
} catch (WriterException ex) {
Logger.getLogger(QrcodeGenerator.class.getName()).log(Level.SEVERE, null, ex);
}
}
}