[英]Barcode4j + qr not working
I have problems trying to generate an image with qr code via barcode4j library. 我试图通过barcode4j库生成带有qr代码的图像时遇到问题。 I've read the following guide but that didn't work out.
我已阅读以下指南,但没有成功。 So here is my code:
所以这是我的代码:
pom.xml 的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 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 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>
When I try to launch the application with 当我尝试启动应用程序时
kirill@kirill:~/work/source/fop$ java -jar target/fopqr-1.0-SNAPSHOT-jar-with-dependencies.jar
I get 我明白了
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)
What am I doing wrong? 我究竟做错了什么?
UPDATE : I also want to add that I read FAQ : 4.1. 更新 :我还想补充一点,我读了FAQ :4.1。 The FOP extension fails.
FOP扩展失败。 But as you can see I added all required libraries to my pom.xml file.
但正如您所看到的,我将所有必需的库添加到我的pom.xml文件中。
There are two issues: 有两个问题:
For 2. I can only offer a workaround: add to your pom.xml
in the build <plugins>
: 对于2.我只能提供一种解决方法:在build
<plugins>
添加到pom.xml
:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<configuration>
<mainClass>Main</mainClass>
</configuration>
</plugin>
then you can run things with mvn exec:java
and suddenly you should see a different error message: 然后你可以使用
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)
After a while I figured out the code wanted to tell me that there is nothing to render qr-codes. 过了一会儿,我发现代码想告诉我没有什么可以渲染qr代码。 (eg replace your
<bc:qr />
by <bc:code39/>
and you should see the lib produces something (not a QR Code of course). (例如,用
<bc:code39/>
替换你的<bc:qr />
,你应该看到lib产生的东西(当然不是QR码)。
So what to do? 那么该怎么办? Build from source!
从源头构建! Have your good old
ant
and cvs
ready! 准备好你的老
ant
和cvs
!
cvs -z3 -d:pserver:anonymous@barcode4j.cvs.sourceforge.net:/cvsroot/barcode4j co barcode4j
cd barcode4j
ant
That should do it, except for telling maven. 应该这样做,除了告诉maven。 Sure there is a sane way to do it, but what worked for me is:
当然有一种理智的方式,但对我有用的是:
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
Now after having faked the jar into our maven repo, fix up the original pom.xml
and update the dependencies (including a downgrade of zxing to 1.7, because the newer version is not compatible): 现在将jar伪装成我们的maven repo后,修复原来的
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>
I guess it should have been better style to update the barcode4j-fop-ext
to 2.2-SNAPSHOT too, but I leave this as an exercise for the reader. 我想将
barcode4j-fop-ext
更新为2.2-SNAPSHOT应该是更好的风格,但我把这作为练习给读者留下了。 ;) ;)
Anyway, if I run mvn exec:java
now, I get a fop.pdf
with a QR-Code in it. 无论如何,如果我现在运行
mvn exec:java
,我会得到一个带有QR码的fop.pdf
。 (It does not look pretty, but some fiddling with the config like adding <bc:module-width>2mm</bc:module-width>
or whatever sure fixes it). (它看起来不漂亮,但有些摆弄配置,比如添加
<bc:module-width>2mm</bc:module-width>
或者确定修复它)。
I have to admit that I prefer to leave it to someone else to figure out why running the same code from an assembled jar does not work. 我不得不承认,我更愿意将它留给其他人来弄清楚为什么从组装的jar中运行相同的代码不起作用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.