java 生成二维码并直接在web输出

二维码是什么玩意,有什么好处我就不做过多解释了,如果你还不懂,可以百科一下。

我还是比较注重于亲自动手写一下。

网上java生成二维码文章很多,无非就是用谷歌的Zxing.jar开发包和小日本写的qrcode。

但是都直接生成图片,本文用的是谷歌Zxing.jar(对小日本印象不好)直接通过response.getOutputStream()输出字节流到页面,在页面上显示。

先下载zxing开发包,这里用到的只是core那个jar包。

下图为实现功能的 imageZxing.jsp;

<%@ page language="java" pageEncoding="utf-8"%>
<%@ page contentType="image/jpeg" import="
java.awt.image.*,
java.util.*,
javax.imageio.*,
com.google.zxing.BarcodeFormat,
com.google.zxing.EncodeHintType,
com.google.zxing.MultiFormatWriter,
com.google.zxing.WriterException,
com.google.zxing.common.BitMatrix"
%>

<%
//设置页面不缓存
response.setHeader("Pragma","No-cache");
response.setHeader("Cache-Control","no-cache");
response.setDateHeader("Expires", 0);

//String text = request.getParameter("str");  如果内容自己定义的话,可以直接传过来
String text = "hello Mr.Xu!";
int width2 = 100;
int height2 = 100;
//二维码的图片格式
String format = "gif";
Hashtable hints = new Hashtable();
//内容所使用编码
hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
try {
BitMatrix bitMatrix = new MultiFormatWriter().encode(text,BarcodeFormat.QR_CODE, width2, height2, 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); //二维码图片为黑白两色
}
}
ImageIO.write(image,"gif",response.getOutputStream());
out.clear();
//生成二维码
//File outputFile = new File("d:"+File.separator+"new.gif");
//MatrixToImageWriter.writeToFile(bitMatrix, format, outputFile);
} catch (WriterException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
%>

代码在jsp中实现,只要你导入开发包,就可以直接拿过来用。

在你页面需要二维码图片的地方直接html调用即可

<img id='imageUrl' src='/路径/js/page/imageZxing.jsp'>

第一次写博客,不足之处多多包涵!


以下是Java生成二维码并下载的代码示例: ```java // 导入相关包 import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import java.util.HashMap; import java.util.Map; import javax.imageio.ImageIO; import javax.servlet.http.HttpServletResponse; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; import com.google.zxing.BarcodeFormat; import com.google.zxing.EncodeHintType; import com.google.zxing.MultiFormatWriter; import com.google.zxing.WriterException; import com.google.zxing.client.j2se.MatrixToImageWriter; import com.google.zxing.common.BitMatrix; import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel; @Controller @RequestMapping("/qrcode") public class QRCodeController { @GetMapping("/download") @ResponseBody public void downloadQRCode(@RequestParam("content") String content, HttpServletResponse response) throws IOException, WriterException { // 设置二维码参数 int width = 300; // 二维码宽度 int height = 300; // 二维码高度 String format = "png"; // 二维码图片格式 Map<EncodeHintType, Object> hints = new HashMap<>(); hints.put(EncodeHintType.CHARACTER_SET, "UTF-8"); // 编码格式 hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H); // 容错等级 hints.put(EncodeHintType.MARGIN, 1); // 边距 // 生成二维码 BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height, hints); BufferedImage image = MatrixToImageWriter.toBufferedImage(bitMatrix); // 将二维码图片写入文件 File file = new File("qrcode.png"); ImageIO.write(image, format, file); // 设置响应头,使浏览器能够下载文件 response.setHeader("Content-Disposition", "attachment;filename=qrcode.png"); response.setContentType("application/octet-stream"); response.setContentLength((int) file.length()); // 将文件写入响应流 FileInputStream in = new FileInputStream(file); OutputStream out = response.getOutputStream(); byte[] buffer = new byte[1024]; int len = 0; while ((len = in.read(buffer)) != -1) { out.write(buffer, 0, len); } in.close(); out.close(); } } ``` 以上代码使用Spring Boot框架,通过调用`/qrcode/download`接口,传入二维码内容`content`,即可生成二维码并下载到本地。其中,二维码参数可以根据需求进行修改。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值