java-Barcode4j qr不起作用
作者:互联网
我在尝试通过条形码4j库使用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文件中.
解决方法:
有两个问题:
>条码4j库的2.1.0版本不包含创建二维码的部分
>从“嵌入了依赖项”的组合jar开始时,出现了问题
对于2.我只能提供一种解决方法:在构建< plugins>中将其添加到pom.xml中:
<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码). 那么该怎么办?从源代码构建!准备好您的老蚂蚁和简历!
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>
我想也应该将条形码4j-fop-ext更新为2.2-SNAPSHOT,这应该是更好的样式,但是我将其作为练习供读者阅读.
标签:barcode4j,apache-fop,xml,java 来源: https://codeday.me/bug/20191120/2045490.html