草庐IT

java - Barcode4j + qr 不工作

coder 2024-06-30 原文

我在尝试通过 barcode4j 库生成带有二维码的图像时遇到问题。 我读过 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。主.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();
        }
    }
}

模板.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. barcode4j 库的 2.1.0 版本不包含创建二维码的部分
  2. 从“嵌入了依赖项”的组装 jar 启动时出现问题

对于 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)

一段时间后,我发现代码想要告诉我没有任何东西可以呈现二维码。 (例如,将您的 <bc:qr /> 替换为 <bc:code39/>,您应该会看到该库生成了一些东西(当然不是二维码)。

那怎么办?从源头构建!有你的好老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现在,我得到一个 fop.pdf里面有二维码。 (它看起来不漂亮,但对配置进行了一些调整,例如添加 <bc:module-width>2mm</bc:module-width> 或任何确定修复它的方法)。

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

关于java - Barcode4j + qr 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28918627/

有关java - Barcode4j + qr 不工作的更多相关文章

  1. ruby-on-rails - 由于 "wkhtmltopdf",PDFKIT 显然无法正常工作 - 2

    我在从html页面生成PDF时遇到问题。我正在使用PDFkit。在安装它的过程中,我注意到我需要wkhtmltopdf。所以我也安装了它。我做了PDFkit的文档所说的一切......现在我在尝试加载PDF时遇到了这个错误。这里是错误:commandfailed:"/usr/local/bin/wkhtmltopdf""--margin-right""0.75in""--page-size""Letter""--margin-top""0.75in""--margin-bottom""0.75in""--encoding""UTF-8""--margin-left""0.75in""-

  2. ruby-on-rails - 'compass watch' 是如何工作的/它是如何与 rails 一起使用的 - 2

    我在我的项目目录中完成了compasscreate.和compassinitrails。几个问题:我已将我的.sass文件放在public/stylesheets中。这是放置它们的正确位置吗?当我运行compasswatch时,它不会自动编译这些.sass文件。我必须手动指定文件:compasswatchpublic/stylesheets/myfile.sass等。如何让它自动运行?文件ie.css、print.css和screen.css已放在stylesheets/compiled。如何在编译后不让它们重新出现的情况下删除它们?我自己编译的.sass文件编译成compiled/t

  3. ruby-on-rails - 如何优雅地重启 thin + nginx? - 2

    我的瘦服务器配置了nginx,我的ROR应用程序正在它们上运行。在我发布代码更新时运行thinrestart会给我的应用程序带来一些停机时间。我试图弄清楚如何优雅地重启正在运行的Thin实例,但找不到好的解决方案。有没有人能做到这一点? 最佳答案 #Restartjustthethinserverdescribedbythatconfigsudothin-C/etc/thin/mysite.ymlrestartNginx将继续运行并代理请求。如果您将Nginx设置为使用多个上游服务器,例如server{listen80;server

  4. ruby - 无法让 RSpec 工作—— 'require' : cannot load such file - 2

    我花了三天的时间用头撞墙,试图弄清楚为什么简单的“rake”不能通过我的规范文件。如果您遇到这种情况:任何文件夹路径中都不要有空格!。严重地。事实上,从现在开始,您命名的任何内容都没有空格。这是我的控制台输出:(在/Users/*****/Desktop/LearningRuby/learn_ruby)$rake/Users/*******/Desktop/LearningRuby/learn_ruby/00_hello/hello_spec.rb:116:in`require':cannotloadsuchfile--hello(LoadError) 最佳

  5. java - 等价于 Java 中的 Ruby Hash - 2

    我真的很习惯使用Ruby编写以下代码:my_hash={}my_hash['test']=1Java中对应的数据结构是什么? 最佳答案 HashMapmap=newHashMap();map.put("test",1);我假设? 关于java-等价于Java中的RubyHash,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/22737685/

  6. ruby-on-rails - rspec should have_select ('cars' , :options => ['volvo' , 'saab' ] 不工作 - 2

    关闭。这个问题需要detailsorclarity.它目前不接受答案。想改进这个问题吗?通过editingthispost添加细节并澄清问题.关闭8年前。Improvethisquestion在首页我有:汽车:VolvoSaabMercedesAudistatic_pages_spec.rb中的测试代码:it"shouldhavetherightselect"dovisithome_pathit{shouldhave_select('cars',:options=>['volvo','saab','mercedes','audi'])}end响应是rspec./spec/request

  7. ruby-on-rails - s3_direct_upload 在生产服务器中不工作 - 2

    在Rails4.0.2中,我使用s3_direct_upload和aws-sdkgems直接为s3存储桶上传文件。在开发环境中它工作正常,但在生产环境中它会抛出如下错误,ActionView::Template::Error(noimplicitconversionofnilintoString)在View中,create_cv_url,:id=>"s3_uploader",:key=>"cv_uploads/{unique_id}/${filename}",:key_starts_with=>"cv_uploads/",:callback_param=>"cv[direct_uplo

  8. java - 从 JRuby 调用 Java 类的问题 - 2

    我正在尝试使用boilerpipe来自JRuby。我看过guide从JRuby调用Java,并成功地将它与另一个Java包一起使用,但无法弄清楚为什么同样的东西不能用于boilerpipe。我正在尝试基本上从JRuby中执行与此Java等效的操作:URLurl=newURL("http://www.example.com/some-location/index.html");Stringtext=ArticleExtractor.INSTANCE.getText(url);在JRuby中试过这个:require'java'url=java.net.URL.new("http://www

  9. ruby - JetBrains RubyMine 3.2.4 调试器不工作 - 2

    使用Ruby1.9.2运行IDE提示说需要gemruby​​-debug-base19x并提供安装它。但是,在尝试安装它时会显示消息Failedtoinstallgems.Followinggemswerenotinstalled:C:/ProgramFiles(x86)/JetBrains/RubyMine3.2.4/rb/gems/ruby-debug-base19x-0.11.30.pre2.gem:Errorinstallingruby-debug-base19x-0.11.30.pre2.gem:The'linecache19'nativegemrequiresinstall

  10. java - 我的模型类或其他类中应该有逻辑吗 - 2

    我只想对我一直在思考的这个问题有其他意见,例如我有classuser_controller和classuserclassUserattr_accessor:name,:usernameendclassUserController//dosomethingaboutanythingaboutusersend问题是我的User类中是否应该有逻辑user=User.newuser.do_something(user1)oritshouldbeuser_controller=UserController.newuser_controller.do_something(user1,user2)我

随机推荐