2015-11-04 745 views
1

我得到使用下面這行我的DataMatrix碼:在編碼GS1兼容的DataMatrix代碼時如何處理FNC1/<GS>?

BitMatrix bitMatrix = new DataMatrixWriter().encode(dmie.preEncodeBarcode(dataToEncode), BarcodeFormat.DATA_MATRIX, 50, 50, null);

輸入字符串,我得到包含「FNC1」(就像串在普通的字符),以紀念動態字段的結尾,並在總共字符串有4個不同的GS1 DataMatrix應用程序標識符及其各自的值。

什麼preEncodeBarcode()的作用是取代 「FNC1」 與<GS>,就像這樣:

input = input.replaceAll("FNC1", new String(new byte[] {0x1d}));

由於否則,我只是得到 「FNC1910005FNC1230202 [...]」 編碼到二維條碼,而什麼我當然想要的是<GS>而不是文本「FNC1」。

Strange-double-data-matrix

然而,隨着<GS>更換FNC1的時候,我得到這個非常奇怪的雙DM-代碼,而不是一個正常的(我一直在使用 '\ u001c' 也試過)只有當我跳過用<GS>替換「FNC1」時,我才能得到一個正確的。

任何想法如何根據我的<GS>包含字符串得到正確的DataMatrix代碼?或者我只是在<GS>直接在字符串中做錯了什麼?我應該怎麼做才能讓zxing能夠給我一個正確的DataMatrix?我一直在讀書,但是我真的無法把這個包裹起來。

更新:我不確定,但我可能會遇到一些奇怪的錯誤。這是我送的DataMatrixWriter一旦我預處理的輸入字符串(空格是):

[d29100001 21000000049347037 24000163718 390300000002990

我發現默默無聞的是什麼,如果我(在時間寫)發送input.substring(2, input.length());input.substring(0, input.length()-3);然後它工作得很好,而如果我反而從頭開始刪除一個(或更少)的字符或從結尾刪除2個或更少的字符,那麼我得到這個奇怪的DataMatrix。更奇怪的是,這種行爲甚至不一致 - 如果我在最後加上6個隨機數然後它可以正常工作,但是如果我刪除了其中三個數,那麼我又會遇到問題。最糟糕的是,一小時前我無法發送input.substring(0, input.length()-3),但現在我可以。

換句話說,我完全困惑。

(PS。我使用的代碼中發現here縮放DataMatrix二維到我想要的大小,但它的斑馬線,讓從一開始就輸出了錯誤。)

回答

0

感謝Lachezar Dobrev over at the zxing Google group我能得到這個按預期工作。事實證明,您可以將EncodeHintType移交給DataMatrixWriter,這將強制它應用方形形狀。

這是否把戲對我來說:

 HashMap hintMap = new HashMap(); 
     hintMap.put(EncodeHintType.DATA_MATRIX_SHAPE, SymbolShapeHint.FORCE_SQUARE); 
     BitMatrix bitMatrix = new DataMatrixWriter().encode(input, BarcodeFormat.DATA_MATRIX, 50, 50, hintMap); 

更新一些一週後:事實證明,斑馬線是不是GS1兼容不管你所付出的投入,所以整個FNC1 /問題除此之外。相反,我會推薦使用Okapi Barcode,它完美地工作。

我還沒有找到任何指南或教程,但根據Okapi Java GUI生成條形碼的方式,我對項目的Make Barcode類一起感到困惑。

DataMatrix dataMatrix = new DataMatrix(); 
dataMatrix.setDataType(Symbol.DataType.GS1); 
dataMatrix.setReaderInit(); 
dataMatrix.setPreferredSize(24); //144x144 
dataMatrix.forceSquare(true); 
dataMatrix.setContent(dataToEncode); 
BufferedImage image = new BufferedImage((dataMatrix.getWidth() * magnification) + (2 * borderSize), 
        (dataMatrix.getHeight() * magnification) + (2 * borderSize), BufferedImage.TYPE_INT_RGB); 
Graphics2D g2d = image.createGraphics(); 
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); 
g2d.setColor(Color.WHITE); 
g2d.fillRect(0, 0, (dataMatrix.getWidth() * magnification) + (2 * borderSize), 
        (dataMatrix.getHeight() * magnification) + (2 * borderSize)); 
Java2DRenderer renderer = new Java2DRenderer(g2d, magnification, borderSize, Color.WHITE, Color.BLACK); 
renderer.render(dataMatrix); 
try { 
    ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
    ImageIO.write(image, "png", baos); 
    //Before Base64-encoding the image and return as a String   
    Base64.Encoder encoder = Base64.getEncoder(); 
    return encoder.encodeToString(baos.toByteArray()); 
} catch (IOException e) { 
//Do some logging 
return "Something went wrong";//Return super-informative error message 
}