2012-04-27 12 views
2

현재 QR 코드를 이미지로 생성하는 ZXing을 사용하는 앱을 개발 중입니다. 다음은 간단한 예입니다.Java로 확장 가능한 EPS로 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))); 
    } 

} 

이렇게하면 "인코딩 할 일부 텍스트"라는 플래시 가능한 QRcode가 포함 된 PNG 파일이 생성됩니다.

내 문제는 형식을 eps로 변경하려고하면 빈 파일이 표시된다는 것입니다. 현재 우리가 사용하는 솔루션은 imagemagick convert 유틸리티를 통해 png 파일을 eps로 변환하는 것입니다. 그러나 주어진 EPS는 원시 이미지를 포함하는 것일 뿐이며, 특히 인쇄 할 때 특히 잘 확장되지 않습니다.

확장 가능한 Eps 파일을 빌드 할 수있는 오픈 소스 솔루션 (Zxing 또는 기타)이 있는지 알고 있습니까? (또는 예를 들어 어떤의 vectorial 형식)

답변

3

편집 :

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 file output

+0

매우 소리 좋은, 고마워. 하지만 BitMatrix를 사용하기 전에 BitMatrix를 픽셀 크기로 줄여야합니다. 나쁘지는 않아. 한번 시도해 볼게. ;) – Nicocube