I need to use barcodes in an xpages application, both to generate the barcode and to read it. Does anyone know of any libraries I can use on the application? Has anyone used these features in a xpages application? How do I find out the barcode pattern?
I am using ZXing for creating and reading different types of barcodes. For Java 6 there is an older version available: Solvoj ZXing
EDIT:
Managed Bean
package ch.hasselba.xpages;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import javax.faces.context.FacesContext;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.MatrixToImageConfig;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.ibm.xsp.webapp.XspHttpServletResponse;
public class ZXingBean {
public void generateDemoQRCode() {
FacesContext fc = FacesContext.getCurrentInstance();
XspHttpServletResponse response = (XspHttpServletResponse) fc.getExternalContext().getResponse();
try {
response.setContentType("image/png");
response.getOutputStream().write(generateQRCode("Hello World!", 200, 200 ));
} catch (IOException e) {
e.printStackTrace();
} catch (WriterException e) {
e.printStackTrace();
}
fc.responseComplete();
}
public byte[] generateQRCode(String text, int width, int height)
throws WriterException, IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
BitMatrix matrix = new MultiFormatWriter().encode(text,
BarcodeFormat.QR_CODE, width, height);
MatrixToImageWriter.writeToStream(matrix, "png", baos,
new MatrixToImageConfig());
return baos.toByteArray();
}
}
faces-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<faces-config>
<managed-bean>
<managed-bean-name>zxingBean</managed-bean-name>
<managed-bean-class>ch.hasselba.xpages.ZXingBean</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
</managed-bean>
</faces-config>
XAgent
<xp:view
xmlns:xp="http://www.ibm.com/xsp/core"
rendered="false"
viewState="nostate">
<xp:this.beforeRenderResponse><![CDATA[#{javascript:zxingBean.generateDemoQRCode()}]]></xp:this.beforeRenderResponse>
</xp:view>