【问题标题】:QR code generation into scalable EPS with Java使用 Java 将二维码生成为可扩展的 EPS
【发布时间】:2023-03-27 01:40:02
【问题描述】:

我目前正在开发一个使用 ZXing 生成 QR 码作为图像的应用程序。这是一个简单的例子:

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;

public class MyQREncoder {

    /**
     * @param args
     * @throws WriterException 
     * @throws IOException 
     * @throws FileNotFoundException 
     */
    public static void main(String[] args) throws WriterException, FileNotFoundException, IOException {
        String text = "Some text to encode";
        int width = 300;
        int height = 300;
        BitMatrix bitMatrix = new MultiFormatWriter().encode(text, BarcodeFormat.QR_CODE,width,height);
        String file = "plop.png";
        String format = "png";
        MatrixToImageWriter.writeToStream(bitMatrix, format, new FileOutputStream(new File(file)));
    }

}

这会生成一个带有可刷写二维码的 PNG 文件,上面写着“一些要编码的文本”。

我的问题是,如果我尝试将格式更改为 eps,那么我会得到一个空文件。我们目前使用的解决方案是通过 imagemagick convert 实用程序将 png 文件转换为 eps。但是给定的 EPS 只是嵌入原始图像,并且不能很好地缩放(特别是在打印时)。

有人知道是否有任何开源解决方案(使用 Zxing 或其他)来构建可扩展的 Eps 文件? (或任何矢量格式,例如)

【问题讨论】:

    标签: java qr-code


    【解决方案1】:

    编辑:这是一个完整的 Java 解决方案:

    PrintStream psFile = /* Open file */;
    
    final int blockSize = 4;
    
    psFile.println("%!PS-Adobe-3.0 EPSF-3.0");
    psFile.println("%%BoundingBox: 0 0 " + bitMatrix.getWidth() * blockSize + " " + bitMatrix.getHeight() * blockSize);
    
    psFile.print("/bits [");
    for(int y = 0; y < bitMatrix.getHeight(); ++y) {
        for(int x = 0; x < bitMatrix.getWidth(); ++x) {
            psFile.print(bitMatrix.get(x, y) ? "1 " : "0 ");
        }           
    }
    psFile.println("] def");
    
    psFile.println("/width " + bitMatrix.getWidth() + " def");
    psFile.println("/height " + bitMatrix.getHeight() + " def");
    
    psFile.println(
            "/y 0 def\n" + 
            blockSize + " " + blockSize + " scale\n" + 
            "height {\n" +
            "   /x 0 def\n" +
            "   width {\n" +
            "      bits y width mul x add get 1 eq {\n" +
            "         newpath\n" +
            "         x y moveto\n" +
            "         0 1 rlineto\n" +
            "         1 0 rlineto\n" +
            "         0 -1 rlineto\n" +
            "         closepath\n" +
            "         fill\n" +
            "      } if\n" +
            "      /x x 1 add def\n" +
            "   } repeat\n" +
            "   /y y 1 add def\n" +
            "} repeat\n");
    psFile.close();
    

    自己生成 PS 文件怎么样?像这样的:

    %!PS-Adobe-3.0 EPSF-3.0
    %%BoundingBox: 0 0 288 288
    
    /bits [0 0 1 0  1 0 1 1  0 1 1 0  1 1 0 0] def
    
    /width 4 def
    /height 4 def
    /y 0 def
    
    72 72 scale
    
    height {
       /x 0 def
       width {
          bits y width mul x add get 1 eq {
             newpath
             x y moveto
             0 1 rlineto
             1 0 rlineto
             0 -1 rlineto
             closepath
             fill
          } if
          /x x 1 add def
       } repeat
       /y y 1 add def
    } repeat
    

    (当然,您可以在顶部用自己的值填写 defs,遍历 BitMatrix 并打印 1 和 0 以填写 bits

    【讨论】:

    • 听起来不错,非常感谢。但是在使用它之前,我必须将我的 BitMatrix 减小到像素大小。没那么差,我试试。 ;)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-09-03
    • 1970-01-01
    • 2013-07-05
    • 1970-01-01
    • 2014-08-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多