生成带logo的二维码图片

源码如下:

private static final int IMG_WIDTH = 160;
private static final int IMG_HEIGHT = 160;
private static final String IMG_FORMAT = "JPEG";

/**
 * 生成二维码
 * @param filepath 图标输出路径
 * @param logoPath logo图片全路径
 * @param content 二维码内容
 * @return 文件名
 */
public static String genQrCode(String filepath, String logoPath, String content, int cutWidth) throws Exception {
	File file = new File(filepath);
	if (!file.exists()) {
		file.mkdirs();
	}

	if (!filepath.endsWith("/") && !filepath.endsWith("\\")) {
		filepath += File.separator;
	}

	String filename = "qrcode_tmp.jpeg";
	String tmpFilePath = filepath + filename;

	//生成二维码图片
	Hashtable<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>();
	hints.put(EncodeHintType.CHARACTER_SET, "GBK");
	hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);

	MultiFormatWriter writer = new MultiFormatWriter();
	BitMatrix matrix = writer.encode(content, BarcodeFormat.QR_CODE, IMG_WIDTH, IMG_HEIGHT, hints);
	
	FileOutputStream fos = null;
	try{
		fos = new FileOutputStream(new File(tmpFilePath));
		MatrixToImageWriter.writeToStream(matrix, IMG_FORMAT, fos);
		fos.flush();
	}finally{
		if(fos != null){
			fos.close();
		}
	}
	
	//裁剪二维码图片
	String destFileName = "qrcode.jpeg";
	String destFilepath = filepath + destFileName;
	
	cut(tmpFilePath, destFilepath, logoPath, cutWidth, cutWidth, IMG_WIDTH-cutWidth*2, IMG_HEIGHT-cutWidth*2);
	
	return destFileName;
}

/**
 * 切割图片
 * @param sourPath 原图片全路径
 * @param destPath 目标图片全路径
 * @param logoPath logo图片全路径
 * @param x x坐标
 * @param y y坐标
 * @param width 宽度
 * @param height 高度
 */
private static void cut(String sourPath, String destPath, String logoPath, 
		int x, int y, int width, int height) throws IOException {  
	FileInputStream is = null;  
	ImageInputStream iis = null;  
	
	try {   
		is = new FileInputStream(sourPath); 
		iis = ImageIO.createImageInputStream(is);   

		Iterator<ImageReader> it = ImageIO.getImageReadersByFormatName(IMG_FORMAT); 
		ImageReader reader = it.next();
		reader.setInput(iis, true);  

		ImageReadParam param = reader.getDefaultReadParam();
		param.setSourceRegion(new Rectangle(x, y, width, height));  

		BufferedImage bi = reader.read(0, param); 
		
		if(StringUtils.isNotEmpty(logoPath)){
			BufferedImage logoImg = ImageIO.read(new File(logoPath));
			createQRCodeBitmap(bi, logoImg, destPath);
		}else{
			ImageIO.write(bi, IMG_FORMAT, new File(destPath));
		}
		
	} finally {  
		if (is != null){
			try {
				is.close();
			} catch (IOException e) {
				e.printStackTrace();
			}  
		}
		
		if (iis != null){
			try {
				iis.close();
			} catch (IOException e) {
				e.printStackTrace();
			}  
		}
	} 
}  

/**
 * 为二维码图片添加logo图片
 * @param qrcodeImg 二维码图片
 * @param logoImg logo图片
 * @param destFilePath 目标图片路径
 */
private static void createQRCodeBitmap(BufferedImage qrcodeImg, BufferedImage logoImg, String destFilePath) throws IOException { 
	BufferedImage image = new BufferedImage(qrcodeImg.getWidth(), qrcodeImg.getHeight(), BufferedImage.TYPE_INT_RGB);
	Graphics2D g = (Graphics2D)image.getGraphics(); 
	
	//画二维码图片
	g.drawImage(qrcodeImg, 0, 0, null);
	
	int x = (qrcodeImg.getWidth() - logoImg.getWidth()) / 2;
	int y = (qrcodeImg.getHeight() - logoImg.getHeight()) / 2;
	
	//在中间画logo图片
	g.drawImage(logoImg, x, y, null);
	
	//生成目标图片
	ImageIO.write(image, IMG_FORMAT, new File(destFilePath));
}  

public static void main(String[] args) {
	try {
		String content = "http://www.163.com";
		String filename = genQrCode("D:\\", "D:\\logo.jpg", content, 25);
		System.out.println(filename);
	} catch (Exception e) {
		e.printStackTrace();
	}
}

猜你喜欢

转载自chenjumin.iteye.com/blog/2300913
今日推荐