使用B4j创建一个字节数组条形码

时间:2016-05-02 06:28:00

标签: java rest barcode barcode4j

@Override
    public String generateBrcodeForId(String Id) {
        BarcodeUtil util = BarcodeUtil.getInstance();
        BarcodeGenerator gen;
        ByteArrayOutputStream byteArrayOutputStream = null;
        try {
            gen = util.createBarcodeGenerator(buildCfg("code128"));
        OutputStream fout;
            fout = new FileOutputStream("code128.jpg");
              byteArrayOutputStream = new ByteArrayOutputStream();
        int resolution = 200;
        BitmapCanvasProvider canvas = new BitmapCanvasProvider(
                fout, "image/jpeg", resolution, BufferedImage.TYPE_BYTE_BINARY, false, 0);
        gen.generateBarcode(canvas, "12345678");
            canvas.finish();
        } catch (ConfigurationException | BarcodeException | IOException  e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }


     private static Configuration buildCfg(String type) {
            DefaultConfiguration cfg = new DefaultConfiguration("barcode");

            //Bar code type
            DefaultConfiguration child = new DefaultConfiguration(type);
              cfg.addChild(child);

              //Human readable text position
              DefaultConfiguration attr = new DefaultConfiguration("human-readable");
              DefaultConfiguration subAttr = new DefaultConfiguration("placement");
                subAttr.setValue("bottom");
                attr.addChild(subAttr);

                child.addChild(attr);
            return cfg;
          }

使用Barcode4j创建了条形码,这里创建了一个图像code128.jpg。我可以创建一个字节数组而不是在文件系统中创建一个图像并将其发送到Web服务客户端。

1 个答案:

答案 0 :(得分:0)

获得解决方案,使用ByteArrayOutputStream解决它

@Override
    public byte[] generateBarcodeForId(String Id) {
        BarcodeUtil util = BarcodeUtil.getInstance();
        BarcodeGenerator gen;
        ByteArrayOutputStream bao = null;
        try {
            gen = util.createBarcodeGenerator(buildCfg("code128"));
        OutputStream fout;
        int resolution = 100;
        bao = new ByteArrayOutputStream();
        BitmapCanvasProvider canvas = new BitmapCanvasProvider(
                bao, "image/jpeg", resolution, BufferedImage.TYPE_BYTE_BINARY, false, 0);
        gen.generateBarcode(canvas, Id);
        canvas.finish();

        } catch (ConfigurationException | BarcodeException | IOException  e) {
            e.printStackTrace();
        } finally {  
            try {
                bao.close();
            } catch (IOException e) {
                e.printStackTrace();
            }  
        }  


        return bao.toByteArray();
    }

     private static Configuration buildCfg(String type) {
            DefaultConfiguration cfg = new DefaultConfiguration("barcode");

            //Bar code type
            DefaultConfiguration child = new DefaultConfiguration(type);
              cfg.addChild(child);

              //Human readable text position
              DefaultConfiguration attr = new DefaultConfiguration("human-readable");
              DefaultConfiguration subAttr = new DefaultConfiguration("placement");
                subAttr.setValue("bottom");
                attr.addChild(subAttr);

                child.addChild(attr);
            return cfg;
          }