酸梅粉冲调比例:怎样用iText导出条形码图片_天津-条形码-条形码制作-条码-条码打印机-条码碳带-条码...

来源:百度文库 编辑:九乡新闻网 时间:2024/04/29 08:35:51

怎样用iText导出条形码图片

时间:2011-03-21 09:12来源:未知 作者:admin 点击:247次用iText可导出条形码图片,但在图片中是不包含条形码的,只有在PDF中才有createImageWithBarcode这个方法,下面的代码是对导出的图片进行处理,将条形码加入到图片中,具体代码如下: Java代码 private static final int HEIGHT_SPACE= 20 ;//图片之间的间隔

用iText可导出条形码图片,但在图片中是不包含条形码的,只有在PDF中才有createImageWithBarcode这个方法,下面的代码是对导出的图片进行处理,将条形码加入到图片中,具体代码如下:

 

 

Java代码
  1. private static final int HEIGHT_SPACE = 20;// 图片之间的间隔   
  2.   
  3. public static void main(String[] args) throws Exception {   
  4.     List codeList = new ArrayList();   
  5.     codeList.add("ABCD124645765");   
  6.     codeList.add("ABCD12-4645-765");   
  7.     codeList.add("ABCD12464-5765");   
  8.     codeList.add("AB-CD1-246457-65");   
  9.     createBarcodeImage(200, 100, codeList);   
  10.     System.out.println("The image is created.");   
  11. }   
  12.   
  13. /**  
  14.  * Creates the barcode image.  
  15.  *   
  16.  * @param barCodeWidth  
  17.  *            生成条形码的宽度  
  18.  * @param barCodeHeight  
  19.  *            生成条形码的高度  
  20.  * @param codeList  
  21.  *            要生成条形码的字符集合  
  22.  *   
  23.  * @throws Exception  
  24.  *             the exception  
  25.  */  
  26. public static void createBarcodeImage(int barCodeWidth, int barCodeHeight,   
  27.         List codeList) throws Exception {   
  28.     // list不能为空   
  29.     Assert.assertTrue("The list can not empty.", codeList.size() > 0);   
  30.     // 图片宽度   
  31.     int imageWidth = (barCodeWidth + barCodeWidth / 2) * codeList.size() + barCodeWidth / 2;   
  32.     // 图片高度   
  33.     int imageHeight = barCodeHeight + HEIGHT_SPACE * 2;   
  34.     BufferedImage img = new BufferedImage(imageWidth, imageHeight, BufferedImage.TYPE_INT_RGB);   
  35.     Graphics2D g = (Graphics2D) img.getGraphics();   
  36.     g.fillRect(0, 0, imageWidth, imageHeight);   
  37.     Font font = new java.awt.Font("", java.awt.Font.PLAIN, 12);   
  38.     Barcode128 barcode128 = new Barcode128();   
  39.     FontRenderContext fontRenderContext = g.getFontRenderContext();   
  40.     // 条形码(文字)的高度   
  41.     int stringHeight = (int) font.getStringBounds("", fontRenderContext).getHeight();   
  42.     // 图片横坐标开始位置   
  43.     int startX = barCodeWidth / 2;   
  44.     // 图片纵坐标开始位置   
  45.     int imageStartY = (imageHeight - barCodeHeight - stringHeight) / 2;   
  46.     int stringStartY = imageStartY * 2 + barCodeHeight;// 条形码(文字)开始位置   
  47.     for (String code : codeList) {   
  48.         int codeWidth = (int) font.getStringBounds(code, fontRenderContext).getWidth();   
  49.         barcode128.setCode(code);   
  50.         Image codeImg = barcode128.createAwtImage(Color.black, Color.white);   
  51.         g.drawImage(codeImg, startX, imageStartY, barCodeWidth, barCodeHeight, Color.white, null);   
  52.         // 为图片添加条形码(文字),位置为条形码图片的下部居中   
  53.         AttributedString ats = new AttributedString(code);   
  54.         ats.addAttribute(TextAttribute.FONT, font, 0, code.length());   
  55.         AttributedCharacterIterator iter = ats.getIterator();   
  56.         // 设置条形码(文字)的颜色为蓝色   
  57.         g.setColor(Color.BLUE);   
  58.         // 绘制条形码(文字)   
  59.         g.drawString(iter, startX + (barCodeWidth - codeWidth) / 2, stringStartY);   
  60.         // 更改图片横坐标开始位置,图片之间的空隙为条形码的宽度的一半   
  61.         startX = startX + barCodeWidth / 2 + barCodeWidth;   
  62.     }   
  63.     g.dispose();   
  64.     OutputStream os = new BufferedOutputStream(new FileOutputStream("/home/admin/codeList.png"));   
  65.     ImageIO.write(img, "PNG", os);   
  66. }  
(责任编辑:admin)