`
屌丝学Java
  • 浏览: 28418 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

简单的生成二维码方法

 
阅读更多
     生成一个简单的二维码:
         1、导入二维码jar包:QRCode.jar;
        2、两个类:二维码图片对象类:TwoCodeImage.java;
/**
 * 二维码图片对象
 * @author liupan
 *
 */
public class TwoCodeImage implements QRCodeImage {
	private BufferedImage bufImg;

	public TwoCodeImage(BufferedImage bufImg) {
		this.bufImg = bufImg;
	}

	@Override
	public int getHeight() {
		return bufImg.getHeight();
	}

	@Override
	public int getPixel(int x, int y) {
		return bufImg.getRGB(x, y);
	}

	@Override
	public int getWidth() {
		return bufImg.getWidth();
	}

	
}

                  二维码生成公共工具类:TwoCodeUtils.java;
public class TwoCodeUtils {
	
	public static String encoder(Object obj , String ent_type) throws BusinessException  {
		//String action = Global.MAP_PUBLIC_URL.get (ent_type);		       //获取企业类型 对应的外网公开查询的请求地址
		D_scyp_register dInfo=null;
		D_yljg_register dyljg=null;
		
		String path ="";
//		if(action == null) {
//			 new BusinessException ("二维码生成错误!");
//		}
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");
		String content = "";
		if(ent_type.equals("SCYP")){
			dInfo=(D_scyp_register) obj;
			path=Global.getAbsUploadPath(Global.QRCODE_PATH) + dInfo.getLicencecode() + ".png";		//生成二维码图片的路径“upload/qrcode/+许可证号.png”

			content +="许可证号:" +  dInfo.getLicencecode();
			content +="\n企业名称:" + dInfo.getCorp_name();
			content+="\n分类码:"+dInfo.getClass_code();
			content+="\n注册地址:"+dInfo.getRegisteraddress();
			content+="\n生产地址和生产范围:"+dInfo.getProd_scope();
			
			if(dInfo.getSocial_credit_code().length()==18){
				
				content +="\n社会信用代码:" + dInfo.getSocial_credit_code().subSequence(0, 10)+"****"+ dInfo.getSocial_credit_code().substring(14, 18);
			}else{
				content +="\n社会信用代码:" + dInfo.getSocial_credit_code();
			}
			
			content +="\n法定代表人:" + dInfo.getArtificialperson();
			content +="\n企业负责人:" + dInfo.getEnterpriseprincipa();
			content +="\n质量负责人:" + dInfo.getMassprincipal();
			content +="\n有效期至:" + sdf.format(dInfo.getValid_end_date());
			content += "\n发证机关:" +  dInfo.getGrant_org();
			content += "\n签发人:" +  dInfo.getSigner();
			content +="\n发证日期:" + sdf.format(dInfo.getGrant_date());
			content +="\n日常监管机构:" + dInfo.getDaily_supervise_organ();
			content +="\n日常监管人员:" + dInfo.getDaily_supervise_person();
			content +="\n监督举报电话:" + 12331;    		//二维码的内容
			content +="\n证书查询地址:" + "http://www.hebfda.gov.cn/CL0182/";  
		}else if(ent_type.equals("YLJG")){
			dyljg=(D_yljg_register) obj;
			path=Global.getAbsUploadPath(Global.QRCODE_PATH) + dyljg.getLicencecode() + ".png";		//生成二维码图片的路径“upload/qrcode/+许可证号.png”
			
			content +="许可证号:" +  dyljg.getLicencecode();
			content +="\n医疗机构名称:" + dyljg.getCorp_name();
			content+="\n注册地址:"+dyljg.getRegisteraddress();
			content+="\n配置地址和配置范围:"+dyljg.getPzfw();
			
			if(dyljg.getSocial_credit_code().length()==18){
				
				content +="\n社会信用代码:" + dyljg.getSocial_credit_code().subSequence(0, 10)+"****"+ dyljg.getSocial_credit_code().substring(14, 18);
			}else{
				content +="\n社会信用代码:" + dyljg.getSocial_credit_code();
			}
			content +="\n医疗机构类别:" + dyljg.getEnterprisetype();
			content +="\n法定代表人:" + dyljg.getArtificialperson();
			content +="\n制剂室负责人:" + dyljg.getZjsfzr();
			content +="\n质量负责人:" + dyljg.getMassprincipal();
			content +="\n有效期至:" + sdf.format(dyljg.getValid_end_date());
			content += "\n发证机关:" +  dyljg.getGrant_org();
			content += "\n签发人:" +  dyljg.getSigner();
			content +="\n发证日期:" + sdf.format(dyljg.getGrant_date());
			content +="\n日常监管机构:" + dyljg.getDaily_supervise_organ();
			content +="\n日常监管人员:" + dyljg.getDaily_supervise_person();
			content +="\n监督举报电话:" + 12331;    		//二维码的内容
			content +="\n证书查询地址:" + "http://www.hebfda.gov.cn/CL0182/"; 
		}
		TwoCodeUtils.encoderQRCode(content, path);																								//开始生成
		
		/*System.out.println("$$$$$$$$$$$$$$$$$$$$$$$$$$$"+Global.XZSP_ADDRESS );
		System.out.println("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%"+Global.QRCODE_PATH  ); //局端访问网址
		
		path = Global.XZSP_ADDRESS +Global.QRCODE_PATH  + dInfo.getLicencecode() + ".png";	//二维码图片的最终路径
*/		return path;
	} 
	
    /** 
     * 生成二维码(QRCode)图片 
     * @param content 存储内容 
     * @param imgPath 图片路径 
     */  
    public static void encoderQRCode(String content, String imgPath) throws BusinessException  {  
    	TwoCodeUtils.encoderQRCode(content, imgPath, "png", 20);  
    }  
      
    /** 
     * 生成二维码(QRCode)图片 
     * @param content 存储内容 
     * @param output 输出流 
     */  
    public static void encoderQRCode(String content, OutputStream output) throws BusinessException  {  
    	TwoCodeUtils.encoderQRCode(content, output, "png", 20);  
    } 
	
	  /** 
     * 生成二维码(QRCode)图片 
     * @param content 存储内容 
     * @param output 输出流 
     * @param imgType 图片类型 
     */  
    public static void encoderQRCode(String content, OutputStream output, String imgType) throws BusinessException  {  
    	TwoCodeUtils.encoderQRCode(content, output, imgType , 20);  
    }
    
    /** 
     * 生成二维码(QRCode)图片 
     * @param content 存储内容 
     * @param imgPath 图片路径 
     * @param imgType 图片类型 
     */  
    public static void encoderQRCode(String content, String imgPath, String imgType) throws BusinessException  {  
        TwoCodeUtils.encoderQRCode(content, imgPath, imgType , 20);  
    } 
	
    /** 
     * 生成二维码(QRCode)图片 
     * @param content 存储内容 
     * @param imgPath 图片路径 
     * @param imgType 图片类型 
     * @param size 二维码尺寸 
     */  
	public static void encoderQRCode (String content , String imgPath , String imgType , int size) throws BusinessException {
		try {
			BufferedImage bufImg = TwoCodeUtils.qRcodeCommon(content, imgType, size);
			File imgFile = new File(imgPath);
			ImageIO.write(bufImg, imgType, imgFile);
		} catch (Exception e) {
			throw new BusinessException("二维码生成错误!");
		}
	}
	
	  /** 
     * 生成二维码(QRCode)图片 
     * @param content 存储内容 
     * @param output 输出流 
     * @param imgType 图片类型 
     * @param size 二维码尺寸 
     */  
	public static void encoderQRCode(String content , OutputStream output , String imgType , int size) throws BusinessException {
		try {
			BufferedImage bufImg = TwoCodeUtils.qRcodeCommon(content , imgType ,size);
			 // 生成二维码QRCode图片  
			ImageIO.write(bufImg, imgType , output);
		} catch (Exception e) {
			throw new BusinessException("二维码生成错误!");
		}
	}

    /** 
     * 生成二维码(QRCode)图片的公共方法 
     * @param content 存储内容 
     * @param imgType 图片类型 
     * @param size 二维码尺寸 
     * @return 
     */ 
	public static BufferedImage qRcodeCommon (String content , String imgType , int size) throws BusinessException {
		BufferedImage bufImg = null;
		try{
			Qrcode qrcode = new Qrcode();
			// 设置二维码排错率,可选L(7%)、M(15%)、Q(25%)、H(30%),排错率越高可存储的信息越少,但对二维码清晰度的要求越小  
			qrcode.setQrcodeErrorCorrect('M');
			qrcode.setQrcodeEncodeMode('B');
			// 设置设置二维码尺寸,取值范围1-40,值越大尺寸越大,可存储的信息越大
			qrcode.setQrcodeVersion(size);
			   // 获得内容的字节数组,设置编码格式
			byte [] contentBytes = content.getBytes("utf-8");
			int imgSize = 67 + 12 * (size - 1);
			bufImg = new BufferedImage(imgSize ,imgSize , BufferedImage.TYPE_INT_RGB);
			Graphics2D gs = bufImg.createGraphics();
		     // 设置背景颜色  
			gs.setBackground(Color.WHITE);
			gs.clearRect(0, 0, imgSize, imgSize);
			 // 设定图像颜色> BLACK 
			gs.setColor(Color.black);
			//设置偏移量,不设置可能导致解析出错  
			int pixoff = 2;
			 // 输出内容> 二维码  
			if (contentBytes.length > 0 && contentBytes.length < 800) {
				boolean[] [] codeOut = qrcode.calQrcode(contentBytes);
				for (int i = 0 ; i < codeOut.length ; i++) {
					for(int j = 0 ; j < codeOut.length ; j++) {
						if(codeOut[j][i]) {
							gs.fillRect(j * 3 + pixoff ,   i * 3 + pixoff , 3 , 3);
						}
					}
				}
			} else {
				throw new BusinessException("二维码生成错误!");
			}
			gs.dispose();
			bufImg.flush();
			
		} catch (Exception e) {
			throw new BusinessException("二维码生成错误!");
		}
		return  bufImg;
	}
	
    /** 
     * 解析二维码(QRCode) 
     * @param imgPath 图片路径 
     * @return 
     */  
	public static String decoderQRCode(String imgPath) throws BusinessException {
		String content = null;
        // QRCode 二维码图片的文件  
		File file = new File(imgPath);
		try {
			BufferedImage bufImg = ImageIO.read(file);
			QRCodeDecoder decoder = new QRCodeDecoder();
			content = new String (decoder.decode(new TwoCodeImage(bufImg)) ,"utf-8");
		} catch (Exception e) {
			throw new BusinessException("解析二维码错误!");
		}
		return content;
	}
	
	/**
    * 解析二维码(QRCode) 
    * @param input 输入流 
    * @return 
    */  
	public static String decoderQRCode(InputStream input) throws BusinessException {
		String content = null;
		BufferedImage bufImg = null;
		try {
			bufImg = ImageIO.read(input);
			QRCodeDecoder decoder = new QRCodeDecoder();
			content = new String(decoder.decode(new TwoCodeImage(bufImg)) , "utf-8");
		} catch (Exception e) {
			throw new BusinessException("解析二维码错误!");
		}
		return content;
	}}


调用工具类的生成方法,就OK了!
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics