二维码

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

你可能感兴趣的:(日常工作)