Issue with reading DataMatrix

329 views
Skip to first unread message

Claus Straube

unread,
Apr 14, 2014, 6:00:04 AM4/14/14
to zx...@googlegroups.com
Hi all,

I've some issues while reading a DataMatrix. I'm using zxing Version 3.0.0. My case seems very simple (it is simple and it works with qrcode or pdf417 the same way perfectly): I'm generating a data matrix image (works) and want to read the text from the generated image (fails with: com.google.zxing.NotFoundException: null). The http://zxing.org/w/decode.jspx can read this file.

Here is my code:
<code>
import com.google.zxing.BarcodeFormat;
import com.google.zxing.BinaryBitmap;
import com.google.zxing.DecodeHintType;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatReader;
import com.google.zxing.NotFoundException;
import com.google.zxing.Result;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.common.HybridBinarizer;
import com.google.zxing.datamatrix.DataMatrixWriter;
import com.google.zxing.datamatrix.encoder.SymbolShapeHint;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.file.Paths;
import java.util.EnumMap;
import java.util.Map;
import javax.imageio.ImageIO;
import org.junit.Test;
import static org.junit.Assert.*;

/**
*
* @author claus.straube
*/
public class ZxingTest {

private final static String PATH = System.getProperty("user.home")+ "/tmp/zxing/";

@Test
public void simpleDataMatrixTest() throws IOException, FileNotFoundException, NotFoundException, InterruptedException {
String filePath = PATH + "/data_matrix.png";
String text = "hello world";

writeDataMatrix(filePath, text);
Thread.sleep(5000);
String result = readDataMatrix(filePath);

assertEquals(text, result);
}

@Test
public void readOnlyTest() throws IOException, FileNotFoundException, NotFoundException {
String filePath = PATH + "/foo.png";

String result = readDataMatrix(filePath);
System.out.println(result);
}


public static void writeDataMatrix(String filePath, String text) throws IOException {
DataMatrixWriter writer = new DataMatrixWriter();
Map<EncodeHintType, Object> writerHintMap = new EnumMap<EncodeHintType, Object>(EncodeHintType.class);
writerHintMap.put(EncodeHintType.DATA_MATRIX_SHAPE, SymbolShapeHint.FORCE_SQUARE);
BitMatrix matrix = writer.encode(text, BarcodeFormat.DATA_MATRIX, 100, 100, writerHintMap);

MatrixToImageWriter.writeToPath(matrix, "PNG", Paths.get(filePath));
System.out.println("file written...");
}

public static String readDataMatrix(String filePath)
throws FileNotFoundException, IOException, NotFoundException {
Map hintMap = new EnumMap<DecodeHintType, Object>(DecodeHintType.class);
BinaryBitmap binaryBitmap = new BinaryBitmap(new HybridBinarizer(
new BufferedImageLuminanceSource(
ImageIO.read(new FileInputStream(filePath)))));
System.out.println("file read...");
Result qrCodeResult = new MultiFormatReader().decode(binaryBitmap, hintMap);
return qrCodeResult.getText();
}
}
</code>

Any ideas? Thanks in advance.

Claus

Sean Owen

unread,
Apr 14, 2014, 6:40:03 AM4/14/14
to zx...@googlegroups.com
What does the barcode look like?

Claus Straube

unread,
Apr 15, 2014, 12:48:09 AM4/15/14
to zx...@googlegroups.com

codes attached.
data_matrix.png
foo.png

Sean Owen

unread,
Apr 15, 2014, 2:55:19 AM4/15/14
to zx...@googlegroups.com
These decode OK for me. They say "This is a testmessage!" and "hello world". Try the "PURE_BARCODE" mode for images like this.

Lachezar Dobrev

unread,
Apr 15, 2014, 4:09:47 AM4/15/14
to Sean Owen, zxing
Ahh... I was afraid of this. The writer does not add a quiet zone
around the generated matrix. However the reader needs a white
rectangle around the Data Matrix, so in this case it will not detect
the code.

Claus. You could manually add a white box around the Data Matrix
when converting the bit matrix to an image and try again.

2014-04-15 9:55 GMT+03:00 Sean Owen <sro...@gmail.com>:
> These decode OK for me. They say "This is a testmessage!" and "hello world".
> Try the "PURE_BARCODE" mode for images like this.
>
> --
> You received this message because you are subscribed to the Google Groups
> "zxing" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to zxing+un...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Claus Straube

unread,
Apr 15, 2014, 6:20:06 AM4/15/14
to zx...@googlegroups.com
The white box doesn't work for me (perhaps it was to small - see code attached), but PURE_BARCODE mode worked. Thanks!

public static String readDataMatrix(String filePath)
throws FileNotFoundException, IOException, NotFoundException {
Map hintMap = new EnumMap<DecodeHintType, Object>(DecodeHintType.class);

hintMap.put(DecodeHintType.PURE_BARCODE, Boolean.TRUE);

foo.png
Reply all
Reply to author
Forward
0 new messages