使用Zxing打造带有数字的二维码
来源:互联网 发布:sql 判断身份证 编辑:程序博客网 时间:2024/05/01 22:29
package com.x.ExportExcel;import java.awt.Color;import java.awt.Font;import java.awt.Graphics2D;import java.awt.RenderingHints;import java.awt.image.BufferedImage;import java.io.ByteArrayOutputStream;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.util.Hashtable;import javax.imageio.ImageIO;import org.apache.poi.xssf.usermodel.XSSFClientAnchor;import org.apache.poi.xssf.usermodel.XSSFDrawing;import org.apache.poi.xssf.usermodel.XSSFSheet;import org.apache.poi.xssf.usermodel.XSSFWorkbook;import com.google.zxing.BarcodeFormat;import com.google.zxing.EncodeHintType;import com.google.zxing.MultiFormatWriter;import com.google.zxing.client.j2se.MatrixToImageWriter;import com.google.zxing.common.BitMatrix;public class Code_128Utils { private static final int WIDTH = 351; private static final int CODEHEIGHT = 55; private static final int HEIGHT = 78; private static final int FONTSIZE = 25; private static final String IMAGETYPE = "JPEG"; //该方法用来生成所需二维码 不带数字 public static void createCode(String no) throws Exception { FileOutputStream fos; fos = new FileOutputStream(new File("E:\\code\\" + no + ".jpg")); int width = WIDTH; int height = CODEHEIGHT; Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>(); hints.put(EncodeHintType.CHARACTER_SET, "utf-8"); BitMatrix m = new MultiFormatWriter().encode(no, BarcodeFormat.CODE_128, width, height, hints); MatrixToImageWriter.writeToStream(m, IMAGETYPE, fos); fos.flush(); fos.close(); createFont(no); } ///该方法用来生成二维码字体并且把二维码拼接到字体图片上 public static void createFont(String no) throws Exception { BufferedImage font = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB); BufferedImage code = ImageIO.read(new File("E:\\code\\" + no + ".jpg")); Graphics2D g = (Graphics2D) font.getGraphics(); //长宽是总的 字体加二维码的 g.clearRect(0, 0, WIDTH, HEIGHT); g.setColor(Color.WHITE); g.fillRect(0, 0, WIDTH, HEIGHT); //字体渲染 g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY); //在图片上把字写好 for (int i = 0; i < no.length(); i++) { g.setColor(Color.black); Font font_ = new Font("Consolas", 0, FONTSIZE); g.setFont(font_); g.drawString(no.charAt(i) + "", (FONTSIZE * 2 + WIDTH - no.length() * FONTSIZE) / 2 + (i - 1) * FONTSIZE, CODEHEIGHT + HEIGHT - CODEHEIGHT); } //然后把二维码加上去 g.drawImage(code, 0, 0, null); g.dispose(); //进行图片处理,防止出现模糊 int[] rgb = new int[3]; for (int i = 0; i < WIDTH; i++) { for (int j = CODEHEIGHT; j < HEIGHT; j++) { int pixel = font.getRGB(i, j); rgb[0] = (pixel & 0xff0000) >> 16; rgb[1] = (pixel & 0xff00) >> 8; rgb[2] = (pixel & 0xff); if (rgb[0] > 125 || rgb[1] > 125 || rgb[2] > 125) { font.setRGB(i, j, -1); } if (rgb[0] < 100 || rgb[1] < 100 || rgb[2] < 100) { font.setRGB(i, j, -16777216); } } } File outputfile = new File("E:\\code\\" + no + ".jpg"); ImageIO.write(font, IMAGETYPE, outputfile); }}
0 0
- 使用Zxing打造带有数字的二维码
- 使用Zxing生成带有logo的二维码
- Zxing.jar生成带有logo的二维码
- zxing二维码的使用
- 二维码Zxing的使用
- Zxing二维码的简单使用
- 用Zxing来生成带有logo图的二维码
- 二维码扫描第三库ZXing的使用
- 二维码扫描:ZBar,ZXing的使用
- 二维码扫描:ZBar,ZXing的使用
- 使用Zxing生成带logo的二维码
- 使用google的zxing生成二维码
- 使用google的zxing生成二维码
- Zxing 二维码扫描 的简单使用
- 二维码框架ZXing的简单使用
- 使用zxing实现二维码的扫描功能
- ZXing二维码编解码的使用
- Zxing 二维码扫描 的简单使用
- 输入一个整数矩阵,计算位于矩阵边缘的元素之和
- input disabled不能提交表单
- 程序员学习 jdk5新特性
- Tiled Map Editor创建地图
- sdut 2604 Thrall’s Dream (强联通分量+缩点)
- 使用Zxing打造带有数字的二维码
- 第十六章 非阻塞connect初识
- strcmp函数实现及分析
- JVM致命错误日志(hs_err_pid.log)解读
- 【Android开发】高级组件-图像切换器
- soj 2222 01背包变形
- HDU 4291 A Short problem(矩阵快速幂+循环节)
- Android中Handler.removeCallbacksAndMessages(null)的作用
- Mysql索引的创建、删除