欢迎光临Software MyZone,有问题可留言或到站点论坛发帖,争取第一时间帮忙解决 || 站点论坛:火龙论坛 || 淘宝小店:应小心的易淘屋 【欢迎大家提建设性意见】

二维码生成及图片合并工具类

欢迎热爱编程的朋友们参与到cocos2d-x编程中,为了给大家提供良好的交流环境,网站以开启QQ群
Software MyZone:66202765(群号,欢迎加入,若满,请加1群)
Software MyZone 1群(2dx):286504621
Software MyZone 2群(山东):204954191
Software MyZone【Java群】:162865493
【加群请写:Software MyZone或者是firedragonpzy】
淘宝店:【应小心的易淘屋】初次开店,大家多多支持……
群论坛:【火龙论坛】正试运营阶段,欢迎大家多提些建设性意见……
群问答:【火龙问答】正试运营阶段,欢迎大家多提些建设性意见……
私有手游:【火龙游戏】公益手游-私有手游

我的digitalocean推广链接:https://www.digitalocean.com/?refcode=65dfdb158f1a,Everyone you refer gets $10 in credit,从此链接注册,你将获得10美元。

package com.zhiqu.utils;

import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

import javax.imageio.ImageIO;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

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.h.base.util.StringUtil;

/**
 * <p>Title: QRCodeUtil</p>
 * <p>Description:二维码生成及图片合并工具类</p>
 * <p>Copyright © 2016 Software MyZone. All Rights Reserved</p> 
 * @author        firedragonpzy
 * @date        2017年4月25日 上午10:09:06
 * @version 1.0
 */
public class QRCodeUtil {

	private static Logger logger = LoggerFactory.getLogger(QRCodeUtil.class);

	public static boolean a() {
		return false;
	}

	/**
	 * <p>Title: generateQR</p>
	 * <p>Description:生成二维码</p>
	 * @author   firedragonpzy
	 * @date     2017-4-17 上午11:24:36
	 * @param content
	 * @param path
	 * @param width
	 * @param height
	 * @param fileName
	 * @param format
	 * @return
	 */
	public static String generateQR(String content, String path, String fileName, int width,
			int height,String format,boolean isForceGenerate) {
		File file = new File(path, fileName);
		if (!isForceGenerate) {
			if (file.exists()) {
				return path+fileName;
			}
		}
		String outputPath="";
		MultiFormatWriter multiFormatWriter = new MultiFormatWriter();
		Map<object , String> hints = new HashMap</object><object , String>();
		hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
		BitMatrix bitMatrix = null;
		try {
			bitMatrix = multiFormatWriter.encode(content,
					BarcodeFormat.QR_CODE, width, height);
		} catch (WriterException e) {
			e.printStackTrace();
			logger.error("#qr_generate failed", e);
		}
		
		try {
			if (null != bitMatrix) {
				MatrixToImageWriter.writeToFile(bitMatrix, format, file);
				outputPath=path+fileName;
			} else {
				logger.error("#qr_generate failed,bitmatrix is null");
			}
		} catch (IOException e) {
			e.printStackTrace();
			logger.error("#qr_generate failed", e);
		}
		return outputPath;
	}

	/**
	 * <p>Title: overlapImage</p>
	 * <p>Description:合并重叠图片</p>
	 * @author   firedragonpzy
	 * @date     2017-4-17 上午11:33:50
	 * @param descPath
	 * @param srcPath
	 * @param outputPath
	 * @param format
	 * @param originalX
	 * @param originalY
	 * @return
	 */
	public static final String overlapImage(String descPath, String srcPath,
			String outputPath, String format, int originalX, int originalY) {
		String returnPath="";
		try {
			BufferedImage desc = null;
			URL url = null;
			if (StringUtil.isEmpty(descPath) || StringUtil.isEmpty(srcPath) || StringUtil.isEmpty(outputPath)) {
				return returnPath;
			}
			if (StringUtil.isEmpty(format)) {
				format="png";
			}
			if (descPath.contains("http://") || descPath.contains("https://")) {
				url = new URL(descPath);
				desc=ImageIO.read(url.openStream());
			}else {
				desc=ImageIO.read(new File(descPath));
			} 
			
			BufferedImage src = null;
			if (srcPath.contains("http://") || descPath.contains("https://")) {
				url = new URL(srcPath);
				src=ImageIO.read(url.openStream());
			}else {
				src=ImageIO.read(new File(srcPath));
			} 
			
			int width = desc.getWidth();
			int height = desc.getHeight();
			Image image = desc.getScaledInstance(width, height,
					Image.SCALE_SMOOTH);
			BufferedImage bufferedImage = new BufferedImage(width, height,
					BufferedImage.TYPE_INT_BGR);
			Graphics2D g = bufferedImage.createGraphics();
			g.drawImage(image, 0, 0, null);
			if (originalX == 0) {
				originalX = width / 2;
			}
			if (originalY == 0) {
				originalY = height / 2;
			}
			g.drawImage(src, originalX, originalY, src.getWidth(),
					src.getHeight(), null);
			ImageIO.write(bufferedImage, format, new File(outputPath));
			returnPath=outputPath;
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("#qr_generate_overlap failed",e);
			return returnPath;
		}
		return returnPath;
	}

}

Tags: