Java 二维码开发

来源:互联网 发布:福彩分析软件 编辑:程序博客网 时间:2024/06/02 05:31

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;

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

原创粉丝点击