直接上代码,以后有时间再添加说明
二维码的基本信息,包括要二维码内包含的文字,嵌入的图片,加到二维码下边的文字
/** * */ package com.tong.util.code; import java.io.File; /** * @author zxm * */ public class CodeModel { private String contents; private int width = 400; private int height = 400; private String format = "gif"; private String character_set = "utf-8"; private int fontSize = 12; private File logoFile; private float logoRatio = 0.20f; private String desc; private int whiteWidth;//白边的宽度 private int[] bottomStart;//二维码最下边的开始坐标 private int[] bottomEnd;//二维码最下边的结束坐标 public String getContents() { return contents; } public void setContents(String contents) { this.contents = contents; } public int getWidth() { return width; } public void setWidth(int width) { this.width = width; } public int getHeight() { return height; } public void setHeight(int height) { this.height = height; } public String getFormat() { return format; } public void setFormat(String format) { this.format = format; } public String getCharacter_set() { return character_set; } public void setCharacter_set(String character_set) { this.character_set = character_set; } public int getFontSize() { return fontSize; } public void setFontSize(int fontSize) { this.fontSize = fontSize; } public File getLogoFile() { return logoFile; } public void setLogoFile(File logoFile) { this.logoFile = logoFile; } public float getLogoRatio() { return logoRatio; } public void setLogoRatio(float logoRatio) { this.logoRatio = logoRatio; } public String getDesc() { return desc; } public void setDesc(String desc) { this.desc = desc; } public int getWhiteWidth() { return whiteWidth; } public void setWhiteWidth(int whiteWidth) { this.whiteWidth = whiteWidth; } public int[] getBottomStart() { return bottomStart; } public void setBottomStart(int[] bottomStart) { this.bottomStart = bottomStart; } public int[] getBottomEnd() { return bottomEnd; } public void setBottomEnd(int[] bottomEnd) { this.bottomEnd = bottomEnd; } }
生成二维码
/** * */ package com.tong.util.code; import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.awt.image.BufferedImage; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; import java.util.HashMap; import java.util.Map; import javax.imageio.ImageIO; import org.springframework.util.StringUtils; import com.google.zxing.BarcodeFormat; import com.google.zxing.EncodeHintType; import com.google.zxing.MultiFormatWriter; import com.google.zxing.WriterException; import com.google.zxing.common.BitMatrix; import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel; /** * @author zxm * */ public class CodeCreator { private static int BLACK = 0x000000; private static int WHITE = 0xFFFFFF; private BufferedImage createCodeImage(CodeModel info){ String contents = info.getContents(); int width = info.getWidth(); int height = info.getHeight(); Map<EncodeHintType, Object> hint = new HashMap<>(); hint.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H); hint.put(EncodeHintType.CHARACTER_SET, info.getCharacter_set()); hint.put(EncodeHintType.MARGIN, 0); MultiFormatWriter writer = new MultiFormatWriter(); BufferedImage img = null; try { BitMatrix bm = writer.encode(contents, BarcodeFormat.QR_CODE, width, height, hint); int[] locationTopLeft = bm.getTopLeftOnBit(); int[] locationBottomRight = bm.getBottomRightOnBit(); info.setBottomStart(new int[]{locationTopLeft[0], locationBottomRight[1]}); info.setBottomEnd(locationBottomRight); int w = bm.getWidth(); int h = bm.getHeight(); img = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB); for(int x=0;x<w;x++){ for(int y=0;y<h;y++){ img.setRGB(x, y, bm.get(x, y) ? BLACK : WHITE); } } } catch (WriterException e) { e.printStackTrace(); } return img; } public void createCodeImage(CodeModel info, OutputStream output){ BufferedImage bm = createCodeImage(info); File logoFile = info.getLogoFile(); if(logoFile!=null && logoFile.exists()){ try{ BufferedImage logoImg = ImageIO.read(logoFile); int logoWidth = logoImg.getWidth(); int logoHeight = logoImg.getHeight(); int width = bm.getWidth(); int height = bm.getHeight(); float ratio = info.getLogoRatio(); if(ratio>0){ logoWidth = logoWidth>width*ratio ? (int)(width*ratio) : logoWidth; logoHeight = logoHeight>height*ratio ? (int)(height*ratio) : logoHeight; } int x = (width-logoWidth)/2; int y = (height-logoHeight)/2; Graphics g = bm.getGraphics(); g.drawImage(logoImg, x, y, logoWidth, logoHeight, null); String desc = info.getDesc(); //int whiteWidth = 8; if(!StringUtils.isEmpty(desc)){ int whiteWidth = info.getHeight()-info.getBottomEnd()[1]; // width = info.getBottomEnd()[0]-info.getBottomStart()[0]; // height = info.getBottomEnd()[1]+1; Font font = new Font("黑体", Font.BOLD, info.getFontSize()); int fontHeight = g.getFontMetrics(font).getHeight(); //计算需要多少行 int lineNum = 1; int currentLineLen = 0; for(int i=0;i<desc.length();i++){ char c = desc.charAt(i); int charWidth = g.getFontMetrics(font).charWidth(c); if(currentLineLen+charWidth>width){ lineNum++; currentLineLen = 0; continue; } currentLineLen += charWidth; } int totalFontHeight = fontHeight*lineNum; int wordTopMargin = 4; BufferedImage bm1 = new BufferedImage(width, height+totalFontHeight+wordTopMargin-whiteWidth, BufferedImage.TYPE_INT_RGB); Graphics g1 = bm1.getGraphics(); if(totalFontHeight+wordTopMargin-whiteWidth>0){ g1.setColor(Color.WHITE); g1.fillRect(0, height, width, totalFontHeight+wordTopMargin-whiteWidth); } g1.setColor(new Color(BLACK)); g1.setFont(font); g1.drawImage(bm, 0, 0, null); width = info.getBottomEnd()[0]-info.getBottomStart()[0]; height = info.getBottomEnd()[1]+1; currentLineLen = 0; int currentLineIndex = 0; int baseLo = g1.getFontMetrics().getAscent(); for(int i=0;i<desc.length();i++){ String c = desc.substring(i, i+1); int charWidth = g.getFontMetrics(font).stringWidth(c); if(currentLineLen+charWidth>width){ currentLineIndex++; currentLineLen = 0; g1.drawString(c, currentLineLen + whiteWidth, height+baseLo+fontHeight*(currentLineIndex)+wordTopMargin); currentLineLen = charWidth; continue; } g1.drawString(c, currentLineLen+whiteWidth, height+baseLo+fontHeight*(currentLineIndex) + wordTopMargin); currentLineLen += charWidth; } g1.dispose(); bm = bm1; } }catch(Exception e){ e.printStackTrace(); } } try{ ImageIO.write(bm, StringUtils.isEmpty(info.getFormat()) ? info.getFormat() : info.getFormat(), output); }catch(Exception e){ e.printStackTrace(); } } public void createCodeImage(CodeModel info, File file){ File parent = file.getParentFile(); if(!parent.exists())parent.mkdirs(); OutputStream output = null; try{ output = new BufferedOutputStream(new FileOutputStream(file)); createCodeImage(info, output); output.flush(); }catch(Exception e){ e.printStackTrace(); }finally{ try { output.close(); } catch (IOException e) { e.printStackTrace(); } } } public void createCodeImage(CodeModel info, String filePath){ createCodeImage(info, new File(filePath)); } }
二维码读取
/** * */ package com.tong.util.code; import java.awt.image.BufferedImage; import java.io.InputStream; import java.util.HashMap; import java.util.Map; import javax.imageio.ImageIO; import com.google.zxing.BarcodeFormat; import com.google.zxing.BinaryBitmap; import com.google.zxing.DecodeHintType; import com.google.zxing.LuminanceSource; import com.google.zxing.RGBLuminanceSource; import com.google.zxing.Result; import com.google.zxing.common.HybridBinarizer; import com.google.zxing.qrcode.QRCodeReader; /** * @author zxm * */ public class CodeDecoder { public String decode(InputStream input){ Map<DecodeHintType, Object> hint = new HashMap<DecodeHintType, Object>(); hint.put(DecodeHintType.POSSIBLE_FORMATS, BarcodeFormat.QR_CODE); String result = ""; try{ BufferedImage img = ImageIO.read(input); int[] pixels = img.getRGB(0, 0, img.getWidth(), img.getHeight(), null, 0, img.getWidth()); LuminanceSource source = new RGBLuminanceSource(img.getWidth(), img.getHeight(), pixels); BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source)); QRCodeReader reader = new QRCodeReader(); Result r = reader.decode(bitmap, hint); result = r.getText(); }catch(Exception e){ result="读取错误"; } return result; } }
测试代码
/** * */ package com.tong.util; import java.awt.image.BufferedImage; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.InputStream; import java.io.OutputStream; import java.util.HashMap; import java.util.Map; import javax.imageio.ImageIO; import com.google.zxing.BarcodeFormat; import com.google.zxing.EncodeHintType; import com.google.zxing.MultiFormatWriter; import com.google.zxing.WriterException; import com.google.zxing.common.BitMatrix; import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel; import com.tong.util.code.CodeCreator; import com.tong.util.code.CodeDecoder; import com.tong.util.code.CodeModel; /** * @author zxm * */ public class QRCodeMain{ private static void encode() { CodeCreator creator = new CodeCreator(); CodeModel info = new CodeModel(); info.setWidth(400); info.setHeight(400); info.setFontSize(24); info.setContents("<a href='http://www.sohu.com'>人生就是拼搏</a>"); //info.setContents("http://www.sohu.com"); info.setContents("万水千山只等闲\n小荷才楼尖尖角\n谁家新燕啄春泥\n无边光景一时新\n万水千山只等闲\n小荷才楼尖尖角\n谁家新燕啄春泥\n无边光景一时新\n万水千山只等闲\n小荷才楼尖尖角\n谁家新燕啄春泥\n无边光景一时新\n万水千山只等闲\n小荷才楼尖尖角\n谁家新燕啄春泥\n无边光景一时新\n万水千山只等闲\n小荷才楼尖尖角\n谁家新燕啄春泥\n无边光景一时新\n万水千山只等闲\n小荷才楼尖尖角\n谁家新燕啄春泥\n无边光景一时新\n万水千山只等闲\n小荷才楼尖尖角\n谁家新燕啄春泥\n无边光景一时新\n万水千山只等闲\n小荷才楼尖尖角\n谁家新燕啄春泥\n无边光景一时新\n万水千山只等闲\n小荷才楼尖尖角\n谁家新燕啄春泥\n无边光景一时新\n万水千山只等闲\n小荷才楼尖尖角\n谁家新燕啄春泥\n无边光景一时新\n万水千山只等闲\n小荷才楼尖尖角\n谁家新燕啄春泥\n无边光景一时新\n万水千山只等闲\n小荷才楼尖尖角\n谁家新燕啄春泥\n无边光景一时新\n万水千山只等闲\n小荷才楼尖尖角\n谁家新燕啄春泥\n无边光景一时新\n万水千山只等闲\n小荷才楼尖尖角\n谁家新燕啄春泥\n无边光景一时新"); info.setLogoFile(new File("D:/workspaces/workspace1/tong3/WebContent/img/girl3.jpg")); info.setDesc("你怎么会知道我的苦,我是一页孤舟,漂流在尘世中asdfsaf33333abCD1234567890"); //info.setLogoDesc("一叶浮萍归大海,adsasfbhtjg人生何处不相逢"); //info.setLogoDesc("一叶浮萍"); creator.createCodeImage(info, "D:/2Dcode/dest." + info.getFormat()); } static public void decode(InputStream input){ CodeDecoder decoder = new CodeDecoder(); String result = decoder.decode(input); System.out.println(result); } public static void main(String[] args) throws Exception{ encode(); //decode(new FileInputStream(new File("D:/2Dcode/dest.gif"))); } }
相关推荐
这是Zxing的源码,没有打包成jar利于学习使用。...使用时注意path的默认地址,Main类中是生成二维码的代码,GetMain类中是读取二维码的代码。 eclipse中Ctrl+Shift+R快速查找类 希望对你有所帮助。
该工具类目前比较简单,只是实现了普通二维码、logo二维码、彩色二维码和二维码条形码解析几个功能。 根据此工具类可扩展生成LOGO+文字的二维码,暂时不需要没有封装。
java 使用zxing生成二维码(带logo和文字说明)已经封装 使用起来很方便 下载下来直接调用传入参数就可以
java 使用zxing生成二维码(带logo和文字说明)已经封装 使用起来很方便 下载下来直接调用传入参数就可以
详细教程:https://blog.csdn.net/wysnxzm/article/details/83305036
java实现创建图片添加图片、文字水印及其自定义位置;以及实现二维码生成与解析,zxing 依赖所需jar包
java实现条形码二维码生成含文字标签信息
java 生成二维码,二维码中间可以夹杂logo图片,两张图片合并成一张或添加水印文字,添加jar包 <!-- google生成二维码 --> <groupId>com.google.zxing</groupId> <artifactId>core <version>3.1.0 ...
本demo基于zxing 能识别二维码数据 并将二维码结果进行展示,后续可自己处理。同样,做了二维码生成处理。可以简单的将输入的文字转换成二维码,并且可以添加带logo的二维码,可直接使用
无生成二维码、主动识别二维码功能 介于以上问题,于是该库就华丽的诞生 修复竖屏不能正向扫描条形码 可定制扫描框与扫描线样式 加入创建二维码、识别图片中的二维码功能 二、效果图 三、或手机扫描下面二维码 四、...
01如何批量为图片添加文字水印02如何批量为图片添加图片水印. 03解决对图片格式进行批量转换的问题. 04按照京东运营要求将图片批量处理为指定大小..05根据图片显示其轮廓... 0图片的浮雕效果显示. 中睡科年日日明.....
基于开源 http://code.google.com/p/zxing/ V4.0 修改优化编译,...名片交换:用这软件生成某联系人的二维码显示在屏幕(或者发送邮件、彩信),让对方扫描即可添加到通讯录; 文字、网址交换; 根据条码搜索产品信息;
使用java和Qrcode_A.jar+qrcode_B.jar+zxing.jar实现的二维码生成器和二维码解析器,使用了两种方法生成多种形式的二维码,支持添加中央logo和底部文字提示,可用于后台生成和解析使用等用途,欢迎学习交流。
● 支持生成二维码操作 ● 支持控制闪光灯开关 ● 集成默认的二维码扫描页面 在具体介绍该扫描库之前我们先看一下其具体的使用方式,看看是不是几行代码就可以集成二维码扫描的功能。 ● 在module的build.gradle...
2、VB2008Print与EasyReport添加了二维码打印的功能,该功能使用开源的Zxing.NET实现; 3、添加了DrawDGVEx函数,一方面可以以DGVprint对象中的参数在VB2008Print组件中打印,另一方面提供了并排打印多个表格的重载...