今天为大家分享一个java语言生成二维码的程序,目前系统已经完成了初步功能,后续会进一步完善。整个系统界面漂亮,有完整源码,希望大家可以喜欢。喜欢的帮忙点赞和关注。一起编程、一起进步
开发语言为Java,开发环境Eclipse或者IDEA都可以,无需要数据库。运行主程序,或者执行打开JAR文件即可以运行本程序。
利用JDK自带的SWING框架开发,下载。纯窗体模式,直接运行Main文件即可以。同时带有详细得设计文档。
本系统主要是根据需要生成动态的二维码,主要功能点有以下几点
1 动态生成二维码的内容
2 动态生成二维码的LOGO
3 对生成的二维码图片进行保存,保存到本地机器上
1 生成二维码采用的是google的开源技术zxing,ZXing是一个开放源码的,用Java实现的多种格式的1D/2D条码图像处理库,它包含了联系到其他语言的端口。Zxing可以实现使用手机的内置的摄像头完成条形码的扫描及解码。
2 zxing官网生成二维码有详细的文档,可以参考
public static String decode(File file) throws Exception {
BufferedImage image;
image = ImageIO.read(file);
if (image == null) {
return null;
}
BufferedImageLuminanceSource source = new BufferedImageLuminanceSource(
image);
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
Result result;
Hashtable hints = new Hashtable();
hints.put(DecodeHintType.CHARACTER_SET, CHARSET);
result = new MultiFormatReader().decode(bitmap, hints);
String resultStr = result.getText();
return resultStr;
}
package com;
import java.awt.BasicStroke;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Shape;
import java.awt.geom.RoundRectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Hashtable;
import javax.imageio.ImageIO;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.filechooser.FileNameExtensionFilter;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.BinaryBitmap;
import com.google.zxing.DecodeHintType;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatReader;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.Result;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.common.HybridBinarizer;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
/**
* 二维码工具类
*/
public class QRCodeUtil {
private static JFileChooser filechooser = new JFileChooser();
private static BufferedImage image;
private static File out = new File("new.jpg");
private static final String CHARSET = "utf-8";
private static final String FORMAT_NAME = "JPG";
private static final int QRCODE_SIZE = 200;// 二维码尺寸
private static final int WIDTH = 60;// LOGO宽度
private static final int HEIGHT = 60;// LOGO高度
private static BufferedImage createImage(String content, String imgPath,
boolean needCompress) throws Exception {
Hashtable hints = new Hashtable();
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
hints.put(EncodeHintType.CHARACTER_SET, CHARSET);
hints.put(EncodeHintType.MARGIN, 1);
BitMatrix bitMatrix = new MultiFormatWriter().encode(content,
BarcodeFormat.QR_CODE, QRCODE_SIZE, QRCODE_SIZE, hints);
int width = bitMatrix.getWidth();
int height = bitMatrix.getHeight();
BufferedImage image = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
image.setRGB(x, y, bitMatrix.get(x, y) ? 0xFF000000
: 0xFFFFFFFF);
}
}
if (imgPath == null || "".equals(imgPath)) {
return image;
}
// 插入图片
QRCodeUtil.insertImage(image, imgPath, needCompress);
return image;
}
/**
* 插入LOGO
* @param source 二维码图片
* @param imgPath LOGO图片地址
* @param needCompress 是否压缩
* @throws Exception
*/
private static void insertImage(BufferedImage source, String imgPath,
boolean needCompress) throws Exception {
File file = new File(imgPath);
if (!file.exists()) {
return;
}
Image src = ImageIO.read(new File(imgPath));
int width = src.getWidth(null);
int height = src.getHeight(null);
if (needCompress) { // 压缩LOGO
if (width > WIDTH) {
width = WIDTH;
}
if (height > HEIGHT) {
height = HEIGHT;
}
Image image = src.getScaledInstance(width, height,
Image.SCALE_SMOOTH);
BufferedImage tag = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
Graphics g = tag.getGraphics();
g.drawImage(image, 0, 0, null); // 绘制缩小后的图
g.dispose();
src = image;
}
// 插入LOGO
Graphics2D graph = source.createGraphics();
int x = (QRCODE_SIZE - width) / 2;
int y = (QRCODE_SIZE - height) / 2;
graph.drawImage(src, x, y, width, height, null);
Shape shape = new RoundRectangle2D.Float(x, y, width, width, 6, 6);
graph.setStroke(new BasicStroke(3f));
graph.draw(shape);
graph.dispose();
}
/**
* 生成二维码(内嵌LOGO)
* @param content 内容
* @param imgPath LOGO地址
* @param needCompress 是否压缩LOGO
* @param panel 放入的面板
* @throws Exception
*/
public static void encode1(String content, String imgPath,
boolean needCompress,JPanel panel) throws Exception {
BufferedImage image = QRCodeUtil.createImage(content, imgPath,
needCompress);
ImageIO.write(image, FORMAT_NAME, new File("new.jpg"));
hq(panel);
}
/**图片当做面板背景投放*/
private static void hq(JPanel panel) {
// TODO Auto-generated method stub
try {
image = ImageIO.read(out);
BackgroundPanel b = new BackgroundPanel(image);
panel.removeAll();
panel.add(b);
panel.repaint();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
out.delete();
}
/**
* 生成二维码
* @param content 内容
* @throws Exception
*/
public static void encode(String content,JPanel panel) throws Exception {
QRCodeUtil.encode1(content, content, false, panel);
}
/**
* 解析二维码
* @param file 二维码图片
* @throws Exception
* @return
*/
public static String decode(File file) throws Exception {
BufferedImage image;
image = ImageIO.read(file);
if (image == null) {
return null;
}
BufferedImageLuminanceSource source = new BufferedImageLuminanceSource(
image);
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
Result result;
Hashtable hints = new Hashtable();
hints.put(DecodeHintType.CHARACTER_SET, CHARSET);
result = new MultiFormatReader().decode(bitmap, hints);
String resultStr = result.getText();
return resultStr;
}
/**
* 解析二维码
* @param path 二维码图片地址
* @throws Exception
* @return
*/
public static String decode(String path) throws Exception {
return QRCodeUtil.decode(new File(path));
}
/**保存二维码*/
public static void Save(){
if(image == null)
JOptionPane.showMessageDialog(null, "没有二维码!","提醒",JOptionPane.ERROR_MESSAGE);
else{
filechooser.setFileFilter(new FileNameExtensionFilter("jpg/png", new String[] { "jpg","png"}));
filechooser.setCurrentDirectory(new File("D:\\"));
filechooser.setDialogTitle("保存图片");
int i = filechooser.showSaveDialog(null);
if(i == 0){
File file = null;
String filename = filechooser.getSelectedFile().getName();
if(filename.substring(filename.length() - 1, filename.length()).equals(".")){
file = new File(filechooser.getSelectedFile()+".jpg");
}else{
if(filename.indexOf(".jpg") < 0 && filename.indexOf(".png") < 0 && filename.indexOf(".") < 0)
file = new File(filechooser.getSelectedFile()+".jpg");
else
file = new File(filechooser.getSelectedFile()+"");
}
try {
ImageIO.write(image, "jpg", file);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
}
}