
正文
qr码生成器代码java qr码在线生成器
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
java编程将文字转为对应数字然后做二维码存入excel
这东西可以自己查阅资料啊。给你一个思路,首先把excel转化为xml文件保存(或者转为数据库),然后读取xml。
再用下面这个类转为二维码。
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package qrcode;
import com.swetake.util.Qrcode;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
/**
* Created with IntelliJ IDEA.
* Date: 10/9/13
* Time: 11:31 AM
*/
public class QRCodeTest {
private static int DEFAULT_WIDTH;
private static int UNIT_WIDTH = 10;
public static void createImg(){
Qrcode qrcode=new Qrcode();
//错误修正容量
//L水平 7%的字码可被修正
//M水平 15%的字码可被修正
//Q水平 25%的字码可被修正
//H水平 30%的字码可被修正
//QR码有容错能力,QR码图形如果有破损,仍然可以被机器读取内容,最高可以到7%~30%面积破损仍可被读取。
//相对而言,容错率愈高,QR码图形面积愈大。所以一般折衷使用15%容错能力。
qrcode.setQrcodeErrorCorrect('M');/* L','M','Q','H' */
qrcode.setQrcodeEncodeMode('B');/* "N","A" or other */
qrcode.setQrcodeVersion(3);/* 0-20 */
String testString = "这里是要转化的文字";
byte[] buff = null;
try {
buff = testString.getBytes("utf-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
boolean[][] bRect = qrcode.calQrcode(buff);
DEFAULT_WIDTH = bRect.length * UNIT_WIDTH;
BufferedImage bi = new BufferedImage(DEFAULT_WIDTH, DEFAULT_WIDTH, BufferedImage.TYPE_INT_RGB);
// int unitWidth = DEFAULT_WIDTH / bRect.length;
// createGraphics
Graphics2D g = bi.createGraphics();
// set background
g.setBackground(Color.WHITE);
g.clearRect(0, 0, DEFAULT_WIDTH, DEFAULT_WIDTH);
g.setColor(Color.BLACK);
if (buff.length0 buff.length 123){
for (int i=0;ibRect.length;i++){
for (int j=0;jbRect.length;j++){
if (bRect[j][i]) {
g.fillRect(j*UNIT_WIDTH, i*UNIT_WIDTH, UNIT_WIDTH-1, UNIT_WIDTH-1);
}
}
}
}
g.dispose();
bi.flush();
String FilePath="QRCode.png";
File f = new File(FilePath);
try {
ImageIO.write(bi, "png", f);
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("Create QRCode finished!");
}
}
相关问答
Q1: java哪种方式生成二维码比较好
最简单的方式使用jQuery的qrCode插件
[html] view plain copy
!DOCTYPE html
html
head
meta charset="UTF-8"
title使用JQueryQrCode生成二维码/title
script type="text/javascript" src="./js/jquery-1.9.1.min.js"/script
script type="text/javascript" src="./js/qrcode.js"/script
/head
body
div id="qrcode"/div
script type="text/javascript"
//参数1表示图像大小,取值范围1-10;参数2表示质量,取值范围'L','M','Q','H'
var content = "使用JQueryQrCode生成二维码"
var qr = qrcode(8, 'M');
qr.addData(content);
qr.make();
/* var dom=document.createElement('DIV');
dom.innerHTML = qr.createImgTag();
var element=document.getElementById("qrcode");
element.appendChild(dom); */
$("#qrcode").html(qr.createImgTag());
/script
/body
/html
Q2: java中怎样用代码生成二维码?
参考代码
import java.io.*;
import java.util.Date;
import java.awt.*;
import java.awt.image.*;
import javax.imageio.*;
public class QRCodeEncoderTest
{
/** Creates a new instance of QRCodeEncoderTest */
public QRCodeEncoderTest()
{
}
public static void create_image(String sms_info)throws Exception{
try{
qrcode testQrcode =new qrcode();
testQrcode.setQrcodeErrorCorrect('M');
testQrcode.setQrcodeEncodeMode('B');
testQrcode.setQrcodeVersion(7);
String testString = sms_info;
byte[] d = testString.getBytes("gbk");
System.out.println(d.length);
//BufferedImage bi = new BufferedImage(98, 98, BufferedImage.TYPE_INT_RGB);
BufferedImage bi = new BufferedImage(98, 98, BufferedImage.TYPE_BYTE_BINARY);
Graphics2D g = bi.createGraphics();
g.setBackground(Color.WHITE);
g.clearRect(0, 0, 98, 98);
g.setColor(Color.BLACK);
// 限制最大字节数为120
if (d.length0 d.length 120){
boolean[][] s = testQrcode.calQrcode(d);
for (int i=0;is.length;i++){
for (int j=0;js.length;j++){
if (s[j][i]) {
g.fillRect(j*2+3,i*2+3,2,2);
}
}
}
}
g.dispose();
bi.flush();
File f = new File("D:\\QRCodeTest\\"+sms_info+".jpg");
if(!f.exists()){
f.createNewFile();
}
//创建图片
ImageIO.write(bi, "jpg", f);
} // end try
catch (Exception e) {
e.printStackTrace();
} // end catch
}
public static void main(String[] args) throws Exception {
System.out.println(new Date());
for(int i =1; i 100000; i ++){
QRCodeEncoderTest.create_image(i+"");
}
System.out.println(new Date());
} // end main
}
Q3: 如何用java生成二维码
package common;
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.util.HashMap;
import java.util.Map;
import javax.imageio.ImageIO;
import jp.sourceforge.qrcode.QRCodeDecoder;
import jp.sourceforge.qrcode.exception.DecodingFailedException;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.Binarizer;
import com.google.zxing.BinaryBitmap;
import com.google.zxing.EncodeHintType;
import com.google.zxing.LuminanceSource;
import com.google.zxing.MultiFormatReader;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.NotFoundException;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.common.HybridBinarizer;
import com.swetake.util.Qrcode;
/**
* 二维码生成工具类
* @author Cloud
* @data 2016-12-15
* QRCode
*/
public class QRCodeUtil {
//二维码颜色
private static final int BLACK = 0xFF000000;
//二维码颜色
private static final int WHITE = 0xFFFFFFFF;
/**
* span style="font-size:18px;font-weight:blod;"ZXing 方式生成二维码/span
* @param text a href="javascript:void();"二维码内容/a
* @param width 二维码宽
* @param height 二维码高
* @param outPutPath 二维码生成保存路径
* @param imageType 二维码生成格式
*/
public static void zxingCodeCreate(String text, int width, int height, String outPutPath, String imageType){
MapEncodeHintType, String his = new HashMapEncodeHintType, String();
//设置编码字符集
his.put(EncodeHintType.CHARACTER_SET, "utf-8");
try {
//1、生成二维码
BitMatrix encode = new MultiFormatWriter().encode(text, BarcodeFormat.QR_CODE, width, height, his);
//2、获取二维码宽高
int codeWidth = encode.getWidth();
int codeHeight = encode.getHeight();
//3、将二维码放入缓冲流
BufferedImage image = new BufferedImage(codeWidth, codeHeight, BufferedImage.TYPE_INT_RGB);
for (int i = 0; i codeWidth; i++) {
for (int j = 0; j codeHeight; j++) {
//4、循环将二维码内容定入图片
image.setRGB(i, j, encode.get(i, j) ? BLACK : WHITE);
}
}
File outPutImage = new File(outPutPath);
//如果图片不存在创建图片
if(!outPutImage.exists())
outPutImage.createNewFile();
//5、将二维码写入图片
ImageIO.write(image, imageType, outPutImage);
} catch (WriterException e) {
e.printStackTrace();
System.out.println("二维码生成失败");
} catch (IOException e) {
e.printStackTrace();
System.out.println("生成二维码图片失败");
}
}
/**
* span style="font-size:18px;font-weight:blod;"二维码解析/span
* @param analyzePath 二维码路径
* @return
* @throws IOException
*/
@SuppressWarnings({ "rawtypes", "unchecked" })
public static Object zxingCodeAnalyze(String analyzePath) throws Exception{
MultiFormatReader formatReader = new MultiFormatReader();
Object result = null;
try {
File file = new File(analyzePath);
if (!file.exists())
{
return "二维码不存在";
}
BufferedImage image = ImageIO.read(file);
LuminanceSource source = new LuminanceSourceUtil(image);
Binarizer binarizer = new HybridBinarizer(source);
BinaryBitmap binaryBitmap = new BinaryBitmap(binarizer);
Map hints = new HashMap();
hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
result = formatReader.decode(binaryBitmap, hints);
} catch (NotFoundException e) {
e.printStackTrace();
}
return result;
}
/**
* span style="font-size:18px;font-weight:blod;"QRCode 方式生成二维码/span
* @param content 二维码内容
* @param imgPath 二维码生成路径
* @param version 二维码版本
* @param isFlag 是否生成Logo图片 为NULL不生成
*/
public static void QRCodeCreate(String content, String imgPath, int version, String logoPath){
try {
Qrcode qrcodeHandler = new Qrcode();
//设置二维码排错率,可选L(7%) M(15%) Q(25%) H(30%),排错率越高可存储的信息越少,但对二维码清晰度的要求越小
qrcodeHandler.setQrcodeErrorCorrect('M');
//N代表数字,A代表字符a-Z,B代表其他字符
qrcodeHandler.setQrcodeEncodeMode('B');
//版本1为21*21矩阵,版本每增1,二维码的两个边长都增4;所以版本7为45*45的矩阵;最高版本为是40,是177*177的矩阵
qrcodeHandler.setQrcodeVersion(version);
//根据版本计算尺寸
int imgSize = 67 + 12 * (version - 1) ;
byte[] contentBytes = content.getBytes("gb2312");
BufferedImage bufImg = new BufferedImage(imgSize , imgSize ,BufferedImage.TYPE_INT_RGB);
Graphics2D gs = bufImg.createGraphics();
gs.setBackground(Color.WHITE);
gs.clearRect(0, 0, imgSize , imgSize);
// 设定图像颜色 BLACK
gs.setColor(Color.BLACK);
// 设置偏移量 不设置可能导致解析出错
int pixoff = 2;
// 输出内容 二维码
if (contentBytes.length 0 contentBytes.length 130) {
boolean[][] codeOut = qrcodeHandler.calQrcode(contentBytes);
for (int i = 0; i codeOut.length; i++) {
for (int j = 0; j codeOut.length; j++) {
if (codeOut[j][i]) {
gs.fillRect(j * 3 + pixoff, i * 3 + pixoff, 3, 3);
}
}
}
} else {
System.err.println("QRCode content bytes length = " + contentBytes.length + " not in [ 0,130 ]. ");
}
/* 判断是否需要添加logo图片 */
if(logoPath != null){
File icon = new File(logoPath);
if(icon.exists()){
int width_4 = imgSize / 4;
int width_8 = width_4 / 2;
int height_4 = imgSize / 4;
int height_8 = height_4 / 2;
Image img = ImageIO.read(icon);
gs.drawImage(img, width_4 + width_8, height_4 + height_8,width_4,height_4, null);
gs.dispose();
bufImg.flush();
}else{
System.out.println("Error: login图片还在在!");
}
}
gs.dispose();
bufImg.flush();
//创建二维码文件
File imgFile = new File(imgPath);
if(!imgFile.exists())
imgFile.createNewFile();
//根据生成图片获取图片
String imgType = imgPath.substring(imgPath.lastIndexOf(".") + 1, imgPath.length());
// 生成二维码QRCode图片
ImageIO.write(bufImg, imgType, imgFile);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* span style="font-size:18px;font-weight:blod;"QRCode二维码解析/span
* @param codePath 二维码路径
* @return 解析结果
*/
public static String QRCodeAnalyze(String codePath) {
File imageFile = new File(codePath);
BufferedImage bufImg = null;
String decodedData = null;
try {
if(!imageFile.exists())
return "二维码不存在";
bufImg = ImageIO.read(imageFile);
QRCodeDecoder decoder = new QRCodeDecoder();
decodedData = new String(decoder.decode(new ImageUtil(bufImg)), "gb2312");
} catch (IOException e) {
System.out.println("Error: " + e.getMessage());
e.printStackTrace();
} catch (DecodingFailedException dfe) {
System.out.println("Error: " + dfe.getMessage());
dfe.printStackTrace();
}
return decodedData;
}
}
3、最后贴测试代码:
package test;
import java.awt.image.BufferedImage;
import java.io.InputStream;
import java.net.URL;
import javax.imageio.ImageIO;
import common.ImageUtil;
import common.QRCodeUtil;
import jp.sourceforge.qrcode.QRCodeDecoder;
/**
* 二维码生成测试类
* @author Cloud
* @data 2016-11-21
* QRCodeTest
*/
public class QRCodeTest {
public static void main(String[] args) throws Exception {
/**
* QRcode 二维码生成测试
* QRCodeUtil.QRCodeCreate("", "E://qrcode.jpg", 15, "E://icon.png");
*/
/**
* QRcode 二维码解析测试
* String qrcodeAnalyze = QRCodeUtil.QRCodeAnalyze("E://qrcode.jpg");
*/
/**
* ZXingCode 二维码生成测试
* QRCodeUtil.zxingCodeCreate("", 300, 300, "E://zxingcode.jpg", "jpg");
*/
/**
* ZxingCode 二维码解析
* String zxingAnalyze = QRCodeUtil.zxingCodeAnalyze("E://zxingcode.jpg").toString();
*/
System.out.println("success");
}
}
Q4: 如何使用java开发二维码代码
1: 使用SwetakeQRCode在Java项目中生成二维码
下载地址
或着
这个是日本人写的,生成的是我们常见的方形的二维码
可以用中文
如:5677777ghjjjjj
2: 使用BarCode4j生成条形码和二维码
BarCode4j网址:
barcode4j是使用datamatrix的二维码生成算法,为支持qr的算法
datamatrix是欧美的标准,qr为日本的标准,
barcode4j一般生成出来是长方形的
如:88777alec000yan
这个博客这方面说的挺清楚的:
3:zxing
zxing 这个是google的
下载地址
Java代码:
import java.io.File;
import java.util.Hashtable;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;
public class QRCodeEvents {
public static void main(String []args)throws Exception{
String text = "你好";
int width = 100;
int height = 100;
String format = "png";
Hashtable hints= new Hashtable();
hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
BitMatrix bitMatrix = new MultiFormatWriter().encode(text, BarcodeFormat.QR_CODE, width, height,hints);
File outputFile = new File("new.png");
MatrixToImageWriter.writeToFile(bitMatrix, format, outputFile);
}
}
4:google chart api就有实现二维码的方法
利用这个api,使用google appengine进行实现。
5:JS生成二维码
使用jQuery-qrcode生成二维码
先简单说一下jquery-qrcode,这个开源的三方库(可以从 获取),
qrcode.js 是实现二维码数据计算的核心类,
jquery.qrcode.js 是把它用jquery方式封装起来的,用它来实现图形渲染,其实就是画图(支持canvas和table两种方式)
支持的功能主要有:
Js代码:
text : "" //设置二维码内容
Js代码:
render : "canvas",//设置渲染方式
width : 256, //设置宽度
height : 256, //设置高度
typeNumber : -1, //计算模式
correctLevel : QRErrorCorrectLevel.H,//纠错等级
background : "#ffffff",//背景颜色
foreground : "#000000" //前景颜色
使用方式非常简单
Js代码:
jQuery('#output').qrcode({width:200,height:200,correctLevel:0,text:content});
经过简单实践,
使用canvas方式渲染性能还是非常不错的,但是如果用table方式,性能不太理想,特别是IE9以下的浏览器,所以需要自行优化一下渲染table的方式,这里就不细述了。
其实上面的js有一个小小的缺点,就是默认不支持中文。
这跟js的机制有关系,jquery-qrcode这个库是采用 charCodeAt() 这个方式进行编码转换的,
而这个方法默认会获取它的 Unicode 编码,一般的解码器都是采用UTF-8, ISO-8859-1等方式,
英文是没有问题,如果是中文,一般情况下Unicode是UTF-16实现,长度2位,而UTF-8编码是3位,这样二维码的编解码就不匹配了。
解决方式当然是,在二维码编码前把字符串转换成UTF-8,具体代码如下:
function utf16to8(str) {
var out, i, len, c;
out = "";
len = str.length;
for(i = 0; i len; i++) {
c = str.charCodeAt(i);
if ((c = 0x0001) (c = 0x007F)) {
out += str.charAt(i);
} else if (c 0x07FF) {
out += String.fromCharCode(0xE0 | ((c 12) 0x0F));
out += String.fromCharCode(0x80 | ((c 6) 0x3F));
out += String.fromCharCode(0x80 | ((c 0) 0x3F));
} else {
out += String.fromCharCode(0xC0 | ((c 6) 0x1F));
out += String.fromCharCode(0x80 | ((c 0) 0x3F));
}
}
return out;
}
Q5: C#生成二维码(QR码)
C# 二维码的代码:
using Spire.Barcode;
using System.Drawing;
namespace CreateQRCode
{
class Program
{
static void Main(string[] args)
{
//创建BarcodeSettings对象
BarcodeSettings settings = new BarcodeSettings();
//应用Key,去logo
BarcodeSettings.ApplyKey("4KRJD-1K294-JJG9Z-SNR36-3P7IU");
settings.Type = BarCodeType.QRCode;//设置条码类型为二维码
settings.Data = "123456789";//设置二维码数据
settings.Data2D = "123456789";//设置显示文本
settings.ShowText = false;//设置二维码数据文本不显示
/*settings.ShowText = true;//显示数据文本
settings.ShowTextOnBottom = true;//数据文本显示在二维码底部*/
settings.QRCodeDataMode = QRCodeDataMode.Numeric;//设置数据类型为数字
settings.QRCodeECL = QRCodeECL.H;//设置二维码错误修正级别
settings.X = 3.0f;//设置宽度
BarCodeGenerator generator = new BarCodeGenerator(settings);//实例化BarCodeGenerator类的对象
//生成二维码图片并保存为PNG格式
Image image = generator.GenerateImage();
image.Save("QRCode.png");
}
}
}
这里的实现方法借助专门的barcode生成根据spire.barcode for .net提供的类以及方法,解析二维码可以参考如下代码:
using System;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Spire.Barcode;
namespace ScanBarcode{
public partial class Form1 : Form {
public Form1()
{
InitializeComponent();
}
private void btnLoadImage_Click(object sender, EventArgs e)
{
//加载条形码图片
Image image = Image.FromFile("Code128.png");
pictureBox1.Image = image;
}
private void btnReadData_Click(object sender, EventArgs e)
{
Bitmap bitmap = new Bitmap(pictureBox1.Image);
//识别条形码图片中的数据(BarcodeScanner类包含多个Scan重载方法,可根据自己的需求选择相应的方法)
string[] data = BarcodeScanner.Scan(bitmap, BarCodeType.Code128);
for (int i = 0; i data.Length; i++)
{
this.textBox1.Text += data[i].ToString();
}
}
}
}
—End—
关于qr码生成器代码java和qr码在线生成器的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。






