Barcode4j + qr无效

时间:2015-03-07 18:45:18

标签: java xml apache-fop barcode4j

尝试通过barcode4j库生成带有qr代码的图像时遇到问题。 我已经阅读了following guide但是没有成功。 所以这是我的代码:

的pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>fopqr</groupId>
    <artifactId>fopqr</artifactId>
    <version>1.0-SNAPSHOT</version>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <configuration>
                    <archive>
                        <manifest>
                            <mainClass>Main</mainClass>
                        </manifest>
                    </archive>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <dependencies>
        <dependency>
            <groupId>org.apache.xmlgraphics</groupId>
            <artifactId>fop</artifactId>
            <version>1.0</version>
        </dependency>

        <dependency>
            <groupId>net.sf.barcode4j</groupId>
            <artifactId>barcode4j</artifactId>
            <version>2.1</version>
        </dependency>

        <dependency>
            <groupId>net.sf.barcode4j</groupId>
            <artifactId>barcode4j-fop-ext</artifactId>
            <version>2.1</version>
        </dependency>

        <dependency>
            <groupId>com.google.zxing</groupId>
            <artifactId>core</artifactId>
            <version>3.2.0</version>
        </dependency>
    </dependencies>
</project>

2。 Main.java

import org.apache.fop.apps.Fop;
import org.apache.fop.apps.FopFactory;
import org.apache.xmlgraphics.util.MimeConstants;

import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.sax.SAXResult;
import javax.xml.transform.stream.StreamSource;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;


public class Main {
    public static void main(String[] args) throws Exception{
        FopFactory fopFactory = FopFactory.newInstance();
        OutputStream out = new BufferedOutputStream(new FileOutputStream(new File("/home/user/fop.pdf")));

        try {
            Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, out);
            TransformerFactory factory = TransformerFactory.newInstance();
            Transformer transformer = factory.newTransformer(); // identity transformer
            Source src = new StreamSource(new File("/home/user/template.xsl"));
            Result res = new SAXResult(fop.getDefaultHandler());
            transformer.transform(src, res);
        }
        finally {
            out.close();
        }
    }
}

template.xsl

<?xml version="1.0" encoding="UTF-8"?>
<fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format" font-family="sans-serif" font-size="10pt">
  <fo:layout-master-set>
    <fo:simple-page-master master-name="A4" page-height="29.7cm" page-width="21cm" margin-top="2cm" margin-bottom="0.4cm" margin-left="2cm" margin-right="2cm">
      <fo:region-body margin-bottom="2.3cm"/>
      <fo:region-after extent="2.2cm"/>
    </fo:simple-page-master>
  </fo:layout-master-set>
  <fo:page-sequence master-reference="A4" language="en">
    <fo:flow flow-name="xsl-region-body">
                <fo:block>
                  <fo:instream-foreign-object>
                    <bc:barcode xmlns:bc="http://barcode4j.krysalis.org/ns" message="hello world">
                      <bc:qr/>
                    </bc:barcode>
                  </fo:instream-foreign-object>
                </fo:block>
                 </fo:flow>
  </fo:page-sequence>
</fo:root>

当我尝试使用

启动应用程序时
kirill@kirill:~/work/source/fop$ java -jar target/fopqr-1.0-SNAPSHOT-jar-with-dependencies.jar

我得到了

Mar 07, 2015 10:42:02 PM org.apache.fop.events.LoggingEventListener processEvent
WARNING: Unknown formatting object "{http://barcode4j.krysalis.org/ns}barcode" encountered (a child of fo:instream-foreign-object}. (See position 13:99)
Mar 07, 2015 10:42:02 PM org.apache.fop.events.LoggingEventListener processEvent
WARNING: Unknown formatting object "{http://barcode4j.krysalis.org/ns}qr" encountered (a child of barcode}. (See position 14:31)
Mar 07, 2015 10:42:02 PM org.apache.fop.events.LoggingEventListener processEvent
SEVERE: The intrinsic dimensions of an instream-foreign-object could not be determined. (See position 12:47)

我做错了什么?

更新:我还想补充一点,我读了FAQ:4.1。 FOP扩展失败。但正如您所看到的,我将所有必需的库添加到我的pom.xml文件中。

1 个答案:

答案 0 :(得分:3)

有两个问题:

  1. barcode4j库的2.1.0版本不包含创建qr代码的部分
  2. 从装配好的jar中启动东西时出现问题&#34;嵌入了依赖关系&#34;
  3. 对于2.我只能提供一种解决方法:在构建pom.xml中添加到<plugins>

            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <configuration>
                    <mainClass>Main</mainClass>
                </configuration>
            </plugin>
    

    然后您可以使用mvn exec:java运行,突然您会看到不同的错误消息:

    org.krysalis.barcode4j.BarcodeException: No barcode configuration element not found
    at org.krysalis.barcode4j.BarcodeUtil.createBarcodeGenerator(BarcodeUtil.java:110)
    at org.krysalis.barcode4j.BarcodeUtil.createBarcodeGenerator(BarcodeUtil.java:146)
    at org.krysalis.barcode4j.fop.BarcodeElement.getDimension(BarcodeElement.java:76)
    

    过了一会儿,我发现代码想告诉我没有什么可以渲染qr代码。 (例如,将<bc:qr />替换为<bc:code39/>,您应该会看到lib产生的内容(当然不是QR代码)。

    那该怎么办?从源头构建!准备好您的好antcvs

    cvs -z3 -d:pserver:anonymous@barcode4j.cvs.sourceforge.net:/cvsroot/barcode4j co barcode4j
    cd barcode4j
    ant
    

    应该这样做,除了告诉maven。当然有一种理智的方式,但对我有用的是:

    cp ~/.m2/repository/net/sf/barcode4j/barcode4j/2.1/barcode4j-2.1.pom  pom.xml
    vi pom.xml  # change  <version>2.1</version> to <version>2.2-SNAPSHOT</version>
    mvn -Dfile=build/barcode4j.jar -DpomFile=pom.xml  install:install-file
    

    现在将jar伪造成我们的maven仓库后,修复原始pom.xml并更新依赖关系(包括将zxing降级到1.7,因为新版本不兼容):

        <dependency>
            <groupId>net.sf.barcode4j</groupId>
            <artifactId>barcode4j</artifactId>
            <version>2.2-SNAPSHOT</version>
        </dependency>
    
        <dependency>
            <groupId>net.sf.barcode4j</groupId>
            <artifactId>barcode4j-fop-ext</artifactId>
            <version>2.1</version>
        </dependency>
    
        <dependency>
            <groupId>com.google.zxing</groupId>
            <artifactId>core</artifactId>
            <version>1.7</version>
        </dependency>
    

    我想将barcode4j-fop-ext更新为2.2-SNAPSHOT也应该是更好的风格,但我将此作为练习留给读者。 ;)

    无论如何,如果我现在运行mvn exec:java,我会得到一张带有QR码的fop.pdf。 (它看起来并不漂亮,但有些人正在摆弄配置,比如添加<bc:module-width>2mm</bc:module-width>或者确定修复它。)

    我必须承认,我更愿意将其留给其他人来弄清楚为什么从装配好的jar中运行相同的代码不起作用。