网站首页 > 文章中心 > 其它

java二维码代码源码

作者:小编 更新时间:2023-08-18 11:49:36 浏览量:8人看过

如何用java生成二维码

package?common;

import?java.awt.Color;

import?java.awt.Image;

import?java.awt.image.BufferedImage;

import?java.io.File;

java二维码代码源码-图1

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;

java二维码代码源码-图2

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;

/**

*?二维码生成工具类

*?QRCode

*/

public?class?QRCodeUtil?{

//二维码颜色

private?static?final?int?BLACK?=?0xFF000000;

private?static?final?int?WHITE?=?0xFFFFFFFF;

public?static?void?zxingCodeCreate(String?text,?int?width,?int?height,?String?outPutPath,?String?imageType){

MapEncodeHintType,?String?his?=?new?HashMapEncodeHintType,?String();

//设置编码字符集

try?{

//1、生成二维码

BitMatrix?encode?=?new?MultiFormatWriter().encode(text,?BarcodeFormat.QR_CODE,?width,?height,?his);

int?codeWidth?=?encode.getWidth();

int?codeHeight?=?encode.getHeight();

BufferedImage?image?=?new?BufferedImage(codeWidth,?codeHeight,?BufferedImage.TYPE_INT_RGB);

for?(int?i?=?0;?i?codeWidth;?i++)?{

for?(int?j?=?0;?j?codeHeight;?j++)?{

image.setRGB(i,?j,?encode.get(i,?j)?BLACK?:?WHITE);

}

File?outPutImage?=?new?File(outPutPath);

//如果图片不存在创建图片

if(!outPutImage.exists())

outPutImage.createNewFile();

ImageIO.write(image,?imageType,?outPutImage);

}?catch?(WriterException?e)?{

e.printStackTrace();

System.out.println("二维码生成失败");

}?catch?(IOException?e)?{

System.out.println("生成二维码图片失败");

public?static?Object?zxingCodeAnalyze(String?analyzePath)?throws?Exception{

MultiFormatReader?formatReader?=?new?MultiFormatReader();

Object?result?=?null;

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();

result?=?formatReader.decode(binaryBitmap,?hints);

}?catch?(NotFoundException?e)?{

}?

return?result;

public?static?void?QRCodeCreate(String?content,?String?imgPath,?int?version,?String?logoPath){

try?{?

Qrcode?qrcodeHandler?=?new?Qrcode();?

qrcodeHandler.setQrcodeErrorCorrect('M');?

//N代表数字,A代表字符a-Z,B代表其他字符?

qrcodeHandler.setQrcodeEncodeMode('B');?

qrcodeHandler.setQrcodeVersion(version);

//根据版本计算尺寸

BufferedImage?bufImg?=?new?BufferedImage(imgSize?,?imgSize?,BufferedImage.TYPE_INT_RGB);?

gs.setBackground(Color.WHITE);?

gs.clearRect(0,?0,?imgSize?,?imgSize);?

//?设定图像颜色?BLACK

gs.setColor(Color.BLACK);

//?设置偏移量?不设置可能导致解析出错?

//?输出内容?二维码?

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])?{?

}?else?{?

/*?判断是否需要添加logo图片?*/

if(logoPath?!=?null){

File?icon?=?new?File(logoPath);

if(icon.exists()){

Image?img?=?ImageIO.read(icon);

gs.dispose();

bufImg.flush();

}else{

System.out.println("Error:?login图片还在在!");

//创建二维码文件

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();?

public?static?String?QRCodeAnalyze(String?codePath)?{

File?imageFile?=?new?File(codePath);

BufferedImage?bufImg?=?null;?

String?decodedData?=?null;?

if(!imageFile.exists())

bufImg?=?ImageIO.read(imageFile);

QRCodeDecoder?decoder?=?new?QRCodeDecoder();?

}?catch?(IOException?e)?{?

java二维码代码源码-图3

System.out.println("Error:?"?+?e.getMessage());?

}?catch?(DecodingFailedException?dfe)?{?

System.out.println("Error:?"?+?dfe.getMessage());?

dfe.printStackTrace();?

return?decodedData;

package?test;

import?java.io.InputStream;

import?java.net.URL;

import?common.ImageUtil;

import?common.QRCodeUtil;

*?二维码生成测试类

*?QRCodeTest

public?class?QRCodeTest?{

public?static?void?main(String[]?args)?throws?Exception?{

*?QRcode?二维码生成测试

*??QRcode?二维码解析测试

*?String?qrcodeAnalyze?=?QRCodeUtil.QRCodeAnalyze("E://qrcode.jpg");

*?ZXingCode?二维码生成测试

*?ZxingCode?二维码解析

*?String?zxingAnalyze?=?QRCodeUtil.zxingCodeAnalyze("E://zxingcode.jpg").toString();

System.out.println("success");

java生成二维码名片,内容太大,转码怎么实现

Java中字符串转码,根据实际运用的环境有以下三种方式 使用Java.lang.String这是最常用的方法,先用对应编码获取字节,然后重新构造新编码,示例代码如下: String?s?=?"清山";? byte[]?b?=?s.getBytes("utf-吧");//编码? String?sa?=?new?String(b,?"gb二三一二");//解码:用什么字符集编码就用什么字符集解码 java.io.InputStreamReader/OutputStreamWriter:桥转换读写文件的应用中,可以使用这种方式,直接在IO流构造中转换,示例代码如下: InputStream?is?=?new?FileInputStream("C:/项目进度跟踪.txt");//文件读取? InputStreamReader?isr?=?new?InputStreamReader(is,?"utf-吧");//解码? OutputStream?os?=?new?FileOutputStream("C:/项目进度跟踪_gb二三一二.txt");//文件输出? OutputStreamWriter?osw?=?new?OutputStreamWriter(os,?"gb二三一二");//开始编码 java.nio.Charset使用nio中的Charset转换字符,示例代码如下: Charset?inSet?=?Charset.forName("utf-吧");?//?解码字符集? Charset?outSet?=?Charset.forName("gb二三一二");?//?编码字符集? CharsetDecoder?de?=?inSet.newDecoder();?//?解码器 CharsetEncoder?en?=?outSet.newEncoder();//?编

如何使用java开发二维码代码

①.: 使用SwetakeQRCode在Java项目中生成二维码

或着

这个是日本人写的,生成的是我们常见的方形的二维码

可以用中文

datamatrix是欧美的标准,qr为日本的标准,

这个博客这方面说的挺清楚的:

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.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();

BitMatrix bitMatrix = new MultiFormatWriter().encode(text, BarcodeFormat.QR_CODE, width, height,hints);

File outputFile = new File("new.png");

MatrixToImageWriter.writeToFile(bitMatrix, format, outputFile);

利用这个api,使用google appengine进行实现.

使用jQuery-qrcode生成二维码

先简单说一下jquery-qrcode,这个开源的三方库(可以从 获取),

qrcode.js 是实现二维码数据计算的核心类,

jquery.qrcode.js 是把它用jquery方式封装起来的,用它来实现图形渲染,其实就是画图(支持canvas和table两种方式)

支持的功能主要有:

Js代码:

text : "" //设置二维码内容

render : "canvas",//设置渲染方式

typeNumber : -1, //计算模式

correctLevel : QRErrorCorrectLevel.H,//纠错等级

background : "#ffffff",//背景颜色

foreground : "#000000" //前景颜色

使用方式非常简单

经过简单实践,

其实上面的js有一个小小的缺点,就是默认不支持中文.

这跟js的机制有关系,jquery-qrcode这个库是采用 charCodeAt() 这个方式进行编码转换的,

var out, i, len, c;

out = "";

len = str.length;

for(i = 0; i len; i++) {

c = str.charCodeAt(i);

out += str.charAt(i);

} else {

return out;

Java如何用代码生成二维码

引用spire.barcode.jar包

//创建BarcodeSettings对象

BarcodeSettings?settings?=?new?BarcodeSettings();

//设置条码类型为

QR二维码settings.setType(BarCodeType.QR_Code);??

//设置二维码数据

//设置二维码显示数据

//设置数据类型

settings.setQRCodeDataMode(QRCodeDataMode.Alpha_Number);

//设置二维码模型宽度

settings.setX(1.0f);

//设置二维码纠错级别settings.setQRCodeECL(QRCodeECL.H);

//创建BarCodeGenerator实例

BarCodeGenerator?barCodeGenerator?=?new?BarCodeGenerator(settings);

//根据settings生成图像数据,保存至BufferedImage

BufferedImage?bufferedImage?=?barCodeGenerator.generateImage();

//将图片数据保存为PNG格式

ImageIO.write(bufferedImage,?"png",?new?File("QRCode.png"));

以上就是土嘎嘎小编为大家整理的java二维码代码源码相关主题介绍,如果您觉得小编更新的文章只要能对粉丝们有用,就是我们最大的鼓励和动力,不要忘记讲本站分享给您身边的朋友哦!!

版权声明:倡导尊重与保护知识产权。未经许可,任何人不得复制、转载、或以其他方式使用本站《原创》内容,违者将追究其法律责任。本站文章内容,部分图片来源于网络,如有侵权,请联系我们修改或者删除处理。

编辑推荐

热门文章