一、通过Zxing生成与读取:
生成二维码:
int width=300;
int height=300;
String format="png";
String content="www.link.com";
HashMap hint=new HashMap();
hint.put(EncodeHintType.CHARACTER_SET, "utf-8");
hint.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);
hint.put(EncodeHintType.MARGIN, 2);
BitMatrix bitMatrix=new MultiFormatWriter().
encode(content, BarcodeFormat.QR_CODE, width, height,hint);
Path file=new File("D:/img.png").toPath();
MatrixToImageWriter.writeToPath(bitMatrix, format, file);
注:此处生成扫描是"www.link.com",添加为链接时转到链接
读取二维码:
MultiFormatReader formatReader=new MultiFormatReader();
File file=new File("D:/img.png");
BufferedImage image=ImageIO.read(file);
BinaryBitmap binaryBitmap=new BinaryBitmap(
new HybridBinarizer(new BufferedImageLuminanceSource(image)));
HashMap hint=new HashMap();
hint.put(EncodeHintType.CHARACTER_SET, "utf-8");
Result result=formatReader.decode(binaryBitmap,hint);
System.out.println(result.toString());
System.out.println(result.getBarcodeFormat());
System.out.println(result.getText());
二、QRcode生成二维码:
生成二维码:
Qrcode x=new Qrcode();
x.setQrcodeErrorCorrect('M');
x.setQrcodeEncodeMode('B');
x.setQrcodeVersion(7);
String qrData="www.imooc.com";
int width=67+12*(7-1);
int height=67+12*(7-1);
int pixoff=2;
BufferedImage bufferedImage=
new BufferedImage
(300, 300, BufferedImage.TYPE_INT_RGB);
Graphics2D gs=bufferedImage.createGraphics();
gs.setBackground(Color.WHITE);
gs.setColor(Color.BLACK);
gs.clearRect(0, 0, width, height);
byte[] d=qrData.getBytes("gb2312");
if(d.length>0&&d.length<120){
boolean[][] s=x.calQrcode(d);
for(int i=0;i<s.length;i++){
for(int j=0;j<s.length;j++){
if(s[j]){
gs.fillRect(j*3+pixoff, i*3+pixoff, 3, 3);
}
}
}
}
gs.dispose();
bufferedImage.flush();
ImageIO.write(bufferedImage, "png", new File("D:/qrcode.png"));
读取二维码:
public class ReadQRcode {
public static void main(String[] args) throws IOException {
File file=new File("D:/qrcode.png");
BufferedImage bufferedImage=ImageIO.read(file);
QRCodeDecoder codeDecoder=new QRCodeDecoder();
String result=
new String(codeDecoder.decode(
new QRcodeImage(bufferedImage)),"gb2312");
System.out.println(result);
}
}
class QRcodeImage implements QRCodeImage{
BufferedImage bufferedImage;
public QRcodeImage(BufferedImage bufferedImage) {
this.bufferedImage=bufferedImage;
}
@Override
public int getHeight() {
return bufferedImage.getHeight();
}
@Override
public int getPixel(int arg0, int arg1) {
return bufferedImage.getRGB(arg0, arg1);
}
@Override
public int getWidth() {
return bufferedImage.getWidth();
}}
三、jquery.qrcode.min.js生成二维码:
在jsp页面引入:
<script type="text/javascript" src="js/jquery.min.js"></script>
<script type="text/javascript"
src="js/jquery.qrcode.min.js"></script>
生成二维码:<br>
<div id="qrcode"></div>
<script type="text/javascript">
jQuery('#qrcode').qrcode("www.imooc.com");
</script>
---------------------
作者:linkingfei
来源:CSDN
原文:https://blog.csdn.net/linkingfei/article/details/83099363
版权声明:本文为博主原创文章,转载请附上博文链接!
二维码生成与读取
最新推荐文章于 2022-07-21 17:22:19 发布