Package de.chris_soft.utilities

Source Code of de.chris_soft.utilities.BarcodeReader

/**
* NanoDoA - File based document archive
*
* Copyright (C) 2011-2012 Christian Packenius, christian.packenius@googlemail.com
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
package de.chris_soft.utilities;

import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;
import com.google.zxing.BinaryBitmap;
import com.google.zxing.DecodeHintType;
import com.google.zxing.LuminanceSource;
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.common.HybridBinarizer;
import com.google.zxing.multi.GenericMultipleBarcodeReader;

/**
* Read barcodes via ZXing 1.7.
* @author Christian Packenius.
*/
public class BarcodeReader {
  /**
   * Get all recognizable barcodes from image.
   * @param image Image to search for barcodes.
   * @return List of Strings (maybe empty).
   */
  public static List<String> getBarcodesAsStringList(BufferedImage image) {
    List<String> list = new ArrayList<String>();
    Result[] results = getBarcodeResults(image);
    if (results != null) {
      for (Result result : results) {
        list.add(result.getText());
      }
    }
    return list;
  }

  /**
   * Returns all Result objects from barcode recognition of the given image.
   * @param image Buffered image for barcode recognition.
   * @return Results array.
   */
  public static Result[] getBarcodeResults(BufferedImage image) {
    LuminanceSource source = new BufferedImageLuminanceSource(image);
    BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
    Hashtable<DecodeHintType, Object> hints = new Hashtable<DecodeHintType, Object>();
    hints.put(DecodeHintType.TRY_HARDER, Boolean.TRUE);
    Result[] results = null;
    try {
      MultiFormatReader multiFormatReader = new MultiFormatReader();
      // Result result = multiFormatReader.decode(bitmap, hints);
      GenericMultipleBarcodeReader reader = new GenericMultipleBarcodeReader(multiFormatReader);
      results = reader.decodeMultiple(bitmap, hints);
    }
    catch (NotFoundException exception) {
      // Ignore.
    }
    return results;
  }
}
TOP

Related Classes of de.chris_soft.utilities.BarcodeReader

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.