惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

酷 壳 – CoolShell
酷 壳 – CoolShell
H
Hacker News: Front Page
P
Palo Alto Networks Blog
T
ThreatConnect
Apple Machine Learning Research
Apple Machine Learning Research
博客园_首页
T
True Tiger Recordings
P
Privacy & Cybersecurity Law Blog
B
Blog
IT之家
IT之家
Last Week in AI
Last Week in AI
F
Full Disclosure
Hacker News: Ask HN
Hacker News: Ask HN
C
Comments on: Blog
Microsoft Azure Blog
Microsoft Azure Blog
C
Cybersecurity and Infrastructure Security Agency CISA
Microsoft Security Blog
Microsoft Security Blog
博客园 - 【当耐特】
N
News and Events Feed by Topic
NISL@THU
NISL@THU
腾讯CDC
雷峰网
雷峰网
Security Latest
Security Latest
李成银的技术随笔
M
Microsoft Research Blog - Microsoft Research
L
LangChain Blog
L
Lohrmann on Cybersecurity
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
C
Check Point Blog
Y
Y Combinator Blog
Recent Announcements
Recent Announcements
博客园 - Franky
N
News | PayPal Newsroom
V
V2EX
A
About on SuperTechFans
The Register - Security
The Register - Security
月光博客
月光博客
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Google Online Security Blog
Google Online Security Blog
MyScale Blog
MyScale Blog
Cisco Talos Blog
Cisco Talos Blog
Vercel News
Vercel News
WordPress大学
WordPress大学
C
Cyber Attacks, Cyber Crime and Cyber Security
The Hacker News
The Hacker News
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
爱范儿
爱范儿
A
Arctic Wolf
L
LINUX DO - 最新话题
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More

博客园 - 努力&快乐

springboot 手动开启事务及手动提交事务 js MD5包含中文串时加密结果与JAVA结果不一致的解决方案 mongodb配置、启动、备份 JavaScript通过递归合并JSON linux系统关闭指定服务的方式 Dart 语言简易教程系列 查看java内存情况命令 安装redis时Newer version of jemalloc required错误解决 代码指标工具 spring mvc接收ajax提交的JSON数据,并反序列化为对象 二进制样式的字符串与byte数组互转函数示例 .net运行时dll的查找路径顺序 WebStorm11 注册 MSSql使用SQL语句快速查看表对的就说明,及表字段描述及字段类型 多个App间传递数据 cordova混合开发:Android中native调用javascript cordova plugin数据传递概要 javascript不用new关键字创建对象示例 .Net事件管道详解图
JAVA用QRCode生成二维码
努力&快乐 · 2017-09-21 · via 博客园 - 努力&快乐

QRCode jar下载地址:

生成:http://www.swetake.com/qrcode/index-e.html

读取:https://zh.osdn.net/projects/qrcode/

示例代码(引用自:http://www.ibloger.net/article/160.html):

package cn.utils;  
  
import java.awt.Color;  
import java.awt.Graphics2D;  
import java.awt.Image;  
import java.awt.image.BufferedImage;  
import java.io.File;  
import java.io.IOException;  
import java.io.UnsupportedEncodingException;  
  
import javax.imageio.ImageIO;  
  
import jp.sourceforge.qrcode.QRCodeDecoder;  
import jp.sourceforge.qrcode.data.QRCodeImage;  
import jp.sourceforge.qrcode.exception.DecodingFailedException;  
  
import com.swetake.util.Qrcode;  
  
public class QRCodeUtils {  
    /** 
     * 编码字符串内容到目标File对象中 
     *  
     * @param encodeddata 编码的内容 
     * @param destFile  生成file文件  1381090722   5029067275903 
     * @throws IOException 
     */  
    public static void qrCodeEncode(String encodeddata, File destFile) throws IOException {  
        Qrcode qrcode = new Qrcode();  
        qrcode.setQrcodeErrorCorrect('M');  // 纠错级别(L 7%、M 15%、Q 25%、H 30%)和版本有关  
        qrcode.setQrcodeEncodeMode('B');      
        qrcode.setQrcodeVersion(7);     // 设置Qrcode包的版本  
          
        byte[] d = encodeddata.getBytes("GBK"); // 字符集  
        BufferedImage bi = new BufferedImage(139, 139, BufferedImage.TYPE_INT_RGB);  
        // createGraphics   // 创建图层  
        Graphics2D g = bi.createGraphics();  
          
        g.setBackground(Color.WHITE);   // 设置背景颜色(白色)  
        g.clearRect(0, 0, 139, 139);    // 矩形 X、Y、width、height  
        g.setColor(Color.BLACK);    // 设置图像颜色(黑色)  
  
        if (d.length > 0 && d.length < 123) {  
            boolean[][] b = qrcode.calQrcode(d);  
            for (int i = 0; i < b.length; i++) {  
                for (int j = 0; j < b.length; j++) {  
                    if (b[j][i]) {  
                        g.fillRect(j * 3 + 2, i * 3 + 2, 3, 3);  
                    }  
                }  
            }  
        }  
          
//      Image img = ImageIO.read(new File("D:/tt.png"));  logo  
//      g.drawImage(img, 25, 55,60,50, null);  
                  
        g.dispose(); // 释放此图形的上下文以及它使用的所有系统资源。调用 dispose 之后,就不能再使用 Graphics 对象  
        bi.flush(); // 刷新此 Image 对象正在使用的所有可重构的资源  
  
        ImageIO.write(bi, "png", destFile);  
        System.out.println("Input Encoded data is:" + encodeddata);  
    }  
  
    /** 
     * 解析二维码,返回解析内容 
     *  
     * @param imageFile 
     * @return 
     */  
    public static String qrCodeDecode(File imageFile) {  
        String decodedData = null;  
        QRCodeDecoder decoder = new QRCodeDecoder();  
        BufferedImage image = null;  
        try {  
            image = ImageIO.read(imageFile);  
        } catch (IOException e) {  
            System.out.println("Error: " + e.getMessage());  
        }  
  
        try {  
            decodedData = new String(decoder.decode(new J2SEImage(image)), "GBK");  
            System.out.println("Output Decoded Data is:" + decodedData);  
        } catch (DecodingFailedException dfe) {  
            System.out.println("Error: " + dfe.getMessage());  
        } catch (UnsupportedEncodingException e) {  
            e.printStackTrace();  
        }  
        return decodedData;  
    }  
  
    public static void main(String[] args) {  
        String FilePath = "d:/qrcode.png";  
        File qrFile = new File(FilePath);  
  
        // 二维码内容  
        String encodeddata = "Hello X-rapido";  
        try {  
            QRCodeUtils.qrCodeEncode(encodeddata, qrFile);  
        } catch (IOException e) {  
            e.printStackTrace();  
        }  
  
        // 解码  
        String reText = QRCodeUtils.qrCodeDecode(qrFile);  
        System.out.println(reText);  
    }  
}  
  
class J2SEImage implements QRCodeImage {  
    BufferedImage image;  
  
    public J2SEImage(BufferedImage image) {  
        this.image = image;  
    }  
  
    public int getWidth() {  
        return image.getWidth();  
    }  
  
    public int getHeight() {  
        return image.getHeight();  
    }  
  
    public int getPixel(int x, int y) {  
        return image.getRGB(x, y);  
    }  
}