понедельник, 1 апреля 2013 г.

Barcode reader in java

Много лет пользовался читалкой штрихкодов от DTK:
http://www.dtksoft.com/?p=barreader
Я ее даже в java использовал (правда костылями через обертку вызова dll):
loadLib("DTKBarReader");
BarcodeReader barReader = new BarcodeReader();
barReader.setBarcodeTypes(BarcodeTypeEnum.BT_PDF417.getValue());
barReader.setBarcodeOrientation(BarcodeOrientationEnum.BO_LeftToRight.getValue() + BarcodeOrientationEnum.BO_RightToLeft.getValue()
                + BarcodeOrientationEnum.BO_TopToBottom.getValue() + BarcodeOrientationEnum.BO_BottomToTop.getValue());


BarcodeCollection barcodes = barReader.ReadFromFile(filesJpeg[i].toString());

if (barcodes.getCount() > 0)
{
              // ...
}


Всё устраивало, кроме ее платности.
Single Developer (+5 RL*)   1D&2D    790 €

А тут оказывается пакет ZXing очень неплохо развился.
Apache License v2.0
Посмотрел - распознаёт без проблем! Пора переключаться!
/**
 * 
 */
package package1;

import java.awt.image.BufferedImage;
import java.io.File;
import java.util.Hashtable;

import javax.imageio.ImageIO;

import com.google.zxing.BinaryBitmap;
import com.google.zxing.DecodeHintType;
import com.google.zxing.LuminanceSource;
import com.google.zxing.Result;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.common.*;
import com.google.zxing.multi.GenericMultipleBarcodeReader;
import com.google.zxing.oned.Code128Reader;

/**
 * @author NBochkarev
 * 
 */
public class ZXingTest
{

    public static void main(String[] args)
    {
        try
        {
            String fileName = "C:\\Work\\E-Архив\\Work\\Code128\\page1.jpg";
            BufferedImage image = ImageIO.read(new File(fileName));

            int imgHeight = image.getHeight();
            int imgWidth = image.getWidth();

            System.out.println("imgHeight, imgWidth : " + imgHeight + " , " + imgWidth);
            LuminanceSource source = new BufferedImageLuminanceSource(image);

            Hashtable<DecodeHintType, Object> hints = new Hashtable<DecodeHintType, Object>(3);
            hints.put(DecodeHintType.TRY_HARDER, Boolean.TRUE);

            Code128Reader c128 = new Code128Reader();
            BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
    //        Result result = null;
            GenericMultipleBarcodeReader reader = new GenericMultipleBarcodeReader(c128);

            try
            {
                // 1 ChCode
                // result = c128.decode(bitmap, hints);
                // System.out.println(result.getText());

                Result[] results = reader.decodeMultiple(bitmap, hints);
                for (Result res : results)
                {
                    System.out.println(res.toString());

                }
            }
            catch (com.google.zxing.NotFoundException e2)
            {
                System.out.println("ChCode Code128 not found!");
            }
        }
        catch (Exception e1)
        {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
    }
}

Комментариев нет:

Отправить комментарий