1

I have images of codes that I want to decode. How can I use zxing so that I specify the image location and get the decoded text back, and in case the decoding fails (it will for some images, that's the project), it gives me an error.

How can I setup zxing on my Windows machine? I downloaded the jar file, but I don't know where to start. I understand I'll have to create a code to read the image and supply it to the library reader method, but a guide how to do that would be very helpful.

nimbudew
  • 958
  • 11
  • 28
  • http://stackoverflow.com/questions/1276635/how-to-include-and-use-zxing-library-in-android-with-eclipse – Deepanshu J bedi Jul 27 '14 at 13:54
  • I found a lot of posts on Android too, but I just want a simple decoder. I don't want to use my camera. Just specify the image to the method and get the decoded result – nimbudew Jul 27 '14 at 13:55

2 Answers2

1

I was able to do it. Downloaded the source and added the following code. Bit rustic, but gets the work done.

import com.google.zxing.NotFoundException;
import com.google.zxing.ChecksumException;
import com.google.zxing.FormatException;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.DecodeHintType;
import com.google.zxing.Reader;
import com.google.zxing.BinaryBitmap;
import com.google.zxing.Result;
import com.google.zxing.LuminanceSource;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.common.HybridBinarizer;

import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import java.io.File;
import java.io.IOException;
import java.util.*;

import com.google.zxing.qrcode.QRCodeReader;

class qr
{
    public static void main(String args[])
    {
        Reader xReader = new QRCodeReader();
        BufferedImage dest = null;

        try
        {
            dest = ImageIO.read(new File(args[0]));
        }
        catch(IOException e)
        {
            System.out.println("Cannot load input image");
        }
        LuminanceSource source = new BufferedImageLuminanceSource(dest);

        BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
        Vector<BarcodeFormat> barcodeFormats = new Vector<BarcodeFormat>();
        barcodeFormats.add(BarcodeFormat.QR_CODE);

        HashMap<DecodeHintType, Object> decodeHints = new HashMap<DecodeHintType, Object>(3);
        decodeHints.put(DecodeHintType.POSSIBLE_FORMATS, barcodeFormats);

        decodeHints.put(DecodeHintType.TRY_HARDER, Boolean.TRUE);

        Result result = null;

        try
        {
            result = xReader.decode(bitmap, decodeHints);
            System.out.println("Code Decoded");
            String text = result.getText();
            System.out.println(text);
        }
        catch(NotFoundException e)
        {
            System.out.println("Decoding Failed");
        }
        catch(ChecksumException e)
        {
            System.out.println("Checksum error");
        }
        catch(FormatException e)
        {
            System.out.println("Wrong format");
        }
    }
}
nimbudew
  • 958
  • 11
  • 28
1

The project includes a class called CommandLineRunner which you can simply call from the command line. You can also look at its source to see how it works and reuse it.

There is nothing to install or set up. It's a library. Typically you don't download the jar but declare it as a dependency in your Maven-based project.

If you just want to send an image to decode, use http://zxing.org/w/decode.jspx

Sean Owen
  • 66,182
  • 23
  • 141
  • 173