java制作一个二维码

制作一个二维码

**1.**手动创建两个文件夹
在这里插入图片描述
2.导入相应的jar整合包(生成与解析)
3.代码如下

package picture;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;

import java.io.FileOutputStream;
import java.io.IOException;

import javax.imageio.ImageIO;


import com.swetake.util.Qrcode;


public class pictureQrcode {
	public static void main(String[] args) throws IOException {
		setImage();
		System.out.println("二维码生成成功");
	}

	public static  void setImage() throws IOException {
		  int v =6;
	        int width = 67 + 12 * (v - 1);
	        int height = 67 + 12 * (v - 1);


	        Qrcode x = new Qrcode();
	        x.setQrcodeErrorCorrect('L');
	        x.setQrcodeEncodeMode('B');//注意版本信息 N代表数字 、A代表 a-z,A-Z、B代表 其他)
	        x.setQrcodeVersion(v);//版本号  1-40

	        //缓冲区
	        BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_BGR);

	        //绘图
	        Graphics2D gs = bufferedImage.createGraphics();

	        gs.setBackground(Color.WHITE);
	        gs.setColor(Color.BLACK);
	        gs.clearRect(0, 0, width, height);

	        //偏移量
	        int pixoff = 2;
		
		File srcFile = new File("D:\\picture\\IN\\Itest.txt");
		//封装目的端根目录
		File destFile = new File("D:\\picture\\OUT\\out.txt");
		
		BufferedInputStream bis = 
				new BufferedInputStream(new FileInputStream(srcFile));
		BufferedOutputStream bos = 
				new BufferedOutputStream(new FileOutputStream(destFile));
		//每次读写一个字节数组
		byte[] buff = new byte[120];
		int len = 0;
		//int index=0;
		while((len=bis.read(buff))!=-1) {
			//System.out.println("运行"+"第"+(index++)+"次");
			//二维码
			//不能大于120,否则二维码是空白
			 if (buff.length > 0 && buff.length <= 120) {
				 
		            boolean[][] s = x.calQrcode(buff);

		            for (int i = 0; i < s.length; i++) {
		                for (int j = 0; j < s.length; j++) {
		                    if (s[j][i]) {
		                        gs.fillRect(j * 3 + pixoff, i * 3 + pixoff, 3, 3);
		                    }
		                }
		            }
			 			}
			
			  gs.dispose();
		        bufferedImage.flush();
		        //设置图片格式,与输出的路径
		        ImageIO.write(bufferedImage, "png", new File("D:\\picture\\OUT\\OUTqrcode2.png"));
		        System.out.println("二维码生成完毕");
			
			bos.write(buff, 0, len);
			
			
		}
		bos.close();
		bis.close();
		
	}
}

二维码生成完毕

复制文件

package picture;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;

import java.io.FileOutputStream;
import java.io.IOException;

public class picturexx {
	public static void main(String[] args) throws IOException {
		setImage();
		System.out.println("复制成功");
	}

	public static  void setImage() throws IOException {
		File srcFile = new File("D:\\picture\\IN\\Itest.txt");
		//封装目的端根目录
		File destFile = new File("D:\\picture\\OUT\\Otest.txt");
		
		BufferedInputStream bis = 
				new BufferedInputStream(new FileInputStream(srcFile));
		BufferedOutputStream bos = 
				new BufferedOutputStream(new FileOutputStream(destFile));
	
		byte[] buff = new byte[1024*5];
		int len = 0;
		while((len=bis.read(buff))!=-1) {
			bos.write(buff, 0, len);
		}
		bos.close();
		bis.close();
		
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,您可以使用第三方库zxing来生成二维码。以下是一个示例代码: ```java 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.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) throws WriterException, IOException { String qrCodeData = "https://www.example.com"; // 二维码内容 String filePath = "qrcode.png"; // 文件路径 String charset = "UTF-8"; // 字符编码,一般使用UTF-8 // 二维码生成参数设置 int qrCodeWidth = 300; // 二维码宽度 int qrCodeHeight = 300; // 二维码高度 String fileType = "png"; // 二维码图片格式 // 生成二维码 createQRCode(qrCodeData, filePath, charset, qrCodeWidth, qrCodeHeight, fileType); System.out.println("QR Code image created successfully!"); } private static void createQRCode(String qrCodeData, String filePath, String charset, int qrCodeWidth, int qrCodeHeight, String fileType) throws WriterException, IOException { // 设置二维码参数 ErrorCorrectionLevel errorCorrectionLevel = ErrorCorrectionLevel.L; // 容错级别,L为最低级别 QRCodeWriter qrCodeWriter = new QRCodeWriter(); BitMatrix bitMatrix = qrCodeWriter.encode(new String(qrCodeData.getBytes(charset), charset), BarcodeFormat.QR_CODE, qrCodeWidth, qrCodeHeight, getHints(errorCorrectionLevel)); // 生成二维码图片 BufferedImage bufferedImage = new BufferedImage(qrCodeWidth, qrCodeHeight, BufferedImage.TYPE_INT_RGB); bufferedImage.createGraphics(); int matrixWidth = bitMatrix.getWidth(); for (int x = 0; x < matrixWidth; x++) { for (int y = 0; y < matrixWidth; y++) { bufferedImage.setRGB(x, y, bitMatrix.get(x, y) ? 0x000000 : 0xFFFFFF); } } // 保存二维码图片 File qrFile = new File(filePath); ImageIO.write(bufferedImage, fileType, qrFile); } private static com.google.zxing.EncodeHintType getHints(ErrorCorrectionLevel errorCorrectionLevel) { com.google.zxing.EncodeHintType hintType = EncodeHintType.ERROR_CORRECTION; java.util.Map<com.google.zxing.EncodeHintType, Object> hints = new java.util.HashMap<>(); hints.put(hintType, errorCorrectionLevel); return hintType; } } ``` 在运行代码之前,您需要引入zxing库。您可以在Maven中添加以下依赖项: ```xml <dependency> <groupId>com.google.zxing</groupId> <artifactId>core</artifactId> <version>3.4.1</version> </dependency> <dependency> <groupId>com.google.zxing</groupId> <artifactId>javase</artifactId> <version>3.4.1</version> </dependency> ``` 当您运行代码后,将会在指定的文件路径下生成一个名称为qrcode.png的二维码图片。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值