package zjpt.zwsm.utils; import com.google.zxing.*; import com.google.zxing.client.j2se.BufferedImageLuminanceSource; import com.google.zxing.common.BitMatrix; import com.google.zxing.common.HybridBinarizer; import javax.imageio.ImageIO; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import java.util.Hashtable; public class QrBarCode { private static final String CODE = "utf-8"; private static final int BLACK = 0xff000000; private static final int WHITE = 0xFFFFFFFF; /** * 生成RQ二维码 * * @param str 内容 * @param height 高度(px) * @author wuhongbo */ public static BufferedImage getRQ(String str, Integer height) { if (height == null || height < 100) { height = 200; } try { // 文字编码 Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>(); hints.put(EncodeHintType.CHARACTER_SET, CODE); BitMatrix bitMatrix = new MultiFormatWriter().encode(str, BarcodeFormat.QR_CODE, height, height, hints); return toBufferedImage(bitMatrix); // 输出方式 // 网页 // ImageIO.write(image, "png", response.getOutputStream()); // 文件 // ImageIO.write(image, "png", file); } catch (Exception e) { e.printStackTrace(); } return null; } /** * 生成一维码(128) * * @param str * @param width * @param height * @return * @author wuhongbo */ public static BufferedImage getBarcode(String str, Integer width, Integer height) { if (width == null || width < 200) { width = 200; } if (height == null || height < 50) { height = 50; } try { // 文字编码 Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>(); hints.put(EncodeHintType.CHARACTER_SET, CODE); BitMatrix bitMatrix = new MultiFormatWriter().encode(str, BarcodeFormat.CODE_128, width, height, hints); return toBufferedImage(bitMatrix); } catch (Exception e) { e.printStackTrace(); } return null; } /** * 生成二维码,写到文件中 * * @param str * @param height * @param file * @throws IOException * @author wuhongbo */ public static void getQRWriteFile(String str, Integer height, File file) throws IOException { BufferedImage image = getRQ(str, height); ImageIO.write(image, "png", file); } /** * 生成一维码,写到文件中 * * @param str * @param height * @param file * @throws IOException * @author wuhongbo */ public static void getBarcodeWriteFile(String str, Integer width, Integer height, File file) throws IOException { BufferedImage image = getBarcode(str, width, height); ImageIO.write(image, "png", file); } /** * 转换成图片 * * @param matrix * @return * @author wuhongbo */ private static BufferedImage toBufferedImage(BitMatrix matrix) { int width = matrix.getWidth(); int height = matrix.getHeight(); BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { image.setRGB(x, y, matrix.get(x, y) ? BLACK : WHITE); } } return image; } /** * 解码(二维、一维均可) */ public static String read(File file) { BufferedImage image; try { if (file == null || file.exists() == false) { throw new Exception(" File not found:" + file.getPath()); } image = ImageIO.read(file); LuminanceSource source = new BufferedImageLuminanceSource(image); BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source)); Result result; // 解码设置编码方式为:utf-8, Hashtable hints = new Hashtable(); hints.put(DecodeHintType.CHARACTER_SET, "utf-8"); result = new MultiFormatReader().decode(bitmap, hints); return result.getText(); } catch (Exception e) { e.printStackTrace(); } return null; } public static void main(String[] args) throws Exception { File file = new File("D://test.png"); // 生成二维码 //QRUtil.getQRWriteFile("http://www.baid.com", 200, file); // 生成一维码 QrBarCode.getBarcodeWriteFile("6940577300018", null, null, file); System.out.println("-----成生成功----"); System.out.println(); // 解析生成的图片 String jxr = QrBarCode.read(file); System.out.println("-----解析成功----"); System.out.println(jxr); } }
相关推荐
生成一维码二维码并调用打印机打印
google zxing读取、生成一维码、二维码图片 google zxing java 一维码 条形码 二维码 生成 读取
spring boot整合二维码和条形码
java生成二维码和条形码(一维码),每个工具类自带main方法,改一下包名和生成路径即可测试。本人使用SpringBoot开发,打包上传的资源
Java调用Jaspersoft生成PDF中带有条形码,所需要的barcode4j-2.1.jar、batik-bridge-1.12.jar等必须使用包
Java实现生成和解析二维码...二维码又称二维条码,常见的二维码为QR Code,QR全称Quick Response,是一个近几年来移动设备上超流行的一种编码方式,它比传统的Bar Code条形码能存更多的信息,也能表示更多的数据类型。
1、C# 斑马(Zebra)Zpl条码、二维码打印测试代码源码; 2、实例采用打印图片方式打印二维码(包含中文文字)。 2、包含web端采用WebSocket调用本地服务zpl打印(含Web端与服务端源码); 3、此源码已经测试,请使用...
本文是利用ZXing.Net在WinForm中生成条形码,二维码的小例子,仅供学习分享使用,如有不足之处,还请指正。 什么是ZXing.Net? ZXing是一个开放源码的,用Java实现的多种格式的1D/2D条码图像处理库,它包含了联系到...
条形码(一维码)和二维码 解决方案 barcode4j、zxing barcode4j开源Java条形码生成库,j适合生成条形码,不适合生成二维码。 zxing是由google开源的1D/2D编解码类库,文档比较全面,能够生成条形码也能够生成二维码...
使用java swing 做一个简单的二维码生成软件
4.译码可靠性高:它比普通条码译码错误率百万分之二要低得多,误码率不超过千万分之一。 5.可引入加密措施:保密性、防伪性好。 6.成本低,易制作,持久耐用。 7.条码符号形状、尺寸大小比例可变。 8.二维条码...
支持多种格式的一维、二维条形码图片处理开发库。
zxing是一个开放源码的,用java实现的多种格式的1D/2D条码图像处理库,它包含了联系到其他语言的接口。 zxing可以实现使用手机的内置的摄像头完成条形码和二维码的扫描与解码。 zxing可以实现条形码和二维码的编码与...
使用Java开发条形码实现生成一维码和二维码的核心jar包
本文是利用ZXing.Net在WinForm中生成条形码,二维码的小例子,仅供学习分享使用,如有不足之处,还请指正。 什么是ZXing.Net? ZXing是一个开放源码的,用Java实现的多种格式的1D/2D条码图像处理库,它包含了联系到...
ZXing(发音为“斑马线”)是一个开放源码的,多种格式的1D/2D条码图像处理库,用Java实现的,与其他语言的端口。我们的重点是使用内置的摄像头在手机上的条形码进行扫描及解码的设备,不与服务器通信。然而,该项目...
java生成二维码,生成二维码图片,也可生成一维的条形码
java二维码生成与解析源码内含核心包文件QRCode.jar。 二维码的特点: 1. 高密度编码,信息容量大 可容纳多达1850个大写字母或2710个数字或1108个字节,或500多个汉字,比普通条码信息容量约高几十倍。 2. 编码...
基于JAVA的生成一维条码、二维码的示例代码,使用方式参考压缩包中的doc目录文档
ZXing,一个支持在图像中解码和生成条形码(如二维码、PDF 417、EAN、UPC、Aztec、Data Matrix、Codabar)的库。ZXing(“zebra crossing”)是一个开源的、多格式的、用Java实现的一维/二维条码图像处理库,具有到其他...