zxing 二维码、带logo二维码生成

博客介绍了带logo二维码,还给出了zxing相关jar包的下载地址,为开发带logo二维码提供了资源支持。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<span style="font-size:18px;">普通二维码生成</span>
<span style="font-size:18px;">public class ZxingEncoderUtil {

	/**
	 * 生成二维码
	 * @param filePath       文本文件路径(文本需以UTF-8)
	 * @param imgFormate     指定生成的二维码图片的后缀名
	 * @param width          指定生成的二维码图片的宽度
	 * @param height		   指定生成的二维码图片的高度
	 */
    public void createTwoDimensionalCode(String filePath, String imgFormate, int width, int height) {
    	File file = new File(filePath);
    	StringBuffer contents = new StringBuffer("");
    	//读取文本文件内容到 contents
		try {
			InputStream is = new FileInputStream(file);
			InputStreamReader isr = new InputStreamReader(is);
			BufferedReader br = new BufferedReader(isr);
			try {
				String temp = br.readLine();
				while(temp != null) {
					contents.append(temp+"\r\n");
					temp = br.readLine();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		//得到图片该存放路径
		String imgPath = file.getParent();
		//得到文件该设置的名字
		String imgName = file.getName();
		int endIndex = imgName.lastIndexOf(".");
		imgName = imgName.substring(0, endIndex);
		
		File imageFile = new File(imgPath, imgName+"."+imgFormate);
		Hashtable<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>();
        // 指定纠错等级
        hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);
        // 指定编码格式
        hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
        try {
            BitMatrix bitMatrix = new MultiFormatWriter().encode(contents.toString(),BarcodeFormat.QR_CODE, width, height, hints);
            MatrixToImageWriter.writeToPath(bitMatrix, imgFormate, imageFile.toPath());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    /**
     * @param args
     */
    public static void main(String[] args) {
    	
    	String filePath = args[0];   //文本文件路径(文本需以UTF-8)
    	String imgFormate = args[1];  //指定生成的二维码图片的后缀名
    	int width = Integer.parseInt(args[2]); //指定生成的二维码图片的宽度
    	int height = Integer.parseInt(args[3]); //指定生成的二维码图片的高度
    	new ZxingEncoderUtil().createTwoDimensionalCode(filePath, imgFormate, width, height);
    }
}</span>


带logo二维码

<span style="font-size:18px;">public class CreatQRCodeImg {

	private static final int BLACK = 0xFF000000;
	private static final int WHITE = 0xFFFFFFFF; 
	
	/**
	 *  最终调用该方法生成二维码图片
	 * @param url 要生成二维码的url
	 * @param imgPath 二维码生成的绝对路径
	 * @param logoPath 二维码中间logo绝对地址
	 * @throws Exception
	 */
	public static void get2CodeImage(String url,String imgPath,String logoPath) throws Exception{
		String format = "png";
		Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>(); 
		hints.put(EncodeHintType.CHARACTER_SET, "utf-8");  
		BitMatrix bitMatrix = new MultiFormatWriter().encode(url, BarcodeFormat.QR_CODE, 300, 300, hints);
		File qrcodeFile = new File(imgPath);  
		CreatQRCodeImg.writeToFile(bitMatrix, format, qrcodeFile, logoPath);  
	}
	
	/**
	 * 
	 * @param matrix 二维码矩阵相关
	 * @param format 二维码图片格式
	 * @param file 二维码图片文件
	 * @param logoPath logo路径
	 * @throws IOException
	 */
	public static void writeToFile(BitMatrix matrix,String format,File file,String logoPath) throws IOException {
		BufferedImage image = toBufferedImage(matrix);
		Graphics2D gs = image.createGraphics();
		
		//载入logo
		Image img = ImageIO.read(new File(logoPath));
		gs.drawImage(img, 125, 125, null);
		gs.dispose();
		img.flush();
		if(!ImageIO.write(image, format, file)){
			throw new IOException("Could not write an image of format " + format + " to " + file);  
		}
	}
	
	public static BufferedImage toBufferedImage(BitMatrix matrix){
		int width = matrix.getWidth();
		int height = matrix.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, matrix.get(x, y) ? BLACK : WHITE);
			}
		}
		return image;	
	}

	
	public static void main(String[] args) {
		try {
			get2CodeImage("www.baidu.com","d:\\test\\logotest\\logocode.jpg","d:\\test\\logotest\\logo.jpg");
		} catch (Exception e) {
			e.printStackTrace();
		} 
	}
}</span>



zxing相关jar包下载地址

http://repo1.maven.org/maven2/com/google/zxing/javase/3.1.0/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值