2

私は現在、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)));
    }

}

これにより、「エンコードするテキスト」というフラッシュ可能なQRコードを含むPNGファイルが生成されます。

私の問題は、フォーマットをepsに変更しようとすると、空のファイルが表示されることです。現在使用しているソリューションは、imagemagick変換ユーティリティを使用してpngファイルをepsに変換することです。ただし、指定されたEPSは生の画像を埋め込んでいるだけであり、(特に印刷時に)適切に拡大縮小されません。

スケーラブルなEpsファイルを構築するためのオープンソースソリューション(Zxingまたはその他)があるかどうか誰かが知っていますか?(または、たとえば任意のベクトル形式)

4

1 に答える 1

4

編集: 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

(もちろん、上部の定義に独自の値を入力し、BitMatrix を反復処理して 1 と 0 を出力して入力しますbits) psファイル出力

于 2012-04-27T17:22:58.797 に答える