Click here to Skip to main content
15,881,089 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
Plz help me out from this problem. I have already developed the coding but i faced only
one problem i am not getting All the six fields should come one by one. One field in one
Line. Kindly help me...Plz..Plz...

Develop a new core java application to generate a QR(Quick Response) code based on a file which will contains characters. After generating code it should print the barcode image. The image size can also be parameterize and All the six fields should come one by one. One field in one line.

and the The basic requirement is:

Input data:

Field1 - Building Number : 8 digits [Numeric]
Field2 - Street Name: 30 Characters [Char]
Field3 - District name: 30 Characters [Char]
Field4 - City name: 30 Characters [Char]
Field5 - Zip Code: 6 digits [Numeric]
Field6 - Additional Number: 30 Characters

So, your input data file will look as per the following:

Field1, Field2, Field3, Field4, Field5, Field6
Field1, Field2, Field3, Field4, Field5, Field6
Field1, Field2, Field3, Field4, Field5, Field6
Field1, Field2, Field3, Field4, Field5, Field6
Field1, Field2, Field3, Field4, Field5, Field6
Field1, Field2, Field3, Field4, Field5, Field6
Field1, Field2, Field3, Field4, Field5, Field6

Output would be,

QR code

The property file should contain (all parameters required to read the file, size of the QR code etc):

From which path the java program should read the input data file
What is the size (dimension) of the QR code
Output file path, etc.

My Coding is that
Java
.................
 QRCodeGenerator.java
......................
import java.awt.*;
import java.awt.event.*;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Properties;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;

public class QRCodeGenerator extends JFrame implements ActionListener {
	private int framewidth;
	private int frameheight;
    private int scrollPaneWidth;
    private int scrollPanelHeight;
    private int panelDimWidth;
    private int panelDimHeight;
    
	QRCodeGenerator() throws IOException {
		InputStream is = new FileInputStream("generator.properties");
		Properties p = new Properties();
		p.load(is);
		framewidth= Integer.valueOf(p.getProperty("framewidth"));
		frameheight = Integer.valueOf(p.getProperty("frameheight"));
		scrollPaneWidth=Integer.valueOf(p.getProperty("scrollPaneWidth"));
		scrollPanelHeight=Integer.valueOf(p.getProperty("scrollPanelHeight"));
		panelDimWidth=Integer.valueOf(p.getProperty("panelDimWidth"));
        panelDimHeight=Integer.valueOf(p.getProperty("panelDimHeight"));		
		initProperties();
	}

	void initProperties() {
		panel_one.setPreferredSize(new Dimension(panelDimWidth,panelDimHeight));
		panel_one.add(selectFile);
		panel_one.add(textFieldSelect);
		panel_one.add(uploadButton);
		mainPanel.add(panel_one);
		mainPanel.add(panel_two);
		mainPanel.add(panel_three);
		mainPanel.add(panel_img);
		setTable();
		// table.setRowHeight(0, 200);
		// table.setRowHeight(1, 200);

		uploadButton.addActionListener(this);
	}

	public static void main(String s[]) throws IOException {
		QRCodeGenerator demoFrame = new QRCodeGenerator();
		
		
		
		JFrame frame = new JFrame("QR CODE IMAGE");

		frame.addWindowListener(new WindowAdapter() {

			public void windowClosing(WindowEvent e) {
				System.exit(0);
			}
		});
		frame.add(demoFrame.getMainPanel(), BorderLayout.CENTER);
		frame.setSize(demoFrame.framewidth, demoFrame.frameheight);
		frame.setVisible(true);
		frame.setResizable(false);
	}

	protected ImageIcon createImageIcon(String path) {
		java.net.URL imgURL = QRCodeGenerator.class.getResource(path);
		if (imgURL != null) {
			return new ImageIcon(imgURL);
		} else {
			System.err.println("Couldn't find file: " + path);
			return null;
		}
	}

	JPanel panel_one = new JPanel();
	JPanel panel_two = new JPanel();
	JPanel panel_three = new JPanel();
	JPanel panel_img = new JPanel();
	JTextField textFieldSelect = new JTextField(35);
	JButton uploadButton = new JButton("Browse");
	JPanel mainPanel = new JPanel();
	JFileChooser fc = new JFileChooser();
	static private final String newline = "\n";
	JLabel selectFile = new JLabel("Select File");
	JLabel lblOneImg = new JLabel("", SwingConstants.CENTER);
	DefaultTableModel model;
	JTable table;

	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == uploadButton) {
			int returnVal = fc.showOpenDialog(mainPanel);
			if (returnVal == JFileChooser.APPROVE_OPTION) {
				File file = fc.getSelectedFile();
				textFieldSelect.setText(file.getPath());
				try {
					QRCodeGeneratorHelper.getInstance().generateImg(file);
					ArrayList qrCodeList = QRCodeGeneratorHelper
							.getQRCodeList();
					
					for (int i = 0; i <= qrCodeList.size(); i++) {
						
						String fileName = QRCodeGeneratorHelper.filename + i
								+ ".png";
						String filePath = QRCodeGeneratorHelper.filepath
								+ fileName;
						ImageIcon aboutIcon = new ImageIcon(filePath);// createImageIcon(filePath);
                        int j=i+1;
						model.insertRow(i, new Object[] { j, qrCodeList.get(i),
								aboutIcon });
						table.setRowHeight(200);
						// model.fireTableDataChanged();
					}
				} catch (Exception ex) {

				}
				// processFile(file);
				// lblOneImg.setIcon(createImageIcon("images\\QRCode.png"));
			}
		}
	}

	public void setTable() {
		model = new DefaultTableModel();
		model.setColumnIdentifiers(new Object[] { "#", "QR Data", "QR Image" });

		String[] columnNames = { "QR Data", "QR Image" };
		table = new JTable(model) {
		
			public Class getColumnClass(int column) {
				//table.getColumnModel().getColumnIndex("#");
				return getValueAt(0, column).getClass();
			}
		};

		table.setPreferredScrollableViewportSize(table.getPreferredSize());
		// table.set

		JScrollPane scrollPane = new JScrollPane(table);
		
		TableColumn column=null;
		int i = 0;
		column = table.getColumnModel().getColumn(i);
		 if (i == 0) {
	            column.setMaxWidth(40);
	        } else {
	            column.setMinWidth(10);
	        }
		
		scrollPane.setPreferredSize(new Dimension(scrollPaneWidth, scrollPanelHeight));
		model.fireTableDataChanged();
		panel_img.add(scrollPane);

	}

	private JPanel getMainPanel() {
		return mainPanel;
	}
}

