您的位置:首页 > 编程语言

借点代码 二维码

2015-12-24 00:00 288 查看


import
java.awt.BasicStroke;

import
java.awt.Graphics;

import
java.awt.Graphics2D;

import
java.awt.Image;

import
java.awt.Shape;

import
java.awt.geom.RoundRectangle2D;

import
java.awt.image.BufferedImage;

import
java.io.File;

import
java.io.OutputStream;

import
java.util.Hashtable;

import
java.util.Random;



import
javax.imageio.ImageIO;



import
com.google.zxing.BarcodeFormat;

import
com.google.zxing.BinaryBitmap;

import
com.google.zxing.DecodeHintType;

import
com.google.zxing.EncodeHintType;

import
com.google.zxing.MultiFormatReader;

import
com.google.zxing.MultiFormatWriter;

import
com.google.zxing.Result;

import
com.google.zxing.common.BitMatrix;

import
com.google.zxing.common.HybridBinarizer;

import
com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;



/**


*二维码工具类


*


*/

public
class
QRCodeUtil{




private
static
final
StringCHARSET=
"utf-8"
;


private
static
final
StringFORMAT_NAME=
"JPG"
;


//二维码尺寸


private
7fe0

static
final
int
QRCODE_SIZE=
300
;


//LOGO宽度


private
static
final
int
WIDTH=
60
;


//LOGO高度


private
static
final
int
HEIGHT=
60
;




private
static
BufferedImagecreateImage(Stringcontent,StringimgPath,


boolean
needCompress)
throws
Exception{


Hashtable<EncodeHintType,Object>hints=
new
Hashtable<EncodeHintType,Object>();


hints.put(EncodeHintType.ERROR_CORRECTION,ErrorCorrectionLevel.H);


hints.put(EncodeHintType.CHARACTER_SET,CHARSET);


hints.put(EncodeHintType.MARGIN,
1
);


BitMatrixbitMatrix=
new
MultiFormatWriter().encode(content,


BarcodeFormat.QR_CODE,QRCODE_SIZE,QRCODE_SIZE,hints);


int
width=bitMatrix.getWidth();


int
height=bitMatrix.getHeight();


BufferedImageimage=
new
BufferedImage(width,height,


BufferedImage.TYPE_INT_RGB);


for
(
int
x=
0
;x<width;x++){


for
(
int
y=
0
;y<height;y++){


image.setRGB(x,y,bitMatrix.get(x,y)?
0xFF000000


:
0xFFFFFFFF
);


}


}


if
(imgPath==
null
||
""
.equals(imgPath)){


return
image;


}


//插入图片


QRCodeUtil.insertImage(image,imgPath,needCompress);


return
image;


}




/**


*插入LOGO


*


*@paramsource


*二维码图片


*@paramimgPath


*LOGO图片地址


*@paramneedCompress


*是否压缩


*@throwsException


*/


private
static
void
insertImage(BufferedImagesource,StringimgPath,


boolean
needCompress)
throws
Exception{


Filefile=
new
File(imgPath);


if
(!file.exists()){


System.err.println(
""
+imgPath+
"该文件不存在!"
);


return
;


}


Imagesrc=ImageIO.read(
new
File(imgPath));


int
width=src.getWidth(
null
);


int
height=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=
new
BufferedImage(width,height,


BufferedImage.TYPE_INT_RGB);


Graphicsg=tag.getGraphics();


g.drawImage(image,
0
,
0
,
null
);
//绘制缩小后的图


g.dispose();


src=image;


}


//插入LOGO


Graphics2Dgraph=source.createGraphics();


int
x=(QRCODE_SIZE-width)/
2
;


int
y=(QRCODE_SIZE-height)/
2
;


graph.drawImage(src,x,y,width,height,
null
);


Shapeshape=
new
RoundRectangle2D.Float(x,y,width,width,
6
,
6
);


graph.setStroke(
new
BasicStroke(3f));


graph.draw(shape);


graph.dispose();


}




/**


*生成二维码(内嵌LOGO)


*


*@paramcontent


*内容


*@paramimgPath


*LOGO地址


*@paramdestPath


*存放目录


*@paramneedCompress


*是否压缩LOGO


*@throwsException


*/


