用java生成一个表白二维码

之前对二维码粗略的看了一下觉得可以自己实现一个二维码用来表白,程序员的浪漫。

其实生成一个二维码非常简单几个类就可以搞定

import java.io.File;
import java.nio.file.Path;
import java.util.HashMap;
import java.util.Scanner;

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;

public class CreateQRcode {
	public static void main(String[] args) {
		int width = 500;
		int height = 500;
		String format = "png";
		String content = "  ";
		//定义二维码的参数
		HashMap hints = new HashMap();
		hints.put(EncodeHintType.CHARACTER_SET,"utf-8");
		hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
		hints.put(EncodeHintType.MARGIN, 1);
		System.out.println("输入你要生成二维码的网址:");
		Scanner scanner  = new Scanner(System.in);
		content = scanner.nextLine();
		//生成二维码
		try {
			BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height,hints);
			File file = new File("D:/img.png");
			MatrixToImageWriter.writeToFile(bitMatrix, format, file);
		} catch (Exception e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		}
	}
}

我就不解释代码了相信都看得懂

刚生成的二维码


  • 4
    点赞
  • 39
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
好的,可以使用ZXing库来生成二维码,以下是Java代码示例: ```java import java.awt.BorderLayout; import java.awt.Color; import java.awt.Font; import java.awt.Graphics2D; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import javax.imageio.ImageIO; import com.google.zxing.BarcodeFormat; import com.google.zxing.common.BitMatrix; import com.google.zxing.qrcode.QRCodeWriter; public class QRCodeGenerator { public static void main(String[] args) throws IOException { String qrCodeText = "https://www.example.com"; int size = 300; String fileType = "png"; File qrFile = new File("MyQRCode.png"); File labelFile = new File("MyQRLabel.png"); // Generate QR code QRCodeWriter qrCodeWriter = new QRCodeWriter(); BitMatrix bitMatrix = qrCodeWriter.encode(qrCodeText, BarcodeFormat.QR_CODE, size, size); BufferedImage qrImage = new BufferedImage(size, size, BufferedImage.TYPE_INT_RGB); qrImage.createGraphics(); Graphics2D qrGraphics = (Graphics2D) qrImage.getGraphics(); qrGraphics.setColor(Color.WHITE); qrGraphics.fillRect(0, 0, size, size); qrGraphics.setColor(Color.BLACK); for (int i = 0; i < size; i++) { for (int j = 0; j < size; j++) { if (bitMatrix.get(i, j)) { qrGraphics.fillRect(i, j, 1, 1); } } } ImageIO.write(qrImage, fileType, qrFile); // Generate label BufferedImage labelImage = new BufferedImage(size, 50, BufferedImage.TYPE_INT_RGB); labelImage.createGraphics(); Graphics2D labelGraphics = (Graphics2D) labelImage.getGraphics(); labelGraphics.setColor(Color.WHITE); labelGraphics.fillRect(0, 0, size, 50); labelGraphics.setColor(Color.BLACK); labelGraphics.setFont(new Font("Arial", Font.BOLD, 20)); labelGraphics.drawString("My QR Code Label", 10, 30); ImageIO.write(labelImage, fileType, labelFile); // Combine QR code and label BufferedImage combinedImage = new BufferedImage(size, size + 50, BufferedImage.TYPE_INT_RGB); combinedImage.createGraphics(); Graphics2D combinedGraphics = (Graphics2D) combinedImage.getGraphics(); combinedGraphics.drawImage(qrImage, 0, 0, null); combinedGraphics.drawImage(labelImage, 0, size, null); ImageIO.write(combinedImage, fileType, new File("MyQRCodeWithLabel.png")); } } ``` 这段代码生成一个包含二维码和标签的图像文件。你可以根据需要修改二维码的内容、大小和输出文件名,以及标签的内容和样式。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值