ZXing生成二维码和带logo的二维码

本文介绍了一种使用Java生成和解析二维码的方法,包括如何创建二维码图片、如何在二维码中嵌入LOGO以及如何解析二维码的内容。提供了详细的代码实现,并对关键步骤进行了说明。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

首先说下,QRCode是日本人开发的,ZXing是google开发,barcode4j也是老美开发的,barcode4j对一维条形码处理的很好,而且支持的格式很多,当然也可以对二维码进行处理,效果个人感觉没有前两种好;ZXing对j2me,j2se,

code:

private static final String CHARSET = "utf-8";
	
	// 二维码尺寸
	private static final int DEFAULT_QRCODE_SIZE = 300;
	// LOGO宽度
	private static final int DEFAULT_LOGO_WIDTH = 50;
	// LOGO高度
	private static final int DEFAULT_LOGO_HEIGHT = 50;
	
	private static final String IMAGE_FORMAT_NAME = "JPG";
	    
	/**
	 * 生成二维码图片
	 * 
	 * @param content
	 *            二维码包含内容
	 * @param QRcodeSize
	 *            正方形二维码图片的大小(单位:像素)	
	 * @throws Exception
	 */
    public static BufferedImage createImage(String content, int QRcodeSize) throws Exception {
    	if(QRcodeSize<=0){
    		QRcodeSize=DEFAULT_QRCODE_SIZE;
    	}
		Hashtable<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>();
		hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
		hints.put(EncodeHintType.CHARACTER_SET, CHARSET);
		hints.put(EncodeHintType.MARGIN, 1);
		BitMatrix bitMatrix = new MultiFormatWriter().encode(content,
				BarcodeFormat.QR_CODE, QRcodeSize, QRcodeSize, hints);
		int width = bitMatrix.getWidth();
		int height = bitMatrix.getHeight();
		BufferedImage image = new BufferedImage(width, height,
				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);
			}
		}		
		return image;
	}

	/**
	 * 向二维码图片中插入LOGO
	 * 
	 * @param source
	 *            二维码图片对象
	 * @param QRcodeSize
	 *            正方形二维码图片的大小(单位:像素)	
	 * @param logoImageFile
	 *            二维码图片文件对象
	 * @param logoNeedCompress
	 *            logo图片是否压缩         
	 * @param logoWidth
	 *            logo图片宽度
	 * @param logoHeight
	 *            logo图片高度
	 * @throws Exception
	 */
    public static void insertLogoImage(BufferedImage source, int QRcodeSize, InputStream logoInputStream,
			boolean logoNeedCompress,int logoWidth, int logoHeight) throws Exception {
		/*if (!logoImageFile.exists()) {
			System.err.println("logo文件不存在!");
			return;
		}*/
		Image src = ImageIO.read(logoInputStream);
		int width = src.getWidth(null);
		int height = src.getHeight(null);
		if(logoWidth<=0)logoWidth=DEFAULT_LOGO_WIDTH;
		if(logoHeight<=0)logoHeight=DEFAULT_LOGO_HEIGHT;
		if (logoNeedCompress) { // 压缩LOGO
			if (width > logoWidth) {
				width = logoWidth;
			}
			if (height > logoHeight) {
				height = logoHeight;
			}
			Image image = src.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;
		}
		// 插入LOGO
		Graphics2D graph = source.createGraphics();
		int x = (QRcodeSize - width) / 2;
		int y = (QRcodeSize - 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();
	}


	/**
	 * 生成二维码(内嵌LOGO)
	 * 
	 * @param content
	 *            内容
	 * @param imgPath
	 *            LOGO地址
	 * @param destPath
	 *            存储地址
	 * @throws Exception
	*/
	public static File encode(String content, InputStream logoInputStream,int QRcodeSize)
			throws Exception {
		File codeFile=null;
		if (StringUtil.isBlank(content))return null;
		try {
			BufferedImage codeImage = QRCodeUtil.createImage(content,QRcodeSize);
			if(null!=logoInputStream){
			QRCodeUtil.insertLogoImage(codeImage, QRcodeSize, logoInputStream, true, DEFAULT_LOGO_WIDTH, DEFAULT_LOGO_HEIGHT);
			}
			codeFile = File.createTempFile("QR"+System.currentTimeMillis(),"."+IMAGE_FORMAT_NAME);
			ImageIO.write(codeImage, IMAGE_FORMAT_NAME, codeFile);
		}catch(Exception e){
			System.err.println("生成二维码失败!"+e.getMessage());
		}
			return codeFile;
	}	


	/**
	 * 解析二维码
	 * 
	 * @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<DecodeHintType, Object> hints = new Hashtable<DecodeHintType, Object>();
		hints.put(DecodeHintType.CHARACTER_SET, CHARSET);
		result = new MultiFormatReader().decode(bitmap, hints);
		String resultStr = result.getText();
		return resultStr;
	}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值