Programa em Java que gera QRCode e BarCode, não apresenta erro no código, mas não funciona
Bom dia amigos, sou novo na programação e estou aprendendo a gerar QRCode e BarCode, eu assistindo um vídeo gringo fiz um programa idêntico, mas o meu não esta funcionando, não apresenta erro no código, mas não gera os códigos. Estou usando as bibliotecas externas core 3.4.1 e a javase 3.4.1.
package demo;
//import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JTextField;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.border.LineBorder;
import helpers.ZXingHelper;
import java.awt.Color;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class JFrameMain extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;
private JPanel contentPane;
private JTextField textFieldProductId;
//private JButton ButtonQRCode;
//private JButton ButtonBarCode;
private JLabel labelImage;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
JFrameMain frame = new JFrameMain();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public JFrameMain() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
textFieldProductId = new JTextField();
textFieldProductId.setBounds(35, 23, 362, 20);
contentPane.add(textFieldProductId);
textFieldProductId.setColumns(10);
JButton ButtonQRCode = new JButton("QR Code");
ButtonQRCode.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
do_ButtonQRCode_actionPerformed(e);
}
});
ButtonQRCode.setBounds(76, 65, 89, 23);
contentPane.add(ButtonQRCode);
JButton ButtonBarCode = new JButton("Bar Code");
ButtonBarCode.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
do_ButtonBarCode_actionPerformed(e);
}
});
ButtonBarCode.setBounds(241, 65, 89, 23);
contentPane.add(ButtonBarCode);
JLabel labelImage = new JLabel("");
labelImage.setBorder(new LineBorder(new Color(0, 0, 0)));
labelImage.setBounds(47, 101, 346, 118);
contentPane.add(labelImage);
}
protected void do_ButtonQRCode_actionPerformed(ActionEvent e) {
String productId = textFieldProductId.getText();
if(productId.isEmpty()) {
JOptionPane.showMessageDialog(null,"Por favor preencha o campo acima.");
}else {
byte [] result = ZXingHelper.getQRCodeImage(productId, 200, 200);
labelImage.setIcon(new ImageIcon(result));
}
}
protected void do_ButtonBarCode_actionPerformed(ActionEvent e) {
String productId = textFieldProductId.getText();
if(productId.isEmpty()) {
JOptionPane.showMessageDialog(null,"Por favor preencha o campo acima.");
}else {
byte [] result = ZXingHelper.getBarCodeImage(productId, 200, 200);
labelImage.setIcon(new ImageIcon(result));
}
}
}
package helpers;
import java.io.ByteArrayOutputStream;
import java.util.Hashtable;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.oned.Code128Writer;
import com.google.zxing.qrcode.QRCodeWriter;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
public class ZXingHelper {
public static byte[] getQRCodeImage (String text, int width, int height) {
try {
QRCodeWriter qrCodeWriter = new QRCodeWriter();
BitMatrix bitMatrix = qrCodeWriter.encode(text, BarcodeFormat.QR_CODE, width, height);
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
MatrixToImageWriter.writeToStream(bitMatrix, "png", byteArrayOutputStream);
return byteArrayOutputStream.toByteArray();
}catch (Exception e) {
return null;
}
}
public static byte [] getBarCodeImage(String text, int width, int height) {
try {
Hashtable<EncodeHintType, ErrorCorrectionLevel> hintMap = new Hashtable<>();
hintMap.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);
Code128Writer writer = new Code128Writer();
BitMatrix bitMatrix = writer.encode(text, BarcodeFormat.CODE_128, width, height);
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
MatrixToImageWriter.writeToStream(bitMatrix, "png", byteArrayOutputStream);
return byteArrayOutputStream.toByteArray();
} catch (Exception e) {
System.err.println(e.getMessage());
}
return null;
}
}Discussão (0)
Carregando comentários...