二维码生成Java实现代码
本文实例为大家分享了二维码生成Java实现代码,供大家参考,具体内容如下
packagecom.yihaomen.barcode;
importjava.awt.BasicStroke;
importjava.awt.Graphics;
importjava.awt.Graphics2D;
importjava.awt.Image;
importjava.awt.Shape;
importjava.awt.geom.RoundRectangle2D;
importjava.awt.image.BufferedImage;
importjava.io.File;
importjava.io.OutputStream;
importjava.util.Hashtable;
importjava.util.Random;
importjavax.imageio.ImageIO;
importcom.google.zxing.BarcodeFormat;
importcom.google.zxing.BinaryBitmap;
importcom.google.zxing.DecodeHintType;
importcom.google.zxing.EncodeHintType;
importcom.google.zxing.MultiFormatReader;
importcom.google.zxing.MultiFormatWriter;
importcom.google.zxing.Result;
importcom.google.zxing.common.BitMatrix;
importcom.google.zxing.common.HybridBinarizer;
importcom.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
/**
*二维码工具类
*
*/
publicclassQRCodeUtil{
privatestaticfinalStringCHARSET="utf-8";
privatestaticfinalStringFORMAT_NAME="JPG";
//二维码尺寸
privatestaticfinalintQRCODE_SIZE=300;
//LOGO宽度
privatestaticfinalintWIDTH=60;
//LOGO高度
privatestaticfinalintHEIGHT=60;
privatestaticBufferedImagecreateImage(Stringcontent,StringimgPath,
booleanneedCompress)throwsException{
Hashtablehints=newHashtable();
hints.put(EncodeHintType.ERROR_CORRECTION,ErrorCorrectionLevel.H);
hints.put(EncodeHintType.CHARACTER_SET,CHARSET);
hints.put(EncodeHintType.MARGIN,1);
BitMatrixbitMatrix=newMultiFormatWriter().encode(content,
BarcodeFormat.QR_CODE,QRCODE_SIZE,QRCODE_SIZE,hints);
intwidth=bitMatrix.getWidth();
intheight=bitMatrix.getHeight();
BufferedImageimage=newBufferedImage(width,height,
BufferedImage.TYPE_INT_RGB);
for(intx=0;xWIDTH){
width=WIDTH;
}
if(height>HEIGHT){
height=HEIGHT;
}
Imageimage=src.getScaledInstance(width,height,
Image.SCALE_SMOOTH);
BufferedImagetag=newBufferedImage(width,height,
BufferedImage.TYPE_INT_RGB);
Graphicsg=tag.getGraphics();
g.drawImage(image,0,0,null);//绘制缩小后的图
g.dispose();
src=image;
}
//插入LOGO
Graphics2Dgraph=source.createGraphics();
intx=(QRCODE_SIZE-width)/2;
inty=(QRCODE_SIZE-height)/2;
graph.drawImage(src,x,y,width,height,null);
Shapeshape=newRoundRectangle2D.Float(x,y,width,width,6,6);
graph.setStroke(newBasicStroke(3f));
graph.draw(shape);
graph.dispose();
}
/**
*生成二维码(内嵌LOGO)
*
*@paramcontent
*内容
*@paramimgPath
*LOGO地址
*@paramdestPath
*存放目录
*@paramneedCompress
*是否压缩LOGO
*@throwsException
*/
publicstaticvoidencode(Stringcontent,StringimgPath,StringdestPath,
booleanneedCompress)throwsException{
BufferedImageimage=QRCodeUtil.createImage(content,imgPath,
needCompress);
mkdirs(destPath);
Stringfile=newRandom().nextInt(99999999)+".jpg";
ImageIO.write(image,FORMAT_NAME,newFile(destPath+"/"+file));
}
/**
*当文件夹不存在时,mkdirs会自动创建多层目录,区别于mkdir.(mkdir如果父目录不存在则会抛出异常)
*@authorlanyuan
*Email:mmm333zzz520@163.com
*@date2013-12-11上午10:16:36
*@paramdestPath存放目录
*/
publicstaticvoidmkdirs(StringdestPath){
Filefile=newFile(destPath);
//当文件夹不存在时,mkdirs会自动创建多层目录,区别于mkdir.(mkdir如果父目录不存在则会抛出异常)
if(!file.exists()&&!file.isDirectory()){
file.mkdirs();
}
}
/**
*生成二维码(内嵌LOGO)
*
*@paramcontent
*内容
*@paramimgPath
*LOGO地址
*@paramdestPath
*存储地址
*@throwsException
*/
publicstaticvoidencode(Stringcontent,StringimgPath,StringdestPath)
throwsException{
QRCodeUtil.encode(content,imgPath,destPath,false);
}
/**
*生成二维码
*
*@paramcontent
*内容
*@paramdestPath
*存储地址
*@paramneedCompress
*是否压缩LOGO
*@throwsException
*/
publicstaticvoidencode(Stringcontent,StringdestPath,
booleanneedCompress)throwsException{
QRCodeUtil.encode(content,null,destPath,needCompress);
}
/**
*生成二维码
*
*@paramcontent
*内容
*@paramdestPath
*存储地址
*@throwsException
*/
publicstaticvoidencode(Stringcontent,StringdestPath)throwsException{
QRCodeUtil.encode(content,null,destPath,false);
}
/**
*生成二维码(内嵌LOGO)
*
*@paramcontent
*内容
*@paramimgPath
*LOGO地址
*@paramoutput
*输出流
*@paramneedCompress
*是否压缩LOGO
*@throwsException
*/
publicstaticvoidencode(Stringcontent,StringimgPath,
OutputStreamoutput,booleanneedCompress)throwsException{
BufferedImageimage=QRCodeUtil.createImage(content,imgPath,
needCompress);
ImageIO.write(image,FORMAT_NAME,output);
}
/**
*生成二维码
*
*@paramcontent
*内容
*@paramoutput
*输出流
*@throwsException
*/
publicstaticvoidencode(Stringcontent,OutputStreamoutput)
throwsException{
QRCodeUtil.encode(content,null,output,false);
}
/**
*解析二维码
*
*@paramfile
*二维码图片
*@return
*@throwsException
*/
publicstaticStringdecode(Filefile)throwsException{
BufferedImageimage;
image=ImageIO.read(file);
if(image==null){
returnnull;
}
BufferedImageLuminanceSourcesource=newBufferedImageLuminanceSource(
image);
BinaryBitmapbitmap=newBinaryBitmap(newHybridBinarizer(source));
Resultresult;
Hashtablehints=newHashtable();
hints.put(DecodeHintType.CHARACTER_SET,CHARSET);
result=newMultiFormatReader().decode(bitmap,hints);
StringresultStr=result.getText();
returnresultStr;
}
/**
*解析二维码
*
*@parampath
*二维码图片地址
*@return
*@throwsException
*/
publicstaticStringdecode(Stringpath)throwsException{
returnQRCodeUtil.decode(newFile(path));
}
publicstaticvoidmain(String[]args)throwsException{
Stringtext="http://www.yihaomen.com";
QRCodeUtil.encode(text,"c:/me.jpg","c:/barcode",true);
}
}
     
大家可以参考专题:java二维码进行学习
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。
