Java实现二维码功能的实例代码
最近突然想写一些文章,所以就陆陆续续的编写一些自我感觉有用的并且大家也可能用到的一些技术代码。方便互相学习交流。
今天这篇文章,主要是利用Java实现二维码:
在写代码之前先讲一下整体思路,以方便更好更快捷的实现功能。
(1).首先要想实现二维码功能需要导入com.google.zxing的核心jar包,我这里导入的是core-3.2.1.jar;
(2).所谓二维码莫过于把需要的内容放入一张图片中,所以这里首先是创建一张带有参数内容的图片,方法如下:
privatestaticBufferedImagecreateImage(Stringcontent,StringimgPath,booleanneedCompress)throwsException{
Hashtable<EncodeHintType,Object>hints=newHashtable<EncodeHintType,Object>();
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;x<width;x++){
for(inty=0;y<height;y++){
image.setRGB(x,y,bitMatrix.get(x,y)?0xFF000000:0xFFFFFFFF);
}
}
if(imgPath==null||"".equals(imgPath)){
returnimage;
}
//插入图片
QRCodeUtil.insertImage(image,imgPath,needCompress);
returnimage;
}
//插入logo如不需要logo可以执行此方法
privatestaticvoidinsertImage(BufferedImagesource,StringimgPath,booleanneedCompress)throwsException{
Filefile=newFile(imgPath);
if(!file.exists()){
System.err.println(""+imgPath+"该文件不存在!");
return;
}
Imagesrc=ImageIO.read(newFile(imgPath));
intwidth=src.getWidth(null);
intheight=src.getHeight(null);
if(needCompress){//压缩LOGO
if(width>WIDTH){
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();
}
(3).至此一张二维码图片生成方法就写完了,是不是很简单,接下来就是调用此方法。
/**
*生成二维码(内嵌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如果父目录不存在则会抛出异常)
*
*@authorLongJin
*@date2013-12-11上午10:16:36
*@paramdestPath
*存放目录
*/
publicstaticvoidmkdirs(StringdestPath){
Filefile=newFile(destPath);
//当文件夹不存在时,mkdirs会自动创建多层目录,区别于mkdir.(mkdir如果父目录不存在则会抛出异常)
if(!file.exists()&&!file.isDirectory()){
file.mkdirs();
}
}
(4).如果不要logo只要在调用时将logo的路径传为null就好。
(5).至此一个二维码生成工具就完成了,当然有人可能需要解析二维码,所以这里就把二维码解析方法也写出,同时也方便我日后查看。
/**
*解析二维码
*
*@paramfile
*二维码图片
*@return
*@throwsException
*/
publicstaticStringdecode(Filefile)throwsException{
BufferedImageimage;
image=ImageIO.read(file);
if(image==null){
returnnull;
}
BufferedImageLuminanceSourcesource=newBufferedImageLuminanceSource(image);
BinaryBitmapbitmap=newBinaryBitmap(newHybridBinarizer(source));
Resultresult;
Hashtable<DecodeHintType,Object>hints=newHashtable<DecodeHintType,Object>();
hints.put(DecodeHintType.CHARACTER_SET,CHARSET);
result=newMultiFormatReader().decode(bitmap,hints);
StringresultStr=result.getText();
returnresultStr;
}
以上所述是小编给大家介绍的Java实现二维码功能的实例代码,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对毛票票网站的支持!