生成二维码

mac2022-07-05  17

主要实现生成二维码程序,共生成两种一种使用QRcode生成,别一种用barcode4j生成,运行环境为JDK1.5

相关JAR包及程序,我会在最后附上,共大家下载,找这些组件还是挺费事的,呵

IBarcode定义条码接口

1 package com.xqrj.barcode; 2 3 import com.xqrj.barcode.exception.BarcodeException; 4 5 public interface IBarcode { 6 /** 7 * 设置高度 8 * @param width 高度 9 * @return void 10 */11 public void setWidth(float width);12 /**13 * 设置宽度14 * @param weight 宽度15 * @return void 16 */17 public void setHeight(float height);18 /**19 * 设置类型20 * @param imgType 图片类型(JPG等)21 * @return 22 */23 public void setImgType(String imgType);24 /**25 * 设置类型26 * @param barcodeType 条码类型27 * @return 28 */29 public void setBarcodeType(String barcodeType);30 /**31 * 构建条码数据32 * @param buff 条码源数据33 * @return byte[] 条码结果数据34 */35 public byte[] buildData(byte[] buff) throws BarcodeException;36 /**37 * 构建条码数据38 * @param buff 条码源数据39 * @param fileName 文件名称40 * @return byte[] 条码结果数据41 */42 public void buildData(byte[] buff,String fileName) throws BarcodeException;43 /**44 * 解析条码数据45 * @param buff 条码结果数据46 * @return byte[] 条码源数据47 */48 public byte[] parseData(byte[] buff) throws BarcodeException;49 }

BarcodeImpl定义抽象类,对常用属性进行初始化设置

1 package com.xqrj.barcode; 2 3 public abstract class BarcodeImpl implements IBarcode { 4 protected float width = 98; 5 protected float height = 98; 6 protected String imgType = "JPG"; 7 protected String barcodeType = "pdf417"; 8 9 public void setHeight(float height) {10 this.height = height;11 }12 13 public void setImgType(String imgType) {14 this.imgType = imgType;15 }16 17 public void setBarcodeType(String barcodeType) {18 this.barcodeType = barcodeType;19 }20 21 public void setWidth(float width) {22 this.width = width;23 }24 25 }

QrcodeHelper助手类,继承BarcodeImpl抽象类生成QR类型的二维条码

