zxing 二维码、带logo二维码生成详解编程语言

普通二维码生成
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); 
    } 
}


带logo二维码

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(); 
		}  
	} 
}


zxing相关jar包下载地址

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

原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/14053.html

(0)
上一篇 2021年7月19日
下一篇 2021年7月19日

相关推荐

发表回复

登录后才能评论