后台
/**
* 下载二维码
*
* @return
* @throws Exception
*/
public StreamedContent getFileUrl() throws Exception {
String text = "www.baidu.com";
System.out.println(text);
Hashtable hints = new Hashtable();
BitMatrix bitMatrix = new MultiFormatWriter().encode(text,
BarcodeFormat.QR_CODE, 300, 300, hints);
int width = bitMatrix.getWidth();
int height = bitMatrix.getHeight();
BufferedImage image = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
image.setRGB(x, y, bitMatrix.get(x, y) ? BLACK : WHITE);
}
}
ByteArrayOutputStream bs = new ByteArrayOutputStream();
ImageOutputStream imOut = ImageIO.createImageOutputStream(bs);
ImageIO.write(image, "png", imOut);
InputStream is = new ByteArrayInputStream(bs.toByteArray());
return new DefaultStreamedContent(is, "image/jpg", "erweima.jpg");
}
页面:
<h:commandLink value="下载二维码">
<p:fileDownload value="#{dowmLoanProductCodeBean.fileUrl}" />
</h:commandLink>