1 package com.xqrj.barcode; 2 3 import java.awt.Color; 4 import java.awt.Graphics2D; 5 import java.awt.image.BufferedImage; 6 import java.io.ByteArrayOutputStream; 7 import java.io.File; 8 import java.io.IOException; 9 10 import javax.imageio.ImageIO; 11 12 import jp.sourceforge.qrcode.QRCodeDecoder; 13 import jp.sourceforge.qrcode.data.QRCodeImage; 14 15 import com.swetake.util.Qrcode; 16 import com.xqrj.barcode.exception.BarcodeException; 17 import com.xqrj.exception.XqrjException; 18 import com.xqrj.util.BarcodeUtil; 19 import com.xqrj.util.FileUtil; 20 21 public class QrcodeHelper extends BarcodeImpl { 22 23 public byte[] buildData(byte[] buff) throws BarcodeException { 24 //声明qrcode实例对象 25 Qrcode qrcode = new Qrcode(); 26 //设置qrcode纠错级别/* L','M','Q','H' */ 27 qrcode.setQrcodeErrorCorrect('M'); 28 //设置qrcode编码模式/* "N","A" or other */ 29 qrcode.setQrcodeEncodeMode('B'); 30 //设置qrcode符号版本/* 0-20 */ 31 qrcode.setQrcodeVersion(7); 32 33 BufferedImage bi = new BufferedImage((int)this.width, (int)this.height, BufferedImage.TYPE_INT_RGB); 34 35 //创建Graphics 36 Graphics2D g = bi.createGraphics(); 37 //设置背景颜色 38 g.setBackground(Color.WHITE); 39 //对矩形进行清除后重新填充 40 g.clearRect(0, 0, (int)this.width,(int)this.height); 41 //设置图形颜色 42 g.setColor(Color.BLACK); 43 44 //限制最大字节数为120 45 if (buff.length>0 && buff.length <120){ 46 boolean[][] s = qrcode.calQrcode(buff); 47 for (int i=0;i<s.length;i++) 48 for (int j=0;j<s.length;j++) 49 if (s[j][i]) g.fillRect(j*2+3,i*2+3,2,2); 50 } else { 51 throw new BarcodeException("数据超长,生成二维条码数据出错!"); 52 } 53 54 //释放图形资源 55 g.dispose(); 56 bi.flush(); 57 58 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 59 try { 60 ImageIO.write(bi, imgType, baos); 61 } catch (IOException e) { 62 throw new BarcodeException("生成二维条码数据出错!"); 63 } 64 65 return baos.toByteArray(); 66 } 67 68 public void buildData(byte[] buff,String fileName) throws BarcodeException { 69 FileUtil.saveFile("", fileName, this.buildData(buff)); 70 } 71 72 public byte[] parseData(byte[] buff) throws BarcodeException { 73 File imageFile = new File("D:\\QRCodeTest\\1.JPG"); 74 BufferedImage bufImg = null; 75 try { 76 bufImg = ImageIO.read(imageFile); 77 QRCodeDecoder qrcodeDecoder = new QRCodeDecoder(); 78 return qrcodeDecoder.decode(new J2SEImage(bufImg)); 79 } catch(Exception e) { 80 throw new BarcodeException("解析二维条码数据出错!"); 81 } 82 } 83 class J2SEImage implements QRCodeImage { 84 BufferedImage image; 85 public J2SEImage(BufferedImage source) { 86 this.image = source; 87 } 88 public int getWidth() { 89 return image.getWidth(); 90 } 91 public int getHeight() { 92 return image.getHeight(); 93 } 94 public int getPixel(int x, int y) { 95 return image.getRGB(x ,y); 96 } 97 } 98 public static void main(String[] args) { 99 try {100 QrcodeHelper qrcodeHelper = new QrcodeHelper();101 for(int i =1; i < 10; i ++){102 qrcodeHelper.buildData((i+"我").getBytes(), "D:\\QRCodeTest\\"+i+".jpg");103 }104 //System.out.println(new String(qrcodeHelper.parseData(null)));105 } catch (BarcodeException e) {106 // TODO Auto-generated catch block107 e.printStackTrace();108 }109 }110 111 }

BarcodeHelper助手类,继承BarcodeImpl抽象类生成PDF417或其它类型的二维条码

1 package com.xqrj.barcode; 2 3 import java.awt.image.BufferedImage; 4 import java.io.ByteArrayOutputStream; 5 6 import org.apache.avalon.framework.configuration.ConfigurationException; 7 import org.apache.avalon.framework.configuration.DefaultConfiguration; 8 import org.krysalis.barcode4j.BarcodeGenerator; 9 import org.krysalis.barcode4j.output.bitmap.BitmapCanvasProvider; 10 import org.krysalis.barcode4j.tools.MimeTypes; 11 12 import com.xqrj.barcode.exception.BarcodeException; 13 import com.xqrj.exception.XqrjException; 14 import com.xqrj.util.FileUtil; 15 16 /** 17 * <pre>类 BarcodeHelper 用barcode生成二维条码,可生成的二维条码 18 * 生成的二维码类型 19 * codabar 20 * code39,code128 21 * ean-128,ean128,2of5,intl2of5,interleaved2of5,ean-13,ean13,ean-8,ean8 22 * upc-a,upca,upc-e,upce 23 * postnet 24 * royal-mail-cbc 25 * usps4cb 26 * pdf417 27 * datamatrix 28 * @author 29 * @version 1.0 2012/03/31 30 * @see BarcodeHelper 31 * 修改记录 : 32 * 日期 版本 修改人 修改内容 33 * 34 * </pre> 35 */ 36 37 public class BarcodeHelper extends BarcodeImpl { 38 private DefaultConfiguration defaultConfig = new DefaultConfiguration("barcode"); 39 private int orientation = 0; 40 private int resolution = 300; 41 42 public byte[] buildData(byte[] buff) throws BarcodeException { 43 //设置条码显示类型 44 DefaultConfiguration defaultConfigchild = new DefaultConfiguration(barcodeType); 45 defaultConfig.addChild(defaultConfigchild); 46 //设置条码图片大小 47 DefaultConfiguration attribute; 48 //设置高度 49 attribute = new DefaultConfiguration("height"); 50 attribute.setValue(this.height); 51 defaultConfigchild.addChild(attribute); 52 //设置宽度 53 attribute = new DefaultConfiguration("module-width"); 54 attribute.setValue(this.width); 55 defaultConfigchild.addChild(attribute); 56 57 ByteArrayOutputStream baos = null; 58 try { 59 if (null==defaultConfig) throw new XqrjException("生成barcode条码参数未初始化,无法生成条码图形!"); 60 org.krysalis.barcode4j.BarcodeUtil barcodeUtil = org.krysalis.barcode4j.BarcodeUtil.getInstance(); 61 BarcodeGenerator barcodeGenerator = barcodeUtil.createBarcodeGenerator(defaultConfig); 62 63 baos = new ByteArrayOutputStream(4096); 64 BitmapCanvasProvider bitmap = new BitmapCanvasProvider(baos, MimeTypes.MIME_JPEG, resolution, BufferedImage.TYPE_BYTE_GRAY, true, orientation); 65 66 String data = new String(buff); 67 if ("PDF417".equals(barcodeType.toUpperCase())) data = new String(data.getBytes("cp437"),"cp437"); 68 barcodeGenerator.generateBarcode(bitmap, data); 69 bitmap.finish(); 70 } catch(Exception e) { 71 72 } 73 74 return baos.toByteArray(); 75 } 76 77 public void buildData(byte[] buff,String fileName) throws BarcodeException { 78 FileUtil.saveFile("", fileName, this.buildData(buff)); 79 } 80 81 public byte[] parseData(byte[] buff) throws BarcodeException { 82 // TODO Auto-generated method stub 83 return null; 84 } 85 86 public static void main(String[] args) { 87 /** 88 * 生成的二维码类型 89 * codabar 90 * code39,code128 91 * ean-128,ean128,2of5,intl2of5,interleaved2of5,ean-13,ean13,ean-8,ean8 92 * upc-a,upca,upc-e,upce 93 * postnet 94 * royal-mail-cbc 95 * usps4cb 96 * pdf417 97 * datamatrix 98 */ 99 try {100 BarcodeHelper barcodeHelper = new BarcodeHelper();101 barcodeHelper.setWidth(0.6f);102 barcodeHelper.setHeight(50);103 barcodeHelper.setBarcodeType("pdf417");104 for(int i =1; i < 10; i ++){105 barcodeHelper.buildData((i+"我").getBytes(), "D:\\QRCodeTest\\"+i+i+".jpg");106 }107 } catch (BarcodeException e) {108 // TODO Auto-generated catch block109 e.printStackTrace();110 }111 }112 }

 

完成生成QR类型及PDF417类型的二维条码,如想生成其它类型只要实现抽象类即可,有各自的特殊属性,直接定义,公共属性就抽象一下,呵

二维条码生成类(里面包含相关JAR包)

转载于:https://www.cnblogs.com/ynjxxk/archive/2012/04/01/2428002.html

相关资源:Java生成二维码
最新回复(0)