Difference between revisions of "User:Mdomarat/Extralabs/qrcode"
From CompSciWiki
Line 12: | Line 12: | ||
public class tryzxing extends JFrame{ | public class tryzxing extends JFrame{ | ||
− | + | private MyPanel qrPan; | |
− | + | private JPanel basePan; | |
− | + | private JTextField text; | |
− | + | private JLabel label; | |
− | + | private JButton goButton; | |
− | + | private JPanel infoPane; | |
− | + | ||
− | + | ||
− | + | ||
− | + | private class ButtonListener implements ActionListener { | |
− | + | public void actionPerformed (ActionEvent e) { | |
− | + | if (e.getSource() == goButton) { | |
− | + | System.out.println(text.getText()); | |
− | + | qrPan.changeQR(text.getText()); | |
− | + | } | |
− | + | ||
} | } | ||
+ | } | ||
− | + | public tryzxing() { | |
− | + | this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | |
− | + | this.setTitle("QRCode"); | |
− | + | basePan = new JPanel(); | |
− | + | basePan.setLayout ( new BorderLayout()); | |
− | + | text = new JTextField(40); | |
− | + | label = new JLabel("Enter QR Code Text:"); | |
− | + | goButton = new JButton("Update QR Code"); | |
− | + | goButton.addActionListener(new ButtonListener()); | |
− | + | infoPane = new JPanel(); | |
− | + | infoPane.add(label); | |
− | + | infoPane.add(text); | |
− | + | infoPane.add(goButton); | |
− | + | qrPan = new MyPanel(); | |
− | + | basePan.add(qrPan,BorderLayout.CENTER); | |
− | + | basePan.add(infoPane,BorderLayout.PAGE_START); | |
− | + | this.add(basePan); | |
− | + | this.pack(); | |
− | + | this.setVisible(true); | |
− | + | } | |
− | + | public static void main (String[] args) { | |
− | + | new tryzxing(); | |
− | + | } | |
− | } | + | } // end tryzxing class |
class MyPanel extends JPanel { | class MyPanel extends JPanel { | ||
− | + | BufferedImage img; | |
− | + | final String content = "this is the text in our QR code. the longer it is, the bigger the matrix."; | |
− | + | public BitMatrix convert(ByteMatrix bMat) { | |
− | + | BitMatrix output = new BitMatrix(bMat.getWidth(),bMat.getHeight()); | |
− | + | for (int i = 0; i < bMat.getHeight(); i++) { | |
− | + | for (int j = 0; j < bMat.getWidth(); j++) { | |
− | + | if (bMat.get(i,j)==1) | |
− | + | output.set(i, j); | |
− | + | } | |
− | + | } | |
− | + | return output; | |
− | + | } | |
− | + | public BitMatrix scale(BitMatrix b, int scale) { | |
− | + | BitMatrix output = new BitMatrix(b.getWidth()*scale, b.getHeight()*scale); | |
− | + | for (int i = 0; i < b.getHeight(); i++) { | |
− | + | for (int j = 0; j < b.getWidth(); j++) { | |
− | + | if (b.get(i,j)) | |
− | + | output.setRegion(i*scale, j*scale, scale, scale); | |
− | + | } | |
− | + | } | |
− | + | return output; | |
− | + | } | |
− | + | ||
− | + | public BufferedImage getImage (String args) { | |
− | + | BufferedImage image; | |
QRCode qr = new QRCode(); | QRCode qr = new QRCode(); | ||
Line 125: | Line 121: | ||
// the following code can scale the QR code by the int scale. but it comes out fuzzy. | // the following code can scale the QR code by the int scale. but it comes out fuzzy. | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
return image; | return image; | ||
} catch (WriterException e) { | } catch (WriterException e) { |
Revision as of 10:19, 7 December 2010
import com.google.zxing.WriterException; import com.google.zxing.qrcode.encoder.*; import com.google.zxing.qrcode.decoder.*; import com.google.zxing.common.BitMatrix; import java.awt.image.*; import com.google.zxing.client.j2se.MatrixToImageWriter; import javax.swing.*; import java.awt.*; import java.awt.event.*; public class tryzxing extends JFrame{ private MyPanel qrPan; private JPanel basePan; private JTextField text; private JLabel label; private JButton goButton; private JPanel infoPane; private class ButtonListener implements ActionListener { public void actionPerformed (ActionEvent e) { if (e.getSource() == goButton) { System.out.println(text.getText()); qrPan.changeQR(text.getText()); } } } public tryzxing() { this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setTitle("QRCode"); basePan = new JPanel(); basePan.setLayout ( new BorderLayout()); text = new JTextField(40); label = new JLabel("Enter QR Code Text:"); goButton = new JButton("Update QR Code"); goButton.addActionListener(new ButtonListener()); infoPane = new JPanel(); infoPane.add(label); infoPane.add(text); infoPane.add(goButton); qrPan = new MyPanel(); basePan.add(qrPan,BorderLayout.CENTER); basePan.add(infoPane,BorderLayout.PAGE_START); this.add(basePan); this.pack(); this.setVisible(true); } public static void main (String[] args) { new tryzxing(); } } // end tryzxing class class MyPanel extends JPanel { BufferedImage img; final String content = "this is the text in our QR code. the longer it is, the bigger the matrix."; public BitMatrix convert(ByteMatrix bMat) { BitMatrix output = new BitMatrix(bMat.getWidth(),bMat.getHeight()); for (int i = 0; i < bMat.getHeight(); i++) { for (int j = 0; j < bMat.getWidth(); j++) { if (bMat.get(i,j)==1) output.set(i, j); } } return output; } public BitMatrix scale(BitMatrix b, int scale) { BitMatrix output = new BitMatrix(b.getWidth()*scale, b.getHeight()*scale); for (int i = 0; i < b.getHeight(); i++) { for (int j = 0; j < b.getWidth(); j++) { if (b.get(i,j)) output.setRegion(i*scale, j*scale, scale, scale); } } return output; } public BufferedImage getImage (String args) { BufferedImage image; QRCode qr = new QRCode(); try { Encoder.encode(args, ErrorCorrectionLevel.H, qr); System.out.println( qr.getMatrixWidth()); ByteMatrix bmat = qr.getMatrix(); // for debugging purposes only. byte[][] x = bmat.getArray(); for (int i = 0; i < x.length; i++) { for (int j = 0; j < x[i].length; j++) { System.out.print(x[i][j] + " " ); } System.out.println(); } // for some reason, we need to convert byteMatrix to bitMatrix. // i couldn't figure out how to do this with the API. BitMatrix bitM = convert(bmat); image = MatrixToImageWriter.toBufferedImage(scale(bitM,4)); // the following code can scale the QR code by the int scale. but it comes out fuzzy. return image; } catch (WriterException e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; } public MyPanel() { img = getImage(content); } public Dimension getPreferredSize() { return new Dimension(img.getWidth(),img.getHeight()); } public void paintComponent(Graphics g) { super.paintComponent(g); g.drawImage(img, 10, 20, null); } public void changeQR (String text) { img = getImage(text); this.repaint(); } }