java使用zxing生成带logo,文字说明二维码
来源:互联网 发布:xbox手柄驱动mac 编辑:程序博客网 时间:2024/06/09 20:58
二维码生成解析帮助类
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.IOException; import java.io.OutputStream; import java.util.Hashtable; import java.util.UUID; 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 String CHARSET = "utf-8"; private static final String FORMAT_NAME = "png"; // 二维码尺寸 private static final int QRCODE_SIZE = 300; // LOGO宽度 private static final int WIDTH = 60; // LOGO高度 private static final int HEIGHT = 60; /** * 检查路径 图片是否存在 * @param imgPath * @return * @throws IOException */ private static BufferedImage CheckImageExist(String imgPath) throws IOException{ File file = new File(imgPath); if (!file.exists()) { System.err.println(""+imgPath+" 该文件不存在!"); return null; } BufferedImage src = ImageIO.read(new File(imgPath)); return src; } /** * 创建 二维码所需图片 * @param content 内容 * @param imgPath Logo图片地址 * @param needCompress 是否压缩Logo大小 * @param needDescription 是否需要底部描述 * @return * @throws Exception */ private static BufferedImage createImage(String content,BufferedImage logoImage, String bottomDes, boolean needCompress) throws Exception { Hashtable hints = new Hashtable(); hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H); //容错级别 H->30% hints.put(EncodeHintType.CHARACTER_SET, CHARSET); hints.put(EncodeHintType.MARGIN, 1); BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, QRCODE_SIZE, QRCODE_SIZE, hints); int width = bitMatrix.getWidth(); int height = bitMatrix.getHeight(); int tempHeight = height; boolean needDescription=(null==bottomDes&&"".equals(bottomDes)); if (needDescription) { tempHeight += 30; } BufferedImage image = new BufferedImage(width, tempHeight, 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(null==logoImage) return image; // 插入图片 QRCodeUtil.insertImage(image, logoImage, needCompress); if(needDescription) return image; QRCodeUtil.addFontImage(image, bottomDes); return image; } /** * 添加 底部图片文字 * @param source 图片源 * @param declareText 文字本文 */ private static void addFontImage(BufferedImage source, String declareText) { BufferedImage textImage = FontImage.getImage(declareText, QRCODE_SIZE, 50); Graphics2D graph = source.createGraphics(); int width = textImage.getWidth(null); int height = textImage.getHeight(null); Image src =textImage; graph.drawImage(src, 0, QRCODE_SIZE - 20, width, height, null); graph.dispose(); } /** * 插入Logo图片 * @param source 图片操作对象 * @param imgPath Logo图片地址 * @param needCompress 是否压缩Logo大小 * @throws Exception */ private static void insertImage(BufferedImage source,BufferedImage logoImage, boolean needCompress) throws Exception { int width = logoImage.getWidth(null); int height = logoImage.getHeight(null); Image src =logoImage; if (needCompress) { if (width > WIDTH) { width = WIDTH; } if (height > HEIGHT) { height = HEIGHT; } Image image = logoImage.getScaledInstance(width, height, Image.SCALE_SMOOTH); BufferedImage tag = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); Graphics g = tag.getGraphics(); g.drawImage(image, 0, 0, null); g.dispose(); src = image; } Graphics2D graph = source.createGraphics(); int x = (QRCODE_SIZE - width) / 2; int y = (QRCODE_SIZE - height) / 2; graph.drawImage(src, x, y, width, height, null); Shape shape = new RoundRectangle2D.Float(x, y, width, width, 6, 6); graph.setStroke(new BasicStroke(3f)); graph.draw(shape); graph.dispose(); } /** * 创建 目录 * @param destPath */ public static void mkdirs(String destPath) { File file =new File(destPath); //当文件夹不存在时,mkdirs会自动创建多层目录,区别于mkdir.(mkdir如果父目录不存在则会抛出异常) if (!file.exists() && !file.isDirectory()) { file.mkdirs(); } } /** * 生成二维码 (以Logo路径) * @param content 内容 (若带http:// 会自动跳转) * @param imgPath Logo 图片地址 * @param destPath 保存二维码 地址 (没有该目录会自动创建) * @param bottomDes 底部文字描述 * @param needCompress 是否压缩Logo大小 * @throws Exception */ public static void encodeOfPath(String content, String imgPath,String bottomDes,String destPath, boolean needCompress) throws Exception { BufferedImage image = QRCodeUtil.createImage(content, CheckImageExist(imgPath), bottomDes,needCompress); mkdirs(destPath); String file = UUID.randomUUID().toString()+".png"; ImageIO.write(image, FORMAT_NAME, new File(destPath+"/"+file)); } /** * * @param content 内容 * @param logoImage 图片源 LOGO * @param bottomDes 底部描述文字 * @param destPath 保存二维码 地址 (没有该目录会自动创建) * @param needCompress 是否压缩logo * @throws Exception */ public static void encode(String content, BufferedImage logoImage,String bottomDes, String destPath, boolean needCompress) throws Exception { BufferedImage image = QRCodeUtil.createImage(content, logoImage, bottomDes, needCompress); mkdirs(destPath); String file = UUID.randomUUID().toString()+".png"; ImageIO.write(image, FORMAT_NAME, new File(destPath+"/"+file)); } /** * * @param content 内容 * @param logoImage 图片源LOGO * @param bottomDes 底部描述文字 * @param picName 图片名 * @param destPath 保存二维码 地址 (没有该目录会自动创建) * @param needCompress 是否压缩logo * @throws Exception */ public static void encode(String content, BufferedImage logoImage,String bottomDes,String picName,String destPath, boolean needCompress) throws Exception { BufferedImage image=QRCodeUtil.createImage(content, logoImage,bottomDes, needCompress); mkdirs(destPath); String file = picName+".png"; ImageIO.write(image, FORMAT_NAME, new File(destPath+"/"+file)); } /** * * @param content 内容 * @param destPath 保存二维码 地址 (没有该目录会自动创建) * @throws Exception */ public static void encode(String content, String destPath) throws Exception { QRCodeUtil.encode(content, null,null,destPath, false); } /** * * @param content 内容 * @param imgPath 图片源地址LOGO * @param bottomDes 底部描述文字 * @param output 输出流 * @param needCompress 是否压缩logo * @throws Exception */ public static void encodeOfPath(String content, String imgPath,String bottomDes, OutputStream output, boolean needCompress) throws Exception { BufferedImage image = QRCodeUtil.createImage(content, CheckImageExist(imgPath), bottomDes, needCompress); ImageIO.write(image, FORMAT_NAME, output); } /** * * @param content 内容 * @param output 输出流 * @throws Exception */ public static void encode(String content, OutputStream output) throws Exception { QRCodeUtil.encode(content, null,null, output, false); } /** * 解析 二维码 * @param file 图片文件 * @return * @throws Exception */ public static String decode(File file) throws Exception { BufferedImage image; image = ImageIO.read(file); if (image == null) { return null; } BufferedImageLuminanceSource source = new BufferedImageLuminanceSource( image); BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source)); Result result; Hashtable hints = new Hashtable(); hints.put(DecodeHintType.CHARACTER_SET, CHARSET); result = new MultiFormatReader().decode(bitmap, hints); String resultStr = result.getText(); return resultStr; } /** * 解析二维码 * @param path 图片地址 * @return * @throws Exception */ public static String decode(String path) throws Exception { return QRCodeUtil.decode(new File(path)); } }
测试生成
public static void main(String[] args) throws Exception { String text = "http://www.baidu.com"; QRCodeUtil.encodeOfPath(text, "d:/test/a1.png",null,"d:/test", true); QRCodeUtil.encode(text,"d:/test"); // 不带logo BufferedImage bi = ImageIO.read(new File("d:/test/a1.png")); QRCodeUtil.encode(text,bi,"ok","123","d:/test", true); }
注:本文转自yuki,点击跳转
阅读全文
0 0
- java使用zxing生成带logo,文字说明二维码
- java使用zxing生成带logo的二维码图片
- java使用zxing生成带logo的二维码图片
- 使用Zxing生成带logo的二维码
- 使用ZXing生成二维码(带logo)
- Java:生成带logo的二维码(ZXing)
- zxing 二维码、带logo二维码生成
- ZXing生成条形码、二维码、带logo二维码
- 使用zxing生成带logo的二维码图片
- java zxing二维码(可带logo)、条形码生成解析
- zxing生成和解析带logo二维码
- Java+zxing生成二维码(带logo和不带logo)
- 使用ZXing实现二维码的扫描和生成带logo的二维码
- 二维码及带logo二维码的生成和解析-Zxing
- ZXing生成二维码和带logo的二维码
- 使用Zxing生成带有logo的二维码
- 使用zxing生成带logo的二维码图片,自动调节logo图片相对二维码图片的大小
- ZXing生成和解析带LOGO的二维码
- java常见的面试题
- 如何获取微信公众号的“biz”?
- Android:在AndroidManifest中注册BroadcastReceiver的权限问题
- 对Webview的实现方式
- 基于live555实现的RTSPServer对底层进行性能优化的方法 标签: live555RTSPServerlive555性能EasyDarwinlive性能优化 2017-09-12 08:1
- java使用zxing生成带logo,文字说明二维码
- 2017-10-7离线赛总结
- C++对象模型之Default Constructor的建构操作
- C++对象模型之 Copy Constructor的建构操作
- RTSP推流方案调优
- Servlet 学习笔记 RequestDispatcher的调派请求
- JavaNote4
- NYOJ-117/atcoder ARC 75-E
- JS基础 对象 循环