使用zxing编写的二维码生成解析工具:QRCoder
TipDialog.java
package com.wolf_pan; import java.util.Timer;
import java.util.TimerTask;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel; public class TipDialog extends JDialog { public TipDialog(JFrame owner, String tip, int width, int height) {
super.setUndecorated(true);
super.setSize(width, height);
super.setLocationRelativeTo(owner);
super.add(new JLabel("<html><font color=\"red\">" + tip + "</font></html>", JLabel.CENTER));
} public void show(long duration) {
setVisible(true);
new Timer().schedule(new TimerTask() {
@Override
public void run() {
dispose();
}
}, duration);
}
}
PicFileChooser.java
package com.wolf_pan; import javax.swing.JFileChooser;
import javax.swing.filechooser.FileNameExtensionFilter; public class PicFileChooser extends JFileChooser { public PicFileChooser() { FileNameExtensionFilter filter1 = new FileNameExtensionFilter("ICO (*.ico)", "ico");
FileNameExtensionFilter filter2 = new FileNameExtensionFilter("PNG (*.png)", "png");
FileNameExtensionFilter filter3 = new FileNameExtensionFilter("TIFF (*.tif;*.tiff)", "tif", "tiff");
FileNameExtensionFilter filter4 = new FileNameExtensionFilter("GIF (*.gif)", "gif");
FileNameExtensionFilter filter5 = new FileNameExtensionFilter("JPEG (*.jpg;*.jpeg;*.jpe;*.jfif)", "jpg", "jpeg", "jpe", "jfif");
FileNameExtensionFilter filter6 = new FileNameExtensionFilter("Bitmap Files (*.bmp)", "bmp", "dib");
FileNameExtensionFilter filter7 = new FileNameExtensionFilter("All Picture Files", "ico", "png", "tif", "tiff", "gif", "jpg", "jpeg", "jpe", "jfif", "bmp", "dib");
super.setFileFilter(filter1);
super.setFileFilter(filter2);
super.setFileFilter(filter3);
super.setFileFilter(filter4);
super.setFileFilter(filter5);
super.setFileFilter(filter6);
super.setFileFilter(filter7);
super.setAcceptAllFileFilterUsed(false);
}
}
MainFrame.java
package com.wolf_pan; import com.google.zxing.BarcodeFormat;
import com.google.zxing.BinaryBitmap;
import com.google.zxing.ChecksumException;
import com.google.zxing.EncodeHintType;
import com.google.zxing.FormatException;
import com.google.zxing.NotFoundException;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.common.HybridBinarizer;
import com.google.zxing.qrcode.QRCodeReader;
import com.google.zxing.qrcode.QRCodeWriter;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Desktop;
import java.awt.FlowLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.util.HashMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;
import javax.swing.event.ChangeEvent;
import javax.swing.filechooser.FileNameExtensionFilter; public class MainFrame extends JFrame { private JTabbedPane tabbedPane;
private HintTextArea textToEncodeHintTextArea;
private JTextArea decodedResultTextArea;
private JLabel encodedImageLabel, qrcodeToDecodeLabel, tipLabel;
private JButton encodeButton, chooseButton, decodeButton, contactAuthorButton;
private JPopupMenu popupMenu;
private JMenuItem copyMenuItem, saveMenuItem;
private BufferedImage qrcodeBufferedImage;
private String qrcodePicFilename;
private static final Logger LOGGER = Logger.getLogger(MainFrame.class.getName()); private MainFrame() {
super("QRCoder");
initializeComponents();
initializeEventListeners();
} private void initializeComponents() {
setSize(400, 600);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
tabbedPane = new JTabbedPane();
JPanel encodePanel = new JPanel(new BorderLayout()), decodePanel = new JPanel(new BorderLayout()), authorPanel = new JPanel(new BorderLayout());
textToEncodeHintTextArea = new HintTextArea("Type text you want to encode here", 4);
encodePanel.add(new JScrollPane(textToEncodeHintTextArea), BorderLayout.NORTH);
encodedImageLabel = new JLabel();
encodedImageLabel.setHorizontalAlignment(JLabel.CENTER);
encodePanel.add(encodedImageLabel);
JPanel southEncodePanel = new JPanel(new FlowLayout(FlowLayout.RIGHT, 10, 0));
tipLabel = new JLabel("<html><font color=\"blue\" size=\"8\">Tip</font></html>");
southEncodePanel.add(tipLabel);
encodeButton = new JButton("Encode");
southEncodePanel.add(encodeButton);
encodePanel.add(southEncodePanel, BorderLayout.SOUTH);
tabbedPane.add("Encode", encodePanel);
JPanel northPanel = new JPanel(new BorderLayout());
JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
chooseButton = new JButton("Choose...");
buttonPanel.add(chooseButton);
decodeButton = new JButton("Decode");
buttonPanel.add(decodeButton);
northPanel.add(buttonPanel, BorderLayout.NORTH);
decodedResultTextArea = new JTextArea(4, 0);
decodedResultTextArea.setLineWrap(true);
decodedResultTextArea.setEditable(false);
JPanel decodedResultPanel = new JPanel(new BorderLayout());
JLabel decodedResultLabel = new JLabel("<html><font size=\"5\">Decoded result:</font></html>");
decodedResultPanel.add(decodedResultLabel, BorderLayout.NORTH);
decodedResultPanel.add(new JScrollPane(decodedResultTextArea));
northPanel.add(decodedResultPanel);
decodePanel.add(northPanel, BorderLayout.NORTH);
qrcodeToDecodeLabel = new JLabel(new ImageIcon(MainFrame.class.getResource("/images/author.jpg")));
decodePanel.add(qrcodeToDecodeLabel);
tabbedPane.add("Decode", decodePanel);
JLabel authorLabel = new JLabel("<html><br /><font size=\"20\">作者:布伊什<br />QQ: 980639902</font></html>", JLabel.CENTER);
authorPanel.add(authorLabel, BorderLayout.NORTH);
JLabel authorQRCodeLabel = new JLabel(new ImageIcon(MainFrame.class.getResource("/images/author.jpg")));
authorPanel.add(authorQRCodeLabel);
contactAuthorButton = new JButton("联系作者");
JPanel contactAuthorButtonPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
contactAuthorButtonPanel.add(contactAuthorButton);
authorPanel.add(contactAuthorButtonPanel, BorderLayout.SOUTH);
tabbedPane.add("Author", authorPanel);
add(tabbedPane);
popupMenu = new JPopupMenu();
copyMenuItem = new JMenuItem("Copy");
popupMenu.add(copyMenuItem);
saveMenuItem = new JMenuItem("Save");
popupMenu.add(saveMenuItem);
} private void initializeEventListeners() {
tabbedPane.addChangeListener((ChangeEvent ev) -> {
if (tabbedPane.getSelectedIndex() == 0) {
encodeButton.requestFocus();
}
});
encodeButton.addActionListener((ActionEvent ev) -> {
onEncodeButtonClicked();
});
chooseButton.addActionListener((ActionEvent ev) -> {
onChooseButtonClicked();
});
decodeButton.addActionListener((ActionEvent ev) -> {
onDecodeButtonClicked();
});
contactAuthorButton.addActionListener((ActionEvent ev) -> {
onContactAuthorButtonClicked();
});
encodedImageLabel.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent ev) {
onEncodedImageLabelClicked(ev);
}
});
tipLabel.addMouseListener(new MouseAdapter() {
private TipDialog tipDialog; @Override
public void mouseEntered(MouseEvent ev) {
tipDialog = new TipDialog(MainFrame.this, "右键单击生成的二维码弹出工具菜单", 220, 30);
tipDialog.setVisible(true);
} @Override
public void mouseExited(MouseEvent ev) {
tipDialog.dispose();
}
});
copyMenuItem.addActionListener((ActionEvent ev) -> {
onCopyMenuItemClicked();
});
saveMenuItem.addActionListener((ActionEvent ev) -> {
onSaveMenuItemClicked();
});
} private void onEncodeButtonClicked() {
try {
String contents = textToEncodeHintTextArea.getText();
int width = encodedImageLabel.getWidth() - 20;
int height = encodedImageLabel.getHeight() - 20;
HashMap<EncodeHintType, String> hints = new HashMap<>();
hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
BitMatrix bitMatrix = new QRCodeWriter().encode(contents, BarcodeFormat.QR_CODE, width, height, hints);
qrcodeBufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
for (int y = 0; y < height; ++y) {
for (int x = 0; x < width; ++x) {
Color color = bitMatrix.get(x, y) ? Color.BLACK : Color.WHITE;
qrcodeBufferedImage.setRGB(x, y, color.getRGB());
}
}
encodedImageLabel.setIcon(new ImageIcon(qrcodeBufferedImage));
} catch (WriterException ex) {
LOGGER.log(Level.SEVERE, null, ex);
JOptionPane.showMessageDialog(this, ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
}
} private void onChooseButtonClicked() {
PicFileChooser picFileChooser = new PicFileChooser();
if (picFileChooser.showOpenDialog(this) == PicFileChooser.APPROVE_OPTION) {
qrcodePicFilename = picFileChooser.getSelectedFile().getAbsolutePath();
qrcodeToDecodeLabel.setIcon(new ImageIcon(qrcodePicFilename));
}
} private void onDecodeButtonClicked() {
try {
BufferedImage qrcodeToDecodeBufferedImage;
if (qrcodePicFilename == null) {
qrcodeToDecodeBufferedImage = ImageIO.read(MainFrame.class.getResource("/images/author.jpg"));
} else {
qrcodeToDecodeBufferedImage = ImageIO.read(new File(qrcodePicFilename));
}
BufferedImageLuminanceSource source = new BufferedImageLuminanceSource(qrcodeToDecodeBufferedImage);
String decodedText = new QRCodeReader().decode(new BinaryBitmap(new HybridBinarizer(source))).getText();
decodedResultTextArea.setText(decodedText);
} catch (ChecksumException | FormatException | NotFoundException | IOException ex) {
LOGGER.log(Level.SEVERE, null, ex);
if (ex instanceof NotFoundException) {
JOptionPane.showMessageDialog(this, "QR code not found", "Error", JOptionPane.ERROR_MESSAGE);
} else {
JOptionPane.showMessageDialog(this, "Cannot decode the QR code", "Unknown Error", JOptionPane.ERROR_MESSAGE);
}
}
} private void onContactAuthorButtonClicked() {
try {
Desktop.getDesktop().browse(URI.create("tencent://message/?uin=980639902"));
} catch (IOException ex) {
LOGGER.log(Level.SEVERE, null, ex);
}
} private void onEncodedImageLabelClicked(MouseEvent ev) {
if (ev.getButton() == MouseEvent.BUTTON3) {
popupMenu.show(encodedImageLabel, ev.getX(), ev.getY());
}
} private void onCopyMenuItemClicked() {
if (qrcodeBufferedImage != null) {
ImageSelection contents = new ImageSelection(qrcodeBufferedImage);
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(contents, null);
TipDialog tipDialog = new TipDialog(this, "Copied to clipboard", 150, 30);
tipDialog.show(1500);
}
} private void onSaveMenuItemClicked() {
if (qrcodeBufferedImage != null) {
JFileChooser fileChooser = new JFileChooser();
fileChooser.setFileFilter(new FileNameExtensionFilter("PNG (*.png)", "png"));
fileChooser.setFileFilter(new FileNameExtensionFilter("Bitmap Files (*.bmp)", "bmp"));
fileChooser.setAcceptAllFileFilterUsed(false);
if (fileChooser.showSaveDialog(this) == PicFileChooser.APPROVE_OPTION) {
try {
String formatName;
if (fileChooser.getFileFilter().getDescription().equals("PNG (*.png)")) {
formatName = "png";
} else {
formatName = "bmp";
}
File destinationFile = new File(fileChooser.getSelectedFile() + "." + formatName);
if (ImageIO.write(qrcodeBufferedImage, formatName, destinationFile)) {
TipDialog tipDialog = new TipDialog(this, "Saved to disk", 150, 30);
tipDialog.show(1500);
}
} catch (IOException ex) {
LOGGER.log(Level.SEVERE, null, ex);
}
}
}
} public static void main(String[] args) {
MainFrame mainFrame = new MainFrame();
mainFrame.setVisible(true);
}
}
ImageSelection.java
package com.wolf_pan; import java.awt.Image;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException; public class ImageSelection implements Transferable { private final Image image; public ImageSelection(Image image) {
this.image = image;
} @Override
public DataFlavor[] getTransferDataFlavors() {
return new DataFlavor[]{DataFlavor.imageFlavor};
} @Override
public boolean isDataFlavorSupported(DataFlavor flavor) {
return flavor.equals(DataFlavor.imageFlavor);
} @Override
public Image getTransferData(DataFlavor flavor) throws UnsupportedFlavorException {
if (!isDataFlavorSupported(flavor)) {
throw new UnsupportedFlavorException(flavor);
}
return image;
}
}
HintTextArea.java
package com.wolf_pan; import java.awt.Color;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import javax.swing.JTextArea; public class HintTextArea extends JTextArea {
public HintTextArea(String hint, int rows) {
super(hint, rows, 0);
super.setForeground(Color.GRAY);
super.addFocusListener(new FocusListener() {
@Override
public void focusGained(FocusEvent e) {
if (getForeground() == Color.GRAY && hint.equals(getText())) {
setText(null);
setForeground(Color.BLACK);
}
} @Override
public void focusLost(FocusEvent e) {
if (getText().equals("")) {
setText(hint);
setForeground(Color.GRAY);
}
}
});
}
}
使用zxing编写的二维码生成解析工具:QRCoder的更多相关文章
- [开源]C#二维码生成解析工具,可添加自定义Logo
二维码又称 QR Code,QR 全称 Quick Response,是一个近几年来移动设备上超流行的一种编码方式,它比传统的 Bar Code 条形码能存更多的信息,也能表示更多的数据类型:比如:字 ...
- [开源]C#二维码生成解析工具,可添加自定义Logo (转)
二维码又称 QR Code,QR 全称 Quick Response,是一个近几年来移动设备上超流行的一种编码方式,它比传统的 Bar Code 条形码能存更多的信息,也能表示更多的数据类型:比如:字 ...
- C#二维码生成解析
C#二维码生成解析工具,可添加自定义Logo 二维码又称 QR Code,QR 全称 Quick Response,是一个近几年来移动设备上超流行的一种编码方式,它比传统的 Bar Code 条形码能 ...
- java二维码生成-谷歌(Google.zxing)开源二维码生成学习及实例
java二维码生成-谷歌(Google.zxing)开源二维码生成的实例及介绍 我们使用比特矩阵(位矩阵)的QR码编码在缓冲图片上画出二维码 实例有以下一个传入参数 OutputStream ou ...
- Java二维码生成与解码
基于google zxing 的Java二维码生成与解码 一.添加Maven依赖(解码时需要上传二维码图片,所以需要依赖文件上传包) <!-- google二维码工具 --> &l ...
- Java二维码生成与解码工具Zxing使用
Zxing是Google研发的一款非常好用的开放源代码的二维码生成工具,目前源码托管在github上,源码地址: https://github.com/zxing/zxing 可以看到Zxing库有很 ...
- atitit.二维码生成总结java zxing
atitit.二维码生成总结java zxing #-----zxing类库.. but zxing3.0 的类库core-3.0.jar 需要jdk7 只好zing2.2.jar ..jdk6走o ...
- java利用Google Zxing实现 二维码生成与解析
1.引入zxing 2.使用下面两个类:QRCodeUtil.java和BufferedImageLuminanceSource.java 3.新建单元测试类 复制下面测试代码即可. 1.pom文件中 ...
- 谷歌zxing 二维码生成工具
一.加入maven依赖 <!-- 谷歌zxing 二维码 --> <dependency> <groupId>com.google.zxing</groupI ...
随机推荐
- Unity3D for iOS初级教程:Part 2/3
转自Unity3D for iOS 这篇文章还可以在这里找到 英语 Learn how to use Unity to make a simple 3D iOS game! 这篇教材是来自教程团队成员 ...
- Spring-IOC源码解读2.2-BeanDefinition的载入和解析过程
1. 对IOC容器来说这个载入过程就像是相当于把BeanDefinition定义的信息转化成Spring内部表示的数据结构.容器对bean的管理和依赖注入过程都是通过对其持有的BeanDefiniti ...
- Nearest Common Ancestors(poj 1330)
题意:给定一棵树,询问两个节点的最近公共祖先. 输入:第一行T,表示测试组数. 每组测试数据包含一个n,表示节点数目,下面n-1行是连接的边,最后一行是询问 输出:共T行,代表每组的测试结果 /* 倍 ...
- 【BZOJ3224】普通平衡树(splay)
题意: 您需要写一种数据结构(可参考题目标题),来维护一些数,其中需要提供以下操作:1. 插入x数2. 删除x数(若有多个相同的数,因只删除一个)3. 查询x数的排名(若有多个相同的数,因输出最小的排 ...
- 改变input的value值,同时在HTML中将value进行改变
在使用lodop进行打印的时候,需求中有这样一个功能:某个字段可以在页面的input框中进行修改. 但是在进行打印时调用的是静态的HTML代码,这就导致在页面的input框中改变字段之后,但是HTML ...
- PSEUDO LEAST RECENTLY USED (PLRU) CACHE REPLACEMENT
A multi-way cache system includes multi-way cache storage circuitry, a pseudo least recently used (P ...
- word2013 交叉引用添加参考文献的尾注编号,通过查找 ^# 替换为 [^&] 的方式添加中括号,在进行“更新域”操作后,中括号消失。
word2013 交叉引用添加的尾注编号,通过查找 ^# 替换为 [^&] 的方式添加中括号,用这个方法添加中括号很多次了,这次却出现问题:在进行“更新域”操作后,中括号消失. 详 ...
- springboot整合mybatis+jetty笔记以及遇到的问题
图文创建一个springboot Demo(IDEA创建)+目录图+返回json springboot创建参考 pom.xml <dependencies> <!--<depe ...
- Word文档转Markdown插件(Windows)
在线的转换,Shell脚本的转换都试过了,都没有原生Word文档集成的插件转换这么好用,并且没有Bug. 下载:http://www.writage.com/
- Android Studio一些常用的快捷键
光标移动和窗口切换:1.esc:光标从功能窗口回到编辑窗口 2.alt+num:打开指定的功能窗口,重复操作关闭该窗口. 3.alt+←→:切换编辑的文件. 4.ctrl+home/end:跳转到文件 ...