注意引入zxing包
zxing依赖是为了生成二维码;
public void testEncode() throws WriterException, IOException {
int width = 200; // 图像宽度 int height = 200; // 图像高度 Map hints = new HashMap();
hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
String temp_path="E:";
File file = new File(temp_path);
File zipFile = new File(temp_path+File.separator+"xx.zip");
ZipOutputStream zipOut = new ZipOutputStream(new FileOutputStream(zipFile));
for (int i = 0; i < 5; i++) {
String content = "http://www.baidu.com";
BitMatrix bitMatrix = new QRCodeWriter().encode(content, BarcodeFormat.QR_CODE, width, height, hints);// 生矩阵 BufferedImage image = MatrixToImageWriter.toBufferedImage(bitMatrix);// 输出图像 ByteArrayOutputStream bs = new ByteArrayOutputStream();
ImageOutputStream imOut = ImageIO.createImageOutputStream(bs);
ImageIO.write(image, "jpg", imOut);
InputStream is = new ByteArrayInputStream(bs.toByteArray());
zipOut.putNextEntry(new ZipEntry( file.getName()+ File.separator +i+".jpg"));
int temp = 0;
while ((temp = is.read()) != -1) {
zipOut.write(temp);
}
is.close();
}
zipOut.close();
}
网友评论