zookeeper python接口实例详解
269
2023-01-06
通过java生成读取二维码详解
前言
在开始之前,我们需要先引入二维码生成和读取的jar包,这里我用的是qrcodejar
新建一个java工程,在工程中添加一个lib目录,把两个Jar包放到lib目录下,不要忘了add as libiary
在build.gradle中添加配置
compile fileTree(dir:'libhttp://',include:['*.jar'])
(这里我用的是gradle)
准备工作结束,马上开始
生成二维码
public class QRcodeTest {
public static void main(String[] args) throws Exception{
Qrcode qrcode = new Qrcode();
qrcode.setQrcodeErrorCorrect('M');//纠错等级(分为L、M、H三个等级)
qrcode.setQrcodeEncodeMode('B');//N代表数字,A代表a-Z,B代表其它字符
qrcode.setQrcodeVersion(10);//版本
//生成二维码中要存储的信息
String qrData ="http://baidu.com";
//设置一下二维码的像素
int width = 67+12*(10-1);
int height = 67+12*(10-1);
BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
//绘图
Graphics2D gs = bufferedImage.createGraphics();
gs.setBackground(Color.WHITE);
gs.setColor(Color.BLACK);
gs.clearRect(0, 0, width, height);//清除下画板内容
//设置下偏移量,如果不加偏移量,有时会导致出错。
int pixoff = 2;
byte[] d = qrData.getBytes("gb2312");
if(d.length > 0 && d.length <120){
boolean[][] s = qrcode.calQrcode(d);
for(int i=0;i for(int j=0;j if(s[j][i]){ gs.fillRect(j*3+pixoff, i*3+pixoff, 3, 3); } } } } gs.dispose(); bufferedImage.flush(); //设置要存储的目录(这里存储在本地) ImageIO.write(bufferedImage, "png", new File("E:/code/qrcodebd.png")); } } 这样就可以生成一张二维码图片,扫描二维码,会跳转到百度首页。 二维码图片 public class MyQrCodeImage implements QRCobsqKHpakdeImage { BufferedImage buffebsqKHpakredImage; public MyQrCodeImage(BufferedImage bufferedImage) { this.bufferedImage = bufferedImage; } public int getHeight() { return bufferedImage.getHeight(); } public int getPixel(int arg0, int arg1) { return bufferedImage.getRGB(arg0, arg1); } public int getWidth() { return bufferedImage.getWidth(); } } 读取二维码 public class ReadQrCode { public static void main(String[] args) throws IOException { File file = new File("E:/code/qrcodebd.png"); BufferedImage bufferedImage = ImageIO.read(file); Qrcode qrcode = new Qrcode(); QRCodeDecoder codeDecoder = new QRCodeDecoder(); String result = new String(codeDecoder.decode(new MyQrCodeImage(bufferedImage)),"gb2312"); System.out.println(result); } } 读取二维码的内容为http://baidu.com
for(int j=0;j if(s[j][i]){ gs.fillRect(j*3+pixoff, i*3+pixoff, 3, 3); } } } } gs.dispose(); bufferedImage.flush(); //设置要存储的目录(这里存储在本地) ImageIO.write(bufferedImage, "png", new File("E:/code/qrcodebd.png")); } } 这样就可以生成一张二维码图片,扫描二维码,会跳转到百度首页。 二维码图片 public class MyQrCodeImage implements QRCobsqKHpakdeImage { BufferedImage buffebsqKHpakredImage; public MyQrCodeImage(BufferedImage bufferedImage) { this.bufferedImage = bufferedImage; } public int getHeight() { return bufferedImage.getHeight(); } public int getPixel(int arg0, int arg1) { return bufferedImage.getRGB(arg0, arg1); } public int getWidth() { return bufferedImage.getWidth(); } } 读取二维码 public class ReadQrCode { public static void main(String[] args) throws IOException { File file = new File("E:/code/qrcodebd.png"); BufferedImage bufferedImage = ImageIO.read(file); Qrcode qrcode = new Qrcode(); QRCodeDecoder codeDecoder = new QRCodeDecoder(); String result = new String(codeDecoder.decode(new MyQrCodeImage(bufferedImage)),"gb2312"); System.out.println(result); } } 读取二维码的内容为http://baidu.com
if(s[j][i]){
gs.fillRect(j*3+pixoff, i*3+pixoff, 3, 3);
}
}
}
}
gs.dispose();
bufferedImage.flush();
//设置要存储的目录(这里存储在本地)
ImageIO.write(bufferedImage, "png", new File("E:/code/qrcodebd.png"));
}
}
这样就可以生成一张二维码图片,扫描二维码,会跳转到百度首页。
二维码图片
public class MyQrCodeImage implements QRCobsqKHpakdeImage {
BufferedImage buffebsqKHpakredImage;
public MyQrCodeImage(BufferedImage bufferedImage) {
this.bufferedImage = bufferedImage;
}
public int getHeight() {
return bufferedImage.getHeight();
}
public int getPixel(int arg0, int arg1) {
return bufferedImage.getRGB(arg0, arg1);
}
public int getWidth() {
return bufferedImage.getWidth();
}
}
读取二维码
public class ReadQrCode {
public static void main(String[] args) throws IOException {
File file = new File("E:/code/qrcodebd.png");
BufferedImage bufferedImage = ImageIO.read(file);
Qrcode qrcode = new Qrcode();
QRCodeDecoder codeDecoder = new QRCodeDecoder();
String result = new String(codeDecoder.decode(new MyQrCodeImage(bufferedImage)),"gb2312");
System.out.println(result);
}
}
读取二维码的内容为http://baidu.com
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~