二维码

public class QrCodeUtil {
	// 定义一个LOGGER常量,用来打印log
	private static final Logger LOGGER = LoggerFactory.getLogger(QrCodeUtil.class);
	// 二维码颜色
	private static final int BLACK = 0xFF000000;
	private static final int WHITE = 0xFFFFFFFF;
	
	/**
	 * @todo  生成二维码
	 * @param content  二维码内容
	 * @param size     二维码大小
	 * @return 二维码图片
	 */
	public static BitMatrix QrCodeCreate(String content,int size) {
		BitMatrix bitMatrix = null;
		
		try {
			
			 Map<EncodeHintType, Object> hints = new HashMap<EncodeHintType, Object>();  
		     hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");  
		     hints.put(EncodeHintType.MARGIN, 0);
		     bitMatrix = new QRCodeWriter().encode(content,
		     BarcodeFormat.QR_CODE, size*size, size*size,hints);
		        
		     int[] rec = bitMatrix.getEnclosingRectangle();  
		     int resWidth = rec[2] + 1;  
		     int resHeight = rec[3] + 1;  
		     BitMatrix resMatrix = new BitMatrix(resWidth, resHeight);  
		     resMatrix.clear();  

			for (int i = 0; i < resWidth; i++) {
				for (int j = 0; j < resHeight; j++) {
					if (bitMatrix.get(i + rec[0], j + rec[1])) {
						resMatrix.set(i, j);
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return bitMatrix;
	}
	
	
	/**
	 * @todo  生成二维码:压缩多个文件成一个zip文件
	 * @param srcfile:源文件列表
	 * @param zipfile:压缩后的文件
	 */
	public static void zipFiles(File[] srcfile, File zipfile) {
		byte[] buf = new byte[1024];
		try {
			// ZipOutputStream类:完成文件或文件夹的压缩
			ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipfile));
			for (int i = 0; i < srcfile.length; i++) {
				FileInputStream in = new FileInputStream(srcfile[i]);
				out.putNextEntry(new ZipEntry(srcfile[i].getName()));
				int len;
				while ((len = in.read(buf)) > 0) {
					out.write(buf, 0, len);
				}
				out.closeEntry();
				in.close();
			}
			out.close();
		} catch (Exception e) {
			e.printStackTrace();
			LOGGER.error("downQrCode error : {}", ExceptionUtil.getTrace(e));
		}
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值