Java生成二维码内边距大的问题

使用zxing 生成二维码,margin 设置为0。但是还存在内边距,网上有很多的处理办法都是在zxing源码的基础上操作.实践意义不大!

/**
     * 裁剪图片
     *
     * @param bufferedImage 原始图片
     * @return java.awt.image.BufferedImage
     */
    public static BufferedImage cropImage(BufferedImage bufferedImage) {
        /*
         * 原始的二维码图片无论怎么设置都是有内边距的,但是二维码是双色图,最外部的就是需要切剪掉的颜色。所以我们取到x,y 轴  = 0的第一个色值
         * 然后逐次循环到左上方码眼位置。这个码眼位置就是我们需要切割的起始位置。x,y轴循环的次数就是我们需要切割的图片的宽高的二分之一。
         *
         * 我们的二维码创建都是正方形的,这里也有好处,省略了计算不同宽高的复杂过程
         * 【描述纯属抽象,example for this !】
         */
        int cropColor = bufferedImage.getRGB(0, 0);
        int width = 0, height = 0;
        label:
        for (int i = 0; i < bufferedImage.getWidth(); i++) {
            for (int j = 0; j < bufferedImage.getHeight(); j++) {
                if (bufferedImage.getRGB(i, j) != cropColor) {
                    width = i;
           
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是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`,即可生成二维码并下载到本地。其中,二维码参数可以根据需求进行修改。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值