java 生成二维码 代码实现

引入Zxing 的corn.jar 包就行了

import java.awt.image.BufferedImage;
import java.io.File;
import java.util.Hashtable;

import javax.imageio.ImageIO;

import com.google.zxing.*;
import com.google.zxing.common.BitMatrix;
/*
 * 图片生成在D盘根目录下
 * */

public class test {

     private static final int BLACK = 0xFF000000;
        private static final int WHITE = 0xFFFFFFFF;
    
        public static void main ( String[] args ) throws Exception
        {
            String text = "你好,世界。";
            String path = "D:/";
            int width = 900;
            int height = 900;
            // 二维码图片格式
            String format = "gif";
            // 设置编码,防止中文乱码
            Hashtable<EncodeHintType, Object> ht = new Hashtable<EncodeHintType, Object> ();
            ht.put (EncodeHintType.CHARACTER_SET, "UTF-8");
            // 设置二维码参数(编码内容,编码类型,图片宽度,图片高度, 编码格式)
            BitMatrix bitMatrix = new MultiFormatWriter ().encode (text, BarcodeFormat.QR_CODE, width, height, ht);
            // 生成二维码(定义二维码输出服务器路径)
            File outputFile = new File (path);
            if (!outputFile.exists ())
            {
                //创建文件夹
                outputFile.mkdir ();
            }
            int b_width = bitMatrix.getWidth ();
            int b_height = bitMatrix.getHeight ();
            // 建立图像缓冲器
            BufferedImage image = new BufferedImage (b_width, b_height, BufferedImage.TYPE_3BYTE_BGR);
            for ( int x = 0; x < b_width; x++ )
            {
                for ( int y = 0; y < b_height; y++ )
                {
                    image.setRGB (x, y, bitMatrix.get (x, y) ? BLACK : WHITE);
                }
            }
            // 生成二维码
            ImageIO.write (image, format, new File (path + "/qrcode." + format));
        }
}





  • 2
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
好的,关于 Java 生成二维码代码,你可以使用 zxing 库来实现。以下是一个简单的示例代码: ``` import java.io.File; import java.io.IOException; import java.util.HashMap; import java.util.Map; import com.google.zxing.BarcodeFormat; import com.google.zxing.EncodeHintType; import com.google.zxing.WriterException; import com.google.zxing.common.BitMatrix; import com.google.zxing.qrcode.QRCodeWriter; import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel; public class QRCodeGenerator { public static void main(String[] args) { String qrCodeData = "https://www.example.com"; String filePath = "qrcode.png"; int size = 250; String fileType = "png"; File qrFile = new File(filePath); try { createQRCode(qrFile, qrCodeData, size, fileType); System.out.println("QR Code generated successfully!"); } catch (WriterException | IOException e) { System.out.println("Error generating QR Code: " + e.getMessage()); } } private static void createQRCode(File qrFile, String qrCodeData, int size, String fileType) throws WriterException, IOException { // Create the QR code writer QRCodeWriter qrWriter = new QRCodeWriter(); // Set the encoding hint Map<EncodeHintType, Object> hints = new HashMap<>(); hints.put(EncodeHintType.CHARACTER_SET, "UTF-8"); hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H); // Generate the QR code matrix BitMatrix qrMatrix = qrWriter.encode(qrCodeData, BarcodeFormat.QR_CODE, size, size, hints); // Create the QR code image int matrixWidth = qrMatrix.getWidth(); int matrixHeight = qrMatrix.getHeight(); int[] pixels = new int[matrixWidth * matrixHeight]; for (int y = 0; y < matrixHeight; y++) { int offset = y * matrixWidth; for (int x = 0; x < matrixWidth; x++) { pixels[offset + x] = qrMatrix.get(x, y) ? 0xFF000000 : 0xFFFFFFFF; } } BufferedImage qrImage = new BufferedImage(matrixWidth, matrixHeight, BufferedImage.TYPE_INT_RGB); qrImage.setRGB(0, 0, matrixWidth, matrixHeight, pixels, 0, matrixWidth); // Write the QR code image to file ImageIO.write(qrImage, fileType, qrFile); } } ``` 这是一个基本的生成二维码的示例代码,你可以根据自己的需求进行适当的修改。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值