User:Mdomarat/Extralabs/qrcode

From CompSciWiki
Jump to: navigation, search
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.*;
import java.io.File;
import java.io.IOException;

import javax.imageio.*;


public class tryzxing extends JFrame{
	private MyPanel qrPan;
	private JPanel basePan;
	private JTextField text;
	private JLabel label;
	private JButton goButton;
	private JButton saveButton; 
	private JPanel infoPane;
	private JFileChooser jf;

	private class ButtonListener implements ActionListener {
		public void actionPerformed (ActionEvent e) {

			if (e.getSource() == goButton) {
				System.out.println(text.getText());
				qrPan.changeQR(text.getText());
			} else if (e.getSource() == saveButton) { 
				qrPan.saveFile();

			}
		}
	}

	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());
		saveButton = new JButton("Save Current Code");
		saveButton.addActionListener(new ButtonListener());

		infoPane = new JPanel();
		infoPane.add(label);
		infoPane.add(text);
		infoPane.add(goButton);
		infoPane.add(saveButton);

		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();
	}

	public void saveFile( ) {

		JFileChooser jf = new JFileChooser();

		int result = jf.showSaveDialog(null);

		if (result==JFileChooser.APPROVE_OPTION) {
			File saveFile = jf.getSelectedFile();
			try {
				ImageIO.write(img, "png", saveFile);

			} catch (IOException e) { 
				System.out.println(e.getMessage());
			}
		}
	}


}