X-Git-Url: http://git.rot13.org/?a=blobdiff_plain;f=csharp%2FMultiFormatReader.cs;h=0c58f9f16500832d30c67355ab8c4ba13d113315;hb=eb6d802f9710cf348eecdbdceaf118197a710428;hp=9c0f09ca1717f1d9bcf058f3e8ec2880081b1016;hpb=68dbcb841d3a184168e1228abc0eeba96d2d3eb8;p=zxing.git diff --git a/csharp/MultiFormatReader.cs b/csharp/MultiFormatReader.cs index 9c0f09ca..0c58f9f1 100755 --- a/csharp/MultiFormatReader.cs +++ b/csharp/MultiFormatReader.cs @@ -1,4 +1,6 @@ -/* +/* +* Copyright 2007 ZXing authors +* * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -11,160 +13,163 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - using System; -using System.Collections; -using com.google.zxing.qrcode; -using com.google.zxing.oned; - +using MultiFormatOneDReader = com.google.zxing.oned.MultiFormatOneDReader; +using PDF417Reader = com.google.zxing.pdf417.PDF417Reader; +using QRCodeReader = com.google.zxing.qrcode.QRCodeReader; +using DataMatrixReader = com.google.zxing.datamatrix.DataMatrixReader; namespace com.google.zxing { - public sealed class MultiFormatReader : Reader - { - private Hashtable hints; - private ArrayList readers; - - /** - * This version of decode honors the intent of Reader.decode(MonochromeBitmapSource) in that it - * passes null as a hint to the decoders. However, that makes it inefficient to call repeatedly. - * Use setHints() followed by decodeWithState() for continuous scan applications. - * - * @param image The pixel data to decode - * @return The contents of the image - * @throws ReaderException Any errors which occurred - */ - public Result decode(MonochromeBitmapSource image){ - try{ - setHints(null); - return decodeInternal(image); - } - catch(Exception e){ - throw new ReaderException(e.Message); - } - } - - /** - * Decode an image using the hints provided. Does not honor existing state. - * - * @param image The pixel data to decode - * @param hints The hints to use, clearing the previous state. - * @return The contents of the image - * @throws ReaderException Any errors which occurred - */ - public Result decode(MonochromeBitmapSource image, Hashtable hints){ - try{ - setHints(hints); - return decodeInternal(image); - }catch(Exception e){ - throw new ReaderException (e.Message); - } - } - - /** - * Decode an image using the state set up by calling setHints() previously. Continuous scan - * clients will get a large speed increase by using this instead of decode(). - * - * @param image The pixel data to decode - * @return The contents of the image - * @throws ReaderException Any errors which occurred - */ - public Result decodeWithState(MonochromeBitmapSource image){ - try{ - // Make sure to set up the default state so we don't crash - if (readers == null) { - setHints(null); - } - return decodeInternal(image); - }catch(Exception e){ - throw new ReaderException(e.Message); - } - } - - /** - * This method adds state to the MultiFormatReader. By setting the hints once, subsequent calls - * to decodeWithState(image) can reuse the same set of readers without reallocating memory. This - * is important for performance in continuous scan clients. - * - * @param hints The set of hints to use for subsequent calls to decode(image) - */ - public void setHints(Hashtable hints) { - this.hints = hints; - - bool tryHarder = hints != null && hints.ContainsKey(DecodeHintType.TRY_HARDER); - - ArrayList possibleFormats = hints == null ? null : (ArrayList)hints[(DecodeHintType.POSSIBLE_FORMATS)]; - readers = new ArrayList(); - if (possibleFormats != null) - { - bool addOneDReader = - possibleFormats.Contains(BarcodeFormat.UPC_A) || - possibleFormats.Contains(BarcodeFormat.UPC_E) || - possibleFormats.Contains(BarcodeFormat.EAN_13) || - possibleFormats.Contains(BarcodeFormat.EAN_8) || - possibleFormats.Contains(BarcodeFormat.CODE_39) || - possibleFormats.Contains(BarcodeFormat.CODE_128); - // Put 1D readers upfront in "normal" mode - - if (addOneDReader && !tryHarder) - { - readers.Add(new MultiFormatOneDReader(hints)); - } - - if (possibleFormats.Contains(BarcodeFormat.QR_CODE)) - { - readers.Add(new QRCodeReader()); - } - // TODO re-enable once Data Matrix is ready - if (possibleFormats.Contains(BarcodeFormat.DATAMATRIX)) { - //readers.Add(new DataMatrixReader()); - } - // At end in "try harder" mode - if (addOneDReader && tryHarder) - { - readers.Add(new MultiFormatOneDReader(hints)); - } - } - - if (readers.Count == 0) - { - if (!tryHarder) - { - //readers.Add(new MultiFormatOneDReader(hints)); - } - readers.Add(new QRCodeReader()); - // TODO re-enable once Data Matrix is ready - // readers.addElement(new DataMatrixReader()); - if (tryHarder) - { - //readers.Add(new MultiFormatOneDReader(hints)); - } - } - } - - private Result decodeInternal(MonochromeBitmapSource image) { - try - { - int size = readers.Count; - for (int i = 0; i < size; i++) - { - Reader reader = (Reader)readers[i]; - try - { - return reader.decode(image, hints); - } - catch (ReaderException re) - { - // continue - } - } - - throw new ReaderException(""); - } - catch (Exception e) { - throw new ReaderException(e.Message); - } - } - - - } + + /// MultiFormatReader is a convenience class and the main entry point into the library for most uses. + /// By default it attempts to decode all barcode formats that the library supports. Optionally, you + /// can provide a hints object to request different behavior, for example only decoding QR codes. + /// + /// + /// Sean Owen + /// + /// dswitkin@google.com (Daniel Switkin) + /// + /// www.Redivivus.in (suraj.supekar@redivivus.in) - Ported from ZXING Java Source + /// + public sealed class MultiFormatReader : Reader + { + /// This method adds state to the MultiFormatReader. By setting the hints once, subsequent calls + /// to decodeWithState(image) can reuse the same set of readers without reallocating memory. This + /// is important for performance in continuous scan clients. + /// + /// + /// The set of hints to use for subsequent calls to decode(image) + /// + public System.Collections.Hashtable Hints + { + set + { + this.hints = value; + + bool tryHarder = value != null && value.ContainsKey(DecodeHintType.TRY_HARDER); + System.Collections.ArrayList formats = value == null?null:(System.Collections.ArrayList) value[DecodeHintType.POSSIBLE_FORMATS]; + readers = System.Collections.ArrayList.Synchronized(new System.Collections.ArrayList(10)); + if (formats != null) + { + bool addOneDReader = formats.Contains(BarcodeFormat.UPC_A) || formats.Contains(BarcodeFormat.UPC_E) || formats.Contains(BarcodeFormat.EAN_13) || formats.Contains(BarcodeFormat.EAN_8) || formats.Contains(BarcodeFormat.CODE_39) || formats.Contains(BarcodeFormat.CODE_128) || formats.Contains(BarcodeFormat.ITF); + // Put 1D readers upfront in "normal" mode + if (addOneDReader && !tryHarder) + { + readers.Add(new MultiFormatOneDReader(value)); + } + if (formats.Contains(BarcodeFormat.QR_CODE)) + { + readers.Add(new QRCodeReader()); + } + if (formats.Contains(BarcodeFormat.DATAMATRIX)) + { + readers.Add(new DataMatrixReader()); + } + if (formats.Contains(BarcodeFormat.PDF417)) + { + readers.Add(new PDF417Reader()); + } + // At end in "try harder" mode + if (addOneDReader && tryHarder) + { + readers.Add(new MultiFormatOneDReader(value)); + } + } + if ((readers.Count == 0)) + { + if (!tryHarder) + { + readers.Add(new MultiFormatOneDReader(value)); + } + readers.Add(new QRCodeReader()); + + // TODO re-enable once Data Matrix is ready + // readers.addElement(new DataMatrixReader()); + + // TODO: Enable once PDF417 has passed QA + //readers.addElement(new PDF417Reader()); + + if (tryHarder) + { + readers.Add(new MultiFormatOneDReader(value)); + } + } + } + + } + + private System.Collections.Hashtable hints; + private System.Collections.ArrayList readers; + + /// This version of decode honors the intent of Reader.decode(BinaryBitmap) in that it + /// passes null as a hint to the decoders. However, that makes it inefficient to call repeatedly. + /// Use setHints() followed by decodeWithState() for continuous scan applications. + /// + /// + /// The pixel data to decode + /// + /// The contents of the image + /// + /// ReaderException Any errors which occurred + public Result decode(BinaryBitmap image) + { + Hints = null; + return decodeInternal(image); + } + + /// Decode an image using the hints provided. Does not honor existing state. + /// + /// + /// The pixel data to decode + /// + /// The hints to use, clearing the previous state. + /// + /// The contents of the image + /// + /// ReaderException Any errors which occurred + public Result decode(BinaryBitmap image, System.Collections.Hashtable hints) + { + Hints = hints; + return decodeInternal(image); + } + + /// Decode an image using the state set up by calling setHints() previously. Continuous scan + /// clients will get a large speed increase by using this instead of decode(). + /// + /// + /// The pixel data to decode + /// + /// The contents of the image + /// + /// ReaderException Any errors which occurred + public Result decodeWithState(BinaryBitmap image) + { + // Make sure to set up the default state so we don't crash + if (readers == null) + { + Hints = null; + } + return decodeInternal(image); + } + + private Result decodeInternal(BinaryBitmap image) + { + int size = readers.Count; + for (int i = 0; i < size; i++) + { + Reader reader = (Reader) readers[i]; + try + { + return reader.decode(image, hints); + } + catch (ReaderException re) + { + // continue + } + } + + throw ReaderException.Instance; + } + } } \ No newline at end of file