Search code examples
javautf-8barcodezxing

ZXing 2D barcode decoding: UTF-8 characters not decoded properly


I'm trying to use ZXing to read 2D barcodes and it mostly works fine, except it doesn't really recognize some UTF-8 characters like č and ć. I'm using this code to set the encoding:

MultiFormatReader reader = new MultiFormatReader();
Hashtable hints = new Hashtable();
hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");  
reader.setHints(hints);
result = reader.decode(bitmap);

Am I doing something wrong?

Edit: I also tried calling the overload for decode which takes hints, but the result was the same.


Solution

  • It looks like I was creating my bitmap the wrong way. This worked:

    MultiFormatReader reader = new MultiFormatReader();
    
    FileInputStream fis = new FileInputStream(filePath);
    
    BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(
        new BufferedImageLuminanceSource(
                ImageIO.read(fis))));
    
    Result result = reader.decode(bitmap);
    
    String originalText = result.getText();
    byte[] bytes = originalText.getBytes("ISO-8859-1");
    String outputText = new String(bytes, "UTF-8");