public
static
void
encode(Stringcontent,StringimgPath,StringdestPath,


boolean
needCompress)
throws
Exception{


BufferedImageimage=QRCodeUtil.createImage(content,imgPath,


needCompress);


mkdirs(destPath);


Stringfile=
new
Random().nextInt(
99999999
)+
".jpg"
;


ImageIO.write(image,FORMAT_NAME,
new
File(destPath+
"/"
+file));


}




/**


*当文件夹不存在时,mkdirs会自动创建多层目录,区别于mkdir.(mkdir如果父目录不存在则会抛出异常)


*@authorlanyuan


*Email:mmm333zzz520@163.com


*@date2013-12-11上午10:16:36


*@paramdestPath存放目录


*/


public
static
void
mkdirs(StringdestPath){


Filefile=
new
File(destPath);


//当文件夹不存在时,mkdirs会自动创建多层目录,区别于mkdir.(mkdir如果父目录不存在则会抛出异常)


if
(!file.exists()&&!file.isDirectory()){


file.mkdirs();


}


}




/**


*生成二维码(内嵌LOGO)


*


*@paramcontent


*内容


*@paramimgPath


*LOGO地址


*@paramdestPath


*存储地址


*@throwsException


*/


public
static
void
encode(Stringcontent,StringimgPath,StringdestPath)


throws
Exception{


QRCodeUtil.encode(content,imgPath,destPath,
true
);


}




/**


*生成二维码


*


*@paramcontent


*内容


*@paramdestPath


*存储地址


*@paramneedCompress


*是否压缩LOGO


*@throwsException


*/


public
static
void
encode(Stringcontent,StringdestPath,


boolean
needCompress)
throws
Exception{


QRCodeUtil.encode(content,
null
,destPath,needCompress);


}




/**


*生成二维码


*


*@paramcontent


*内容


*@paramdestPath


*存储地址


*@throwsException


*/


public
static
void
encode(Stringcontent,StringdestPath)
throws
Exception{


QRCodeUtil.encode(content,
null
,destPath,
false
);


}




/**


*生成二维码(内嵌LOGO)


*


*@paramcontent


*内容


*@paramimgPath


*LOGO地址


*@paramoutput


*输出流


*@paramneedCompress


*是否压缩LOGO


*@throwsException


*/


public
static
void
encode(Stringcontent,StringimgPath,


OutputStreamoutput,
boolean
needCompress)
throws
Exception{


BufferedImageimage=QRCodeUtil.createImage(content,imgPath,


needCompress);


ImageIO.write(image,FORMAT_NAME,output);


}




/**


*生成二维码


*


*@paramcontent


*内容


*@paramoutput


*输出流


*@throwsException


*/


public
static
void
encode(Stringcontent,OutputStreamoutput)


throws
Exception{


QRCodeUtil.encode(content,
null
,output,
false
);


}




/**


*解析二维码


*


*@paramfile


*二维码图片


*@return


*@throwsException


*/


public
static
Stringdecode(Filefile)
throws
Exception{


BufferedImageimage;


image=ImageIO.read(file);


if
(image==
null
){


return
null
;


}


BufferedImageLuminanceSourcesource=
new
BufferedImageLuminanceSource(


image);


BinaryBitmapbitmap=
new
BinaryBitmap(
new
HybridBinarizer(source));


Resultresult;


Hashtable<DecodeHintType,Object>hints=
new
Hashtable<DecodeHintType,Object>();


hints.put(DecodeHintType.CHARACTER_SET,CHARSET);


result=
new
MultiFormatReader().decode(bitmap,hints);


StringresultStr=result.getText();


return
resultStr;


}




/**


*解析二维码


*


*@parampath


*二维码图片地址


*@return


*@throwsException


*/


public
static
Stringdecode(Stringpath)
throws
Exception{


return
QRCodeUtil.decode(
new
File(path));


}




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


Stringtext=
"http://www.thinkphp.cn/extend/295.html"
;


QRCodeUtil.encode(text,
"C:/Users/Public/Pictures/SamplePictures/images.jpg"
,
"C:/Users/Public/Pictures/SamplePictures/"
,
true
);


}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: