ZXing,JBarcodeBean 生成條形碼 并拼接圖片

jopen 8年前發布 | 46K 次閱讀 條形碼/二維碼開發包

由于JBarcodeBean 和JBarcode maven庫里資源很少,然后改為ZXing 生成條形碼

  public static void getJabCode(String code) throws IOException, WriterException{
          int width = 200, height = 100;   
            code="301010025000001877,301010025000001878";
            String[] args=code.split("\\,");
                ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream();
                BufferedImage imageNew1 = new BufferedImage(width, height*args.length ,
                        BufferedImage.TYPE_INT_RGB);
            for(int i=0;i<args.length;i++){
                  BitMatrix bitMatrix = new MultiFormatWriter().encode(args[i],     
                            BarcodeFormat.CODE_128, width, height, null);     
                  BufferedImage tempimg = toBufferedImage(bitMatrix);
                    int[] ImageArrayOne1 = new int[width * height];
                    ImageArrayOne1 = tempimg.getRGB(0, 0, width, height-10, ImageArrayOne1, 0,
                            width);
                    imageNew1.setRGB(0, 100*i, width, height-20, ImageArrayOne1, 0, width);
                    MatrixToImageWriter     
                            .writeToFile(bitMatrix, "png", new File("d:/test/zxing_01.png"));     
                    MatrixToImageWriter.writeToStream(bitMatrix, "png", byteOutputStream);

            }
            ImageIO.write(imageNew1, "png", new File("d:/test/zxing_03.png"));  
        }





查了好多博客 好多都用的 EAN8/EAN13/Code39 后來發現生成的條形碼都不能把 數字顯示完成 就改為了code128

JBarcodeBean jBarcodeBean = new JBarcodeBean();
         jBarcodeBean.setCodeType(new Code128());
            jBarcodeBean.setCode("301010025000001877");
            BufferedImage img1 = new BufferedImage(300, 100,
                    BufferedImage.TYPE_INT_RGB);
            img1 = jBarcodeBean.draw(img1);
            saveToPNG(img1, "4.png");

static void saveToJPEG(BufferedImage paramBufferedImage, String paramString) {
        saveToFile(paramBufferedImage, paramString, "jpeg");
    }
static void saveToFile(BufferedImage paramBufferedImage,
            String paramString1, String paramString2) {
        try {
            FileOutputStream localFileOutputStream = new FileOutputStream(
                    "d:/test/" + paramString1);
            ImageUtil.encodeAndWrite(paramBufferedImage, paramString2,
                    localFileOutputStream, 100, 100);
            localFileOutputStream.close();
        } catch (Exception localException) {
            localException.printStackTrace();
        }
    }

因為有需要把生成的條形碼 拼接在一個圖片里 保存

public byte[] getJabCode(String code) throws IOException{
        //String code="301010025000001877,301010025000001878";
        String[] args=code.split("\\,");
        JBarcodeBean jBarcodeBean = new JBarcodeBean();
        jBarcodeBean.setLabelPosition(JBarcodeBean.LABEL_BOTTOM);
        // 條形碼類型
        jBarcodeBean.setCodeType(new Code128());
        // jBarcodeBean.setCodeType(new Code39());
        //jBarcodeBean1.setLabelPosition(JBarcodeBean.LABEL_BOTTOM);
        int width1=200;
        int height1 =100;
        BufferedImage imageNew1 = new BufferedImage(width1, height1*args.length ,
                BufferedImage.TYPE_INT_RGB);

        for(int i=0;i<args.length;i++){
            jBarcodeBean.setCode(args[i]);
            BufferedImage tempimg = new BufferedImage(width1, height1,
                    BufferedImage.TYPE_INT_RGB);
            tempimg = jBarcodeBean.draw(tempimg); 
            int[] ImageArrayOne1 = new int[width1 * height1];
            ImageArrayOne1 = tempimg.getRGB(0, 0, width1, height1, ImageArrayOne1, 0,
                    width1);
            imageNew1.setRGB(0, 100*i, width1, height1, ImageArrayOne1, 0, width1);

        }
           ByteArrayOutputStream out = new ByteArrayOutputStream();
           boolean flag = ImageIO.write(imageNew1, "gif", out);
           byte[] b = out.toByteArray();
        //saveToPNG(imageNew1, "imageNew1.png");
        return b;
    }


來自: http://my.oschina.net/ruibo/blog/546364

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!