我正在使用lowagie.text.jar生成条形码。我需要使用spring MVC在jsp中显示这个生成的条形码。有人可以帮忙解决这个问题吗?
class BarcodeController{
public void service(HttpServletRequest request, HttpServletResponse response) throws IOException {
String strBarCode = request.getParameter(“barcode”);
byte[] pngImageData = null;
try {
Barcode39 code39ext = new Barcode39();
code39ext.setCode(strBarCode);
code39ext.setStartStopText(false);
code39ext.setExtended(true);
java.awt.Image rawImage = code39ext.createAwtImage(Color.BLACK, Color.WHITE);
BufferedImage outImage = new BufferedImage(rawImage.getWidth(null), rawImage.getHeight(null), BufferedImage.TYPE_INT_RGB);
outImage.getGraphics().drawImage(rawImage, 0, 0, null);
ByteArrayOutputStream bytesOut = new ByteArrayOutputStream();
ImageIO.write(outImage, “png”, bytesOut);
bytesOut.flush();
pngImageData = bytesOut.toByteArray();
} catch (Exception e) {
e.printStackTrace();
}
if (pngImageData != null) {
response.setContentLength(pngImageData.length);
response.setContentType(“image/png”);
OutputStream out = response.getOutputStream();
out.write(pngImageData);
out.flush();
out.close();
}
}
}