Java链接生成二维码

1、EnCodeTwoCode工具类

package com.bycotrun.framework.utils;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.util.HashMap;

import javax.imageio.ImageIO;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;

public class EnCodeTwoCode {
    private static final int BLACK = 0xFF000000;
    private static final int WHITE = 0xFFFFFFFF;

    private static BufferedImage toBufferedImage(BitMatrix bm) {
	int width = bm.getWidth();
	int height = bm.getHeight();
	BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR);
	for (int i = 0; i < width; i++) {
	    for (int j = 0; j < height; j++) {
		image.setRGB(i, j, bm.get(i, j) ? BLACK : WHITE);
	    }
	}
	return image;
    }

    public static void writeBitMatricToFile(BitMatrix bm, String format, File file) {
	BufferedImage image = toBufferedImage(bm);
	try {
	    if (!ImageIO.write(image, format, file)) {
		throw new RuntimeException("Can not write an image to file" + file);
	    }
	} catch (IOException e) {
	    e.printStackTrace();
	}
    }

    public static void writeToStream(BitMatrix matrix, String format, OutputStream stream) throws IOException {
	BufferedImage image = toBufferedImage(matrix);
	if (!ImageIO.write(image, format, stream)) {
	    throw new IOException("Could not write an image of format " + format);
	}
    }

    public static String encodeTwoCode(String msg, String path, String pathName) {
	int width = 300;
	int height = 300;
	String format = "png";
	HashMap<EncodeHintType, String> hints = new HashMap<EncodeHintType, String>();
	hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
	// 条形码的格式是 BarcodeFormat.EAN_13
	// 二维码的格式是BarcodeFormat.QR_CODE
	BitMatrix bm;
	try {
	    bm = new MultiFormatWriter().encode(msg, BarcodeFormat.QR_CODE, width, height, hints);
	    File out = new File(path, pathName);// 定义一个文件流
	    EnCodeTwoCode.writeBitMatricToFile(bm, format, out);
	} catch (WriterException e) {
	    // TODO Auto-generated catch block
	    e.printStackTrace();
	}
	return path + pathName;
    }

    public static String encodeZxingTmCode(String msg, String path, String pathName) {
	int width = 300;
	int height = 50;
	String format = "png";
	HashMap<EncodeHintType, String> hints = new HashMap<EncodeHintType, String>();
	hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
	// 条形码的格式是 BarcodeFormat.EAN_13
	// 二维码的格式是BarcodeFormat.QR_CODE
	BitMatrix bm;
	try {
	    bm = new MultiFormatWriter().encode(msg, BarcodeFormat.CODE_128, width, height, hints);
	    File out = new File(path, pathName);// 定义一个文件流
	    EnCodeTwoCode.writeBitMatricToFile(bm, format, out);
	} catch (WriterException e) {
	    // TODO Auto-generated catch block
	    e.printStackTrace();
	}
	return path + pathName;
    }
}

2、controller调用

 @RequestMapping(value = "/qrcoding", method = RequestMethod.GET)
    public void qrcoding(HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException {
	response.setContentType("text/html;charset=UTF-8");
	response.setCharacterEncoding("UTF-8");
	response.setHeader("Access-Control-Allow-Origin", "*");
	String text = request.getParameter("text");
	// ********使用海报生成二维码方式*****************
	String contextpath = request.getSession().getServletContext().getRealPath("/file");
	// 根据日期创建文件夹
	String picPath = Utils.mkdirs(contextpath);
	contextpath = contextpath + "\\" + picPath + "\\";
	String randomSTR = SystemConfig.sellerCODE + IdGenerator.generateStr().substring(3)
		+ String.valueOf(10 + (int) (Math.random() * 90));
	String pathNamew = randomSTR + "w.jpg";// 文件名
	// 生成二维码并上传至服务器
	EnCodeTwoCode.encodeTwoCode(text, contextpath, pathNamew);
	String path1 = contextpath+ pathNamew;
	FileInputStream fis = null;
	File file = new File(path1);
	try {
	    fis = new FileInputStream(file);
	    response.setContentType("image/jpg"); // 设置返回的文件类型
	    IOUtils.copy(fis, response.getOutputStream());
	} catch (Exception e) {
	    e.printStackTrace();
	}
	}

3、jsp页面

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    <title>My JSP 'MyJsp.jsp' starting page</title>
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
  </head>
  <body>
   <img alt="二维码" src="http://127.0.0.1/dmsys2.2/platform/syscommon/qrcoding?text=https://www.baidu.com">
  </body>
</html>

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值