QRCodeGeneratorHelper.java
...........................

Java
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Map;
import java.util.Properties;

import javax.imageio.ImageIO;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.BinaryBitmap;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatReader;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.NotFoundException;
import com.google.zxing.Result;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.common.HybridBinarizer;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;

import java.awt.Image;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.HashMap;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.List;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;

public class QRCodeGeneratorHelper {
	static QRCodeGeneratorHelper s_instance = null;

	static String filename;
	static String filepath;
	static ArrayList qrCodeList = new ArrayList(10);

	public static QRCodeGeneratorHelper getInstance() {
		if (s_instance == null) {
			return new QRCodeGeneratorHelper();
		}
		return s_instance;
	}

	protected void generateImg(File file) throws Exception, WriterException,
			NotFoundException {
		InputStream is = new FileInputStream("one.properties");
		Properties p = new Properties();
		p.load(is);
		filename = p.getProperty("name");
		filepath = p.getProperty("path");

		int prowval = Integer.valueOf(p.getProperty("prowval"));
		int pcolval = Integer.valueOf(p.getProperty("pcolval"));

		int TotalStringCount = 0;
		int CharacterLength = Integer.valueOf(p.getProperty("CharacterLength"));
		int i = 0;
		String substring = "";
		int width = Integer.valueOf(p.getProperty("imgwidth"));
		int height = Integer.valueOf(p.getProperty("imgheight"));
		BufferedImage bufferedImage = new BufferedImage(width, height,
				BufferedImage.TYPE_INT_RGB);
		
		try {
			FileInputStream fstream = new FileInputStream(file);
			BufferedReader br = new BufferedReader(new InputStreamReader(
					fstream));
			String strLine;
			int j = 0;
			while ((strLine = br.readLine()) != null) {
				TotalStringCount = strLine.length();
				if ((TotalStringCount % 180) == 0) {

					substring = strLine.substring(i, CharacterLength);
					qrCodeList.add(substring);
					File imgfile = new File(filepath + filename + j + ".png");
					ImageIO.write(bufferedImage, "png", imgfile);
					System.out.println(substring);

					String qrCodeData = substring;
					String filePath = imgfile.toString();

					String charset = "UTF-8"; // or "ISO-8859-1"
					Map<encodehinttype,> hintMap = new HashMap<encodehinttype,>();
					hintMap.put(EncodeHintType.ERROR_CORRECTION,
							ErrorCorrectionLevel.L);

					createQRCode(qrCodeData, filePath, charset, hintMap,
							prowval, pcolval);
					System.out.println("QR Code image created successfully!");

					System.out.println("Data read from QR Code: "
							+ readQRCode(filePath, charset, hintMap));

				}

				else {
					System.out.println("Sorry!!! BarCode Cannot Be Generated");
				}
				j++;

			}

			br.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void createQRCode(String qrCodeData, String filePath,
			String charset, Map hintMap, int qrCodeheight, int qrCodewidth)
			throws WriterException, IOException {
		BitMatrix matrix = new MultiFormatWriter().encode(
				new String(qrCodeData.getBytes(charset), charset),
				BarcodeFormat.QR_CODE, qrCodewidth, qrCodeheight);
		MatrixToImageWriter.writeToFile(matrix, filePath.substring(filePath
				.lastIndexOf('.') + 1), new File(filePath));
	}

	public static String readQRCode(String filePath, String charset, Map hintMap)
			throws FileNotFoundException, IOException, NotFoundException {
		BinaryBitmap binaryBitmap = new BinaryBitmap(new HybridBinarizer(
				new BufferedImageLuminanceSource(
						ImageIO.read(new FileInputStream(filePath)))));
		Result qrCodeResult = new MultiFormatReader().decode(binaryBitmap);
		return qrCodeResult.getText();
	}

	public static ArrayList getQRCodeList() {
		return qrCodeList;
	}

}

Two properties file:
....................
1)generator.properties(For reference of QRCodeGenerator.java File )
...................................................................
frameheight=700
framewidth=600

scrollPaneWidth=600
scrollPanelHeight=600

panelDimWidth=600
panelDimHeight=30

2)one.properties(For reference of QRCodeGeneratorHelper.java File )
....................................................................
name=img
path=C://jayarup//jaya//Assignment3 Trial//jayarup QRImages//
prowval=200
pcolval=200

imgwidth=250
imgheight=250

CharacterLength =180</pre>
Posted
Updated 30-Jan-14 0:02am
v2
Comments
Richard MacCutchan 30-Jan-14 7:07am    
Looks like your college assignment. However, you have not given any indication of where the problem occurs, and most people here are not going to wade through all that code to try and find out.
Jayarup 30-Jan-14 7:28am    
My problem is that how i softcoded the file type(extension .png) those are passed in
QRCodeGeneratorHelper.java file. Kindly give me some suggestion.
Richard MacCutchan 30-Jan-14 7:33am    
Sorry, I don't understand what you are trying to say.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900