Java 二维码开发

http://viralpatel.net/blogs/create-qr-codes-java-servlet-qr-code-java/ 这里可以去下载二维码的jar 文件。

如果是maven 开发,在pom.xml文件中加入:

            <dependency>
	      <groupId>net.glxn</groupId>
	      <artifactId>qrgen</artifactId>
	      <version>1.2</version>
	    </dependency>

会自动下载jar文件。

jar包就绪,开发二维码:

private static final String FORMAT = "yyyyMMddHHmmss";

	/**
	 * 
	 * @param codeContext
	 *            内容
	 * @param imageType
	 *            类型
	 * @param prefix
	 *            前缀
	 * @throws IOException
	 */
	public static File createCRCode(String codeContext, ImageType imageType,
			String prefix) throws IOException {
		ByteArrayOutputStream out = QRCode.from(codeContext).withSize(100, 100)
				.to(imageType == null ? imageType.JPG : imageType).stream();
		String postFix = DateUtil.formatDateByFormat(new Date(), FORMAT);
		String savePath = ServletActionContext.getServletContext().getRealPath(
				SystemConfig.getProperty(Constant.QR_CODE_PATH));
		File floder = new File(savePath);
		if (!floder.exists()) {
			floder.mkdir();
		}
		File file = new File(savePath + "/" + prefix + postFix + "."
				+ imageType.JPG);
		FileOutputStream fout = new FileOutputStream(file);
		fout.write(out.toByteArray());
		fout.flush();
		fout.close();
		return file;
	}


这个方法生成的是二维码的File对象。生成的二维码被保存到了“savePath”下,文件名称为prefix + postFix + "."+ imageType.JPG;

读取就很简单了,和读取图片一样,这里就不介绍了。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值