title: How to display a barcode image in jsp
body: <p>I'm using lowagie.text.jar to generate barcode. I need to display this generated barcode in jsp using spring MVC. Can someone help how to acheieve this?</p>
  
  <pre><code>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();
  }
  }
  }
  </code></pre>