Difference between revisions of "User:Mdomarat/Extralabs/qrcode"

From CompSciWiki
Jump to: navigation, search
Line 12: Line 12:
  
 
public class tryzxing extends JFrame{
 
public class tryzxing extends JFrame{
+
  private MyPanel qrPan;
+
  private JPanel basePan;
   
+
  private JTextField text;
    private MyPanel qrPan;
+
  private JLabel label;
    private JPanel basePan;
+
  private JButton goButton;
  private JTextField text;
+
  private JPanel infoPane;
  private JLabel label;
+
  private JButton goButton;
+
  private JPanel infoPane;
+
 
      
 
      
    private class ButtonListener implements ActionListener {
+
  private class ButtonListener implements ActionListener {
        public void actionPerformed (ActionEvent e) {
+
    public void actionPerformed (ActionEvent e) {
  
                if (e.getSource() == goButton) {
+
      if (e.getSource() == goButton) {
                System.out.println(text.getText());
+
        System.out.println(text.getText());
              qrPan.changeQR(text.getText());
+
        qrPan.changeQR(text.getText());
                }
+
      }
      }
+
 
     }
 
     }
 +
  }
 
          
 
          
public tryzxing() {
+
  public tryzxing() {
 
 
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
+
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setTitle("QRCode");
+
    this.setTitle("QRCode");
 
 
 
 
basePan = new JPanel();
+
    basePan = new JPanel();
basePan.setLayout ( new BorderLayout());
+
    basePan.setLayout ( new BorderLayout());
  
 
 
 
 
text = new JTextField(40);
+
    text = new JTextField(40);
label = new JLabel("Enter QR Code Text:");
+
    label = new JLabel("Enter QR Code Text:");
goButton = new JButton("Update QR Code");
+
    goButton = new JButton("Update QR Code");
goButton.addActionListener(new ButtonListener());
+
    goButton.addActionListener(new ButtonListener());
 
 
 
 
infoPane = new JPanel();
+
    infoPane = new JPanel();
infoPane.add(label);
+
    infoPane.add(label);
infoPane.add(text);
+
    infoPane.add(text);
infoPane.add(goButton);
+
    infoPane.add(goButton);
 
 
 
 
        qrPan = new MyPanel();
+
    qrPan = new MyPanel();
 
        
 
        
        basePan.add(qrPan,BorderLayout.CENTER);
+
    basePan.add(qrPan,BorderLayout.CENTER);
        basePan.add(infoPane,BorderLayout.PAGE_START);
+
    basePan.add(infoPane,BorderLayout.PAGE_START);
        this.add(basePan);
+
    this.add(basePan);
        this.pack();
+
    this.pack();
this.setVisible(true);
+
    this.setVisible(true);
 
 
}
+
  }
 
 
public static void main (String[] args) {
+
  public static void main (String[] args) {
new tryzxing();
+
    new tryzxing();
}
+
  }
}
+
} // end tryzxing class
  
 
class MyPanel extends JPanel {
 
class MyPanel extends JPanel {
BufferedImage img;
+
  BufferedImage img;
final String content = "this is the text in our QR code. the longer it is, the bigger the matrix.";
+
  final String content = "this is the text in our QR code. the longer it is, the bigger the matrix.";
 
 
public BitMatrix convert(ByteMatrix bMat) {
+
  public BitMatrix convert(ByteMatrix bMat) {
BitMatrix output = new BitMatrix(bMat.getWidth(),bMat.getHeight());
+
    BitMatrix output = new BitMatrix(bMat.getWidth(),bMat.getHeight());
for (int i = 0; i < bMat.getHeight(); i++) {
+
    for (int i = 0; i < bMat.getHeight(); i++) {
for (int j = 0; j < bMat.getWidth(); j++) {
+
      for (int j = 0; j < bMat.getWidth(); j++) {
if (bMat.get(i,j)==1)  
+
        if (bMat.get(i,j)==1)  
output.set(i, j);
+
          output.set(i, j);
}
+
      }
}
+
    }
return output;
+
    return output;
}
+
  }
 
 
public BitMatrix scale(BitMatrix b, int scale) {
+
  public BitMatrix scale(BitMatrix b, int scale) {
BitMatrix output = new BitMatrix(b.getWidth()*scale, b.getHeight()*scale);
+
    BitMatrix output = new BitMatrix(b.getWidth()*scale, b.getHeight()*scale);
 
 
for (int i = 0; i < b.getHeight(); i++) {
+
    for (int i = 0; i < b.getHeight(); i++) {
for (int j = 0; j < b.getWidth(); j++) {
+
      for (int j = 0; j < b.getWidth(); j++) {
if (b.get(i,j))
+
        if (b.get(i,j))
output.setRegion(i*scale, j*scale, scale, scale);
+
          output.setRegion(i*scale, j*scale, scale, scale);
}
+
      }
}
+
    }
return output;
+
    return output;
+
  }
}
+
 
 
 
 
public BufferedImage getImage (String args) {
+
  public BufferedImage getImage (String args) {
BufferedImage image;
+
    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.
 
 
/*
 
int scale = 4;
 
BufferedImage bdest =
 
      new BufferedImage(scale*bitM.getWidth(), scale*bitM.getHeight(), BufferedImage.TYPE_INT_RGB);
 
 
 
  Graphics2D g = bdest.createGraphics();
 
  AffineTransform at = AffineTransform.getScaleInstance(scale,scale);
 
  g.drawRenderedImage(image,at);
 
  image = bdest;
 
  */
 
 
  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();
    }
    
    
}