ZXing是开源的,是一种用Java实现的各种形式的1D/2D条形码图像处理库,其中包含可连接到其他语言的端口。 Zxing可以使用手机内置摄像头扫描和解码条形码。 本章介绍ZXing的代码生成和扫描二维码。
依赖
将pom.xml添加到Java项目中:从属关系
groupid com.Google.zxing/groupid
影响标识/影响标识
版本$ {版本} /版本
/从属关系
从属关系
groupid com.Google.zxing/groupid
影响Javase /影响id
版本$ {版本} /版本
/dependency当前的最新版本为3.4.1,如果是Java开发的安卓项目,则导入安卓-酷睿。
生成二维码
普通二维码//二维码内容的生成字符串文本=' https://Engr-z.com ';
//二维码的大小
int width=500,height=500;
//二维码输出文件
文件文件=新文件((/家庭/能源- z/QR代码. png );
qrcodewriterwriter=newqrcodewriter (;
bitmatrixm=writer.encode (文本,条形码格式. QR _代码,宽度,高度);
matrixtoimagewriter.write to path (m,' png ',文件到路径); 内容较多时,需要增大二维码大小。 尺寸越小内容越多,二维码图形越复杂越难识别。
生成徽标二维码//二维码内容
字符串文本=' https://Engr-z.com ';
//二维码的大小
int width=500,height=500;
//二维码参数
代码样式=新代码样式(;
syle.set width (宽度);
style.setheight (海);
MapEncodeHintType,对象hints=新散列映射(;
//内容编码格式
hints.put (编码类型.字符集,' UTF-8 ';
//指定错误修正等级
hints.put (编码器类型.错误校正,错误校正. h );
//设置二维码边缘的空间,而不是负数
hints.put (编码类型.标记,1 );
//生成二维码图像
qrcodewriterwriter=newqrcodewriter (;
bitmatrixbm=writer.encode (文本,条形码格式. QR _代码,样式.获取),样式.获取,hints );
int margin=style.getMargin (;
int tempM=margin*2;
int [ ] rec=BM.getenclosingrectangle (;//获取二维码模式的属性
int RES width=rec [2]时间段;
int resheight=rec [3]时间段;
bitmatrixresmatrix=newbitmatrix (RES width,resHeight ); //根据自定义边框生成新的位矩阵
RES矩阵. clear (;
for (整数=马格林; I RES宽度标记; I ()//循环,在新的位矩阵中绘制二维码模式
for(intj=Margin; j resHeight - margin; j ) {2}
if(BM.get(I-marginrec[0],j - margin rec[1] ) )
RES矩阵集(I,j );
}
}
}
BM=RES矩阵;
int w=bm.getWidth (;
int h=bm.getHeight (;
bufferedimageqrcodebuffimg=newbufferedima
ge(w, h, BufferedImage.TYPE_INT_RGB); // 开始利用二维码数据创建Bitmap图片 for (int x = 0; x < w; x++) { for (int y = 0; y < h; y++) { qrcodeBuffImg.setRGB(x, y, bm.get(x, y) ? style.getCodeColor() : style.getBackgroundColor()); } } /** * 读取Logo图片 */ File logoFile = new File("/home/engr-z/logo.png"); BufferedImage logo = ImageIO.read(logoFile); /** * 设置logo的大小,设置为二维码图片的20% */ int widthLogo = logo.getWidth(null) > qrcodeBuffImg.getWidth() * 3 / 10 ? (qrcodeBuffImg.getWidth() * 3 / 10) : logo.getWidth(null), heightLogo = logo.getHeight(null) > qrcodeBuffImg.getHeight() * 3 / 10 ? (qrcodeBuffImg.getHeight() * 3 / 10) : logo.getWidth(null); /** * logo在二维码的位置 */ int x = (qrcodeBuffImg.getWidth() - widthLogo) / 2; int y = (qrcodeBuffImg.getHeight() - heightLogo) / 2; Graphics2D qrcodeOutg = qrcodeBuffImg.createGraphics(); // 把logo写到二维码图片中间 qrcodeOutg.drawImage(logo, x, y, widthLogo, heightLogo, null); qrcodeOutg.dispose(); qrcodeBuffImg.flush(); // 新的图片,把带logo的二维码下面加上文字 String desc = "https://engr-z.com"; int textHeight = 26; int textMargin = 10; BufferedImage outImage = new BufferedImage(qrcodeBuffImg.getWidth(), qrcodeBuffImg.getHeight() + textHeight + (textMargin * 2), BufferedImage.TYPE_4BYTE_ABGR); Graphics2D outg = outImage.createGraphics(); // 画二维码到新的面板 outg.drawImage(qrcodeBuffImg, 0, 0, qrcodeBuffImg.getWidth(), qrcodeBuffImg.getHeight(), null); outg.setFont(new Font("宋体", Font.BOLD, 26)); // 字体、字型、字号 int strWidth = outg.getFontMetrics().stringWidth(desc); outg.setColor(Color.BLACK); outg.drawString(desc, (outImage.getWidth() - strWidth) / 2, outImage.getHeight() - textMargin); outg.dispose(); // 二维码输出文件 File file = new File("/home/engr-z/qrcode.png"); ImageIO.write(outImage, "png", file);CodeStyle是我封装的类,用来设置二维码样式:
/** * @author Engr-Z * @since 2020/12/18 */ @Data public class CodeStyle { /** * 背景颜色,如:0xFFFFFFFF */ private int backgroundColor = 0xFFFFFFFF; /** * 码颜色,如:0xFF000000 */ private int codeColor = 0xFF000000; /** * 二维码宽,px */ private int width; /** * 二维码高,px */ private int height; /** * 边框大小 */ private int margin = 5; }以下是我执行生成的二维码:
读取二维码
File qrcodeFile = new File("D:/qrcode.png"); BufferedImage qrcodeImg = ImageIO.read(qrcodeFile); MultiFormatReader formatReader = new MultiFormatReader(); //读取指定的二维码文件 BinaryBitmap binaryBitmap= new BinaryBitmap(new HybridBinarizer(new BufferedImageLuminanceSource(qrcodeImg))); //定义二维码参数 Map<DecodeHintType, Object> hints= new HashMap<>(); hints.put(DecodeHintType.CHARACTER_SET, "utf-8"); // 读取 Result result = formatReader.decode(binaryBitmap, hints); log.info("格式类型:{}", result.getBarcodeFormat()); log.info("二维码内容:{}", result.getText());链接
ZXing GitHub:https://github.com/zxing/zxing
除非注明,否则均为"攻城狮·正"原创文章,转载请注明出处。
本文链接:https://engr-z.com/292.html