From 418941cb758a12f6ce18ac18c27c177516617937 Mon Sep 17 00:00:00 2001 From: clowwindy Date: Mon, 12 Jan 2015 00:20:09 +0800 Subject: [PATCH] add qrcode detector --- shadowsocks-csharp/3rd/zxing/BitArray.cs | 160 ---- .../3rd/zxing/DecodeHintType.cs | 125 +++ .../3rd/zxing/EncodeHintType.cs | 131 +++ shadowsocks-csharp/3rd/zxing/ResultPoint.cs | 191 +++++ .../3rd/zxing/ResultPointCallback.cs | 26 + shadowsocks-csharp/3rd/zxing/Version.cs | 342 -------- .../3rd/zxing/WriterException.cs | 55 ++ .../3rd/zxing/common/BitArray.cs | 488 +++++++++++ .../3rd/zxing/common/BitMatrix.cs | 427 +++++++++ .../3rd/zxing/common/BitSource.cs | 124 +++ .../3rd/zxing/common/DecoderResult.cs | 76 ++ .../3rd/zxing/common/DefaultGridSampler.cs | 82 ++ .../3rd/zxing/common/DetectorResult.cs | 39 + .../3rd/zxing/common/EncodingOptions.cs | 121 +++ .../3rd/zxing/common/GridSampler.cs | 192 +++++ .../3rd/zxing/common/PerspectiveTransform.cs | 159 ++++ .../3rd/zxing/common/detector/MathUtils.cs | 48 ++ .../detector/MonochromeRectangleDetector.cs | 252 ++++++ .../common/detector/WhiteRectangleDetector.cs | 433 ++++++++++ .../{ => common/reedsolomon}/GenericGF.cs | 79 ++ .../{ => common/reedsolomon}/GenericGFPoly.cs | 103 ++- .../common/reedsolomon/ReedSolomonDecoder.cs | 227 +++++ .../reedsolomon}/ReedSolomonEncoder.cs | 0 .../zxing/qrcode/decoder/BitMatrixParser.cs | 281 ++++++ .../3rd/zxing/qrcode/decoder/DataBlock.cs | 146 ++++ .../3rd/zxing/qrcode/decoder/DataMask.cs | 165 ++++ .../qrcode/decoder/DecodedBitStreamParser.cs | 293 +++++++ .../3rd/zxing/qrcode/decoder/Decoder.cs | 195 +++++ .../decoder}/ErrorCorrectionLevel.cs | 0 .../zxing/qrcode/decoder/FormatInformation.cs | 197 +++++ .../3rd/zxing/{ => qrcode/decoder}/Mode.cs | 64 ++ .../qrcode/decoder/QRCodeDecoderMetaData.cs | 60 ++ .../3rd/zxing/qrcode/decoder/Version.cs | 424 +++++++++ .../zxing/qrcode/detector/AlignmentPattern.cs | 68 ++ .../qrcode/detector/AlignmentPatternFinder.cs | 324 +++++++ .../3rd/zxing/qrcode/detector/Detector.cs | 429 ++++++++++ .../zxing/qrcode/detector/FinderPattern.cs | 107 +++ .../qrcode/detector/FinderPatternFinder.cs | 808 ++++++++++++++++++ .../qrcode/detector/FinderPatternInfo.cs | 74 ++ .../zxing/{ => qrcode/encoder}/BlockPair.cs | 0 .../zxing/{ => qrcode/encoder}/ByteMatrix.cs | 0 .../3rd/zxing/{ => qrcode/encoder}/Encoder.cs | 288 ++++++- .../zxing/{ => qrcode/encoder}/MaskUtil.cs | 0 .../zxing/{ => qrcode/encoder}/MatrixUtil.cs | 18 +- .../3rd/zxing/{ => qrcode/encoder}/QRCode.cs | 36 +- .../qrcode/encoder/QrCodeEncodingOptions.cs | 110 +++ shadowsocks-csharp/shadowsocks-csharp.csproj | 57 +- 47 files changed, 7462 insertions(+), 562 deletions(-) delete mode 100755 shadowsocks-csharp/3rd/zxing/BitArray.cs create mode 100755 shadowsocks-csharp/3rd/zxing/DecodeHintType.cs create mode 100755 shadowsocks-csharp/3rd/zxing/EncodeHintType.cs create mode 100755 shadowsocks-csharp/3rd/zxing/ResultPoint.cs create mode 100755 shadowsocks-csharp/3rd/zxing/ResultPointCallback.cs delete mode 100755 shadowsocks-csharp/3rd/zxing/Version.cs create mode 100755 shadowsocks-csharp/3rd/zxing/WriterException.cs create mode 100755 shadowsocks-csharp/3rd/zxing/common/BitArray.cs create mode 100755 shadowsocks-csharp/3rd/zxing/common/BitMatrix.cs create mode 100755 shadowsocks-csharp/3rd/zxing/common/BitSource.cs create mode 100755 shadowsocks-csharp/3rd/zxing/common/DecoderResult.cs create mode 100755 shadowsocks-csharp/3rd/zxing/common/DefaultGridSampler.cs create mode 100755 shadowsocks-csharp/3rd/zxing/common/DetectorResult.cs create mode 100755 shadowsocks-csharp/3rd/zxing/common/EncodingOptions.cs create mode 100755 shadowsocks-csharp/3rd/zxing/common/GridSampler.cs create mode 100755 shadowsocks-csharp/3rd/zxing/common/PerspectiveTransform.cs create mode 100755 shadowsocks-csharp/3rd/zxing/common/detector/MathUtils.cs create mode 100755 shadowsocks-csharp/3rd/zxing/common/detector/MonochromeRectangleDetector.cs create mode 100755 shadowsocks-csharp/3rd/zxing/common/detector/WhiteRectangleDetector.cs rename shadowsocks-csharp/3rd/zxing/{ => common/reedsolomon}/GenericGF.cs (68%) rename shadowsocks-csharp/3rd/zxing/{ => common/reedsolomon}/GenericGFPoly.cs (69%) create mode 100755 shadowsocks-csharp/3rd/zxing/common/reedsolomon/ReedSolomonDecoder.cs rename shadowsocks-csharp/3rd/zxing/{ => common/reedsolomon}/ReedSolomonEncoder.cs (100%) create mode 100755 shadowsocks-csharp/3rd/zxing/qrcode/decoder/BitMatrixParser.cs create mode 100755 shadowsocks-csharp/3rd/zxing/qrcode/decoder/DataBlock.cs create mode 100755 shadowsocks-csharp/3rd/zxing/qrcode/decoder/DataMask.cs create mode 100755 shadowsocks-csharp/3rd/zxing/qrcode/decoder/DecodedBitStreamParser.cs create mode 100755 shadowsocks-csharp/3rd/zxing/qrcode/decoder/Decoder.cs rename shadowsocks-csharp/3rd/zxing/{ => qrcode/decoder}/ErrorCorrectionLevel.cs (100%) create mode 100755 shadowsocks-csharp/3rd/zxing/qrcode/decoder/FormatInformation.cs rename shadowsocks-csharp/3rd/zxing/{ => qrcode/decoder}/Mode.cs (55%) create mode 100755 shadowsocks-csharp/3rd/zxing/qrcode/decoder/QRCodeDecoderMetaData.cs create mode 100755 shadowsocks-csharp/3rd/zxing/qrcode/decoder/Version.cs create mode 100755 shadowsocks-csharp/3rd/zxing/qrcode/detector/AlignmentPattern.cs create mode 100755 shadowsocks-csharp/3rd/zxing/qrcode/detector/AlignmentPatternFinder.cs create mode 100755 shadowsocks-csharp/3rd/zxing/qrcode/detector/Detector.cs create mode 100755 shadowsocks-csharp/3rd/zxing/qrcode/detector/FinderPattern.cs create mode 100755 shadowsocks-csharp/3rd/zxing/qrcode/detector/FinderPatternFinder.cs create mode 100755 shadowsocks-csharp/3rd/zxing/qrcode/detector/FinderPatternInfo.cs rename shadowsocks-csharp/3rd/zxing/{ => qrcode/encoder}/BlockPair.cs (100%) rename shadowsocks-csharp/3rd/zxing/{ => qrcode/encoder}/ByteMatrix.cs (100%) rename shadowsocks-csharp/3rd/zxing/{ => qrcode/encoder}/Encoder.cs (67%) rename shadowsocks-csharp/3rd/zxing/{ => qrcode/encoder}/MaskUtil.cs (100%) rename shadowsocks-csharp/3rd/zxing/{ => qrcode/encoder}/MatrixUtil.cs (96%) rename shadowsocks-csharp/3rd/zxing/{ => qrcode/encoder}/QRCode.cs (67%) create mode 100755 shadowsocks-csharp/3rd/zxing/qrcode/encoder/QrCodeEncodingOptions.cs diff --git a/shadowsocks-csharp/3rd/zxing/BitArray.cs b/shadowsocks-csharp/3rd/zxing/BitArray.cs deleted file mode 100755 index b41484ca..00000000 --- a/shadowsocks-csharp/3rd/zxing/BitArray.cs +++ /dev/null @@ -1,160 +0,0 @@ -/* -* 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 -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -using System; - -namespace ZXing.Common -{ - /// - /// A simple, fast array of bits, represented compactly by an array of ints internally. - /// - /// Sean Owen - public sealed class BitArray - { - private int[] bits; - private int size; - - public int Size - { - get - { - return size; - } - } - - public int SizeInBytes - { - get - { - return (size + 7) >> 3; - } - } - - public bool this[int i] - { - get - { - return (bits[i >> 5] & (1 << (i & 0x1F))) != 0; - } - } - - public BitArray() - { - this.size = 0; - this.bits = new int[1]; - } - - - private void ensureCapacity(int size) - { - if (size > bits.Length << 5) - { - int[] newBits = makeArray(size); - System.Array.Copy(bits, 0, newBits, 0, bits.Length); - bits = newBits; - } - } - - /// - /// Appends the bit. - /// - /// The bit. - public void appendBit(bool bit) - { - ensureCapacity(size + 1); - if (bit) - { - bits[size >> 5] |= 1 << (size & 0x1F); - } - size++; - } - - /// - /// Appends the least-significant bits, from value, in order from most-significant to - /// least-significant. For example, appending 6 bits from 0x000001E will append the bits - /// 0, 1, 1, 1, 1, 0 in that order. - /// - /// containing bits to append - /// bits from value to append - public void appendBits(int value, int numBits) - { - if (numBits < 0 || numBits > 32) - { - throw new ArgumentException("Num bits must be between 0 and 32"); - } - ensureCapacity(size + numBits); - for (int numBitsLeft = numBits; numBitsLeft > 0; numBitsLeft--) - { - appendBit(((value >> (numBitsLeft - 1)) & 0x01) == 1); - } - } - - public void appendBitArray(BitArray other) - { - int otherSize = other.size; - ensureCapacity(size + otherSize); - for (int i = 0; i < otherSize; i++) - { - appendBit(other[i]); - } - } - - public void xor(BitArray other) - { - if (bits.Length != other.bits.Length) - { - throw new ArgumentException("Sizes don't match"); - } - for (int i = 0; i < bits.Length; i++) - { - // The last byte could be incomplete (i.e. not have 8 bits in - // it) but there is no problem since 0 XOR 0 == 0. - bits[i] ^= other.bits[i]; - } - } - - /// - /// Toes the bytes. - /// - /// first bit to start writing - /// array to write into. Bytes are written most-significant byte first. This is the opposite - /// of the internal representation, which is exposed by BitArray - /// position in array to start writing - /// how many bytes to write - public void toBytes(int bitOffset, byte[] array, int offset, int numBytes) - { - for (int i = 0; i < numBytes; i++) - { - int theByte = 0; - for (int j = 0; j < 8; j++) - { - if (this[bitOffset]) - { - theByte |= 1 << (7 - j); - } - bitOffset++; - } - array[offset + i] = (byte)theByte; - } - } - - private static int[] makeArray(int size) - { - return new int[(size + 31) >> 5]; - } - - } -} \ No newline at end of file diff --git a/shadowsocks-csharp/3rd/zxing/DecodeHintType.cs b/shadowsocks-csharp/3rd/zxing/DecodeHintType.cs new file mode 100755 index 00000000..8ba21bbb --- /dev/null +++ b/shadowsocks-csharp/3rd/zxing/DecodeHintType.cs @@ -0,0 +1,125 @@ +/* +* 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 +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +using System; +using System.Collections.Generic; + +namespace ZXing +{ + /// + /// Encapsulates a type of hint that a caller may pass to a barcode reader to help it + /// more quickly or accurately decode it. It is up to implementations to decide what, + /// if anything, to do with the information that is supplied. + /// + /// + /// Sean Owen + /// dswitkin@google.com (Daniel Switkin) + public enum DecodeHintType + { + /// + /// Unspecified, application-specific hint. Maps to an unspecified . + /// + OTHER, + + /// + /// Image is a pure monochrome image of a barcode. Doesn't matter what it maps to; + /// use = true. + /// + PURE_BARCODE, + + /// + /// Image is known to be of one of a few possible formats. + /// Maps to a of s. + /// + POSSIBLE_FORMATS, + + /// + /// Spend more time to try to find a barcode; optimize for accuracy, not speed. + /// Doesn't matter what it maps to; use = true. + /// + TRY_HARDER, + + /// + /// Specifies what character encoding to use when decoding, where applicable (type String) + /// + CHARACTER_SET, + + /// + /// Allowed lengths of encoded data -- reject anything else. Maps to an int[]. + /// + ALLOWED_LENGTHS, + + /// + /// Assume Code 39 codes employ a check digit. Maps to . + /// + ASSUME_CODE_39_CHECK_DIGIT, + + /// + /// The caller needs to be notified via callback when a possible + /// is found. Maps to a . + /// + NEED_RESULT_POINT_CALLBACK, + + /// + /// Assume MSI codes employ a check digit. Maps to . + /// + ASSUME_MSI_CHECK_DIGIT, + + /// + /// if Code39 could be detected try to use extended mode for full ASCII character set + /// Maps to . + /// + USE_CODE_39_EXTENDED_MODE, + + /// + /// Don't fail if a Code39 is detected but can't be decoded in extended mode. + /// Return the raw Code39 result instead. Maps to . + /// + RELAXED_CODE_39_EXTENDED_MODE, + + /// + /// 1D readers supporting rotation with TRY_HARDER enabled. + /// But BarcodeReader class can do auto-rotating for 1D and 2D codes. + /// Enabling that option prevents 1D readers doing double rotation. + /// BarcodeReader enables that option automatically if "global" auto-rotation is enabled. + /// Maps to . + /// + TRY_HARDER_WITHOUT_ROTATION, + + /// + /// Assume the barcode is being processed as a GS1 barcode, and modify behavior as needed. + /// For example this affects FNC1 handling for Code 128 (aka GS1-128). Doesn't matter what it maps to; + /// use . + /// + ASSUME_GS1, + + /// + /// If true, return the start and end digits in a Codabar barcode instead of stripping them. They + /// are alpha, whereas the rest are numeric. By default, they are stripped, but this causes them + /// to not be. Doesn't matter what it maps to; use . + /// + RETURN_CODABAR_START_END, + + /// + /// Allowed extension lengths for EAN or UPC barcodes. Other formats will ignore this. + /// Maps to an of the allowed extension lengths, for example [2], [5], or [2, 5]. + /// If it is optional to have an extension, do not set this hint. If this is set, + /// and a UPC or EAN barcode is found but an extension is not, then no result will be returned + /// at all. + /// + ALLOWED_EAN_EXTENSIONS + } +} \ No newline at end of file diff --git a/shadowsocks-csharp/3rd/zxing/EncodeHintType.cs b/shadowsocks-csharp/3rd/zxing/EncodeHintType.cs new file mode 100755 index 00000000..01b17f8c --- /dev/null +++ b/shadowsocks-csharp/3rd/zxing/EncodeHintType.cs @@ -0,0 +1,131 @@ +/* +* Copyright 2008 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 +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +namespace ZXing +{ + /// + /// These are a set of hints that you may pass to Writers to specify their behavior. + /// + /// dswitkin@google.com (Daniel Switkin) + public enum EncodeHintType + { + /// + /// Specifies the width of the barcode image + /// type: + /// + WIDTH, + + /// + /// Specifies the height of the barcode image + /// type: + /// + HEIGHT, + + /// + /// Don't put the content string into the output image. + /// type: + /// + PURE_BARCODE, + + /// + /// Specifies what degree of error correction to use, for example in QR Codes. + /// Type depends on the encoder. For example for QR codes it's type + /// + /// For Aztec it is of type , representing the minimal percentage of error correction words. + /// Note: an Aztec symbol should have a minimum of 25% EC words. + /// For PDF417 it is of type or (between 0 and 8), + /// + ERROR_CORRECTION, + + /// + /// Specifies what character encoding to use where applicable. + /// type: + /// + CHARACTER_SET, + + /// + /// Specifies margin, in pixels, to use when generating the barcode. The meaning can vary + /// by format; for example it controls margin before and after the barcode horizontally for + /// most 1D formats. + /// type: + /// + MARGIN, + + /// + /// Specifies whether to use compact mode for PDF417. + /// type: + /// + PDF417_COMPACT, + + /// + /// Specifies what compaction mode to use for PDF417. + /// type: + /// + PDF417_COMPACTION, + + /// + /// Specifies the minimum and maximum number of rows and columns for PDF417. + /// type: + /// + PDF417_DIMENSIONS, + + /// + /// Don't append ECI segment. + /// That is against the specification of QR Code but some + /// readers have problems if the charset is switched from + /// ISO-8859-1 (default) to UTF-8 with the necessary ECI segment. + /// If you set the property to true you can use UTF-8 encoding + /// and the ECI segment is omitted. + /// type: + /// + DISABLE_ECI, + + /// + /// Specifies the matrix shape for Data Matrix (type ) + /// + DATA_MATRIX_SHAPE, + + /// + /// Specifies a minimum barcode size (type ). Only applicable to Data Matrix now. + /// + MIN_SIZE, + + /// + /// Specifies a maximum barcode size (type ). Only applicable to Data Matrix now. + /// + MAX_SIZE, + + /// + /// if true, don't switch to codeset C for numbers + /// + CODE128_FORCE_CODESET_B, + + /// + /// Specifies the default encodation for Data Matrix (type ) + /// Make sure that the content fits into the encodation value, otherwise there will be an exception thrown. + /// standard value: Encodation.ASCII + /// + DATA_MATRIX_DEFAULT_ENCODATION, + + /// + /// Specifies the required number of layers for an Aztec code: + /// a negative number (-1, -2, -3, -4) specifies a compact Aztec code + /// 0 indicates to use the minimum number of layers (the default) + /// a positive number (1, 2, .. 32) specifies a normal (non-compact) Aztec code + /// + AZTEC_LAYERS, + } +} \ No newline at end of file diff --git a/shadowsocks-csharp/3rd/zxing/ResultPoint.cs b/shadowsocks-csharp/3rd/zxing/ResultPoint.cs new file mode 100755 index 00000000..d3f39f2d --- /dev/null +++ b/shadowsocks-csharp/3rd/zxing/ResultPoint.cs @@ -0,0 +1,191 @@ +/* +* 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 +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +using System; + +using ZXing.Common.Detector; + +namespace ZXing +{ + /// + /// Encapsulates a point of interest in an image containing a barcode. Typically, this + /// would be the location of a finder pattern or the corner of the barcode, for example. + /// + /// Sean Owen + /// www.Redivivus.in (suraj.supekar@redivivus.in) - Ported from ZXING Java Source + public class ResultPoint + { + private readonly float x; + private readonly float y; + private readonly byte[] bytesX; + private readonly byte[] bytesY; + private String toString; + + /// + /// Initializes a new instance of the class. + /// + public ResultPoint() + { + } + + /// + /// Initializes a new instance of the class. + /// + /// The x. + /// The y. + public ResultPoint(float x, float y) + { + this.x = x; + this.y = y; + // calculate only once for GetHashCode + bytesX = BitConverter.GetBytes(x); + bytesY = BitConverter.GetBytes(y); + } + + /// + /// Gets the X. + /// + virtual public float X + { + get + { + return x; + } + } + + /// + /// Gets the Y. + /// + virtual public float Y + { + get + { + return y; + } + } + + /// + /// Determines whether the specified is equal to this instance. + /// + /// The to compare with this instance. + /// + /// true if the specified is equal to this instance; otherwise, false. + /// + public override bool Equals(Object other) + { + var otherPoint = other as ResultPoint; + if (otherPoint == null) + return false; + return x == otherPoint.x && y == otherPoint.y; + } + + /// + /// Returns a hash code for this instance. + /// + /// + /// A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table. + /// + public override int GetHashCode() + { + return 31 * ((bytesX[0] << 24) + (bytesX[1] << 16) + (bytesX[2] << 8) + bytesX[3]) + + (bytesY[0] << 24) + (bytesY[1] << 16) + (bytesY[2] << 8) + bytesY[3]; + } + + /// + /// Returns a that represents this instance. + /// + /// + /// A that represents this instance. + /// + public override String ToString() + { + if (toString == null) + { + var result = new System.Text.StringBuilder(25); + result.AppendFormat(System.Globalization.CultureInfo.CurrentUICulture, "({0}, {1})", x, y); + toString = result.ToString(); + } + return toString; + } + + /// + /// Orders an array of three ResultPoints in an order [A,B,C] such that AB < AC and + /// BC < AC and the angle between BC and BA is less than 180 degrees. + /// + public static void orderBestPatterns(ResultPoint[] patterns) + { + // Find distances between pattern centers + float zeroOneDistance = distance(patterns[0], patterns[1]); + float oneTwoDistance = distance(patterns[1], patterns[2]); + float zeroTwoDistance = distance(patterns[0], patterns[2]); + + ResultPoint pointA, pointB, pointC; + // Assume one closest to other two is B; A and C will just be guesses at first + if (oneTwoDistance >= zeroOneDistance && oneTwoDistance >= zeroTwoDistance) + { + pointB = patterns[0]; + pointA = patterns[1]; + pointC = patterns[2]; + } + else if (zeroTwoDistance >= oneTwoDistance && zeroTwoDistance >= zeroOneDistance) + { + pointB = patterns[1]; + pointA = patterns[0]; + pointC = patterns[2]; + } + else + { + pointB = patterns[2]; + pointA = patterns[0]; + pointC = patterns[1]; + } + + // Use cross product to figure out whether A and C are correct or flipped. + // This asks whether BC x BA has a positive z component, which is the arrangement + // we want for A, B, C. If it's negative, then we've got it flipped around and + // should swap A and C. + if (crossProductZ(pointA, pointB, pointC) < 0.0f) + { + ResultPoint temp = pointA; + pointA = pointC; + pointC = temp; + } + + patterns[0] = pointA; + patterns[1] = pointB; + patterns[2] = pointC; + } + + + /// + /// distance between two points + /// + public static float distance(ResultPoint pattern1, ResultPoint pattern2) + { + return MathUtils.distance(pattern1.x, pattern1.y, pattern2.x, pattern2.y); + } + + /// + /// Returns the z component of the cross product between vectors BC and BA. + /// + private static float crossProductZ(ResultPoint pointA, ResultPoint pointB, ResultPoint pointC) + { + float bX = pointB.x; + float bY = pointB.y; + return ((pointC.x - bX) * (pointA.y - bY)) - ((pointC.y - bY) * (pointA.x - bX)); + } + } +} \ No newline at end of file diff --git a/shadowsocks-csharp/3rd/zxing/ResultPointCallback.cs b/shadowsocks-csharp/3rd/zxing/ResultPointCallback.cs new file mode 100755 index 00000000..19c6f23a --- /dev/null +++ b/shadowsocks-csharp/3rd/zxing/ResultPointCallback.cs @@ -0,0 +1,26 @@ +/* +* Copyright 2009 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 +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +namespace ZXing +{ + /// Callback which is invoked when a possible result point (significant + /// point in the barcode image such as a corner) is found. + /// + /// + /// + /// + public delegate void ResultPointCallback(ResultPoint point); +} \ No newline at end of file diff --git a/shadowsocks-csharp/3rd/zxing/Version.cs b/shadowsocks-csharp/3rd/zxing/Version.cs deleted file mode 100755 index be531ca4..00000000 --- a/shadowsocks-csharp/3rd/zxing/Version.cs +++ /dev/null @@ -1,342 +0,0 @@ -/* -* 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 -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -using System; - -using ZXing.Common; - -namespace ZXing.QrCode.Internal -{ - /// - /// See ISO 18004:2006 Annex D - /// - /// Sean Owen - public sealed class Version - { - - private static readonly Version[] VERSIONS = buildVersions(); - - private readonly int versionNumber; - private readonly int[] alignmentPatternCenters; - private readonly ECBlocks[] ecBlocks; - private readonly int totalCodewords; - - private Version(int versionNumber, int[] alignmentPatternCenters, params ECBlocks[] ecBlocks) - { - this.versionNumber = versionNumber; - this.alignmentPatternCenters = alignmentPatternCenters; - this.ecBlocks = ecBlocks; - int total = 0; - int ecCodewords = ecBlocks[0].ECCodewordsPerBlock; - ECB[] ecbArray = ecBlocks[0].getECBlocks(); - foreach (var ecBlock in ecbArray) - { - total += ecBlock.Count * (ecBlock.DataCodewords + ecCodewords); - } - this.totalCodewords = total; - } - - /// - /// Gets the version number. - /// - public int VersionNumber - { - get - { - return versionNumber; - } - - } - - /// - /// Gets the total codewords. - /// - public int TotalCodewords - { - get - { - return totalCodewords; - } - - } - - /// - /// Gets the dimension for version. - /// - public int DimensionForVersion - { - get - { - return 17 + 4 * versionNumber; - } - - } - - /// - /// Gets the EC blocks for level. - /// - /// The ec level. - /// - public ECBlocks getECBlocksForLevel(ErrorCorrectionLevel ecLevel) - { - return ecBlocks[ecLevel.ordinal()]; - } - - /// - /// Gets the version for number. - /// - /// The version number. - /// - public static Version getVersionForNumber(int versionNumber) - { - if (versionNumber < 1 || versionNumber > 40) - { - throw new ArgumentException(); - } - return VERSIONS[versionNumber - 1]; - } - - - ///

Encapsulates a set of error-correction blocks in one symbol version. Most versions will - /// use blocks of differing sizes within one version, so, this encapsulates the parameters for - /// each set of blocks. It also holds the number of error-correction codewords per block since it - /// will be the same across all blocks within one version.

- ///
- public sealed class ECBlocks - { - private readonly int ecCodewordsPerBlock; - private readonly ECB[] ecBlocks; - - internal ECBlocks(int ecCodewordsPerBlock, params ECB[] ecBlocks) - { - this.ecCodewordsPerBlock = ecCodewordsPerBlock; - this.ecBlocks = ecBlocks; - } - - /// - /// Gets the EC codewords per block. - /// - public int ECCodewordsPerBlock - { - get - { - return ecCodewordsPerBlock; - } - } - - /// - /// Gets the num blocks. - /// - public int NumBlocks - { - get - { - int total = 0; - foreach (var ecBlock in ecBlocks) - { - total += ecBlock.Count; - } - return total; - } - } - - /// - /// Gets the total EC codewords. - /// - public int TotalECCodewords - { - get - { - return ecCodewordsPerBlock * NumBlocks; - } - } - - /// - /// Gets the EC blocks. - /// - /// - public ECB[] getECBlocks() - { - return ecBlocks; - } - } - - ///

Encapsualtes the parameters for one error-correction block in one symbol version. - /// This includes the number of data codewords, and the number of times a block with these - /// parameters is used consecutively in the QR code version's format.

- ///
- public sealed class ECB - { - private readonly int count; - private readonly int dataCodewords; - - internal ECB(int count, int dataCodewords) - { - this.count = count; - this.dataCodewords = dataCodewords; - } - - /// - /// Gets the count. - /// - public int Count - { - get - { - return count; - } - - } - /// - /// Gets the data codewords. - /// - public int DataCodewords - { - get - { - return dataCodewords; - } - - } - } - - /// - /// Returns a that represents this instance. - /// - /// - /// A that represents this instance. - /// - public override String ToString() - { - return Convert.ToString(versionNumber); - } - - /// See ISO 18004:2006 6.5.1 Table 9 - private static Version[] buildVersions() - { - return new Version[] - { - new Version(1, new int[] {}, - new ECBlocks(7, new ECB(1, 19)), - new ECBlocks(10, new ECB(1, 16)), - new ECBlocks(13, new ECB(1, 13)), - new ECBlocks(17, new ECB(1, 9))), - new Version(2, new int[] {6, 18}, - new ECBlocks(10, new ECB(1, 34)), - new ECBlocks(16, new ECB(1, 28)), - new ECBlocks(22, new ECB(1, 22)), - new ECBlocks(28, new ECB(1, 16))), - new Version(3, new int[] {6, 22}, - new ECBlocks(15, new ECB(1, 55)), - new ECBlocks(26, new ECB(1, 44)), - new ECBlocks(18, new ECB(2, 17)), - new ECBlocks(22, new ECB(2, 13))), - new Version(4, new int[] {6, 26}, - new ECBlocks(20, new ECB(1, 80)), - new ECBlocks(18, new ECB(2, 32)), - new ECBlocks(26, new ECB(2, 24)), - new ECBlocks(16, new ECB(4, 9))), - new Version(5, new int[] {6, 30}, - new ECBlocks(26, new ECB(1, 108)), - new ECBlocks(24, new ECB(2, 43)), - new ECBlocks(18, new ECB(2, 15), - new ECB(2, 16)), - new ECBlocks(22, new ECB(2, 11), - new ECB(2, 12))), - new Version(6, new int[] {6, 34}, - new ECBlocks(18, new ECB(2, 68)), - new ECBlocks(16, new ECB(4, 27)), - new ECBlocks(24, new ECB(4, 19)), - new ECBlocks(28, new ECB(4, 15))), - new Version(7, new int[] {6, 22, 38}, - new ECBlocks(20, new ECB(2, 78)), - new ECBlocks(18, new ECB(4, 31)), - new ECBlocks(18, new ECB(2, 14), - new ECB(4, 15)), - new ECBlocks(26, new ECB(4, 13), - new ECB(1, 14))), - new Version(8, new int[] {6, 24, 42}, - new ECBlocks(24, new ECB(2, 97)), - new ECBlocks(22, new ECB(2, 38), - new ECB(2, 39)), - new ECBlocks(22, new ECB(4, 18), - new ECB(2, 19)), - new ECBlocks(26, new ECB(4, 14), - new ECB(2, 15))), - new Version(9, new int[] {6, 26, 46}, - new ECBlocks(30, new ECB(2, 116)), - new ECBlocks(22, new ECB(3, 36), - new ECB(2, 37)), - new ECBlocks(20, new ECB(4, 16), - new ECB(4, 17)), - new ECBlocks(24, new ECB(4, 12), - new ECB(4, 13))), - new Version(10, new int[] {6, 28, 50}, - new ECBlocks(18, new ECB(2, 68), - new ECB(2, 69)), - new ECBlocks(26, new ECB(4, 43), - new ECB(1, 44)), - new ECBlocks(24, new ECB(6, 19), - new ECB(2, 20)), - new ECBlocks(28, new ECB(6, 15), - new ECB(2, 16))), - new Version(11, new int[] {6, 30, 54}, - new ECBlocks(20, new ECB(4, 81)), - new ECBlocks(30, new ECB(1, 50), - new ECB(4, 51)), - new ECBlocks(28, new ECB(4, 22), - new ECB(4, 23)), - new ECBlocks(24, new ECB(3, 12), - new ECB(8, 13))), - new Version(12, new int[] {6, 32, 58}, - new ECBlocks(24, new ECB(2, 92), - new ECB(2, 93)), - new ECBlocks(22, new ECB(6, 36), - new ECB(2, 37)), - new ECBlocks(26, new ECB(4, 20), - new ECB(6, 21)), - new ECBlocks(28, new ECB(7, 14), - new ECB(4, 15))), - new Version(13, new int[] {6, 34, 62}, - new ECBlocks(26, new ECB(4, 107)), - new ECBlocks(22, new ECB(8, 37), - new ECB(1, 38)), - new ECBlocks(24, new ECB(8, 20), - new ECB(4, 21)), - new ECBlocks(22, new ECB(12, 11), - new ECB(4, 12))), - new Version(14, new int[] {6, 26, 46, 66}, - new ECBlocks(30, new ECB(3, 115), - new ECB(1, 116)), - new ECBlocks(24, new ECB(4, 40), - new ECB(5, 41)), - new ECBlocks(20, new ECB(11, 16), - new ECB(5, 17)), - new ECBlocks(24, new ECB(11, 12), - new ECB(5, 13))), - new Version(15, new int[] {6, 26, 48, 70}, - new ECBlocks(22, new ECB(5, 87), - new ECB(1, 88)), - new ECBlocks(24, new ECB(5, 41), - new ECB(5, 42)), - new ECBlocks(30, new ECB(5, 24), - new ECB(7, 25)), - new ECBlocks(24, new ECB(11, 12), - new ECB(7, 13))) - }; - } - } -} \ No newline at end of file diff --git a/shadowsocks-csharp/3rd/zxing/WriterException.cs b/shadowsocks-csharp/3rd/zxing/WriterException.cs new file mode 100755 index 00000000..f6cd3395 --- /dev/null +++ b/shadowsocks-csharp/3rd/zxing/WriterException.cs @@ -0,0 +1,55 @@ +/* +* Copyright 2008 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 +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +using System; + +namespace ZXing +{ + /// + /// A base class which covers the range of exceptions which may occur when encoding a barcode using + /// the Writer framework. + /// + /// dswitkin@google.com (Daniel Switkin) + [Serializable] + public sealed class WriterException : Exception + { + /// + /// Initializes a new instance of the class. + /// + public WriterException() + { + } + + /// + /// Initializes a new instance of the class. + /// + /// The message. + public WriterException(String message) + :base(message) + { + } + + /// + /// Initializes a new instance of the class. + /// + /// The message. + /// The inner exc. + public WriterException(String message, Exception innerExc) + : base(message, innerExc) + { + } + } +} \ No newline at end of file diff --git a/shadowsocks-csharp/3rd/zxing/common/BitArray.cs b/shadowsocks-csharp/3rd/zxing/common/BitArray.cs new file mode 100755 index 00000000..c3676142 --- /dev/null +++ b/shadowsocks-csharp/3rd/zxing/common/BitArray.cs @@ -0,0 +1,488 @@ +/* +* 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 +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +using System; + +namespace ZXing.Common +{ + /// + /// A simple, fast array of bits, represented compactly by an array of ints internally. + /// + /// Sean Owen + public sealed class BitArray + { + private int[] bits; + private int size; + + public int Size + { + get + { + return size; + } + } + + public int SizeInBytes + { + get + { + return (size + 7) >> 3; + } + } + + public bool this[int i] + { + get + { + return (bits[i >> 5] & (1 << (i & 0x1F))) != 0; + } + set + { + if (value) + bits[i >> 5] |= 1 << (i & 0x1F); + } + } + + public BitArray() + { + this.size = 0; + this.bits = new int[1]; + } + + public BitArray(int size) + { + if (size < 1) + { + throw new ArgumentException("size must be at least 1"); + } + this.size = size; + this.bits = makeArray(size); + } + + // For testing only + private BitArray(int[] bits, int size) + { + this.bits = bits; + this.size = size; + } + + private void ensureCapacity(int size) + { + if (size > bits.Length << 5) + { + int[] newBits = makeArray(size); + System.Array.Copy(bits, 0, newBits, 0, bits.Length); + bits = newBits; + } + } + + /// Flips bit i. + /// + /// + /// bit to set + /// + public void flip(int i) + { + bits[i >> 5] ^= 1 << (i & 0x1F); + } + + private static int numberOfTrailingZeros(int num) + { + var index = (-num & num)%37; + if (index < 0) + index *= -1; + return _lookup[index]; + } + + private static readonly int[] _lookup = + { + 32, 0, 1, 26, 2, 23, 27, 0, 3, 16, 24, 30, 28, 11, 0, 13, 4, 7, 17, + 0, 25, 22, 31, 15, 29, 10, 12, 6, 0, 21, 14, 9, 5, 20, 8, 19, 18 + }; + + /// + /// Gets the next set. + /// + /// first bit to check + /// index of first bit that is set, starting from the given index, or size if none are set + /// at or beyond this given index + public int getNextSet(int from) + { + if (from >= size) + { + return size; + } + int bitsOffset = from >> 5; + int currentBits = bits[bitsOffset]; + // mask off lesser bits first + currentBits &= ~((1 << (from & 0x1F)) - 1); + while (currentBits == 0) + { + if (++bitsOffset == bits.Length) + { + return size; + } + currentBits = bits[bitsOffset]; + } + int result = (bitsOffset << 5) + numberOfTrailingZeros(currentBits); + return result > size ? size : result; + } + + /// + /// see getNextSet(int) + /// + /// + /// + public int getNextUnset(int from) + { + if (from >= size) + { + return size; + } + int bitsOffset = from >> 5; + int currentBits = ~bits[bitsOffset]; + // mask off lesser bits first + currentBits &= ~((1 << (from & 0x1F)) - 1); + while (currentBits == 0) + { + if (++bitsOffset == bits.Length) + { + return size; + } + currentBits = ~bits[bitsOffset]; + } + int result = (bitsOffset << 5) + numberOfTrailingZeros(currentBits); + return result > size ? size : result; + } + + /// Sets a block of 32 bits, starting at bit i. + /// + /// + /// first bit to set + /// + /// the new value of the next 32 bits. Note again that the least-significant bit + /// corresponds to bit i, the next-least-significant to i+1, and so on. + /// + public void setBulk(int i, int newBits) + { + bits[i >> 5] = newBits; + } + + /// + /// Sets a range of bits. + /// + /// start of range, inclusive. + /// end of range, exclusive + public void setRange(int start, int end) + { + if (end < start) + { + throw new ArgumentException(); + } + if (end == start) + { + return; + } + end--; // will be easier to treat this as the last actually set bit -- inclusive + int firstInt = start >> 5; + int lastInt = end >> 5; + for (int i = firstInt; i <= lastInt; i++) + { + int firstBit = i > firstInt ? 0 : start & 0x1F; + int lastBit = i < lastInt ? 31 : end & 0x1F; + int mask; + if (firstBit == 0 && lastBit == 31) + { + mask = -1; + } + else + { + mask = 0; + for (int j = firstBit; j <= lastBit; j++) + { + mask |= 1 << j; + } + } + bits[i] |= mask; + } + } + + /// Clears all bits (sets to false). + public void clear() + { + int max = bits.Length; + for (int i = 0; i < max; i++) + { + bits[i] = 0; + } + } + + /// Efficient method to check if a range of bits is set, or not set. + /// + /// + /// start of range, inclusive. + /// + /// end of range, exclusive + /// + /// if true, checks that bits in range are set, otherwise checks that they are not set + /// + /// true iff all bits are set or not set in range, according to value argument + /// + /// IllegalArgumentException if end is less than or equal to start + public bool isRange(int start, int end, bool value) + { + if (end < start) + { + throw new System.ArgumentException(); + } + if (end == start) + { + return true; // empty range matches + } + end--; // will be easier to treat this as the last actually set bit -- inclusive + int firstInt = start >> 5; + int lastInt = end >> 5; + for (int i = firstInt; i <= lastInt; i++) + { + int firstBit = i > firstInt ? 0 : start & 0x1F; + int lastBit = i < lastInt ? 31 : end & 0x1F; + int mask; + if (firstBit == 0 && lastBit == 31) + { + mask = -1; + } + else + { + mask = 0; + for (int j = firstBit; j <= lastBit; j++) + { + mask |= 1 << j; + } + } + + // Return false if we're looking for 1s and the masked bits[i] isn't all 1s (that is, + // equals the mask, or we're looking for 0s and the masked portion is not all 0s + if ((bits[i] & mask) != (value ? mask : 0)) + { + return false; + } + } + return true; + } + + /// + /// Appends the bit. + /// + /// The bit. + public void appendBit(bool bit) + { + ensureCapacity(size + 1); + if (bit) + { + bits[size >> 5] |= 1 << (size & 0x1F); + } + size++; + } + + /// underlying array of ints. The first element holds the first 32 bits, and the least + /// significant bit is bit 0. + /// + public int[] Array + { + get { return bits; } + } + + /// + /// Appends the least-significant bits, from value, in order from most-significant to + /// least-significant. For example, appending 6 bits from 0x000001E will append the bits + /// 0, 1, 1, 1, 1, 0 in that order. + /// + /// The value. + /// The num bits. + public void appendBits(int value, int numBits) + { + if (numBits < 0 || numBits > 32) + { + throw new ArgumentException("Num bits must be between 0 and 32"); + } + ensureCapacity(size + numBits); + for (int numBitsLeft = numBits; numBitsLeft > 0; numBitsLeft--) + { + appendBit(((value >> (numBitsLeft - 1)) & 0x01) == 1); + } + } + + public void appendBitArray(BitArray other) + { + int otherSize = other.size; + ensureCapacity(size + otherSize); + for (int i = 0; i < otherSize; i++) + { + appendBit(other[i]); + } + } + + public void xor(BitArray other) + { + if (bits.Length != other.bits.Length) + { + throw new ArgumentException("Sizes don't match"); + } + for (int i = 0; i < bits.Length; i++) + { + // The last byte could be incomplete (i.e. not have 8 bits in + // it) but there is no problem since 0 XOR 0 == 0. + bits[i] ^= other.bits[i]; + } + } + + /// + /// Toes the bytes. + /// + /// first bit to start writing + /// array to write into. Bytes are written most-significant byte first. This is the opposite + /// of the internal representation, which is exposed by BitArray + /// position in array to start writing + /// how many bytes to write + public void toBytes(int bitOffset, byte[] array, int offset, int numBytes) + { + for (int i = 0; i < numBytes; i++) + { + int theByte = 0; + for (int j = 0; j < 8; j++) + { + if (this[bitOffset]) + { + theByte |= 1 << (7 - j); + } + bitOffset++; + } + array[offset + i] = (byte)theByte; + } + } + + /// Reverses all bits in the array. + public void reverse() + { + var newBits = new int[bits.Length]; + // reverse all int's first + var len = ((size - 1) >> 5); + var oldBitsLen = len + 1; + for (var i = 0; i < oldBitsLen; i++) + { + var x = (long)bits[i]; + x = ((x >> 1) & 0x55555555u) | ((x & 0x55555555u) << 1); + x = ((x >> 2) & 0x33333333u) | ((x & 0x33333333u) << 2); + x = ((x >> 4) & 0x0f0f0f0fu) | ((x & 0x0f0f0f0fu) << 4); + x = ((x >> 8) & 0x00ff00ffu) | ((x & 0x00ff00ffu) << 8); + x = ((x >> 16) & 0x0000ffffu) | ((x & 0x0000ffffu) << 16); + newBits[len - i] = (int)x; + } + // now correct the int's if the bit size isn't a multiple of 32 + if (size != oldBitsLen * 32) + { + var leftOffset = oldBitsLen * 32 - size; + var mask = 1; + for (var i = 0; i < 31 - leftOffset; i++) + mask = (mask << 1) | 1; + var currentInt = (newBits[0] >> leftOffset) & mask; + for (var i = 1; i < oldBitsLen; i++) + { + var nextInt = newBits[i]; + currentInt |= nextInt << (32 - leftOffset); + newBits[i - 1] = currentInt; + currentInt = (nextInt >> leftOffset) & mask; + } + newBits[oldBitsLen - 1] = currentInt; + } + bits = newBits; + } + + private static int[] makeArray(int size) + { + return new int[(size + 31) >> 5]; + } + + /// + /// Determines whether the specified is equal to this instance. + /// + /// The to compare with this instance. + /// + /// true if the specified is equal to this instance; otherwise, false. + /// + public override bool Equals(Object o) + { + var other = o as BitArray; + if (other == null) + return false; + if (size != other.size) + return false; + for (var index = 0; index < size; index++) + { + if (bits[index] != other.bits[index]) + return false; + } + return true; + } + + /// + /// Returns a hash code for this instance. + /// + /// + /// A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table. + /// + public override int GetHashCode() + { + var hash = size; + foreach (var bit in bits) + { + hash = 31 * hash + bit.GetHashCode(); + } + return hash; + } + + /// + /// Returns a that represents this instance. + /// + /// + /// A that represents this instance. + /// + public override String ToString() + { + var result = new System.Text.StringBuilder(size); + for (int i = 0; i < size; i++) + { + if ((i & 0x07) == 0) + { + result.Append(' '); + } + result.Append(this[i] ? 'X' : '.'); + } + return result.ToString(); + } + + /// + /// Erstellt ein neues Objekt, das eine Kopie der aktuellen Instanz darstellt. + /// + /// + /// Ein neues Objekt, das eine Kopie dieser Instanz darstellt. + /// + public object Clone() + { + return new BitArray((int[])bits.Clone(), size); + } + } +} \ No newline at end of file diff --git a/shadowsocks-csharp/3rd/zxing/common/BitMatrix.cs b/shadowsocks-csharp/3rd/zxing/common/BitMatrix.cs new file mode 100755 index 00000000..52dce4bd --- /dev/null +++ b/shadowsocks-csharp/3rd/zxing/common/BitMatrix.cs @@ -0,0 +1,427 @@ +/* +* 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 +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +using System; + +namespace ZXing.Common +{ + /// + ///

Represents a 2D matrix of bits. In function arguments below, and throughout the common + /// module, x is the column position, and y is the row position. The ordering is always x, y. + /// The origin is at the top-left.

+ ///

Internally the bits are represented in a 1-D array of 32-bit ints. However, each row begins + /// with a new int. This is done intentionally so that we can copy out a row into a BitArray very + /// efficiently.

+ ///

The ordering of bits is row-major. Within each int, the least significant bits are used first, + /// meaning they represent lower x values. This is compatible with BitArray's implementation.

+ ///
+ /// Sean Owen + /// dswitkin@google.com (Daniel Switkin) + public sealed partial class BitMatrix + { + private readonly int width; + private readonly int height; + private readonly int rowSize; + private readonly int[] bits; + + /// The width of the matrix + /// + public int Width + { + get + { + return width; + } + + } + /// The height of the matrix + /// + public int Height + { + get + { + return height; + } + + } + /// This method is for compatibility with older code. It's only logical to call if the matrix + /// is square, so I'm throwing if that's not the case. + /// + /// + /// row/column dimension of this matrix + /// + public int Dimension + { + get + { + if (width != height) + { + throw new System.ArgumentException("Can't call getDimension() on a non-square matrix"); + } + return width; + } + + } + + // A helper to construct a square matrix. + public BitMatrix(int dimension) + : this(dimension, dimension) + { + } + + public BitMatrix(int width, int height) + { + if (width < 1 || height < 1) + { + throw new System.ArgumentException("Both dimensions must be greater than 0"); + } + this.width = width; + this.height = height; + this.rowSize = (width + 31) >> 5; + bits = new int[rowSize * height]; + } + + private BitMatrix(int width, int height, int rowSize, int[] bits) + { + this.width = width; + this.height = height; + this.rowSize = rowSize; + this.bits = bits; + } + + ///

Gets the requested bit, where true means black.

+ /// + ///
+ /// The horizontal component (i.e. which column) + /// + /// The vertical component (i.e. which row) + /// + /// value of given bit in matrix + /// + public bool this[int x, int y] + { + get + { + int offset = y * rowSize + (x >> 5); + return (((int)((uint)(bits[offset]) >> (x & 0x1f))) & 1) != 0; + } + set + { + if (value) + { + int offset = y * rowSize + (x >> 5); + bits[offset] |= 1 << (x & 0x1f); + } + } + } + + ///

Flips the given bit.

+ /// + ///
+ /// The horizontal component (i.e. which column) + /// + /// The vertical component (i.e. which row) + /// + public void flip(int x, int y) + { + int offset = y * rowSize + (x >> 5); + bits[offset] ^= 1 << (x & 0x1f); + } + + /// Clears all bits (sets to false). + public void clear() + { + int max = bits.Length; + for (int i = 0; i < max; i++) + { + bits[i] = 0; + } + } + + ///

Sets a square region of the bit matrix to true.

+ /// + ///
+ /// The horizontal position to begin at (inclusive) + /// + /// The vertical position to begin at (inclusive) + /// + /// The width of the region + /// + /// The height of the region + /// + public void setRegion(int left, int top, int width, int height) + { + if (top < 0 || left < 0) + { + throw new System.ArgumentException("Left and top must be nonnegative"); + } + if (height < 1 || width < 1) + { + throw new System.ArgumentException("Height and width must be at least 1"); + } + int right = left + width; + int bottom = top + height; + if (bottom > this.height || right > this.width) + { + throw new System.ArgumentException("The region must fit inside the matrix"); + } + for (int y = top; y < bottom; y++) + { + int offset = y * rowSize; + for (int x = left; x < right; x++) + { + bits[offset + (x >> 5)] |= 1 << (x & 0x1f); + } + } + } + + /// A fast method to retrieve one row of data from the matrix as a BitArray. + /// + /// + /// The row to retrieve + /// + /// An optional caller-allocated BitArray, will be allocated if null or too small + /// + /// The resulting BitArray - this reference should always be used even when passing + /// your own row + /// + public BitArray getRow(int y, BitArray row) + { + if (row == null || row.Size < width) + { + row = new BitArray(width); + } + else + { + row.clear(); + } + int offset = y * rowSize; + for (int x = 0; x < rowSize; x++) + { + row.setBulk(x << 5, bits[offset + x]); + } + return row; + } + + /// + /// Sets the row. + /// + /// row to set + /// {@link BitArray} to copy from + public void setRow(int y, BitArray row) + { + Array.Copy(row.Array, 0, bits, y * rowSize, rowSize); + } + + /// + /// Modifies this {@code BitMatrix} to represent the same but rotated 180 degrees + /// + public void rotate180() + { + var width = Width; + var height = Height; + var topRow = new BitArray(width); + var bottomRow = new BitArray(width); + for (int i = 0; i < (height + 1)/2; i++) + { + topRow = getRow(i, topRow); + bottomRow = getRow(height - 1 - i, bottomRow); + topRow.reverse(); + bottomRow.reverse(); + setRow(i, bottomRow); + setRow(height - 1 - i, topRow); + } + } + + /// + /// This is useful in detecting the enclosing rectangle of a 'pure' barcode. + /// + /// {left,top,width,height} enclosing rectangle of all 1 bits, or null if it is all white + public int[] getEnclosingRectangle() + { + int left = width; + int top = height; + int right = -1; + int bottom = -1; + + for (int y = 0; y < height; y++) + { + for (int x32 = 0; x32 < rowSize; x32++) + { + int theBits = bits[y * rowSize + x32]; + if (theBits != 0) + { + if (y < top) + { + top = y; + } + if (y > bottom) + { + bottom = y; + } + if (x32 * 32 < left) + { + int bit = 0; + while ((theBits << (31 - bit)) == 0) + { + bit++; + } + if ((x32 * 32 + bit) < left) + { + left = x32 * 32 + bit; + } + } + if (x32 * 32 + 31 > right) + { + int bit = 31; + while (((int)((uint)theBits >> bit)) == 0) // (theBits >>> bit) + { + bit--; + } + if ((x32 * 32 + bit) > right) + { + right = x32 * 32 + bit; + } + } + } + } + } + + int widthTmp = right - left; + int heightTmp = bottom - top; + + if (widthTmp < 0 || heightTmp < 0) + { + return null; + } + + return new [] { left, top, widthTmp, heightTmp }; + } + + /// + /// This is useful in detecting a corner of a 'pure' barcode. + /// + /// {x,y} coordinate of top-left-most 1 bit, or null if it is all white + public int[] getTopLeftOnBit() + { + int bitsOffset = 0; + while (bitsOffset < bits.Length && bits[bitsOffset] == 0) + { + bitsOffset++; + } + if (bitsOffset == bits.Length) + { + return null; + } + int y = bitsOffset / rowSize; + int x = (bitsOffset % rowSize) << 5; + + int theBits = bits[bitsOffset]; + int bit = 0; + while ((theBits << (31 - bit)) == 0) + { + bit++; + } + x += bit; + return new[] { x, y }; + } + + public int[] getBottomRightOnBit() + { + int bitsOffset = bits.Length - 1; + while (bitsOffset >= 0 && bits[bitsOffset] == 0) + { + bitsOffset--; + } + if (bitsOffset < 0) + { + return null; + } + + int y = bitsOffset / rowSize; + int x = (bitsOffset % rowSize) << 5; + + int theBits = bits[bitsOffset]; + int bit = 31; + + while (((int)((uint)theBits >> bit)) == 0) // (theBits >>> bit) + { + bit--; + } + x += bit; + + return new int[] { x, y }; + } + + public override bool Equals(object obj) + { + if (!(obj is BitMatrix)) + { + return false; + } + BitMatrix other = (BitMatrix)obj; + if (width != other.width || height != other.height || + rowSize != other.rowSize || bits.Length != other.bits.Length) + { + return false; + } + for (int i = 0; i < bits.Length; i++) + { + if (bits[i] != other.bits[i]) + { + return false; + } + } + return true; + } + + public override int GetHashCode() + { + int hash = width; + hash = 31 * hash + width; + hash = 31 * hash + height; + hash = 31 * hash + rowSize; + foreach (var bit in bits) + { + hash = 31 * hash + bit.GetHashCode(); + } + return hash; + } + + public override String ToString() + { + var result = new System.Text.StringBuilder(height * (width + 1)); + for (int y = 0; y < height; y++) + { + for (int x = 0; x < width; x++) + { + result.Append(this[x, y] ? "X " : " "); + } +#if WindowsCE + result.Append("\r\n"); +#else + result.AppendLine(""); +#endif + } + return result.ToString(); + } + + public object Clone() + { + return new BitMatrix(width, height, rowSize, (int[])bits.Clone()); + } + } +} \ No newline at end of file diff --git a/shadowsocks-csharp/3rd/zxing/common/BitSource.cs b/shadowsocks-csharp/3rd/zxing/common/BitSource.cs new file mode 100755 index 00000000..ba9156d2 --- /dev/null +++ b/shadowsocks-csharp/3rd/zxing/common/BitSource.cs @@ -0,0 +1,124 @@ +/* +* 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 +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +using System; + +namespace ZXing.Common +{ + ///

This provides an easy abstraction to read bits at a time from a sequence of bytes, where the + /// number of bits read is not often a multiple of 8.

+ /// + ///

This class is thread-safe but not reentrant. Unless the caller modifies the bytes array + /// it passed in, in which case all bets are off.

+ /// + ///
+ /// Sean Owen + /// + /// www.Redivivus.in (suraj.supekar@redivivus.in) - Ported from ZXING Java Source + /// + public sealed class BitSource + { + private readonly byte[] bytes; + private int byteOffset; + private int bitOffset; + + /// bytes from which this will read bits. Bits will be read from the first byte first. + /// Bits are read within a byte from most-significant to least-significant bit. + /// + public BitSource(byte[] bytes) + { + this.bytes = bytes; + } + + /// + /// index of next bit in current byte which would be read by the next call to {@link #readBits(int)}. + /// + public int BitOffset + { + get { return bitOffset; } + } + + /// + /// index of next byte in input byte array which would be read by the next call to {@link #readBits(int)}. + /// + public int ByteOffset + { + get { return byteOffset; } + } + + /// number of bits to read + /// + /// int representing the bits read. The bits will appear as the least-significant + /// bits of the int + /// + /// if numBits isn't in [1,32] or more than is available + public int readBits(int numBits) + { + if (numBits < 1 || numBits > 32 || numBits > available()) + { + throw new ArgumentException(numBits.ToString(), "numBits"); + } + + int result = 0; + + // First, read remainder from current byte + if (bitOffset > 0) + { + int bitsLeft = 8 - bitOffset; + int toRead = numBits < bitsLeft ? numBits : bitsLeft; + int bitsToNotRead = bitsLeft - toRead; + int mask = (0xFF >> (8 - toRead)) << bitsToNotRead; + result = (bytes[byteOffset] & mask) >> bitsToNotRead; + numBits -= toRead; + bitOffset += toRead; + if (bitOffset == 8) + { + bitOffset = 0; + byteOffset++; + } + } + + // Next read whole bytes + if (numBits > 0) + { + while (numBits >= 8) + { + result = (result << 8) | (bytes[byteOffset] & 0xFF); + byteOffset++; + numBits -= 8; + } + + // Finally read a partial byte + if (numBits > 0) + { + int bitsToNotRead = 8 - numBits; + int mask = (0xFF >> bitsToNotRead) << bitsToNotRead; + result = (result << numBits) | ((bytes[byteOffset] & mask) >> bitsToNotRead); + bitOffset += numBits; + } + } + + return result; + } + + /// number of bits that can be read successfully + /// + public int available() + { + return 8 * (bytes.Length - byteOffset) - bitOffset; + } + } +} \ No newline at end of file diff --git a/shadowsocks-csharp/3rd/zxing/common/DecoderResult.cs b/shadowsocks-csharp/3rd/zxing/common/DecoderResult.cs new file mode 100755 index 00000000..024930f2 --- /dev/null +++ b/shadowsocks-csharp/3rd/zxing/common/DecoderResult.cs @@ -0,0 +1,76 @@ +/* +* 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 +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +using System; +using System.Collections.Generic; + +namespace ZXing.Common +{ + /// + /// Encapsulates the result of decoding a matrix of bits. This typically + /// applies to 2D barcode formats. For now it contains the raw bytes obtained, + /// as well as a String interpretation of those bytes, if applicable. + /// Sean Owen + /// + public sealed class DecoderResult + { + public byte[] RawBytes { get; private set; } + + public String Text { get; private set; } + + public IList ByteSegments { get; private set; } + + public String ECLevel { get; private set; } + + public bool StructuredAppend + { + get { return StructuredAppendParity >= 0 && StructuredAppendSequenceNumber >= 0; } + } + + public int ErrorsCorrected { get; set; } + + public int StructuredAppendSequenceNumber { get; private set; } + + public int Erasures { get; set; } + + public int StructuredAppendParity { get; private set; } + + /// + /// Miscellanseous data value for the various decoders + /// + /// The other. + public object Other { get; set; } + + public DecoderResult(byte[] rawBytes, String text, IList byteSegments, String ecLevel) + : this(rawBytes, text, byteSegments, ecLevel, -1, -1) + { + } + + public DecoderResult(byte[] rawBytes, String text, IList byteSegments, String ecLevel, int saSequence, int saParity) + { + if (rawBytes == null && text == null) + { + throw new ArgumentException(); + } + RawBytes = rawBytes; + Text = text; + ByteSegments = byteSegments; + ECLevel = ecLevel; + StructuredAppendParity = saParity; + StructuredAppendSequenceNumber = saSequence; + } + } +} \ No newline at end of file diff --git a/shadowsocks-csharp/3rd/zxing/common/DefaultGridSampler.cs b/shadowsocks-csharp/3rd/zxing/common/DefaultGridSampler.cs new file mode 100755 index 00000000..b3afaf8e --- /dev/null +++ b/shadowsocks-csharp/3rd/zxing/common/DefaultGridSampler.cs @@ -0,0 +1,82 @@ +/* +* 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 +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ +using System; + +namespace ZXing.Common +{ + + /// Sean Owen + /// + /// www.Redivivus.in (suraj.supekar@redivivus.in) - Ported from ZXING Java Source + /// + public sealed class DefaultGridSampler : GridSampler + { + public override BitMatrix sampleGrid(BitMatrix image, int dimensionX, int dimensionY, float p1ToX, float p1ToY, float p2ToX, float p2ToY, float p3ToX, float p3ToY, float p4ToX, float p4ToY, float p1FromX, float p1FromY, float p2FromX, float p2FromY, float p3FromX, float p3FromY, float p4FromX, float p4FromY) + { + PerspectiveTransform transform = PerspectiveTransform.quadrilateralToQuadrilateral( + p1ToX, p1ToY, p2ToX, p2ToY, p3ToX, p3ToY, p4ToX, p4ToY, + p1FromX, p1FromY, p2FromX, p2FromY, p3FromX, p3FromY, p4FromX, p4FromY); + return sampleGrid(image, dimensionX, dimensionY, transform); + } + + public override BitMatrix sampleGrid(BitMatrix image, int dimensionX, int dimensionY, PerspectiveTransform transform) + { + if (dimensionX <= 0 || dimensionY <= 0) + { + return null; + } + BitMatrix bits = new BitMatrix(dimensionX, dimensionY); + float[] points = new float[dimensionX << 1]; + for (int y = 0; y < dimensionY; y++) + { + int max = points.Length; + //UPGRADE_WARNING: Data types in Visual C# might be different. Verify the accuracy of narrowing conversions. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1042'" + float iValue = (float)y + 0.5f; + for (int x = 0; x < max; x += 2) + { + //UPGRADE_WARNING: Data types in Visual C# might be different. Verify the accuracy of narrowing conversions. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1042'" + points[x] = (float)(x >> 1) + 0.5f; + points[x + 1] = iValue; + } + transform.transformPoints(points); + // Quick check to see if points transformed to something inside the image; + // sufficient to check the endpoints + if (!checkAndNudgePoints(image, points)) + return null; + try + { + for (int x = 0; x < max; x += 2) + { + //UPGRADE_WARNING: Data types in Visual C# might be different. Verify the accuracy of narrowing conversions. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1042'" + bits[x >> 1, y] = image[(int)points[x], (int)points[x + 1]]; + } + } + catch (System.IndexOutOfRangeException) + { + // This feels wrong, but, sometimes if the finder patterns are misidentified, the resulting + // transform gets "twisted" such that it maps a straight line of points to a set of points + // whose endpoints are in bounds, but others are not. There is probably some mathematical + // way to detect this about the transformation that I don't know yet. + // This results in an ugly runtime exception despite our clever checks above -- can't have + // that. We could check each point's coordinates but that feels duplicative. We settle for + // catching and wrapping ArrayIndexOutOfBoundsException. + return null; + } + } + return bits; + } + } +} \ No newline at end of file diff --git a/shadowsocks-csharp/3rd/zxing/common/DetectorResult.cs b/shadowsocks-csharp/3rd/zxing/common/DetectorResult.cs new file mode 100755 index 00000000..0f99b9ba --- /dev/null +++ b/shadowsocks-csharp/3rd/zxing/common/DetectorResult.cs @@ -0,0 +1,39 @@ +/* +* 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 +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +namespace ZXing.Common +{ + ///

Encapsulates the result of detecting a barcode in an image. This includes the raw + /// matrix of black/white pixels corresponding to the barcode, and possibly points of interest + /// in the image, like the location of finder patterns or corners of the barcode in the image.

+ /// + ///
+ /// Sean Owen + /// + /// www.Redivivus.in (suraj.supekar@redivivus.in) - Ported from ZXING Java Source + /// + public class DetectorResult + { + public BitMatrix Bits { get; private set; } + public ResultPoint[] Points { get; private set; } + + public DetectorResult(BitMatrix bits, ResultPoint[] points) + { + Bits = bits; + Points = points; + } + } +} \ No newline at end of file diff --git a/shadowsocks-csharp/3rd/zxing/common/EncodingOptions.cs b/shadowsocks-csharp/3rd/zxing/common/EncodingOptions.cs new file mode 100755 index 00000000..683453ec --- /dev/null +++ b/shadowsocks-csharp/3rd/zxing/common/EncodingOptions.cs @@ -0,0 +1,121 @@ +/* + * Copyright 2012 ZXing.Net 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 + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +using System; +using System.Collections.Generic; +using System.ComponentModel; + +namespace ZXing.Common +{ + /// + /// Defines an container for encoder options + /// + [Serializable] + public class EncodingOptions + { + /// + /// Gets the data container for all options + /// + [Browsable(false)] + public IDictionary Hints { get; private set; } + + /// + /// Specifies the height of the barcode image + /// + public int Height + { + get + { + if (Hints.ContainsKey(EncodeHintType.HEIGHT)) + { + return (int)Hints[EncodeHintType.HEIGHT]; + } + return 0; + } + set + { + Hints[EncodeHintType.HEIGHT] = value; + } + } + + /// + /// Specifies the width of the barcode image + /// + public int Width + { + get + { + if (Hints.ContainsKey(EncodeHintType.WIDTH)) + { + return (int)Hints[EncodeHintType.WIDTH]; + } + return 0; + } + set + { + Hints[EncodeHintType.WIDTH] = value; + } + } + + /// + /// Don't put the content string into the output image. + /// + public bool PureBarcode + { + get + { + if (Hints.ContainsKey(EncodeHintType.PURE_BARCODE)) + { + return (bool)Hints[EncodeHintType.PURE_BARCODE]; + } + return false; + } + set + { + Hints[EncodeHintType.PURE_BARCODE] = value; + } + } + + /// + /// Specifies margin, in pixels, to use when generating the barcode. The meaning can vary + /// by format; for example it controls margin before and after the barcode horizontally for + /// most 1D formats. + /// + public int Margin + { + get + { + if (Hints.ContainsKey(EncodeHintType.MARGIN)) + { + return (int) Hints[EncodeHintType.MARGIN]; + } + return 0; + } + set + { + Hints[EncodeHintType.MARGIN] = value; + } + } + + /// + /// Initializes a new instance of the class. + /// + public EncodingOptions() + { + Hints = new Dictionary(); + } + } +} diff --git a/shadowsocks-csharp/3rd/zxing/common/GridSampler.cs b/shadowsocks-csharp/3rd/zxing/common/GridSampler.cs new file mode 100755 index 00000000..a1bffa99 --- /dev/null +++ b/shadowsocks-csharp/3rd/zxing/common/GridSampler.cs @@ -0,0 +1,192 @@ +/* +* 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 +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +namespace ZXing.Common +{ + /// Implementations of this class can, given locations of finder patterns for a QR code in an + /// image, sample the right points in the image to reconstruct the QR code, accounting for + /// perspective distortion. It is abstracted since it is relatively expensive and should be allowed + /// to take advantage of platform-specific optimized implementations, like Sun's Java Advanced + /// Imaging library, but which may not be available in other environments such as J2ME, and vice + /// versa. + /// + /// The implementation used can be controlled by calling {@link #setGridSampler(GridSampler)} + /// with an instance of a class which implements this interface. + /// + /// + /// Sean Owen + /// + /// www.Redivivus.in (suraj.supekar@redivivus.in) - Ported from ZXING Java Source + /// + public abstract class GridSampler + { + /// the current implementation of {@link GridSampler} + /// + public static GridSampler Instance + { + get + { + return gridSampler; + } + + } + + private static GridSampler gridSampler = new DefaultGridSampler(); + + /// Sets the implementation of {@link GridSampler} used by the library. One global + /// instance is stored, which may sound problematic. But, the implementation provided + /// ought to be appropriate for the entire platform, and all uses of this library + /// in the whole lifetime of the JVM. For instance, an Android activity can swap in + /// an implementation that takes advantage of native platform libraries. + /// + /// + /// The platform-specific object to install. + /// + public static void setGridSampler(GridSampler newGridSampler) + { + if (newGridSampler == null) + { + throw new System.ArgumentException(); + } + gridSampler = newGridSampler; + } + + ///

Samples an image for a square matrix of bits of the given dimension. This is used to extract + /// the black/white modules of a 2D barcode like a QR Code found in an image. Because this barcode + /// may be rotated or perspective-distorted, the caller supplies four points in the source image + /// that define known points in the barcode, so that the image may be sampled appropriately.

+ /// + ///

The last eight "from" parameters are four X/Y coordinate pairs of locations of points in + /// the image that define some significant points in the image to be sample. For example, + /// these may be the location of finder pattern in a QR Code.

+ /// + ///

The first eight "to" parameters are four X/Y coordinate pairs measured in the destination + /// {@link BitMatrix}, from the top left, where the known points in the image given by the "from" + /// parameters map to.

+ /// + ///

These 16 parameters define the transformation needed to sample the image.

+ /// + ///
+ /// image to sample + /// + /// width/height of {@link BitMatrix} to sample from image + /// + /// {@link BitMatrix} representing a grid of points sampled from the image within a region + /// defined by the "from" parameters + /// + /// ReaderException if image can't be sampled, for example, if the transformation defined + /// by the given points is invalid or results in sampling outside the image boundaries + /// + public abstract BitMatrix sampleGrid(BitMatrix image, int dimensionX, int dimensionY, float p1ToX, float p1ToY, float p2ToX, float p2ToY, float p3ToX, float p3ToY, float p4ToX, float p4ToY, float p1FromX, float p1FromY, float p2FromX, float p2FromY, float p3FromX, float p3FromY, float p4FromX, float p4FromY); + + public virtual BitMatrix sampleGrid(BitMatrix image, int dimensionX, int dimensionY, PerspectiveTransform transform) + { + throw new System.NotSupportedException(); + } + + + ///

Checks a set of points that have been transformed to sample points on an image against + /// the image's dimensions to see if the point are even within the image.

+ /// + ///

This method will actually "nudge" the endpoints back onto the image if they are found to be + /// barely (less than 1 pixel) off the image. This accounts for imperfect detection of finder + /// patterns in an image where the QR Code runs all the way to the image border.

+ /// + ///

For efficiency, the method will check points from either end of the line until one is found + /// to be within the image. Because the set of points are assumed to be linear, this is valid.

+ /// + ///
+ /// image into which the points should map + /// + /// actual points in x1,y1,...,xn,yn form + /// + protected internal static bool checkAndNudgePoints(BitMatrix image, float[] points) + { + int width = image.Width; + int height = image.Height; + // Check and nudge points from start until we see some that are OK: + bool nudged = true; + for (int offset = 0; offset < points.Length && nudged; offset += 2) + { + //UPGRADE_WARNING: Data types in Visual C# might be different. Verify the accuracy of narrowing conversions. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1042'" + int x = (int)points[offset]; + //UPGRADE_WARNING: Data types in Visual C# might be different. Verify the accuracy of narrowing conversions. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1042'" + int y = (int)points[offset + 1]; + if (x < -1 || x > width || y < -1 || y > height) + { + return false; + } + nudged = false; + if (x == -1) + { + points[offset] = 0.0f; + nudged = true; + } + else if (x == width) + { + points[offset] = width - 1; + nudged = true; + } + if (y == -1) + { + points[offset + 1] = 0.0f; + nudged = true; + } + else if (y == height) + { + points[offset + 1] = height - 1; + nudged = true; + } + } + // Check and nudge points from end: + nudged = true; + for (int offset = points.Length - 2; offset >= 0 && nudged; offset -= 2) + { + //UPGRADE_WARNING: Data types in Visual C# might be different. Verify the accuracy of narrowing conversions. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1042'" + int x = (int)points[offset]; + //UPGRADE_WARNING: Data types in Visual C# might be different. Verify the accuracy of narrowing conversions. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1042'" + int y = (int)points[offset + 1]; + if (x < -1 || x > width || y < -1 || y > height) + { + return false; + } + nudged = false; + if (x == -1) + { + points[offset] = 0.0f; + nudged = true; + } + else if (x == width) + { + points[offset] = width - 1; + nudged = true; + } + if (y == -1) + { + points[offset + 1] = 0.0f; + nudged = true; + } + else if (y == height) + { + points[offset + 1] = height - 1; + nudged = true; + } + } + + return true; + } + } +} \ No newline at end of file diff --git a/shadowsocks-csharp/3rd/zxing/common/PerspectiveTransform.cs b/shadowsocks-csharp/3rd/zxing/common/PerspectiveTransform.cs new file mode 100755 index 00000000..3515ddd8 --- /dev/null +++ b/shadowsocks-csharp/3rd/zxing/common/PerspectiveTransform.cs @@ -0,0 +1,159 @@ +/* +* 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 +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +namespace ZXing.Common +{ + ///

This class implements a perspective transform in two dimensions. Given four source and four + /// destination points, it will compute the transformation implied between them. The code is based + /// directly upon section 3.4.2 of George Wolberg's "Digital Image Warping"; see pages 54-56.

+ /// + ///
+ /// Sean Owen + /// + /// www.Redivivus.in (suraj.supekar@redivivus.in) - Ported from ZXING Java Source + /// + public sealed class PerspectiveTransform + { + private float a11; + private float a12; + private float a13; + private float a21; + private float a22; + private float a23; + private float a31; + private float a32; + private float a33; + + private PerspectiveTransform(float a11, float a21, float a31, float a12, float a22, float a32, float a13, float a23, float a33) + { + this.a11 = a11; + this.a12 = a12; + this.a13 = a13; + this.a21 = a21; + this.a22 = a22; + this.a23 = a23; + this.a31 = a31; + this.a32 = a32; + this.a33 = a33; + } + + public static PerspectiveTransform quadrilateralToQuadrilateral(float x0, float y0, float x1, float y1, float x2, float y2, float x3, float y3, float x0p, float y0p, float x1p, float y1p, float x2p, float y2p, float x3p, float y3p) + { + + PerspectiveTransform qToS = quadrilateralToSquare(x0, y0, x1, y1, x2, y2, x3, y3); + PerspectiveTransform sToQ = squareToQuadrilateral(x0p, y0p, x1p, y1p, x2p, y2p, x3p, y3p); + return sToQ.times(qToS); + } + + public void transformPoints(float[] points) + { + int max = points.Length; + float a11 = this.a11; + float a12 = this.a12; + float a13 = this.a13; + float a21 = this.a21; + float a22 = this.a22; + float a23 = this.a23; + float a31 = this.a31; + float a32 = this.a32; + float a33 = this.a33; + for (int i = 0; i < max; i += 2) + { + float x = points[i]; + float y = points[i + 1]; + float denominator = a13 * x + a23 * y + a33; + points[i] = (a11 * x + a21 * y + a31) / denominator; + points[i + 1] = (a12 * x + a22 * y + a32) / denominator; + } + } + + /// Convenience method, not optimized for performance. + public void transformPoints(float[] xValues, float[] yValues) + { + int n = xValues.Length; + for (int i = 0; i < n; i++) + { + float x = xValues[i]; + float y = yValues[i]; + float denominator = a13 * x + a23 * y + a33; + xValues[i] = (a11 * x + a21 * y + a31) / denominator; + yValues[i] = (a12 * x + a22 * y + a32) / denominator; + } + } + + public static PerspectiveTransform squareToQuadrilateral(float x0, float y0, + float x1, float y1, + float x2, float y2, + float x3, float y3) + { + float dx3 = x0 - x1 + x2 - x3; + float dy3 = y0 - y1 + y2 - y3; + if (dx3 == 0.0f && dy3 == 0.0f) + { + // Affine + return new PerspectiveTransform(x1 - x0, x2 - x1, x0, + y1 - y0, y2 - y1, y0, + 0.0f, 0.0f, 1.0f); + } + else + { + float dx1 = x1 - x2; + float dx2 = x3 - x2; + float dy1 = y1 - y2; + float dy2 = y3 - y2; + float denominator = dx1*dy2 - dx2*dy1; + float a13 = (dx3*dy2 - dx2*dy3)/denominator; + float a23 = (dx1*dy3 - dx3*dy1)/denominator; + return new PerspectiveTransform(x1 - x0 + a13*x1, x3 - x0 + a23*x3, x0, + y1 - y0 + a13*y1, y3 - y0 + a23*y3, y0, + a13, a23, 1.0f); + } + } + + public static PerspectiveTransform quadrilateralToSquare(float x0, float y0, float x1, float y1, float x2, float y2, float x3, float y3) + { + // Here, the adjoint serves as the inverse: + return squareToQuadrilateral(x0, y0, x1, y1, x2, y2, x3, y3).buildAdjoint(); + } + + internal PerspectiveTransform buildAdjoint() + { + // Adjoint is the transpose of the cofactor matrix: + return new PerspectiveTransform(a22 * a33 - a23 * a32, + a23 * a31 - a21 * a33, + a21 * a32 - a22 * a31, + a13 * a32 - a12 * a33, + a11 * a33 - a13 * a31, + a12 * a31 - a11 * a32, + a12 * a23 - a13 * a22, + a13 * a21 - a11 * a23, + a11 * a22 - a12 * a21); + } + + internal PerspectiveTransform times(PerspectiveTransform other) + { + return new PerspectiveTransform(a11 * other.a11 + a21 * other.a12 + a31 * other.a13, + a11 * other.a21 + a21 * other.a22 + a31 * other.a23, + a11 * other.a31 + a21 * other.a32 + a31 * other.a33, + a12 * other.a11 + a22 * other.a12 + a32 * other.a13, + a12 * other.a21 + a22 * other.a22 + a32 * other.a23, + a12 * other.a31 + a22 * other.a32 + a32 * other.a33, + a13 * other.a11 + a23 * other.a12 + a33 * other.a13, + a13 * other.a21 + a23 * other.a22 + a33 * other.a23, + a13 * other.a31 + a23 * other.a32 + a33 * other.a33); + } + } +} \ No newline at end of file diff --git a/shadowsocks-csharp/3rd/zxing/common/detector/MathUtils.cs b/shadowsocks-csharp/3rd/zxing/common/detector/MathUtils.cs new file mode 100755 index 00000000..8782202f --- /dev/null +++ b/shadowsocks-csharp/3rd/zxing/common/detector/MathUtils.cs @@ -0,0 +1,48 @@ +/* + * Copyright 2012 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 + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +using System; + +namespace ZXing.Common.Detector +{ + public static class MathUtils + { + /// + /// Ends up being a bit faster than {@link Math#round(float)}. This merely rounds its + /// argument to the nearest int, where x.5 rounds up to x+1. + /// + /// The d. + /// + public static int round(float d) + { + return (int)(d + 0.5f); + } + + public static float distance(float aX, float aY, float bX, float bY) + { + float xDiff = aX - bX; + float yDiff = aY - bY; + return (float)Math.Sqrt(xDiff * xDiff + yDiff * yDiff); + } + + public static float distance(int aX, int aY, int bX, int bY) + { + int xDiff = aX - bX; + int yDiff = aY - bY; + return (float)Math.Sqrt(xDiff * xDiff + yDiff * yDiff); + } + } +} diff --git a/shadowsocks-csharp/3rd/zxing/common/detector/MonochromeRectangleDetector.cs b/shadowsocks-csharp/3rd/zxing/common/detector/MonochromeRectangleDetector.cs new file mode 100755 index 00000000..9fb4515f --- /dev/null +++ b/shadowsocks-csharp/3rd/zxing/common/detector/MonochromeRectangleDetector.cs @@ -0,0 +1,252 @@ +/* +* Copyright 2009 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 +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +namespace ZXing.Common.Detector +{ + ///

A somewhat generic detector that looks for a barcode-like rectangular region within an image. + /// It looks within a mostly white region of an image for a region of black and white, but mostly + /// black. It returns the four corners of the region, as best it can determine.

+ /// + ///
+ /// Sean Owen + /// + /// www.Redivivus.in (suraj.supekar@redivivus.in) - Ported from ZXING Java Source + /// + public sealed class MonochromeRectangleDetector + { + private const int MAX_MODULES = 32; + + private BitMatrix image; + + public MonochromeRectangleDetector(BitMatrix image) + { + this.image = image; + } + + ///

Detects a rectangular region of black and white -- mostly black -- with a region of mostly + /// white, in an image.

+ /// + ///
+ /// {@link ResultPoint}[] describing the corners of the rectangular region. The first and + /// last points are opposed on the diagonal, as are the second and third. The first point will be + /// the topmost point and the last, the bottommost. The second point will be leftmost and the + /// third, the rightmost + /// + public ResultPoint[] detect() + { + int height = image.Height; + int width = image.Width; + int halfHeight = height >> 1; + int halfWidth = width >> 1; + int deltaY = System.Math.Max(1, height / (MAX_MODULES << 3)); + int deltaX = System.Math.Max(1, width / (MAX_MODULES << 3)); + + int top = 0; + int bottom = height; + int left = 0; + int right = width; + ResultPoint pointA = findCornerFromCenter(halfWidth, 0, left, right, halfHeight, -deltaY, top, bottom, halfWidth >> 1); + if (pointA == null) + return null; + top = (int)pointA.Y - 1; + ResultPoint pointB = findCornerFromCenter(halfWidth, -deltaX, left, right, halfHeight, 0, top, bottom, halfHeight >> 1); + if (pointB == null) + return null; + left = (int)pointB.X - 1; + ResultPoint pointC = findCornerFromCenter(halfWidth, deltaX, left, right, halfHeight, 0, top, bottom, halfHeight >> 1); + if (pointC == null) + return null; + right = (int)pointC.X + 1; + ResultPoint pointD = findCornerFromCenter(halfWidth, 0, left, right, halfHeight, deltaY, top, bottom, halfWidth >> 1); + if (pointD == null) + return null; + bottom = (int)pointD.Y + 1; + + // Go try to find point A again with better information -- might have been off at first. + pointA = findCornerFromCenter(halfWidth, 0, left, right, halfHeight, -deltaY, top, bottom, halfWidth >> 2); + if (pointA == null) + return null; + + return new ResultPoint[] { pointA, pointB, pointC, pointD }; + } + + /// Attempts to locate a corner of the barcode by scanning up, down, left or right from a center + /// point which should be within the barcode. + /// + /// + /// center's x component (horizontal) + /// + /// same as deltaY but change in x per step instead + /// + /// minimum value of x + /// + /// maximum value of x + /// + /// center's y component (vertical) + /// + /// change in y per step. If scanning up this is negative; down, positive; + /// left or right, 0 + /// + /// minimum value of y to search through (meaningless when di == 0) + /// + /// maximum value of y + /// + /// maximum run of white pixels that can still be considered to be within + /// the barcode + /// + /// a {@link com.google.zxing.ResultPoint} encapsulating the corner that was found + /// + private ResultPoint findCornerFromCenter(int centerX, int deltaX, int left, int right, int centerY, int deltaY, int top, int bottom, int maxWhiteRun) + { + int[] lastRange = null; + for (int y = centerY, x = centerX; y < bottom && y >= top && x < right && x >= left; y += deltaY, x += deltaX) + { + int[] range; + if (deltaX == 0) + { + // horizontal slices, up and down + range = blackWhiteRange(y, maxWhiteRun, left, right, true); + } + else + { + // vertical slices, left and right + range = blackWhiteRange(x, maxWhiteRun, top, bottom, false); + } + if (range == null) + { + if (lastRange == null) + { + return null; + } + // lastRange was found + if (deltaX == 0) + { + int lastY = y - deltaY; + if (lastRange[0] < centerX) + { + if (lastRange[1] > centerX) + { + // straddle, choose one or the other based on direction + return new ResultPoint(deltaY > 0 ? lastRange[0] : lastRange[1], lastY); + } + return new ResultPoint(lastRange[0], lastY); + } + else + { + return new ResultPoint(lastRange[1], lastY); + } + } + else + { + int lastX = x - deltaX; + if (lastRange[0] < centerY) + { + if (lastRange[1] > centerY) + { + return new ResultPoint(lastX, deltaX < 0 ? lastRange[0] : lastRange[1]); + } + return new ResultPoint(lastX, lastRange[0]); + } + else + { + return new ResultPoint(lastX, lastRange[1]); + } + } + } + lastRange = range; + } + return null; + } + + /// Computes the start and end of a region of pixels, either horizontally or vertically, that could + /// be part of a Data Matrix barcode. + /// + /// + /// if scanning horizontally, this is the row (the fixed vertical location) + /// where we are scanning. If scanning vertically it's the column, the fixed horizontal location + /// + /// largest run of white pixels that can still be considered part of the + /// barcode region + /// + /// minimum pixel location, horizontally or vertically, to consider + /// + /// maximum pixel location, horizontally or vertically, to consider + /// + /// if true, we're scanning left-right, instead of up-down + /// + /// int[] with start and end of found range, or null if no such range is found + /// (e.g. only white was found) + /// + private int[] blackWhiteRange(int fixedDimension, int maxWhiteRun, int minDim, int maxDim, bool horizontal) + { + int center = (minDim + maxDim) >> 1; + + // Scan left/up first + int start = center; + while (start >= minDim) + { + if (horizontal ? image[start, fixedDimension] : image[fixedDimension, start]) + { + start--; + } + else + { + int whiteRunStart = start; + do + { + start--; + } + while (start >= minDim && !(horizontal ? image[start, fixedDimension] : image[fixedDimension, start])); + int whiteRunSize = whiteRunStart - start; + if (start < minDim || whiteRunSize > maxWhiteRun) + { + start = whiteRunStart; + break; + } + } + } + start++; + + // Then try right/down + int end = center; + while (end < maxDim) + { + if (horizontal ? image[end, fixedDimension] : image[fixedDimension, end]) + { + end++; + } + else + { + int whiteRunStart = end; + do + { + end++; + } + while (end < maxDim && !(horizontal ? image[end, fixedDimension] : image[fixedDimension, end])); + int whiteRunSize = end - whiteRunStart; + if (end >= maxDim || whiteRunSize > maxWhiteRun) + { + end = whiteRunStart; + break; + } + } + } + end--; + + return end > start ? new int[] { start, end } : null; + } + } +} \ No newline at end of file diff --git a/shadowsocks-csharp/3rd/zxing/common/detector/WhiteRectangleDetector.cs b/shadowsocks-csharp/3rd/zxing/common/detector/WhiteRectangleDetector.cs new file mode 100755 index 00000000..9a4d4cd2 --- /dev/null +++ b/shadowsocks-csharp/3rd/zxing/common/detector/WhiteRectangleDetector.cs @@ -0,0 +1,433 @@ +/* + * Copyright 2010 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 + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +using System; + +namespace ZXing.Common.Detector +{ + /// + /// Detects a candidate barcode-like rectangular region within an image. It + /// starts around the center of the image, increases the size of the candidate + /// region until it finds a white rectangular region. By keeping track of the + /// last black points it encountered, it determines the corners of the barcode. + /// + /// David Olivier + public sealed class WhiteRectangleDetector + { + private const int INIT_SIZE = 10; + private const int CORR = 1; + + private readonly BitMatrix image; + private readonly int height; + private readonly int width; + private readonly int leftInit; + private readonly int rightInit; + private readonly int downInit; + private readonly int upInit; + + /// + /// Creates a WhiteRectangleDetector instance + /// + /// The image. + /// null, if image is too small, otherwise a WhiteRectangleDetector instance + public static WhiteRectangleDetector Create(BitMatrix image) + { + if (image == null) + return null; + + var instance = new WhiteRectangleDetector(image); + + if (instance.upInit < 0 || instance.leftInit < 0 || instance.downInit >= instance.height || instance.rightInit >= instance.width) + { + return null; + } + + return instance; + } + + /// + /// Creates a WhiteRectangleDetector instance + /// + /// The image. + /// Size of the init. + /// The x. + /// The y. + /// + /// null, if image is too small, otherwise a WhiteRectangleDetector instance + /// + public static WhiteRectangleDetector Create(BitMatrix image, int initSize, int x, int y) + { + var instance = new WhiteRectangleDetector(image, initSize, x, y); + + if (instance.upInit < 0 || instance.leftInit < 0 || instance.downInit >= instance.height || instance.rightInit >= instance.width) + { + return null; + } + + return instance; + } + + + /// + /// Initializes a new instance of the class. + /// + /// The image. + /// if image is too small + internal WhiteRectangleDetector(BitMatrix image) + : this(image, INIT_SIZE, image.Width/2, image.Height/2) + { + } + + /// + /// Initializes a new instance of the class. + /// + /// The image. + /// Size of the init. + /// The x. + /// The y. + internal WhiteRectangleDetector(BitMatrix image, int initSize, int x, int y) + { + this.image = image; + height = image.Height; + width = image.Width; + int halfsize = initSize / 2; + leftInit = x - halfsize; + rightInit = x + halfsize; + upInit = y - halfsize; + downInit = y + halfsize; + } + + /// + /// Detects a candidate barcode-like rectangular region within an image. It + /// starts around the center of the image, increases the size of the candidate + /// region until it finds a white rectangular region. + /// + /// [] describing the corners of the rectangular + /// region. The first and last points are opposed on the diagonal, as + /// are the second and third. The first point will be the topmost + /// point and the last, the bottommost. The second point will be + /// leftmost and the third, the rightmost + public ResultPoint[] detect() + { + int left = leftInit; + int right = rightInit; + int up = upInit; + int down = downInit; + bool sizeExceeded = false; + bool aBlackPointFoundOnBorder = true; + bool atLeastOneBlackPointFoundOnBorder = false; + + bool atLeastOneBlackPointFoundOnRight = false; + bool atLeastOneBlackPointFoundOnBottom = false; + bool atLeastOneBlackPointFoundOnLeft = false; + bool atLeastOneBlackPointFoundOnTop = false; + + while (aBlackPointFoundOnBorder) + { + + aBlackPointFoundOnBorder = false; + + // ..... + // . | + // ..... + bool rightBorderNotWhite = true; + while ((rightBorderNotWhite || !atLeastOneBlackPointFoundOnRight) && right < width) + { + rightBorderNotWhite = containsBlackPoint(up, down, right, false); + if (rightBorderNotWhite) + { + right++; + aBlackPointFoundOnBorder = true; + atLeastOneBlackPointFoundOnRight = true; + } + else if (!atLeastOneBlackPointFoundOnRight) + { + right++; + } + } + + if (right >= width) + { + sizeExceeded = true; + break; + } + + // ..... + // . . + // .___. + bool bottomBorderNotWhite = true; + while ((bottomBorderNotWhite || !atLeastOneBlackPointFoundOnBottom) && down < height) + { + bottomBorderNotWhite = containsBlackPoint(left, right, down, true); + if (bottomBorderNotWhite) + { + down++; + aBlackPointFoundOnBorder = true; + atLeastOneBlackPointFoundOnBottom = true; + } + else if (!atLeastOneBlackPointFoundOnBottom) + { + down++; + } + } + + if (down >= height) + { + sizeExceeded = true; + break; + } + + // ..... + // | . + // ..... + bool leftBorderNotWhite = true; + while ((leftBorderNotWhite || !atLeastOneBlackPointFoundOnLeft) && left >= 0) + { + leftBorderNotWhite = containsBlackPoint(up, down, left, false); + if (leftBorderNotWhite) + { + left--; + aBlackPointFoundOnBorder = true; + atLeastOneBlackPointFoundOnLeft = true; + } + else if (!atLeastOneBlackPointFoundOnLeft) + { + left--; + } + } + + if (left < 0) + { + sizeExceeded = true; + break; + } + + // .___. + // . . + // ..... + bool topBorderNotWhite = true; + while ((topBorderNotWhite || !atLeastOneBlackPointFoundOnTop) && up >= 0) + { + topBorderNotWhite = containsBlackPoint(left, right, up, true); + if (topBorderNotWhite) + { + up--; + aBlackPointFoundOnBorder = true; + atLeastOneBlackPointFoundOnTop = true; + } + else if (!atLeastOneBlackPointFoundOnTop) + { + up--; + } + } + + if (up < 0) + { + sizeExceeded = true; + break; + } + + if (aBlackPointFoundOnBorder) + { + atLeastOneBlackPointFoundOnBorder = true; + } + + } + + if (!sizeExceeded && atLeastOneBlackPointFoundOnBorder) + { + + int maxSize = right - left; + + ResultPoint z = null; + for (int i = 1; i < maxSize; i++) + { + z = getBlackPointOnSegment(left, down - i, left + i, down); + if (z != null) + { + break; + } + } + + if (z == null) + { + return null; + } + + ResultPoint t = null; + //go down right + for (int i = 1; i < maxSize; i++) + { + t = getBlackPointOnSegment(left, up + i, left + i, up); + if (t != null) + { + break; + } + } + + if (t == null) + { + return null; + } + + ResultPoint x = null; + //go down left + for (int i = 1; i < maxSize; i++) + { + x = getBlackPointOnSegment(right, up + i, right - i, up); + if (x != null) + { + break; + } + } + + if (x == null) + { + return null; + } + + ResultPoint y = null; + //go up left + for (int i = 1; i < maxSize; i++) + { + y = getBlackPointOnSegment(right, down - i, right - i, down); + if (y != null) + { + break; + } + } + + if (y == null) + { + return null; + } + + return centerEdges(y, z, x, t); + + } + else + { + return null; + } + } + + private ResultPoint getBlackPointOnSegment(float aX, float aY, float bX, float bY) + { + int dist = MathUtils.round(MathUtils.distance(aX, aY, bX, bY)); + float xStep = (bX - aX) / dist; + float yStep = (bY - aY) / dist; + + for (int i = 0; i < dist; i++) + { + int x = MathUtils.round(aX + i * xStep); + int y = MathUtils.round(aY + i * yStep); + if (image[x, y]) + { + return new ResultPoint(x, y); + } + } + return null; + } + + /// + /// recenters the points of a constant distance towards the center + /// + /// bottom most point + /// left most point + /// right most point + /// top most point + /// [] describing the corners of the rectangular + /// region. The first and last points are opposed on the diagonal, as + /// are the second and third. The first point will be the topmost + /// point and the last, the bottommost. The second point will be + /// leftmost and the third, the rightmost + private ResultPoint[] centerEdges(ResultPoint y, ResultPoint z, + ResultPoint x, ResultPoint t) + { + // + // t t + // z x + // x OR z + // y y + // + + float yi = y.X; + float yj = y.Y; + float zi = z.X; + float zj = z.Y; + float xi = x.X; + float xj = x.Y; + float ti = t.X; + float tj = t.Y; + + if (yi < width / 2.0f) + { + return new[] + { + new ResultPoint(ti - CORR, tj + CORR), + new ResultPoint(zi + CORR, zj + CORR), + new ResultPoint(xi - CORR, xj - CORR), + new ResultPoint(yi + CORR, yj - CORR) + }; + } + else + { + return new[] + { + new ResultPoint(ti + CORR, tj + CORR), + new ResultPoint(zi + CORR, zj - CORR), + new ResultPoint(xi - CORR, xj + CORR), + new ResultPoint(yi - CORR, yj - CORR) + }; + } + } + + /// + /// Determines whether a segment contains a black point + /// + /// min value of the scanned coordinate + /// max value of the scanned coordinate + /// value of fixed coordinate + /// set to true if scan must be horizontal, false if vertical + /// + /// true if a black point has been found, else false. + /// + private bool containsBlackPoint(int a, int b, int @fixed, bool horizontal) + { + if (horizontal) + { + for (int x = a; x <= b; x++) + { + if (image[x, @fixed]) + { + return true; + } + } + } + else + { + for (int y = a; y <= b; y++) + { + if (image[@fixed, y]) + { + return true; + } + } + } + return false; + } + } +} \ No newline at end of file diff --git a/shadowsocks-csharp/3rd/zxing/GenericGF.cs b/shadowsocks-csharp/3rd/zxing/common/reedsolomon/GenericGF.cs similarity index 68% rename from shadowsocks-csharp/3rd/zxing/GenericGF.cs rename to shadowsocks-csharp/3rd/zxing/common/reedsolomon/GenericGF.cs index 08f8ca0e..276dc999 100755 --- a/shadowsocks-csharp/3rd/zxing/GenericGF.cs +++ b/shadowsocks-csharp/3rd/zxing/common/reedsolomon/GenericGF.cs @@ -28,7 +28,16 @@ namespace ZXing.Common.ReedSolomon /// Sean Owen public sealed class GenericGF { + public static GenericGF AZTEC_DATA_12 = new GenericGF(0x1069, 4096, 1); // x^12 + x^6 + x^5 + x^3 + 1 + public static GenericGF AZTEC_DATA_10 = new GenericGF(0x409, 1024, 1); // x^10 + x^3 + 1 + public static GenericGF AZTEC_DATA_6 = new GenericGF(0x43, 64, 1); // x^6 + x + 1 + public static GenericGF AZTEC_PARAM = new GenericGF(0x13, 16, 1); // x^4 + x + 1 public static GenericGF QR_CODE_FIELD_256 = new GenericGF(0x011D, 256, 0); // x^8 + x^4 + x^3 + x^2 + 1 + public static GenericGF DATA_MATRIX_FIELD_256 = new GenericGF(0x012D, 256, 1); // x^8 + x^5 + x^3 + x^2 + 1 + public static GenericGF AZTEC_DATA_8 = DATA_MATRIX_FIELD_256; + public static GenericGF MAXICODE_FIELD_64 = AZTEC_DATA_6; + + private const int INITIALIZATION_THRESHOLD = 0; private int[] expTable; private int[] logTable; @@ -37,6 +46,7 @@ namespace ZXing.Common.ReedSolomon private readonly int size; private readonly int primitive; private readonly int generatorBase; + private bool initialized = false; /// /// Create a representation of GF(size) using the given primitive polynomial. @@ -54,6 +64,14 @@ namespace ZXing.Common.ReedSolomon this.size = size; this.generatorBase = genBase; + if (size <= INITIALIZATION_THRESHOLD) + { + initialize(); + } + } + + private void initialize() + { expTable = new int[size]; logTable = new int[size]; int x = 1; @@ -74,16 +92,35 @@ namespace ZXing.Common.ReedSolomon // logTable[0] == 0 but this should never be used zero = new GenericGFPoly(this, new int[] { 0 }); one = new GenericGFPoly(this, new int[] { 1 }); + initialized = true; + } + + private void checkInit() + { + if (!initialized) + { + initialize(); + } } internal GenericGFPoly Zero { get { + checkInit(); return zero; } } + internal GenericGFPoly One + { + get + { + checkInit(); + return one; + } + } + /// /// Builds the monomial. /// @@ -92,6 +129,8 @@ namespace ZXing.Common.ReedSolomon /// the monomial representing coefficient * x^degree internal GenericGFPoly buildMonomial(int degree, int coefficient) { + checkInit(); + if (degree < 0) { throw new ArgumentException(); @@ -120,9 +159,26 @@ namespace ZXing.Common.ReedSolomon /// 2 to the power of a in GF(size) internal int exp(int a) { + checkInit(); + return expTable[a]; } + /// + /// Logs the specified a. + /// + /// A. + /// base 2 log of a in GF(size) + internal int log(int a) + { + checkInit(); + + if (a == 0) + { + throw new ArgumentException(); + } + return logTable[a]; + } /// /// Inverses the specified a. @@ -130,6 +186,8 @@ namespace ZXing.Common.ReedSolomon /// multiplicative inverse of a internal int inverse(int a) { + checkInit(); + if (a == 0) { throw new ArithmeticException(); @@ -145,6 +203,8 @@ namespace ZXing.Common.ReedSolomon /// product of a and b in GF(size) internal int multiply(int a, int b) { + checkInit(); + if (a == 0 || b == 0) { return 0; @@ -152,6 +212,14 @@ namespace ZXing.Common.ReedSolomon return expTable[(logTable[a] + logTable[b]) % (size - 1)]; } + /// + /// Gets the size. + /// + public int Size + { + get { return size; } + } + /// /// Gets the generator base. /// @@ -159,5 +227,16 @@ namespace ZXing.Common.ReedSolomon { get { return generatorBase; } } + + /// + /// Returns a that represents this instance. + /// + /// + /// A that represents this instance. + /// + override public String ToString() + { + return "GF(0x" + primitive.ToString("X") + ',' + size + ')'; + } } } \ No newline at end of file diff --git a/shadowsocks-csharp/3rd/zxing/GenericGFPoly.cs b/shadowsocks-csharp/3rd/zxing/common/reedsolomon/GenericGFPoly.cs similarity index 69% rename from shadowsocks-csharp/3rd/zxing/GenericGFPoly.cs rename to shadowsocks-csharp/3rd/zxing/common/reedsolomon/GenericGFPoly.cs index eb966579..427fe11d 100755 --- a/shadowsocks-csharp/3rd/zxing/GenericGFPoly.cs +++ b/shadowsocks-csharp/3rd/zxing/common/reedsolomon/GenericGFPoly.cs @@ -59,7 +59,7 @@ namespace ZXing.Common.ReedSolomon } if (firstNonZero == coefficientsLength) { - this.coefficients = new int[]{0}; + this.coefficients = field.Zero.coefficients; } else { @@ -112,6 +112,36 @@ namespace ZXing.Common.ReedSolomon return coefficients[coefficients.Length - 1 - degree]; } + /// + /// evaluation of this polynomial at a given point + /// + /// A. + /// evaluation of this polynomial at a given point + internal int evaluateAt(int a) + { + int result = 0; + if (a == 0) + { + // Just return the x^0 coefficient + return getCoefficient(0); + } + int size = coefficients.Length; + if (a == 1) + { + // Just the sum of the coefficients + foreach (var coefficient in coefficients) + { + result = GenericGF.addOrSubtract(result, coefficient); + } + return result; + } + result = coefficients[0]; + for (int i = 1; i < size; i++) + { + result = GenericGF.addOrSubtract(field.multiply(a, result), coefficients[i]); + } + return result; + } internal GenericGFPoly addOrSubtract(GenericGFPoly other) { @@ -176,6 +206,24 @@ namespace ZXing.Common.ReedSolomon return new GenericGFPoly(field, product); } + internal GenericGFPoly multiply(int scalar) + { + if (scalar == 0) + { + return field.Zero; + } + if (scalar == 1) + { + return this; + } + int size = coefficients.Length; + int[] product = new int[size]; + for (int i = 0; i < size; i++) + { + product[i] = field.multiply(coefficients[i], scalar); + } + return new GenericGFPoly(field, product); + } internal GenericGFPoly multiplyByMonomial(int degree, int coefficient) { @@ -226,5 +274,58 @@ namespace ZXing.Common.ReedSolomon return new GenericGFPoly[] { quotient, remainder }; } + public override String ToString() + { + StringBuilder result = new StringBuilder(8 * Degree); + for (int degree = Degree; degree >= 0; degree--) + { + int coefficient = getCoefficient(degree); + if (coefficient != 0) + { + if (coefficient < 0) + { + result.Append(" - "); + coefficient = -coefficient; + } + else + { + if (result.Length > 0) + { + result.Append(" + "); + } + } + if (degree == 0 || coefficient != 1) + { + int alphaPower = field.log(coefficient); + if (alphaPower == 0) + { + result.Append('1'); + } + else if (alphaPower == 1) + { + result.Append('a'); + } + else + { + result.Append("a^"); + result.Append(alphaPower); + } + } + if (degree != 0) + { + if (degree == 1) + { + result.Append('x'); + } + else + { + result.Append("x^"); + result.Append(degree); + } + } + } + } + return result.ToString(); + } } } \ No newline at end of file diff --git a/shadowsocks-csharp/3rd/zxing/common/reedsolomon/ReedSolomonDecoder.cs b/shadowsocks-csharp/3rd/zxing/common/reedsolomon/ReedSolomonDecoder.cs new file mode 100755 index 00000000..1030a56d --- /dev/null +++ b/shadowsocks-csharp/3rd/zxing/common/reedsolomon/ReedSolomonDecoder.cs @@ -0,0 +1,227 @@ +/* +* 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 +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +namespace ZXing.Common.ReedSolomon +{ + ///

Implements Reed-Solomon decoding, as the name implies.

+ /// + ///

The algorithm will not be explained here, but the following references were helpful + /// in creating this implementation:

+ /// + /// + /// + ///

Much credit is due to William Rucklidge since portions of this code are an indirect + /// port of his C++ Reed-Solomon implementation.

+ /// + ///
+ /// Sean Owen + /// William Rucklidge + /// sanfordsquires + public sealed class ReedSolomonDecoder + { + private readonly GenericGF field; + + public ReedSolomonDecoder(GenericGF field) + { + this.field = field; + } + + /// + ///

Decodes given set of received codewords, which include both data and error-correction + /// codewords. Really, this means it uses Reed-Solomon to detect and correct errors, in-place, + /// in the input.

+ ///
+ /// data and error-correction codewords + /// number of error-correction codewords available + /// false: decoding fails + public bool decode(int[] received, int twoS) + { + var poly = new GenericGFPoly(field, received); + var syndromeCoefficients = new int[twoS]; + var noError = true; + for (var i = 0; i < twoS; i++) + { + var eval = poly.evaluateAt(field.exp(i + field.GeneratorBase)); + syndromeCoefficients[syndromeCoefficients.Length - 1 - i] = eval; + if (eval != 0) + { + noError = false; + } + } + if (noError) + { + return true; + } + var syndrome = new GenericGFPoly(field, syndromeCoefficients); + + var sigmaOmega = runEuclideanAlgorithm(field.buildMonomial(twoS, 1), syndrome, twoS); + if (sigmaOmega == null) + return false; + + var sigma = sigmaOmega[0]; + var errorLocations = findErrorLocations(sigma); + if (errorLocations == null) + return false; + + var omega = sigmaOmega[1]; + var errorMagnitudes = findErrorMagnitudes(omega, errorLocations); + for (var i = 0; i < errorLocations.Length; i++) + { + var position = received.Length - 1 - field.log(errorLocations[i]); + if (position < 0) + { + // throw new ReedSolomonException("Bad error location"); + return false; + } + received[position] = GenericGF.addOrSubtract(received[position], errorMagnitudes[i]); + } + + return true; + } + + internal GenericGFPoly[] runEuclideanAlgorithm(GenericGFPoly a, GenericGFPoly b, int R) + { + // Assume a's degree is >= b's + if (a.Degree < b.Degree) + { + GenericGFPoly temp = a; + a = b; + b = temp; + } + + GenericGFPoly rLast = a; + GenericGFPoly r = b; + GenericGFPoly tLast = field.Zero; + GenericGFPoly t = field.One; + + // Run Euclidean algorithm until r's degree is less than R/2 + while (r.Degree >= R / 2) + { + GenericGFPoly rLastLast = rLast; + GenericGFPoly tLastLast = tLast; + rLast = r; + tLast = t; + + // Divide rLastLast by rLast, with quotient in q and remainder in r + if (rLast.isZero) + { + // Oops, Euclidean algorithm already terminated? + // throw new ReedSolomonException("r_{i-1} was zero"); + return null; + } + r = rLastLast; + GenericGFPoly q = field.Zero; + int denominatorLeadingTerm = rLast.getCoefficient(rLast.Degree); + int dltInverse = field.inverse(denominatorLeadingTerm); + while (r.Degree >= rLast.Degree && !r.isZero) + { + int degreeDiff = r.Degree - rLast.Degree; + int scale = field.multiply(r.getCoefficient(r.Degree), dltInverse); + q = q.addOrSubtract(field.buildMonomial(degreeDiff, scale)); + r = r.addOrSubtract(rLast.multiplyByMonomial(degreeDiff, scale)); + } + + t = q.multiply(tLast).addOrSubtract(tLastLast); + + if (r.Degree >= rLast.Degree) + { + // throw new IllegalStateException("Division algorithm failed to reduce polynomial?"); + return null; + } + } + + int sigmaTildeAtZero = t.getCoefficient(0); + if (sigmaTildeAtZero == 0) + { + // throw new ReedSolomonException("sigmaTilde(0) was zero"); + return null; + } + + int inverse = field.inverse(sigmaTildeAtZero); + GenericGFPoly sigma = t.multiply(inverse); + GenericGFPoly omega = r.multiply(inverse); + return new GenericGFPoly[] { sigma, omega }; + } + + private int[] findErrorLocations(GenericGFPoly errorLocator) + { + // This is a direct application of Chien's search + int numErrors = errorLocator.Degree; + if (numErrors == 1) + { + // shortcut + return new int[] { errorLocator.getCoefficient(1) }; + } + int[] result = new int[numErrors]; + int e = 0; + for (int i = 1; i < field.Size && e < numErrors; i++) + { + if (errorLocator.evaluateAt(i) == 0) + { + result[e] = field.inverse(i); + e++; + } + } + if (e != numErrors) + { + // throw new ReedSolomonException("Error locator degree does not match number of roots"); + return null; + } + return result; + } + + private int[] findErrorMagnitudes(GenericGFPoly errorEvaluator, int[] errorLocations) + { + // This is directly applying Forney's Formula + int s = errorLocations.Length; + int[] result = new int[s]; + for (int i = 0; i < s; i++) + { + int xiInverse = field.inverse(errorLocations[i]); + int denominator = 1; + for (int j = 0; j < s; j++) + { + if (i != j) + { + //denominator = field.multiply(denominator, + // GenericGF.addOrSubtract(1, field.multiply(errorLocations[j], xiInverse))); + // Above should work but fails on some Apple and Linux JDKs due to a Hotspot bug. + // Below is a funny-looking workaround from Steven Parkes + int term = field.multiply(errorLocations[j], xiInverse); + int termPlus1 = (term & 0x1) == 0 ? term | 1 : term & ~1; + denominator = field.multiply(denominator, termPlus1); + + // removed in java version, not sure if this is right + // denominator = field.multiply(denominator, GenericGF.addOrSubtract(1, field.multiply(errorLocations[j], xiInverse))); + } + } + result[i] = field.multiply(errorEvaluator.evaluateAt(xiInverse), field.inverse(denominator)); + if (field.GeneratorBase != 0) + { + result[i] = field.multiply(result[i], xiInverse); + } + } + return result; + } + } +} \ No newline at end of file diff --git a/shadowsocks-csharp/3rd/zxing/ReedSolomonEncoder.cs b/shadowsocks-csharp/3rd/zxing/common/reedsolomon/ReedSolomonEncoder.cs similarity index 100% rename from shadowsocks-csharp/3rd/zxing/ReedSolomonEncoder.cs rename to shadowsocks-csharp/3rd/zxing/common/reedsolomon/ReedSolomonEncoder.cs diff --git a/shadowsocks-csharp/3rd/zxing/qrcode/decoder/BitMatrixParser.cs b/shadowsocks-csharp/3rd/zxing/qrcode/decoder/BitMatrixParser.cs new file mode 100755 index 00000000..6a328faa --- /dev/null +++ b/shadowsocks-csharp/3rd/zxing/qrcode/decoder/BitMatrixParser.cs @@ -0,0 +1,281 @@ +/* +* 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 +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +using ZXing.Common; + +namespace ZXing.QrCode.Internal +{ + /// Sean Owen + sealed class BitMatrixParser + { + private readonly BitMatrix bitMatrix; + private Version parsedVersion; + private FormatInformation parsedFormatInfo; + private bool mirrored; + + /// {@link BitMatrix} to parse + /// ReaderException if dimension is not >= 21 and 1 mod 4 + internal static BitMatrixParser createBitMatrixParser(BitMatrix bitMatrix) + { + int dimension = bitMatrix.Height; + if (dimension < 21 || (dimension & 0x03) != 1) + { + return null; + } + return new BitMatrixParser(bitMatrix); + } + + private BitMatrixParser(BitMatrix bitMatrix) + { + // Should only be called from createBitMatrixParser with the important checks before + this.bitMatrix = bitMatrix; + } + + ///

Reads format information from one of its two locations within the QR Code.

+ /// + ///
+ /// {@link FormatInformation} encapsulating the QR Code's format info + /// + /// ReaderException if both format information locations cannot be parsed as + /// the valid encoding of format information + /// + internal FormatInformation readFormatInformation() + { + if (parsedFormatInfo != null) + { + return parsedFormatInfo; + } + + // Read top-left format info bits + int formatInfoBits1 = 0; + for (int i = 0; i < 6; i++) + { + formatInfoBits1 = copyBit(i, 8, formatInfoBits1); + } + // .. and skip a bit in the timing pattern ... + formatInfoBits1 = copyBit(7, 8, formatInfoBits1); + formatInfoBits1 = copyBit(8, 8, formatInfoBits1); + formatInfoBits1 = copyBit(8, 7, formatInfoBits1); + // .. and skip a bit in the timing pattern ... + for (int j = 5; j >= 0; j--) + { + formatInfoBits1 = copyBit(8, j, formatInfoBits1); + } + // Read the top-right/bottom-left pattern too + int dimension = bitMatrix.Height; + int formatInfoBits2 = 0; + int jMin = dimension - 7; + for (int j = dimension - 1; j >= jMin; j--) + { + formatInfoBits2 = copyBit(8, j, formatInfoBits2); + } + for (int i = dimension - 8; i < dimension; i++) + { + formatInfoBits2 = copyBit(i, 8, formatInfoBits2); + } + + parsedFormatInfo = FormatInformation.decodeFormatInformation(formatInfoBits1, formatInfoBits2); + if (parsedFormatInfo != null) + { + return parsedFormatInfo; + } + return null; + } + + ///

Reads version information from one of its two locations within the QR Code.

+ /// + ///
+ /// {@link Version} encapsulating the QR Code's version + /// + /// ReaderException if both version information locations cannot be parsed as + /// the valid encoding of version information + /// + internal Version readVersion() + { + if (parsedVersion != null) + { + return parsedVersion; + } + + int dimension = bitMatrix.Height; + + int provisionalVersion = (dimension - 17) >> 2; + if (provisionalVersion <= 6) + { + return Version.getVersionForNumber(provisionalVersion); + } + + // Read top-right version info: 3 wide by 6 tall + int versionBits = 0; + int ijMin = dimension - 11; + for (int j = 5; j >= 0; j--) + { + for (int i = dimension - 9; i >= ijMin; i--) + { + versionBits = copyBit(i, j, versionBits); + } + } + + parsedVersion = Version.decodeVersionInformation(versionBits); + if (parsedVersion != null && parsedVersion.DimensionForVersion == dimension) + { + return parsedVersion; + } + + // Hmm, failed. Try bottom left: 6 wide by 3 tall + versionBits = 0; + for (int i = 5; i >= 0; i--) + { + for (int j = dimension - 9; j >= ijMin; j--) + { + versionBits = copyBit(i, j, versionBits); + } + } + + parsedVersion = Version.decodeVersionInformation(versionBits); + if (parsedVersion != null && parsedVersion.DimensionForVersion == dimension) + { + return parsedVersion; + } + return null; + } + + private int copyBit(int i, int j, int versionBits) + { + bool bit = mirrored ? bitMatrix[j, i] : bitMatrix[i, j]; + return bit ? (versionBits << 1) | 0x1 : versionBits << 1; + } + + ///

Reads the bits in the {@link BitMatrix} representing the finder pattern in the + /// correct order in order to reconstruct the codewords bytes contained within the + /// QR Code.

+ /// + ///
+ /// bytes encoded within the QR Code + /// + /// ReaderException if the exact number of bytes expected is not read + internal byte[] readCodewords() + { + FormatInformation formatInfo = readFormatInformation(); + if (formatInfo == null) + return null; + Version version = readVersion(); + if (version == null) + return null; + + // Get the data mask for the format used in this QR Code. This will exclude + // some bits from reading as we wind through the bit matrix. + DataMask dataMask = DataMask.forReference(formatInfo.DataMask); + int dimension = bitMatrix.Height; + dataMask.unmaskBitMatrix(bitMatrix, dimension); + + BitMatrix functionPattern = version.buildFunctionPattern(); + + bool readingUp = true; + byte[] result = new byte[version.TotalCodewords]; + int resultOffset = 0; + int currentByte = 0; + int bitsRead = 0; + // Read columns in pairs, from right to left + for (int j = dimension - 1; j > 0; j -= 2) + { + if (j == 6) + { + // Skip whole column with vertical alignment pattern; + // saves time and makes the other code proceed more cleanly + j--; + } + // Read alternatingly from bottom to top then top to bottom + for (int count = 0; count < dimension; count++) + { + int i = readingUp ? dimension - 1 - count : count; + for (int col = 0; col < 2; col++) + { + // Ignore bits covered by the function pattern + if (!functionPattern[j - col, i]) + { + // Read a bit + bitsRead++; + currentByte <<= 1; + if (bitMatrix[j - col, i]) + { + currentByte |= 1; + } + // If we've made a whole byte, save it off + if (bitsRead == 8) + { + result[resultOffset++] = (byte)currentByte; + bitsRead = 0; + currentByte = 0; + } + } + } + } + readingUp ^= true; // readingUp = !readingUp; // switch directions + } + if (resultOffset != version.TotalCodewords) + { + return null; + } + return result; + } + + /** + * Revert the mask removal done while reading the code words. The bit matrix should revert to its original state. + */ + internal void remask() + { + if (parsedFormatInfo == null) + { + return; // We have no format information, and have no data mask + } + DataMask dataMask = DataMask.forReference(parsedFormatInfo.DataMask); + int dimension = bitMatrix.Height; + dataMask.unmaskBitMatrix(bitMatrix, dimension); + } + + /** + * Prepare the parser for a mirrored operation. + * This flag has effect only on the {@link #readFormatInformation()} and the + * {@link #readVersion()}. Before proceeding with {@link #readCodewords()} the + * {@link #mirror()} method should be called. + * + * @param mirror Whether to read version and format information mirrored. + */ + internal void setMirror(bool mirror) + { + parsedVersion = null; + parsedFormatInfo = null; + mirrored = mirror; + } + + /** Mirror the bit matrix in order to attempt a second reading. */ + internal void mirror() + { + for (int x = 0; x < bitMatrix.Width; x++) + { + for (int y = x + 1; y < bitMatrix.Height; y++) + { + if (bitMatrix[x, y] != bitMatrix[y, x]) + { + bitMatrix.flip(y, x); + bitMatrix.flip(x, y); + } + } + } + } + } +} \ No newline at end of file diff --git a/shadowsocks-csharp/3rd/zxing/qrcode/decoder/DataBlock.cs b/shadowsocks-csharp/3rd/zxing/qrcode/decoder/DataBlock.cs new file mode 100755 index 00000000..5860fe6a --- /dev/null +++ b/shadowsocks-csharp/3rd/zxing/qrcode/decoder/DataBlock.cs @@ -0,0 +1,146 @@ +/* +* 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 +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +namespace ZXing.QrCode.Internal +{ + ///

Encapsulates a block of data within a QR Code. QR Codes may split their data into + /// multiple blocks, each of which is a unit of data and error-correction codewords. Each + /// is represented by an instance of this class.

+ /// + ///
+ /// Sean Owen + /// + /// www.Redivivus.in (suraj.supekar@redivivus.in) - Ported from ZXING Java Source + /// + internal sealed class DataBlock + { + private readonly int numDataCodewords; + private readonly byte[] codewords; + + private DataBlock(int numDataCodewords, byte[] codewords) + { + this.numDataCodewords = numDataCodewords; + this.codewords = codewords; + } + + ///

When QR Codes use multiple data blocks, they are actually interleaved. + /// That is, the first byte of data block 1 to n is written, then the second bytes, and so on. This + /// method will separate the data into original blocks.

+ /// + ///
+ /// bytes as read directly from the QR Code + /// + /// version of the QR Code + /// + /// error-correction level of the QR Code + /// + /// {@link DataBlock}s containing original bytes, "de-interleaved" from representation in the + /// QR Code + /// + internal static DataBlock[] getDataBlocks(byte[] rawCodewords, Version version, ErrorCorrectionLevel ecLevel) + { + + if (rawCodewords.Length != version.TotalCodewords) + { + throw new System.ArgumentException(); + } + + // Figure out the number and size of data blocks used by this version and + // error correction level + Version.ECBlocks ecBlocks = version.getECBlocksForLevel(ecLevel); + + // First count the total number of data blocks + int totalBlocks = 0; + Version.ECB[] ecBlockArray = ecBlocks.getECBlocks(); + foreach (var ecBlock in ecBlockArray) + { + totalBlocks += ecBlock.Count; + } + + // Now establish DataBlocks of the appropriate size and number of data codewords + DataBlock[] result = new DataBlock[totalBlocks]; + int numResultBlocks = 0; + foreach (var ecBlock in ecBlockArray) + { + for (int i = 0; i < ecBlock.Count; i++) + { + int numDataCodewords = ecBlock.DataCodewords; + int numBlockCodewords = ecBlocks.ECCodewordsPerBlock + numDataCodewords; + result[numResultBlocks++] = new DataBlock(numDataCodewords, new byte[numBlockCodewords]); + } + } + + // All blocks have the same amount of data, except that the last n + // (where n may be 0) have 1 more byte. Figure out where these start. + int shorterBlocksTotalCodewords = result[0].codewords.Length; + int longerBlocksStartAt = result.Length - 1; + while (longerBlocksStartAt >= 0) + { + int numCodewords = result[longerBlocksStartAt].codewords.Length; + if (numCodewords == shorterBlocksTotalCodewords) + { + break; + } + longerBlocksStartAt--; + } + longerBlocksStartAt++; + + int shorterBlocksNumDataCodewords = shorterBlocksTotalCodewords - ecBlocks.ECCodewordsPerBlock; + // The last elements of result may be 1 element longer; + // first fill out as many elements as all of them have + int rawCodewordsOffset = 0; + for (int i = 0; i < shorterBlocksNumDataCodewords; i++) + { + for (int j = 0; j < numResultBlocks; j++) + { + result[j].codewords[i] = rawCodewords[rawCodewordsOffset++]; + } + } + // Fill out the last data block in the longer ones + for (int j = longerBlocksStartAt; j < numResultBlocks; j++) + { + result[j].codewords[shorterBlocksNumDataCodewords] = rawCodewords[rawCodewordsOffset++]; + } + // Now add in error correction blocks + int max = result[0].codewords.Length; + for (int i = shorterBlocksNumDataCodewords; i < max; i++) + { + for (int j = 0; j < numResultBlocks; j++) + { + int iOffset = j < longerBlocksStartAt ? i : i + 1; + result[j].codewords[iOffset] = rawCodewords[rawCodewordsOffset++]; + } + } + return result; + } + + internal int NumDataCodewords + { + get + { + return numDataCodewords; + } + } + + internal byte[] Codewords + { + get + { + return codewords; + } + } + } +} \ No newline at end of file diff --git a/shadowsocks-csharp/3rd/zxing/qrcode/decoder/DataMask.cs b/shadowsocks-csharp/3rd/zxing/qrcode/decoder/DataMask.cs new file mode 100755 index 00000000..a6ac93e6 --- /dev/null +++ b/shadowsocks-csharp/3rd/zxing/qrcode/decoder/DataMask.cs @@ -0,0 +1,165 @@ +/* +* 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 +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +using ZXing.Common; + +namespace ZXing.QrCode.Internal +{ + ///

Encapsulates data masks for the data bits in a QR code, per ISO 18004:2006 6.8. Implementations + /// of this class can un-mask a raw BitMatrix. For simplicity, they will unmask the entire BitMatrix, + /// including areas used for finder patterns, timing patterns, etc. These areas should be unused + /// after the point they are unmasked anyway.

+ /// + ///

Note that the diagram in section 6.8.1 is misleading since it indicates that i is column position + /// and j is row position. In fact, as the text says, i is row position and j is column position.

+ /// + ///
+ /// Sean Owen + /// + /// www.Redivivus.in (suraj.supekar@redivivus.in) - Ported from ZXING Java Source + /// + abstract class DataMask + { + /// See ISO 18004:2006 6.8.1 + private static readonly DataMask[] DATA_MASKS = new DataMask[] + { + new DataMask000(), + new DataMask001(), + new DataMask010(), + new DataMask011(), + new DataMask100(), + new DataMask101(), + new DataMask110(), + new DataMask111() + }; + + private DataMask() + { + } + + ///

Implementations of this method reverse the data masking process applied to a QR Code and + /// make its bits ready to read.

+ /// + ///
+ /// representation of QR Code bits + /// + /// dimension of QR Code, represented by bits, being unmasked + /// + internal void unmaskBitMatrix(BitMatrix bits, int dimension) + { + for (int i = 0; i < dimension; i++) + { + for (int j = 0; j < dimension; j++) + { + if (isMasked(i, j)) + { + bits.flip(j, i); + } + } + } + } + + internal abstract bool isMasked(int i, int j); + + /// a value between 0 and 7 indicating one of the eight possible + /// data mask patterns a QR Code may use + /// + /// {@link DataMask} encapsulating the data mask pattern + /// + internal static DataMask forReference(int reference) + { + if (reference < 0 || reference > 7) + { + throw new System.ArgumentException(); + } + return DATA_MASKS[reference]; + } + + /// 000: mask bits for which (x + y) mod 2 == 0 + private sealed class DataMask000 : DataMask + { + internal override bool isMasked(int i, int j) + { + return ((i + j) & 0x01) == 0; + } + } + + /// 001: mask bits for which x mod 2 == 0 + private sealed class DataMask001 : DataMask + { + internal override bool isMasked(int i, int j) + { + return (i & 0x01) == 0; + } + } + + /// 010: mask bits for which y mod 3 == 0 + private sealed class DataMask010 : DataMask + { + internal override bool isMasked(int i, int j) + { + return j % 3 == 0; + } + } + + /// 011: mask bits for which (x + y) mod 3 == 0 + private sealed class DataMask011 : DataMask + { + internal override bool isMasked(int i, int j) + { + return (i + j) % 3 == 0; + } + } + + /// 100: mask bits for which (x/2 + y/3) mod 2 == 0 + private sealed class DataMask100 : DataMask + { + internal override bool isMasked(int i, int j) + { + return ((((int)((uint)i >> 1)) + (j / 3)) & 0x01) == 0; + } + } + + /// 101: mask bits for which xy mod 2 + xy mod 3 == 0 + private sealed class DataMask101 : DataMask + { + internal override bool isMasked(int i, int j) + { + int temp = i * j; + return (temp & 0x01) + (temp % 3) == 0; + } + } + + /// 110: mask bits for which (xy mod 2 + xy mod 3) mod 2 == 0 + private sealed class DataMask110 : DataMask + { + internal override bool isMasked(int i, int j) + { + int temp = i * j; + return (((temp & 0x01) + (temp % 3)) & 0x01) == 0; + } + } + + /// 111: mask bits for which ((x+y)mod 2 + xy mod 3) mod 2 == 0 + private sealed class DataMask111 : DataMask + { + internal override bool isMasked(int i, int j) + { + return ((((i + j) & 0x01) + ((i * j) % 3)) & 0x01) == 0; + } + } + } +} \ No newline at end of file diff --git a/shadowsocks-csharp/3rd/zxing/qrcode/decoder/DecodedBitStreamParser.cs b/shadowsocks-csharp/3rd/zxing/qrcode/decoder/DecodedBitStreamParser.cs new file mode 100755 index 00000000..cb59141b --- /dev/null +++ b/shadowsocks-csharp/3rd/zxing/qrcode/decoder/DecodedBitStreamParser.cs @@ -0,0 +1,293 @@ +/* +* 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 +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +using System; +using System.Collections.Generic; +using System.Text; + +using ZXing.Common; + +namespace ZXing.QrCode.Internal +{ + ///

QR Codes can encode text as bits in one of several modes, and can use multiple modes + /// in one QR Code. This class decodes the bits back into text.

+ /// + ///

See ISO 18004:2006, 6.4.3 - 6.4.7

+ /// Sean Owen + ///
+ internal static class DecodedBitStreamParser + { + /// + /// See ISO 18004:2006, 6.4.4 Table 5 + /// + private static readonly char[] ALPHANUMERIC_CHARS = { + '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', + 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', + 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', + ' ', '$', '%', '*', '+', '-', '.', '/', ':' + }; + private const int GB2312_SUBSET = 1; + + internal static DecoderResult decode(byte[] bytes, + Version version, + ErrorCorrectionLevel ecLevel, + IDictionary hints) + { + var bits = new BitSource(bytes); + var result = new StringBuilder(50); + var byteSegments = new List(1); + var symbolSequence = -1; + var parityData = -1; + + try + { + //CharacterSetECI currentCharacterSetECI = null; + bool fc1InEffect = false; + Mode mode; + do + { + // While still another segment to read... + if (bits.available() < 4) + { + // OK, assume we're done. Really, a TERMINATOR mode should have been recorded here + mode = Mode.TERMINATOR; + } + else + { + try + { + mode = Mode.forBits(bits.readBits(4)); // mode is encoded by 4 bits + } + catch (ArgumentException) + { + return null; + } + } + if (mode != Mode.TERMINATOR) + { + if (mode == Mode.FNC1_FIRST_POSITION || mode == Mode.FNC1_SECOND_POSITION) + { + // We do little with FNC1 except alter the parsed result a bit according to the spec + fc1InEffect = true; + } + else if (mode == Mode.STRUCTURED_APPEND) + { + if (bits.available() < 16) + { + return null; + } + // not really supported; but sequence number and parity is added later to the result metadata + // Read next 8 bits (symbol sequence #) and 8 bits (parity data), then continue + symbolSequence = bits.readBits(8); + parityData = bits.readBits(8); + } + else + { + // First handle Hanzi mode which does not start with character count + if (mode == Mode.HANZI) + { + //chinese mode contains a sub set indicator right after mode indicator + //int subset = bits.readBits(4); + //int countHanzi = bits.readBits(mode.getCharacterCountBits(version)); + } + else + { + // "Normal" QR code modes: + // How many characters will follow, encoded in this mode? + int count = bits.readBits(mode.getCharacterCountBits(version)); + if (mode == Mode.NUMERIC) + { + if (!decodeNumericSegment(bits, result, count)) + return null; + } + else if (mode == Mode.ALPHANUMERIC) + { + if (!decodeAlphanumericSegment(bits, result, count, fc1InEffect)) + return null; + } + else + { + return null; + } + } + } + } + } while (mode != Mode.TERMINATOR); + } + catch (ArgumentException) + { + // from readBits() calls + return null; + } + +#if WindowsCE + var resultString = result.ToString().Replace("\n", "\r\n"); +#else + var resultString = result.ToString().Replace("\r\n", "\n").Replace("\n", Environment.NewLine); +#endif + return new DecoderResult(bytes, + resultString, + byteSegments.Count == 0 ? null : byteSegments, + ecLevel == null ? null : ecLevel.ToString(), + symbolSequence, parityData); + } + + + + + private static char toAlphaNumericChar(int value) + { + if (value >= ALPHANUMERIC_CHARS.Length) + { + //throw FormatException.Instance; + } + return ALPHANUMERIC_CHARS[value]; + } + + private static bool decodeAlphanumericSegment(BitSource bits, + StringBuilder result, + int count, + bool fc1InEffect) + { + // Read two characters at a time + int start = result.Length; + while (count > 1) + { + if (bits.available() < 11) + { + return false; + } + int nextTwoCharsBits = bits.readBits(11); + result.Append(toAlphaNumericChar(nextTwoCharsBits / 45)); + result.Append(toAlphaNumericChar(nextTwoCharsBits % 45)); + count -= 2; + } + if (count == 1) + { + // special case: one character left + if (bits.available() < 6) + { + return false; + } + result.Append(toAlphaNumericChar(bits.readBits(6))); + } + + // See section 6.4.8.1, 6.4.8.2 + if (fc1InEffect) + { + // We need to massage the result a bit if in an FNC1 mode: + for (int i = start; i < result.Length; i++) + { + if (result[i] == '%') + { + if (i < result.Length - 1 && result[i + 1] == '%') + { + // %% is rendered as % + result.Remove(i + 1, 1); + } + else + { + // In alpha mode, % should be converted to FNC1 separator 0x1D + result.Remove(i, 1); + result.Insert(i, new[] { (char)0x1D }); + } + } + } + } + + return true; + } + + private static bool decodeNumericSegment(BitSource bits, + StringBuilder result, + int count) + { + // Read three digits at a time + while (count >= 3) + { + // Each 10 bits encodes three digits + if (bits.available() < 10) + { + return false; + } + int threeDigitsBits = bits.readBits(10); + if (threeDigitsBits >= 1000) + { + return false; + } + result.Append(toAlphaNumericChar(threeDigitsBits / 100)); + result.Append(toAlphaNumericChar((threeDigitsBits / 10) % 10)); + result.Append(toAlphaNumericChar(threeDigitsBits % 10)); + + count -= 3; + } + if (count == 2) + { + // Two digits left over to read, encoded in 7 bits + if (bits.available() < 7) + { + return false; + } + int twoDigitsBits = bits.readBits(7); + if (twoDigitsBits >= 100) + { + return false; + } + result.Append(toAlphaNumericChar(twoDigitsBits / 10)); + result.Append(toAlphaNumericChar(twoDigitsBits % 10)); + } + else if (count == 1) + { + // One digit left over to read + if (bits.available() < 4) + { + return false; + } + int digitBits = bits.readBits(4); + if (digitBits >= 10) + { + return false; + } + result.Append(toAlphaNumericChar(digitBits)); + } + + return true; + } + + private static int parseECIValue(BitSource bits) + { + int firstByte = bits.readBits(8); + if ((firstByte & 0x80) == 0) + { + // just one byte + return firstByte & 0x7F; + } + if ((firstByte & 0xC0) == 0x80) + { + // two bytes + int secondByte = bits.readBits(8); + return ((firstByte & 0x3F) << 8) | secondByte; + } + if ((firstByte & 0xE0) == 0xC0) + { + // three bytes + int secondThirdBytes = bits.readBits(16); + return ((firstByte & 0x1F) << 16) | secondThirdBytes; + } + throw new ArgumentException("Bad ECI bits starting with byte " + firstByte); + } + } +} \ No newline at end of file diff --git a/shadowsocks-csharp/3rd/zxing/qrcode/decoder/Decoder.cs b/shadowsocks-csharp/3rd/zxing/qrcode/decoder/Decoder.cs new file mode 100755 index 00000000..a5ec8645 --- /dev/null +++ b/shadowsocks-csharp/3rd/zxing/qrcode/decoder/Decoder.cs @@ -0,0 +1,195 @@ +/* +* 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 +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +using System.Collections.Generic; + +using ZXing.Common; +using ZXing.Common.ReedSolomon; + +namespace ZXing.QrCode.Internal +{ + /// + ///

The main class which implements QR Code decoding -- as opposed to locating and extracting + /// the QR Code from an image.

+ ///
+ /// + /// Sean Owen + /// + public sealed class Decoder + { + private readonly ReedSolomonDecoder rsDecoder; + + /// + /// Initializes a new instance of the class. + /// + public Decoder() + { + rsDecoder = new ReedSolomonDecoder(GenericGF.QR_CODE_FIELD_256); + } + + /// + ///

Convenience method that can decode a QR Code represented as a 2D array of booleans. + /// "true" is taken to mean a black module.

+ ///
+ /// booleans representing white/black QR Code modules + /// The hints. + /// + /// text and bytes encoded within the QR Code + /// + public DecoderResult decode(bool[][] image, IDictionary hints) + { + var dimension = image.Length; + var bits = new BitMatrix(dimension); + for (int i = 0; i < dimension; i++) + { + for (int j = 0; j < dimension; j++) + { + bits[j, i] = image[i][j]; + } + } + return decode(bits, hints); + } + + /// + ///

Decodes a QR Code represented as a {@link BitMatrix}. A 1 or "true" is taken to mean a black module.

+ ///
+ /// booleans representing white/black QR Code modules + /// The hints. + /// + /// text and bytes encoded within the QR Code + /// + public DecoderResult decode(BitMatrix bits, IDictionary hints) + { + // Construct a parser and read version, error-correction level + var parser = BitMatrixParser.createBitMatrixParser(bits); + if (parser == null) + return null; + + var result = decode(parser, hints); + if (result == null) + { + // Revert the bit matrix + parser.remask(); + + // Will be attempting a mirrored reading of the version and format info. + parser.setMirror(true); + + // Preemptively read the version. + var version = parser.readVersion(); + if (version == null) + return null; + + // Preemptively read the format information. + var formatinfo = parser.readFormatInformation(); + if (formatinfo == null) + return null; + + /* + * Since we're here, this means we have successfully detected some kind + * of version and format information when mirrored. This is a good sign, + * that the QR code may be mirrored, and we should try once more with a + * mirrored content. + */ + // Prepare for a mirrored reading. + parser.mirror(); + + result = decode(parser, hints); + + if (result != null) + { + // Success! Notify the caller that the code was mirrored. + result.Other = new QRCodeDecoderMetaData(true); + } + } + + return result; + } + + private DecoderResult decode(BitMatrixParser parser, IDictionary hints) + { + Version version = parser.readVersion(); + if (version == null) + return null; + var formatinfo = parser.readFormatInformation(); + if (formatinfo == null) + return null; + ErrorCorrectionLevel ecLevel = formatinfo.ErrorCorrectionLevel; + + // Read codewords + byte[] codewords = parser.readCodewords(); + if (codewords == null) + return null; + // Separate into data blocks + DataBlock[] dataBlocks = DataBlock.getDataBlocks(codewords, version, ecLevel); + + // Count total number of data bytes + int totalBytes = 0; + foreach (var dataBlock in dataBlocks) + { + totalBytes += dataBlock.NumDataCodewords; + } + byte[] resultBytes = new byte[totalBytes]; + int resultOffset = 0; + + // Error-correct and copy data blocks together into a stream of bytes + foreach (var dataBlock in dataBlocks) + { + byte[] codewordBytes = dataBlock.Codewords; + int numDataCodewords = dataBlock.NumDataCodewords; + if (!correctErrors(codewordBytes, numDataCodewords)) + return null; + for (int i = 0; i < numDataCodewords; i++) + { + resultBytes[resultOffset++] = codewordBytes[i]; + } + } + + // Decode the contents of that stream of bytes + return DecodedBitStreamParser.decode(resultBytes, version, ecLevel, hints); + } + + /// + ///

Given data and error-correction codewords received, possibly corrupted by errors, attempts to + /// correct the errors in-place using Reed-Solomon error correction.

+ ///
+ /// data and error correction codewords + /// number of codewords that are data bytes + /// + private bool correctErrors(byte[] codewordBytes, int numDataCodewords) + { + int numCodewords = codewordBytes.Length; + // First read into an array of ints + int[] codewordsInts = new int[numCodewords]; + for (int i = 0; i < numCodewords; i++) + { + codewordsInts[i] = codewordBytes[i] & 0xFF; + } + int numECCodewords = codewordBytes.Length - numDataCodewords; + + if (!rsDecoder.decode(codewordsInts, numECCodewords)) + return false; + + // Copy back into array of bytes -- only need to worry about the bytes that were data + // We don't care about errors in the error-correction codewords + for (int i = 0; i < numDataCodewords; i++) + { + codewordBytes[i] = (byte)codewordsInts[i]; + } + + return true; + } + } +} \ No newline at end of file diff --git a/shadowsocks-csharp/3rd/zxing/ErrorCorrectionLevel.cs b/shadowsocks-csharp/3rd/zxing/qrcode/decoder/ErrorCorrectionLevel.cs similarity index 100% rename from shadowsocks-csharp/3rd/zxing/ErrorCorrectionLevel.cs rename to shadowsocks-csharp/3rd/zxing/qrcode/decoder/ErrorCorrectionLevel.cs diff --git a/shadowsocks-csharp/3rd/zxing/qrcode/decoder/FormatInformation.cs b/shadowsocks-csharp/3rd/zxing/qrcode/decoder/FormatInformation.cs new file mode 100755 index 00000000..88b5045e --- /dev/null +++ b/shadowsocks-csharp/3rd/zxing/qrcode/decoder/FormatInformation.cs @@ -0,0 +1,197 @@ +/* +* 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 +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +using System; + +namespace ZXing.QrCode.Internal +{ + + ///

Encapsulates a QR Code's format information, including the data mask used and + /// error correction level.

+ /// + ///
+ /// Sean Owen + /// + /// www.Redivivus.in (suraj.supekar@redivivus.in) - Ported from ZXING Java Source + /// + /// + /// + /// + /// + sealed class FormatInformation + { + private const int FORMAT_INFO_MASK_QR = 0x5412; + + /// See ISO 18004:2006, Annex C, Table C.1 + private static readonly int[][] FORMAT_INFO_DECODE_LOOKUP = new int[][] + { + new [] { 0x5412, 0x00 }, + new [] { 0x5125, 0x01 }, + new [] { 0x5E7C, 0x02 }, + new [] { 0x5B4B, 0x03 }, + new [] { 0x45F9, 0x04 }, + new [] { 0x40CE, 0x05 }, + new [] { 0x4F97, 0x06 }, + new [] { 0x4AA0, 0x07 }, + new [] { 0x77C4, 0x08 }, + new [] { 0x72F3, 0x09 }, + new [] { 0x7DAA, 0x0A }, + new [] { 0x789D, 0x0B }, + new [] { 0x662F, 0x0C }, + new [] { 0x6318, 0x0D }, + new [] { 0x6C41, 0x0E }, + new [] { 0x6976, 0x0F }, + new [] { 0x1689, 0x10 }, + new [] { 0x13BE, 0x11 }, + new [] { 0x1CE7, 0x12 }, + new [] { 0x19D0, 0x13 }, + new [] { 0x0762, 0x14 }, + new [] { 0x0255, 0x15 }, + new [] { 0x0D0C, 0x16 }, + new [] { 0x083B, 0x17 }, + new [] { 0x355F, 0x18 }, + new [] { 0x3068, 0x19 }, + new [] { 0x3F31, 0x1A }, + new [] { 0x3A06, 0x1B }, + new [] { 0x24B4, 0x1C }, + new [] { 0x2183, 0x1D }, + new [] { 0x2EDA, 0x1E }, + new [] { 0x2BED, 0x1F } + }; + + /// Offset i holds the number of 1 bits in the binary representation of i + private static readonly int[] BITS_SET_IN_HALF_BYTE = new [] + { 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4 }; + + private readonly ErrorCorrectionLevel errorCorrectionLevel; + private readonly byte dataMask; + + private FormatInformation(int formatInfo) + { + // Bits 3,4 + errorCorrectionLevel = ErrorCorrectionLevel.forBits((formatInfo >> 3) & 0x03); + // Bottom 3 bits + dataMask = (byte)(formatInfo & 0x07); + } + + internal static int numBitsDiffering(int a, int b) + { + a ^= b; // a now has a 1 bit exactly where its bit differs with b's + // Count bits set quickly with a series of lookups: + return BITS_SET_IN_HALF_BYTE[a & 0x0F] + + BITS_SET_IN_HALF_BYTE[(((int)((uint)a >> 4)) & 0x0F)] + + BITS_SET_IN_HALF_BYTE[(((int)((uint)a >> 8)) & 0x0F)] + + BITS_SET_IN_HALF_BYTE[(((int)((uint)a >> 12)) & 0x0F)] + + BITS_SET_IN_HALF_BYTE[(((int)((uint)a >> 16)) & 0x0F)] + + BITS_SET_IN_HALF_BYTE[(((int)((uint)a >> 20)) & 0x0F)] + + BITS_SET_IN_HALF_BYTE[(((int)((uint)a >> 24)) & 0x0F)] + + BITS_SET_IN_HALF_BYTE[(((int)((uint)a >> 28)) & 0x0F)]; + } + + /// + /// Decodes the format information. + /// + /// format info indicator, with mask still applied + /// The masked format info2. + /// + /// information about the format it specifies, or null + /// if doesn't seem to match any known pattern + /// + internal static FormatInformation decodeFormatInformation(int maskedFormatInfo1, int maskedFormatInfo2) + { + FormatInformation formatInfo = doDecodeFormatInformation(maskedFormatInfo1, maskedFormatInfo2); + if (formatInfo != null) + { + return formatInfo; + } + // Should return null, but, some QR codes apparently + // do not mask this info. Try again by actually masking the pattern + // first + return doDecodeFormatInformation(maskedFormatInfo1 ^ FORMAT_INFO_MASK_QR, + maskedFormatInfo2 ^ FORMAT_INFO_MASK_QR); + } + + private static FormatInformation doDecodeFormatInformation(int maskedFormatInfo1, int maskedFormatInfo2) + { + // Find the int in FORMAT_INFO_DECODE_LOOKUP with fewest bits differing + int bestDifference = Int32.MaxValue; + int bestFormatInfo = 0; + foreach (var decodeInfo in FORMAT_INFO_DECODE_LOOKUP) + { + int targetInfo = decodeInfo[0]; + if (targetInfo == maskedFormatInfo1 || targetInfo == maskedFormatInfo2) + { + // Found an exact match + return new FormatInformation(decodeInfo[1]); + } + int bitsDifference = numBitsDiffering(maskedFormatInfo1, targetInfo); + if (bitsDifference < bestDifference) + { + bestFormatInfo = decodeInfo[1]; + bestDifference = bitsDifference; + } + if (maskedFormatInfo1 != maskedFormatInfo2) + { + // also try the other option + bitsDifference = numBitsDiffering(maskedFormatInfo2, targetInfo); + if (bitsDifference < bestDifference) + { + bestFormatInfo = decodeInfo[1]; + bestDifference = bitsDifference; + } + } + } + // Hamming distance of the 32 masked codes is 7, by construction, so <= 3 bits + // differing means we found a match + if (bestDifference <= 3) + { + return new FormatInformation(bestFormatInfo); + } + return null; + } + + internal ErrorCorrectionLevel ErrorCorrectionLevel + { + get + { + return errorCorrectionLevel; + } + } + + internal byte DataMask + { + get + { + return dataMask; + } + } + + public override int GetHashCode() + { + return (errorCorrectionLevel.ordinal() << 3) | dataMask; + } + + public override bool Equals(Object o) + { + if (!(o is FormatInformation)) + { + return false; + } + var other = (FormatInformation)o; + return errorCorrectionLevel == other.errorCorrectionLevel && dataMask == other.dataMask; + } + } +} \ No newline at end of file diff --git a/shadowsocks-csharp/3rd/zxing/Mode.cs b/shadowsocks-csharp/3rd/zxing/qrcode/decoder/Mode.cs similarity index 55% rename from shadowsocks-csharp/3rd/zxing/Mode.cs rename to shadowsocks-csharp/3rd/zxing/qrcode/decoder/Mode.cs index df1a5ebb..548ea6d7 100755 --- a/shadowsocks-csharp/3rd/zxing/Mode.cs +++ b/shadowsocks-csharp/3rd/zxing/qrcode/decoder/Mode.cs @@ -38,10 +38,44 @@ namespace ZXing.QrCode.Internal // No, we can't use an enum here. J2ME doesn't support it. + /// + /// + /// + public static readonly Mode TERMINATOR = new Mode(new int[] { 0, 0, 0 }, 0x00, "TERMINATOR"); // Not really a mode... + /// + /// + /// + public static readonly Mode NUMERIC = new Mode(new int[] { 10, 12, 14 }, 0x01, "NUMERIC"); + /// + /// + /// + public static readonly Mode ALPHANUMERIC = new Mode(new int[] { 9, 11, 13 }, 0x02, "ALPHANUMERIC"); + /// + /// + /// + public static readonly Mode STRUCTURED_APPEND = new Mode(new int[] { 0, 0, 0 }, 0x03, "STRUCTURED_APPEND"); // Not supported /// /// /// public static readonly Mode BYTE = new Mode(new int[] { 8, 16, 16 }, 0x04, "BYTE"); + /// + /// + /// + public static readonly Mode ECI = new Mode(null, 0x07, "ECI"); // character counts don't apply + /// + /// + /// + public static readonly Mode KANJI = new Mode(new int[] { 8, 10, 12 }, 0x08, "KANJI"); + /// + /// + /// + public static readonly Mode FNC1_FIRST_POSITION = new Mode(null, 0x05, "FNC1_FIRST_POSITION"); + /// + /// + /// + public static readonly Mode FNC1_SECOND_POSITION = new Mode(null, 0x09, "FNC1_SECOND_POSITION"); + /// See GBT 18284-2000; "Hanzi" is a transliteration of this mode name. + public static readonly Mode HANZI = new Mode(new int[] { 8, 10, 12 }, 0x0D, "HANZI"); private readonly int[] characterCountBitsForVersions; private readonly int bits; @@ -66,8 +100,27 @@ namespace ZXing.QrCode.Internal { switch (bits) { + case 0x0: + return TERMINATOR; + case 0x1: + return NUMERIC; + case 0x2: + return ALPHANUMERIC; + case 0x3: + return STRUCTURED_APPEND; case 0x4: return BYTE; + case 0x5: + return FNC1_FIRST_POSITION; + case 0x7: + return ECI; + case 0x8: + return KANJI; + case 0x9: + return FNC1_SECOND_POSITION; + case 0xD: + // 0xD is defined in GBT 18284-2000, may not be supported in foreign country + return HANZI; default: throw new ArgumentException(); } @@ -111,5 +164,16 @@ namespace ZXing.QrCode.Internal return bits; } } + + /// + /// Returns a that represents this instance. + /// + /// + /// A that represents this instance. + /// + public override String ToString() + { + return name; + } } } \ No newline at end of file diff --git a/shadowsocks-csharp/3rd/zxing/qrcode/decoder/QRCodeDecoderMetaData.cs b/shadowsocks-csharp/3rd/zxing/qrcode/decoder/QRCodeDecoderMetaData.cs new file mode 100755 index 00000000..cc059aa9 --- /dev/null +++ b/shadowsocks-csharp/3rd/zxing/qrcode/decoder/QRCodeDecoderMetaData.cs @@ -0,0 +1,60 @@ +/* + * Copyright 2013 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 + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +namespace ZXing.QrCode.Internal +{ + /// + /// Meta-data container for QR Code decoding. Instances of this class may be used to convey information back to the + /// decoding caller. Callers are expected to process this. + /// + public sealed class QRCodeDecoderMetaData + { + private readonly bool mirrored; + + /// + /// Initializes a new instance of the class. + /// + /// if set to true [mirrored]. + public QRCodeDecoderMetaData(bool mirrored) + { + this.mirrored = mirrored; + } + + /// + /// true if the QR Code was mirrored. + /// + public bool IsMirrored + { + get { return mirrored; } + } + + /// + /// Apply the result points' order correction due to mirroring. + /// + /// Array of points to apply mirror correction to. + public void applyMirroredCorrection(ResultPoint[] points) + { + if (!mirrored || points == null || points.Length < 3) + { + return; + } + ResultPoint bottomLeft = points[0]; + points[0] = points[2]; + points[2] = bottomLeft; + // No need to 'fix' top-left and alignment pattern. + } + } +} \ No newline at end of file diff --git a/shadowsocks-csharp/3rd/zxing/qrcode/decoder/Version.cs b/shadowsocks-csharp/3rd/zxing/qrcode/decoder/Version.cs new file mode 100755 index 00000000..e619933d --- /dev/null +++ b/shadowsocks-csharp/3rd/zxing/qrcode/decoder/Version.cs @@ -0,0 +1,424 @@ +/* +* 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 +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +using System; + +using ZXing.Common; + +namespace ZXing.QrCode.Internal +{ + /// + /// See ISO 18004:2006 Annex D + /// + /// Sean Owen + public sealed class Version + { + /// See ISO 18004:2006 Annex D. + /// Element i represents the raw version bits that specify version i + 7 + /// + private static readonly int[] VERSION_DECODE_INFO = new[] + { + 0x07C94, 0x085BC, 0x09A99, 0x0A4D3, 0x0BBF6, + 0x0C762, 0x0D847, 0x0E60D, 0x0F928, 0x10B78, + 0x1145D, 0x12A17, 0x13532, 0x149A6, 0x15683, + 0x168C9, 0x177EC, 0x18EC4, 0x191E1, 0x1AFAB, + 0x1B08E, 0x1CC1A, 0x1D33F, 0x1ED75, 0x1F250, + 0x209D5, 0x216F0, 0x228BA, 0x2379F, 0x24B0B, + 0x2542E, 0x26A64, 0x27541, 0x28C69 + }; + + private static readonly Version[] VERSIONS = buildVersions(); + + private readonly int versionNumber; + private readonly int[] alignmentPatternCenters; + private readonly ECBlocks[] ecBlocks; + private readonly int totalCodewords; + + private Version(int versionNumber, int[] alignmentPatternCenters, params ECBlocks[] ecBlocks) + { + this.versionNumber = versionNumber; + this.alignmentPatternCenters = alignmentPatternCenters; + this.ecBlocks = ecBlocks; + int total = 0; + int ecCodewords = ecBlocks[0].ECCodewordsPerBlock; + ECB[] ecbArray = ecBlocks[0].getECBlocks(); + foreach (var ecBlock in ecbArray) + { + total += ecBlock.Count * (ecBlock.DataCodewords + ecCodewords); + } + this.totalCodewords = total; + } + + /// + /// Gets the version number. + /// + public int VersionNumber + { + get + { + return versionNumber; + } + + } + + /// + /// Gets the alignment pattern centers. + /// + public int[] AlignmentPatternCenters + { + get + { + return alignmentPatternCenters; + } + + } + + /// + /// Gets the total codewords. + /// + public int TotalCodewords + { + get + { + return totalCodewords; + } + + } + + /// + /// Gets the dimension for version. + /// + public int DimensionForVersion + { + get + { + return 17 + 4 * versionNumber; + } + + } + + /// + /// Gets the EC blocks for level. + /// + /// The ec level. + /// + public ECBlocks getECBlocksForLevel(ErrorCorrectionLevel ecLevel) + { + return ecBlocks[ecLevel.ordinal()]; + } + + ///

Deduces version information purely from QR Code dimensions.

+ /// + ///
+ /// dimension in modules + /// + /// for a QR Code of that dimension or null + public static Version getProvisionalVersionForDimension(int dimension) + { + if (dimension % 4 != 1) + { + return null; + } + try + { + return getVersionForNumber((dimension - 17) >> 2); + } + catch (ArgumentException) + { + return null; + } + } + + /// + /// Gets the version for number. + /// + /// The version number. + /// + public static Version getVersionForNumber(int versionNumber) + { + if (versionNumber < 1 || versionNumber > 40) + { + throw new ArgumentException(); + } + return VERSIONS[versionNumber - 1]; + } + + internal static Version decodeVersionInformation(int versionBits) + { + int bestDifference = Int32.MaxValue; + int bestVersion = 0; + for (int i = 0; i < VERSION_DECODE_INFO.Length; i++) + { + int targetVersion = VERSION_DECODE_INFO[i]; + // Do the version info bits match exactly? done. + if (targetVersion == versionBits) + { + return getVersionForNumber(i + 7); + } + // Otherwise see if this is the closest to a real version info bit string + // we have seen so far + int bitsDifference = FormatInformation.numBitsDiffering(versionBits, targetVersion); + if (bitsDifference < bestDifference) + { + bestVersion = i + 7; + bestDifference = bitsDifference; + } + } + // We can tolerate up to 3 bits of error since no two version info codewords will + // differ in less than 8 bits. + if (bestDifference <= 3) + { + return getVersionForNumber(bestVersion); + } + // If we didn't find a close enough match, fail + return null; + } + + /// See ISO 18004:2006 Annex E + internal BitMatrix buildFunctionPattern() + { + int dimension = DimensionForVersion; + BitMatrix bitMatrix = new BitMatrix(dimension); + + // Top left finder pattern + separator + format + bitMatrix.setRegion(0, 0, 9, 9); + // Top right finder pattern + separator + format + bitMatrix.setRegion(dimension - 8, 0, 8, 9); + // Bottom left finder pattern + separator + format + bitMatrix.setRegion(0, dimension - 8, 9, 8); + + // Alignment patterns + int max = alignmentPatternCenters.Length; + for (int x = 0; x < max; x++) + { + int i = alignmentPatternCenters[x] - 2; + for (int y = 0; y < max; y++) + { + if ((x == 0 && (y == 0 || y == max - 1)) || (x == max - 1 && y == 0)) + { + // No alignment patterns near the three finder paterns + continue; + } + bitMatrix.setRegion(alignmentPatternCenters[y] - 2, i, 5, 5); + } + } + + // Vertical timing pattern + bitMatrix.setRegion(6, 9, 1, dimension - 17); + // Horizontal timing pattern + bitMatrix.setRegion(9, 6, dimension - 17, 1); + + if (versionNumber > 6) + { + // Version info, top right + bitMatrix.setRegion(dimension - 11, 0, 3, 6); + // Version info, bottom left + bitMatrix.setRegion(0, dimension - 11, 6, 3); + } + + return bitMatrix; + } + + ///

Encapsulates a set of error-correction blocks in one symbol version. Most versions will + /// use blocks of differing sizes within one version, so, this encapsulates the parameters for + /// each set of blocks. It also holds the number of error-correction codewords per block since it + /// will be the same across all blocks within one version.

+ ///
+ public sealed class ECBlocks + { + private readonly int ecCodewordsPerBlock; + private readonly ECB[] ecBlocks; + + internal ECBlocks(int ecCodewordsPerBlock, params ECB[] ecBlocks) + { + this.ecCodewordsPerBlock = ecCodewordsPerBlock; + this.ecBlocks = ecBlocks; + } + + /// + /// Gets the EC codewords per block. + /// + public int ECCodewordsPerBlock + { + get + { + return ecCodewordsPerBlock; + } + } + + /// + /// Gets the num blocks. + /// + public int NumBlocks + { + get + { + int total = 0; + foreach (var ecBlock in ecBlocks) + { + total += ecBlock.Count; + } + return total; + } + } + + /// + /// Gets the total EC codewords. + /// + public int TotalECCodewords + { + get + { + return ecCodewordsPerBlock * NumBlocks; + } + } + + /// + /// Gets the EC blocks. + /// + /// + public ECB[] getECBlocks() + { + return ecBlocks; + } + } + + ///

Encapsualtes the parameters for one error-correction block in one symbol version. + /// This includes the number of data codewords, and the number of times a block with these + /// parameters is used consecutively in the QR code version's format.

+ ///
+ public sealed class ECB + { + private readonly int count; + private readonly int dataCodewords; + + internal ECB(int count, int dataCodewords) + { + this.count = count; + this.dataCodewords = dataCodewords; + } + + /// + /// Gets the count. + /// + public int Count + { + get + { + return count; + } + + } + /// + /// Gets the data codewords. + /// + public int DataCodewords + { + get + { + return dataCodewords; + } + + } + } + + /// + /// Returns a that represents this instance. + /// + /// + /// A that represents this instance. + /// + public override String ToString() + { + return Convert.ToString(versionNumber); + } + + /// See ISO 18004:2006 6.5.1 Table 9 + private static Version[] buildVersions() + { + return new Version[]{ + new Version(1, new int[]{}, + new ECBlocks(7, new ECB(1, 19)), + new ECBlocks(10, new ECB(1, 16)), + new ECBlocks(13, new ECB(1, 13)), + new ECBlocks(17, new ECB(1, 9))), + new Version(2, new int[]{6, 18}, + new ECBlocks(10, new ECB(1, 34)), + new ECBlocks(16, new ECB(1, 28)), + new ECBlocks(22, new ECB(1, 22)), + new ECBlocks(28, new ECB(1, 16))), + new Version(3, new int[]{6, 22}, + new ECBlocks(15, new ECB(1, 55)), + new ECBlocks(26, new ECB(1, 44)), + new ECBlocks(18, new ECB(2, 17)), + new ECBlocks(22, new ECB(2, 13))), + new Version(4, new int[]{6, 26}, + new ECBlocks(20, new ECB(1, 80)), + new ECBlocks(18, new ECB(2, 32)), + new ECBlocks(26, new ECB(2, 24)), + new ECBlocks(16, new ECB(4, 9))), + new Version(5, new int[]{6, 30}, + new ECBlocks(26, new ECB(1, 108)), + new ECBlocks(24, new ECB(2, 43)), + new ECBlocks(18, new ECB(2, 15), + new ECB(2, 16)), + new ECBlocks(22, new ECB(2, 11), + new ECB(2, 12))), + new Version(6, new int[]{6, 34}, + new ECBlocks(18, new ECB(2, 68)), + new ECBlocks(16, new ECB(4, 27)), + new ECBlocks(24, new ECB(4, 19)), + new ECBlocks(28, new ECB(4, 15))), + new Version(7, new int[]{6, 22, 38}, + new ECBlocks(20, new ECB(2, 78)), + new ECBlocks(18, new ECB(4, 31)), + new ECBlocks(18, new ECB(2, 14), + new ECB(4, 15)), + new ECBlocks(26, new ECB(4, 13), + new ECB(1, 14))), + new Version(8, new int[]{6, 24, 42}, + new ECBlocks(24, new ECB(2, 97)), + new ECBlocks(22, new ECB(2, 38), + new ECB(2, 39)), + new ECBlocks(22, new ECB(4, 18), + new ECB(2, 19)), + new ECBlocks(26, new ECB(4, 14), + new ECB(2, 15))), + new Version(9, new int[]{6, 26, 46}, + new ECBlocks(30, new ECB(2, 116)), + new ECBlocks(22, new ECB(3, 36), + new ECB(2, 37)), + new ECBlocks(20, new ECB(4, 16), + new ECB(4, 17)), + new ECBlocks(24, new ECB(4, 12), + new ECB(4, 13))), + new Version(10, new int[]{6, 28, 50}, + new ECBlocks(18, new ECB(2, 68), + new ECB(2, 69)), + new ECBlocks(26, new ECB(4, 43), + new ECB(1, 44)), + new ECBlocks(24, new ECB(6, 19), + new ECB(2, 20)), + new ECBlocks(28, new ECB(6, 15), + new ECB(2, 16))), + new Version(11, new int[]{6, 30, 54}, new ECBlocks(20, new ECB(4, 81)), + new ECBlocks(30, new ECB(1, 50), new ECB(4, 51)), new ECBlocks(28, new ECB(4, 22), new ECB(4, 23)), new ECBlocks(24, new ECB(3, 12), new ECB(8, 13))), new Version(12, new int[]{6, 32, 58}, new ECBlocks(24, new ECB(2, 92), new ECB(2, 93)), new ECBlocks(22, new ECB(6, 36), new ECB(2, 37)), new ECBlocks(26, new ECB(4, 20), new ECB(6, 21)), new ECBlocks(28, new ECB(7, 14), new ECB(4, 15))), new Version(13, new int[]{6, 34, 62}, new ECBlocks(26, new ECB(4, 107)), new ECBlocks(22, new ECB(8, 37), new ECB(1, 38)), new ECBlocks(24, new ECB(8, 20), new ECB(4, 21)), new ECBlocks(22, new ECB(12, 11), new ECB(4, 12))), new Version(14, new int[]{6, 26, 46, 66}, new ECBlocks(30, new ECB(3, 115), new ECB(1, 116)), new ECBlocks(24, new ECB(4, 40), new ECB(5, 41)), new ECBlocks(20, new ECB(11, 16), new ECB(5, 17)), new ECBlocks(24, new ECB(11, 12), new ECB(5, 13))), new Version(15, new int[]{6, 26, 48, 70}, new ECBlocks(22, new ECB(5, 87), new ECB(1, 88)), new ECBlocks(24, new ECB(5, 41), new ECB(5, 42)), new ECBlocks(30, new ECB(5, 24), new ECB(7, 25)), new ECBlocks(24, new ECB(11, 12), new ECB(7, 13))), new Version(16, new int[]{6, 26, 50, 74}, new ECBlocks(24, new ECB(5, 98), new ECB(1, 99)), new ECBlocks(28, new ECB(7, 45), new ECB(3, 46)), new ECBlocks(24, new ECB(15, 19), new ECB(2, 20)), new ECBlocks(30, new ECB(3, 15), new ECB(13, 16))), new Version(17, new int[]{6, 30, 54, 78}, new ECBlocks(28, new ECB(1, 107), new ECB(5, 108)), new ECBlocks(28, new ECB(10, 46), new ECB(1, 47)), new ECBlocks(28, new ECB(1, 22), new ECB(15, 23)), new ECBlocks(28, new ECB(2, 14), new ECB(17, 15))), new Version(18, new int[]{6, 30, 56, 82}, new ECBlocks(30, new ECB(5, 120), new ECB(1, 121)), new ECBlocks(26, new ECB(9, 43), new ECB(4, 44)), new ECBlocks(28, new ECB(17, 22), new ECB(1, 23)), new ECBlocks(28, new ECB(2, 14), new ECB(19, 15))), new Version(19, new int[]{6, 30, 58, 86}, new ECBlocks(28, new ECB(3, 113), new ECB(4, 114)), new ECBlocks(26, new ECB(3, 44), new ECB(11, 45)), new ECBlocks(26, new ECB(17, 21), + new ECB(4, 22)), new ECBlocks(26, new ECB(9, 13), new ECB(16, 14))), new Version(20, new int[]{6, 34, 62, 90}, new ECBlocks(28, new ECB(3, 107), new ECB(5, 108)), new ECBlocks(26, new ECB(3, 41), new ECB(13, 42)), new ECBlocks(30, new ECB(15, 24), new ECB(5, 25)), new ECBlocks(28, new ECB(15, 15), new ECB(10, 16))), new Version(21, new int[]{6, 28, 50, 72, 94}, new ECBlocks(28, new ECB(4, 116), new ECB(4, 117)), new ECBlocks(26, new ECB(17, 42)), new ECBlocks(28, new ECB(17, 22), new ECB(6, 23)), new ECBlocks(30, new ECB(19, 16), new ECB(6, 17))), new Version(22, new int[]{6, 26, 50, 74, 98}, new ECBlocks(28, new ECB(2, 111), new ECB(7, 112)), new ECBlocks(28, new ECB(17, 46)), new ECBlocks(30, new ECB(7, 24), new ECB(16, 25)), new ECBlocks(24, new ECB(34, 13))), new Version(23, new int[]{6, 30, 54, 74, 102}, new ECBlocks(30, new ECB(4, 121), new ECB(5, 122)), new ECBlocks(28, new ECB(4, 47), new ECB(14, 48)), new ECBlocks(30, new ECB(11, 24), new ECB(14, 25)), new ECBlocks(30, new ECB(16, 15), new ECB(14, 16))), new Version(24, new int[]{6, 28, 54, 80, 106}, new ECBlocks(30, new ECB(6, 117), new ECB(4, 118)), new ECBlocks(28, new ECB(6, 45), new ECB(14, 46)), new ECBlocks(30, new ECB(11, 24), new ECB(16, 25)), new ECBlocks(30, new ECB(30, 16), new ECB(2, 17))), new Version(25, new int[]{6, 32, 58, 84, 110}, new ECBlocks(26, new ECB(8, 106), new ECB(4, 107)), new ECBlocks(28, new ECB(8, 47), new ECB(13, 48)), new ECBlocks(30, new ECB(7, 24), new ECB(22, 25)), new ECBlocks(30, new ECB(22, 15), new ECB(13, 16))), new Version(26, new int[]{6, 30, 58, 86, 114}, new ECBlocks(28, new ECB(10, 114), new ECB(2, 115)), new ECBlocks(28, new ECB(19, 46), new ECB(4, 47)), new ECBlocks(28, new ECB(28, 22), new ECB(6, 23)), new ECBlocks(30, new ECB(33, 16), new ECB(4, 17))), new Version(27, new int[]{6, 34, 62, 90, 118}, new ECBlocks(30, new ECB(8, 122), new ECB(4, 123)), new ECBlocks(28, new ECB(22, 45), new ECB(3, 46)), new ECBlocks(30, new ECB(8, 23), new ECB(26, 24)), new ECBlocks(30, new ECB(12, 15), + new ECB(28, 16))), new Version(28, new int[]{6, 26, 50, 74, 98, 122}, new ECBlocks(30, new ECB(3, 117), new ECB(10, 118)), new ECBlocks(28, new ECB(3, 45), new ECB(23, 46)), new ECBlocks(30, new ECB(4, 24), new ECB(31, 25)), new ECBlocks(30, new ECB(11, 15), new ECB(31, 16))), new Version(29, new int[]{6, 30, 54, 78, 102, 126}, new ECBlocks(30, new ECB(7, 116), new ECB(7, 117)), new ECBlocks(28, new ECB(21, 45), new ECB(7, 46)), new ECBlocks(30, new ECB(1, 23), new ECB(37, 24)), new ECBlocks(30, new ECB(19, 15), new ECB(26, 16))), new Version(30, new int[]{6, 26, 52, 78, 104, 130}, new ECBlocks(30, new ECB(5, 115), new ECB(10, 116)), new ECBlocks(28, new ECB(19, 47), new ECB(10, 48)), new ECBlocks(30, new ECB(15, 24), new ECB(25, 25)), new ECBlocks(30, new ECB(23, 15), new ECB(25, 16))), new Version(31, new int[]{6, 30, 56, 82, 108, 134}, new ECBlocks(30, new ECB(13, 115), new ECB(3, 116)), new ECBlocks(28, new ECB(2, 46), new ECB(29, 47)), new ECBlocks(30, new ECB(42, 24), new ECB(1, 25)), new ECBlocks(30, new ECB(23, 15), new ECB(28, 16))), new Version(32, new int[]{6, 34, 60, 86, 112, 138}, new ECBlocks(30, new ECB(17, 115)), new ECBlocks(28, new ECB(10, 46), new ECB(23, 47)), new ECBlocks(30, new ECB(10, 24), new ECB(35, 25)), new ECBlocks(30, new ECB(19, 15), new ECB(35, 16))), new Version(33, new int[]{6, 30, 58, 86, 114, 142}, new ECBlocks(30, new ECB(17, 115), new ECB(1, 116)), new ECBlocks(28, new ECB(14, 46), new ECB(21, 47)), new ECBlocks(30, new ECB(29, 24), new ECB(19, 25)), new ECBlocks(30, new ECB(11, 15), new ECB(46, 16))), new Version(34, new int[]{6, 34, 62, 90, 118, 146}, new ECBlocks(30, new ECB(13, 115), new ECB(6, 116)), new ECBlocks(28, new ECB(14, 46), new ECB(23, 47)), new ECBlocks(30, new ECB(44, 24), new ECB(7, 25)), new ECBlocks(30, new ECB(59, 16), new ECB(1, 17))), new Version(35, new int[]{6, 30, 54, 78, 102, 126, 150}, new ECBlocks(30, new ECB(12, 121), new ECB(7, 122)), new ECBlocks(28, new ECB(12, 47), new ECB(26, 48)), new ECBlocks(30, new ECB(39, 24), new + ECB(14, 25)), new ECBlocks(30, new ECB(22, 15), new ECB(41, 16))), new Version(36, new int[]{6, 24, 50, 76, 102, 128, 154}, new ECBlocks(30, new ECB(6, 121), new ECB(14, 122)), new ECBlocks(28, new ECB(6, 47), new ECB(34, 48)), new ECBlocks(30, new ECB(46, 24), new ECB(10, 25)), new ECBlocks(30, new ECB(2, 15), new ECB(64, 16))), new Version(37, new int[]{6, 28, 54, 80, 106, 132, 158}, new ECBlocks(30, new ECB(17, 122), new ECB(4, 123)), new ECBlocks(28, new ECB(29, 46), new ECB(14, 47)), new ECBlocks(30, new ECB(49, 24), new ECB(10, 25)), new ECBlocks(30, new ECB(24, 15), new ECB(46, 16))), new Version(38, new int[]{6, 32, 58, 84, 110, 136, 162}, new ECBlocks(30, new ECB(4, 122), new ECB(18, 123)), new ECBlocks(28, new ECB(13, 46), new ECB(32, 47)), new ECBlocks(30, new ECB(48, 24), new ECB(14, 25)), new ECBlocks(30, new ECB(42, 15), new ECB(32, 16))), new Version(39, new int[]{6, 26, 54, 82, 110, 138, 166}, new ECBlocks(30, new ECB(20, 117), new ECB(4, 118)), new ECBlocks(28, new ECB(40, 47), new ECB(7, 48)), new ECBlocks(30, new ECB(43, 24), new ECB(22, 25)), new ECBlocks(30, new ECB(10, 15), new ECB(67, 16))), new Version(40, new int[]{6, 30, 58, 86, 114, 142, 170}, new ECBlocks(30, new ECB(19, 118), new ECB(6, 119)), new ECBlocks(28, new ECB(18, 47), new ECB(31, 48)), new ECBlocks(30, new ECB(34, 24), new ECB(34, 25)), new ECBlocks(30, new ECB(20, 15), new ECB(61, 16)))}; + } + } +} \ No newline at end of file diff --git a/shadowsocks-csharp/3rd/zxing/qrcode/detector/AlignmentPattern.cs b/shadowsocks-csharp/3rd/zxing/qrcode/detector/AlignmentPattern.cs new file mode 100755 index 00000000..e7dd9460 --- /dev/null +++ b/shadowsocks-csharp/3rd/zxing/qrcode/detector/AlignmentPattern.cs @@ -0,0 +1,68 @@ +/* +* 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 +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +using System; + +namespace ZXing.QrCode.Internal +{ + ///

Encapsulates an alignment pattern, which are the smaller square patterns found in + /// all but the simplest QR Codes.

+ /// + ///
+ /// Sean Owen + /// + /// www.Redivivus.in (suraj.supekar@redivivus.in) - Ported from ZXING Java Source + /// + public sealed class AlignmentPattern : ResultPoint + { + private float estimatedModuleSize; + + internal AlignmentPattern(float posX, float posY, float estimatedModuleSize) + : base(posX, posY) + { + this.estimatedModuleSize = estimatedModuleSize; + } + + ///

Determines if this alignment pattern "about equals" an alignment pattern at the stated + /// position and size -- meaning, it is at nearly the same center with nearly the same size.

+ ///
+ internal bool aboutEquals(float moduleSize, float i, float j) + { + if (Math.Abs(i - Y) <= moduleSize && Math.Abs(j - X) <= moduleSize) + { + float moduleSizeDiff = Math.Abs(moduleSize - estimatedModuleSize); + return moduleSizeDiff <= 1.0f || moduleSizeDiff <= estimatedModuleSize; + } + return false; + } + + /// + /// Combines this object's current estimate of a finder pattern position and module size + /// with a new estimate. It returns a new {@code FinderPattern} containing an average of the two. + /// + /// The i. + /// The j. + /// New size of the module. + /// + internal AlignmentPattern combineEstimate(float i, float j, float newModuleSize) + { + float combinedX = (X + j) / 2.0f; + float combinedY = (Y + i) / 2.0f; + float combinedModuleSize = (estimatedModuleSize + newModuleSize) / 2.0f; + return new AlignmentPattern(combinedX, combinedY, combinedModuleSize); + } + } +} \ No newline at end of file diff --git a/shadowsocks-csharp/3rd/zxing/qrcode/detector/AlignmentPatternFinder.cs b/shadowsocks-csharp/3rd/zxing/qrcode/detector/AlignmentPatternFinder.cs new file mode 100755 index 00000000..b8cdec5b --- /dev/null +++ b/shadowsocks-csharp/3rd/zxing/qrcode/detector/AlignmentPatternFinder.cs @@ -0,0 +1,324 @@ +/* +* 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 +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +using System; +using System.Collections.Generic; + +using ZXing.Common; + +namespace ZXing.QrCode.Internal +{ + ///

This class attempts to find alignment patterns in a QR Code. Alignment patterns look like finder + /// patterns but are smaller and appear at regular intervals throughout the image.

+ /// + ///

At the moment this only looks for the bottom-right alignment pattern.

+ /// + ///

This is mostly a simplified copy of {@link FinderPatternFinder}. It is copied, + /// pasted and stripped down here for maximum performance but does unfortunately duplicate + /// some code.

+ /// + ///

This class is thread-safe but not reentrant. Each thread must allocate its own object.

+ /// + ///
+ /// Sean Owen + /// + /// www.Redivivus.in (suraj.supekar@redivivus.in) - Ported from ZXING Java Source + /// + sealed class AlignmentPatternFinder + { + private readonly BitMatrix image; + private readonly IList possibleCenters; + private readonly int startX; + private readonly int startY; + private readonly int width; + private readonly int height; + private readonly float moduleSize; + private readonly int[] crossCheckStateCount; + private readonly ResultPointCallback resultPointCallback; + + ///

Creates a finder that will look in a portion of the whole image.

+ /// + ///
+ /// image to search + /// + /// left column from which to start searching + /// + /// top row from which to start searching + /// + /// width of region to search + /// + /// height of region to search + /// + /// estimated module size so far + /// + internal AlignmentPatternFinder(BitMatrix image, int startX, int startY, int width, int height, float moduleSize, ResultPointCallback resultPointCallback) + { + this.image = image; + this.possibleCenters = new List(5); + this.startX = startX; + this.startY = startY; + this.width = width; + this.height = height; + this.moduleSize = moduleSize; + this.crossCheckStateCount = new int[3]; + this.resultPointCallback = resultPointCallback; + } + + ///

This method attempts to find the bottom-right alignment pattern in the image. It is a bit messy since + /// it's pretty performance-critical and so is written to be fast foremost.

+ /// + ///
+ /// {@link AlignmentPattern} if found + /// + internal AlignmentPattern find() + { + int startX = this.startX; + int height = this.height; + int maxJ = startX + width; + int middleI = startY + (height >> 1); + // We are looking for black/white/black modules in 1:1:1 ratio; + // this tracks the number of black/white/black modules seen so far + int[] stateCount = new int[3]; + for (int iGen = 0; iGen < height; iGen++) + { + // Search from middle outwards + int i = middleI + ((iGen & 0x01) == 0 ? ((iGen + 1) >> 1) : -((iGen + 1) >> 1)); + stateCount[0] = 0; + stateCount[1] = 0; + stateCount[2] = 0; + int j = startX; + // Burn off leading white pixels before anything else; if we start in the middle of + // a white run, it doesn't make sense to count its length, since we don't know if the + // white run continued to the left of the start point + while (j < maxJ && !image[j, i]) + { + j++; + } + int currentState = 0; + while (j < maxJ) + { + if (image[j, i]) + { + // Black pixel + if (currentState == 1) + { + // Counting black pixels + stateCount[currentState]++; + } + else + { + // Counting white pixels + if (currentState == 2) + { + // A winner? + if (foundPatternCross(stateCount)) + { + // Yes + AlignmentPattern confirmed = handlePossibleCenter(stateCount, i, j); + if (confirmed != null) + { + return confirmed; + } + } + stateCount[0] = stateCount[2]; + stateCount[1] = 1; + stateCount[2] = 0; + currentState = 1; + } + else + { + stateCount[++currentState]++; + } + } + } + else + { + // White pixel + if (currentState == 1) + { + // Counting black pixels + currentState++; + } + stateCount[currentState]++; + } + j++; + } + if (foundPatternCross(stateCount)) + { + AlignmentPattern confirmed = handlePossibleCenter(stateCount, i, maxJ); + if (confirmed != null) + { + return confirmed; + } + } + } + + // Hmm, nothing we saw was observed and confirmed twice. If we had + // any guess at all, return it. + if (possibleCenters.Count != 0) + { + return possibleCenters[0]; + } + + return null; + } + + /// Given a count of black/white/black pixels just seen and an end position, + /// figures the location of the center of this black/white/black run. + /// + private static float? centerFromEnd(int[] stateCount, int end) + { + var result = (end - stateCount[2]) - stateCount[1] / 2.0f; + if (Single.IsNaN(result)) + return null; + return result; + } + + /// count of black/white/black pixels just read + /// + /// true iff the proportions of the counts is close enough to the 1/1/1 ratios + /// used by alignment patterns to be considered a match + /// + private bool foundPatternCross(int[] stateCount) + { + float maxVariance = moduleSize / 2.0f; + for (int i = 0; i < 3; i++) + { + if (Math.Abs(moduleSize - stateCount[i]) >= maxVariance) + { + return false; + } + } + return true; + } + + /// + ///

After a horizontal scan finds a potential alignment pattern, this method + /// "cross-checks" by scanning down vertically through the center of the possible + /// alignment pattern to see if the same proportion is detected.

+ ///
+ /// row where an alignment pattern was detected + /// center of the section that appears to cross an alignment pattern + /// maximum reasonable number of modules that should be + /// observed in any reading state, based on the results of the horizontal scan + /// The original state count total. + /// + /// vertical center of alignment pattern, or null if not found + /// + private float? crossCheckVertical(int startI, int centerJ, int maxCount, int originalStateCountTotal) + { + int maxI = image.Height; + int[] stateCount = crossCheckStateCount; + stateCount[0] = 0; + stateCount[1] = 0; + stateCount[2] = 0; + + // Start counting up from center + int i = startI; + while (i >= 0 && image[centerJ, i] && stateCount[1] <= maxCount) + { + stateCount[1]++; + i--; + } + // If already too many modules in this state or ran off the edge: + if (i < 0 || stateCount[1] > maxCount) + { + return null; + } + while (i >= 0 && !image[centerJ, i] && stateCount[0] <= maxCount) + { + stateCount[0]++; + i--; + } + if (stateCount[0] > maxCount) + { + return null; + } + + // Now also count down from center + i = startI + 1; + while (i < maxI && image[centerJ, i] && stateCount[1] <= maxCount) + { + stateCount[1]++; + i++; + } + if (i == maxI || stateCount[1] > maxCount) + { + return null; + } + while (i < maxI && !image[centerJ, i] && stateCount[2] <= maxCount) + { + stateCount[2]++; + i++; + } + if (stateCount[2] > maxCount) + { + return null; + } + + int stateCountTotal = stateCount[0] + stateCount[1] + stateCount[2]; + if (5 * Math.Abs(stateCountTotal - originalStateCountTotal) >= 2 * originalStateCountTotal) + { + return null; + } + + return foundPatternCross(stateCount) ? centerFromEnd(stateCount, i) : null; + } + + ///

This is called when a horizontal scan finds a possible alignment pattern. It will + /// cross check with a vertical scan, and if successful, will see if this pattern had been + /// found on a previous horizontal scan. If so, we consider it confirmed and conclude we have + /// found the alignment pattern.

+ /// + ///
+ /// reading state module counts from horizontal scan + /// + /// row where alignment pattern may be found + /// + /// end of possible alignment pattern in row + /// + /// {@link AlignmentPattern} if we have found the same pattern twice, or null if not + /// + private AlignmentPattern handlePossibleCenter(int[] stateCount, int i, int j) + { + int stateCountTotal = stateCount[0] + stateCount[1] + stateCount[2]; + float? centerJ = centerFromEnd(stateCount, j); + if (centerJ == null) + return null; + float? centerI = crossCheckVertical(i, (int)centerJ, 2 * stateCount[1], stateCountTotal); + if (centerI != null) + { + float estimatedModuleSize = (stateCount[0] + stateCount[1] + stateCount[2]) / 3.0f; + foreach (var center in possibleCenters) + { + // Look for about the same center and module size: + if (center.aboutEquals(estimatedModuleSize, centerI.Value, centerJ.Value)) + { + return center.combineEstimate(centerI.Value, centerJ.Value, estimatedModuleSize); + } + } + // Hadn't found this before; save it + var point = new AlignmentPattern(centerJ.Value, centerI.Value, estimatedModuleSize); + possibleCenters.Add(point); + if (resultPointCallback != null) + { + resultPointCallback(point); + } + } + return null; + } + } +} \ No newline at end of file diff --git a/shadowsocks-csharp/3rd/zxing/qrcode/detector/Detector.cs b/shadowsocks-csharp/3rd/zxing/qrcode/detector/Detector.cs new file mode 100755 index 00000000..7ae8e3db --- /dev/null +++ b/shadowsocks-csharp/3rd/zxing/qrcode/detector/Detector.cs @@ -0,0 +1,429 @@ +/* +* 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 +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +using System; +using System.Collections.Generic; + +using ZXing.Common; +using ZXing.Common.Detector; + +namespace ZXing.QrCode.Internal +{ + /// + ///

Encapsulates logic that can detect a QR Code in an image, even if the QR Code + /// is rotated or skewed, or partially obscured.

+ ///
+ /// Sean Owen + public class Detector + { + private readonly BitMatrix image; + private ResultPointCallback resultPointCallback; + + /// + /// Initializes a new instance of the class. + /// + /// The image. + public Detector(BitMatrix image) + { + this.image = image; + } + + /// + /// Gets the image. + /// + virtual protected internal BitMatrix Image + { + get + { + return image; + } + } + + /// + /// Gets the result point callback. + /// + virtual protected internal ResultPointCallback ResultPointCallback + { + get + { + return resultPointCallback; + } + } + + /// + ///

Detects a QR Code in an image, simply.

+ ///
+ /// + /// encapsulating results of detecting a QR Code + /// + public virtual DetectorResult detect() + { + return detect(null); + } + + /// + ///

Detects a QR Code in an image, simply.

+ ///
+ /// optional hints to detector + /// + /// encapsulating results of detecting a QR Code + /// + public virtual DetectorResult detect(IDictionary hints) + { + resultPointCallback = hints == null || !hints.ContainsKey(DecodeHintType.NEED_RESULT_POINT_CALLBACK) ? null : (ResultPointCallback)hints[DecodeHintType.NEED_RESULT_POINT_CALLBACK]; + + FinderPatternFinder finder = new FinderPatternFinder(image, resultPointCallback); + FinderPatternInfo info = finder.find(hints); + if (info == null) + return null; + + return processFinderPatternInfo(info); + } + + /// + /// Processes the finder pattern info. + /// + /// The info. + /// + protected internal virtual DetectorResult processFinderPatternInfo(FinderPatternInfo info) + { + FinderPattern topLeft = info.TopLeft; + FinderPattern topRight = info.TopRight; + FinderPattern bottomLeft = info.BottomLeft; + + float moduleSize = calculateModuleSize(topLeft, topRight, bottomLeft); + if (moduleSize < 1.0f) + { + return null; + } + int dimension; + if (!computeDimension(topLeft, topRight, bottomLeft, moduleSize, out dimension)) + return null; + Internal.Version provisionalVersion = Internal.Version.getProvisionalVersionForDimension(dimension); + if (provisionalVersion == null) + return null; + int modulesBetweenFPCenters = provisionalVersion.DimensionForVersion - 7; + + AlignmentPattern alignmentPattern = null; + // Anything above version 1 has an alignment pattern + if (provisionalVersion.AlignmentPatternCenters.Length > 0) + { + + // Guess where a "bottom right" finder pattern would have been + float bottomRightX = topRight.X - topLeft.X + bottomLeft.X; + float bottomRightY = topRight.Y - topLeft.Y + bottomLeft.Y; + + // Estimate that alignment pattern is closer by 3 modules + // from "bottom right" to known top left location + //UPGRADE_WARNING: Data types in Visual C# might be different. Verify the accuracy of narrowing conversions. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1042'" + float correctionToTopLeft = 1.0f - 3.0f / (float)modulesBetweenFPCenters; + //UPGRADE_WARNING: Data types in Visual C# might be different. Verify the accuracy of narrowing conversions. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1042'" + int estAlignmentX = (int)(topLeft.X + correctionToTopLeft * (bottomRightX - topLeft.X)); + //UPGRADE_WARNING: Data types in Visual C# might be different. Verify the accuracy of narrowing conversions. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1042'" + int estAlignmentY = (int)(topLeft.Y + correctionToTopLeft * (bottomRightY - topLeft.Y)); + + // Kind of arbitrary -- expand search radius before giving up + for (int i = 4; i <= 16; i <<= 1) + { + alignmentPattern = findAlignmentInRegion(moduleSize, estAlignmentX, estAlignmentY, (float)i); + if (alignmentPattern == null) + continue; + break; + } + // If we didn't find alignment pattern... well try anyway without it + } + + PerspectiveTransform transform = createTransform(topLeft, topRight, bottomLeft, alignmentPattern, dimension); + + BitMatrix bits = sampleGrid(image, transform, dimension); + if (bits == null) + return null; + + ResultPoint[] points; + if (alignmentPattern == null) + { + points = new ResultPoint[] { bottomLeft, topLeft, topRight }; + } + else + { + points = new ResultPoint[] { bottomLeft, topLeft, topRight, alignmentPattern }; + } + return new DetectorResult(bits, points); + } + + private static PerspectiveTransform createTransform(ResultPoint topLeft, ResultPoint topRight, ResultPoint bottomLeft, ResultPoint alignmentPattern, int dimension) + { + //UPGRADE_WARNING: Data types in Visual C# might be different. Verify the accuracy of narrowing conversions. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1042'" + float dimMinusThree = (float)dimension - 3.5f; + float bottomRightX; + float bottomRightY; + float sourceBottomRightX; + float sourceBottomRightY; + if (alignmentPattern != null) + { + bottomRightX = alignmentPattern.X; + bottomRightY = alignmentPattern.Y; + sourceBottomRightX = sourceBottomRightY = dimMinusThree - 3.0f; + } + else + { + // Don't have an alignment pattern, just make up the bottom-right point + bottomRightX = (topRight.X - topLeft.X) + bottomLeft.X; + bottomRightY = (topRight.Y - topLeft.Y) + bottomLeft.Y; + sourceBottomRightX = sourceBottomRightY = dimMinusThree; + } + + return PerspectiveTransform.quadrilateralToQuadrilateral( + 3.5f, + 3.5f, + dimMinusThree, + 3.5f, + sourceBottomRightX, + sourceBottomRightY, + 3.5f, + dimMinusThree, + topLeft.X, + topLeft.Y, + topRight.X, + topRight.Y, + bottomRightX, + bottomRightY, + bottomLeft.X, + bottomLeft.Y); + } + + private static BitMatrix sampleGrid(BitMatrix image, PerspectiveTransform transform, int dimension) + { + GridSampler sampler = GridSampler.Instance; + return sampler.sampleGrid(image, dimension, dimension, transform); + } + + ///

Computes the dimension (number of modules on a size) of the QR Code based on the position + /// of the finder patterns and estimated module size.

+ ///
+ private static bool computeDimension(ResultPoint topLeft, ResultPoint topRight, ResultPoint bottomLeft, float moduleSize, out int dimension) + { + int tltrCentersDimension = MathUtils.round(ResultPoint.distance(topLeft, topRight) / moduleSize); + int tlblCentersDimension = MathUtils.round(ResultPoint.distance(topLeft, bottomLeft) / moduleSize); + dimension = ((tltrCentersDimension + tlblCentersDimension) >> 1) + 7; + switch (dimension & 0x03) + { + // mod 4 + case 0: + dimension++; + break; + // 1? do nothing + case 2: + dimension--; + break; + case 3: + return true; + } + return true; + } + + ///

Computes an average estimated module size based on estimated derived from the positions + /// of the three finder patterns.

+ ///
+ protected internal virtual float calculateModuleSize(ResultPoint topLeft, ResultPoint topRight, ResultPoint bottomLeft) + { + // Take the average + return (calculateModuleSizeOneWay(topLeft, topRight) + calculateModuleSizeOneWay(topLeft, bottomLeft)) / 2.0f; + } + + ///

Estimates module size based on two finder patterns -- it uses + /// {@link #sizeOfBlackWhiteBlackRunBothWays(int, int, int, int)} to figure the + /// width of each, measuring along the axis between their centers.

+ ///
+ private float calculateModuleSizeOneWay(ResultPoint pattern, ResultPoint otherPattern) + { + //UPGRADE_WARNING: Data types in Visual C# might be different. Verify the accuracy of narrowing conversions. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1042'" + float moduleSizeEst1 = sizeOfBlackWhiteBlackRunBothWays((int)pattern.X, (int)pattern.Y, (int)otherPattern.X, (int)otherPattern.Y); + //UPGRADE_WARNING: Data types in Visual C# might be different. Verify the accuracy of narrowing conversions. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1042'" + float moduleSizeEst2 = sizeOfBlackWhiteBlackRunBothWays((int)otherPattern.X, (int)otherPattern.Y, (int)pattern.X, (int)pattern.Y); + if (Single.IsNaN(moduleSizeEst1)) + { + return moduleSizeEst2 / 7.0f; + } + if (Single.IsNaN(moduleSizeEst2)) + { + return moduleSizeEst1 / 7.0f; + } + // Average them, and divide by 7 since we've counted the width of 3 black modules, + // and 1 white and 1 black module on either side. Ergo, divide sum by 14. + return (moduleSizeEst1 + moduleSizeEst2) / 14.0f; + } + + /// See {@link #sizeOfBlackWhiteBlackRun(int, int, int, int)}; computes the total width of + /// a finder pattern by looking for a black-white-black run from the center in the direction + /// of another point (another finder pattern center), and in the opposite direction too. + /// + private float sizeOfBlackWhiteBlackRunBothWays(int fromX, int fromY, int toX, int toY) + { + + float result = sizeOfBlackWhiteBlackRun(fromX, fromY, toX, toY); + + // Now count other way -- don't run off image though of course + float scale = 1.0f; + int otherToX = fromX - (toX - fromX); + if (otherToX < 0) + { + //UPGRADE_WARNING: Data types in Visual C# might be different. Verify the accuracy of narrowing conversions. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1042'" + scale = (float)fromX / (float)(fromX - otherToX); + otherToX = 0; + } + else if (otherToX >= image.Width) + { + //UPGRADE_WARNING: Data types in Visual C# might be different. Verify the accuracy of narrowing conversions. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1042'" + scale = (float)(image.Width - 1 - fromX) / (float)(otherToX - fromX); + otherToX = image.Width - 1; + } + //UPGRADE_WARNING: Data types in Visual C# might be different. Verify the accuracy of narrowing conversions. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1042'" + int otherToY = (int)(fromY - (toY - fromY) * scale); + + scale = 1.0f; + if (otherToY < 0) + { + //UPGRADE_WARNING: Data types in Visual C# might be different. Verify the accuracy of narrowing conversions. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1042'" + scale = (float)fromY / (float)(fromY - otherToY); + otherToY = 0; + } + else if (otherToY >= image.Height) + { + //UPGRADE_WARNING: Data types in Visual C# might be different. Verify the accuracy of narrowing conversions. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1042'" + scale = (float)(image.Height - 1 - fromY) / (float)(otherToY - fromY); + otherToY = image.Height - 1; + } + //UPGRADE_WARNING: Data types in Visual C# might be different. Verify the accuracy of narrowing conversions. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1042'" + otherToX = (int)(fromX + (otherToX - fromX) * scale); + + result += sizeOfBlackWhiteBlackRun(fromX, fromY, otherToX, otherToY); + return result - 1.0f; // -1 because we counted the middle pixel twice + } + + ///

This method traces a line from a point in the image, in the direction towards another point. + /// It begins in a black region, and keeps going until it finds white, then black, then white again. + /// It reports the distance from the start to this point.

+ /// + ///

This is used when figuring out how wide a finder pattern is, when the finder pattern + /// may be skewed or rotated.

+ ///
+ private float sizeOfBlackWhiteBlackRun(int fromX, int fromY, int toX, int toY) + { + // Mild variant of Bresenham's algorithm; + // see http://en.wikipedia.org/wiki/Bresenham's_line_algorithm + bool steep = Math.Abs(toY - fromY) > Math.Abs(toX - fromX); + if (steep) + { + int temp = fromX; + fromX = fromY; + fromY = temp; + temp = toX; + toX = toY; + toY = temp; + } + + int dx = Math.Abs(toX - fromX); + int dy = Math.Abs(toY - fromY); + int error = -dx >> 1; + int xstep = fromX < toX ? 1 : -1; + int ystep = fromY < toY ? 1 : -1; + + // In black pixels, looking for white, first or second time. + int state = 0; + // Loop up until x == toX, but not beyond + int xLimit = toX + xstep; + for (int x = fromX, y = fromY; x != xLimit; x += xstep) + { + int realX = steep ? y : x; + int realY = steep ? x : y; + + // Does current pixel mean we have moved white to black or vice versa? + // Scanning black in state 0,2 and white in state 1, so if we find the wrong + // color, advance to next state or end if we are in state 2 already + if ((state == 1) == image[realX, realY]) + { + if (state == 2) + { + return MathUtils.distance(x, y, fromX, fromY); + } + state++; + } + error += dy; + if (error > 0) + { + if (y == toY) + { + + + break; + } + y += ystep; + error -= dx; + } + } + // Found black-white-black; give the benefit of the doubt that the next pixel outside the image + // is "white" so this last point at (toX+xStep,toY) is the right ending. This is really a + // small approximation; (toX+xStep,toY+yStep) might be really correct. Ignore this. + if (state == 2) + { + return MathUtils.distance(toX + xstep, toY, fromX, fromY); + } + // else we didn't find even black-white-black; no estimate is really possible + return Single.NaN; + + } + + /// + ///

Attempts to locate an alignment pattern in a limited region of the image, which is + /// guessed to contain it. This method uses {@link AlignmentPattern}.

+ ///
+ /// estimated module size so far + /// x coordinate of center of area probably containing alignment pattern + /// y coordinate of above + /// number of pixels in all directions to search from the center + /// + /// if found, or null otherwise + /// + protected AlignmentPattern findAlignmentInRegion(float overallEstModuleSize, int estAlignmentX, int estAlignmentY, float allowanceFactor) + { + // Look for an alignment pattern (3 modules in size) around where it + // should be + //UPGRADE_WARNING: Data types in Visual C# might be different. Verify the accuracy of narrowing conversions. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1042'" + int allowance = (int)(allowanceFactor * overallEstModuleSize); + int alignmentAreaLeftX = Math.Max(0, estAlignmentX - allowance); + int alignmentAreaRightX = Math.Min(image.Width - 1, estAlignmentX + allowance); + if (alignmentAreaRightX - alignmentAreaLeftX < overallEstModuleSize * 3) + { + return null; + } + + int alignmentAreaTopY = Math.Max(0, estAlignmentY - allowance); + int alignmentAreaBottomY = Math.Min(image.Height - 1, estAlignmentY + allowance); + + var alignmentFinder = new AlignmentPatternFinder( + image, + alignmentAreaLeftX, + alignmentAreaTopY, + alignmentAreaRightX - alignmentAreaLeftX, + alignmentAreaBottomY - alignmentAreaTopY, + overallEstModuleSize, + resultPointCallback); + + return alignmentFinder.find(); + } + } +} \ No newline at end of file diff --git a/shadowsocks-csharp/3rd/zxing/qrcode/detector/FinderPattern.cs b/shadowsocks-csharp/3rd/zxing/qrcode/detector/FinderPattern.cs new file mode 100755 index 00000000..80469bf7 --- /dev/null +++ b/shadowsocks-csharp/3rd/zxing/qrcode/detector/FinderPattern.cs @@ -0,0 +1,107 @@ +/* +* 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 +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +using System; + +namespace ZXing.QrCode.Internal +{ + /// + ///

Encapsulates a finder pattern, which are the three square patterns found in + /// the corners of QR Codes. It also encapsulates a count of similar finder patterns, + /// as a convenience to the finder's bookkeeping.

+ ///
+ /// Sean Owen + public sealed class FinderPattern : ResultPoint + { + private readonly float estimatedModuleSize; + private int count; + + internal FinderPattern(float posX, float posY, float estimatedModuleSize) + : this(posX, posY, estimatedModuleSize, 1) + { + this.estimatedModuleSize = estimatedModuleSize; + this.count = 1; + } + + internal FinderPattern(float posX, float posY, float estimatedModuleSize, int count) + : base(posX, posY) + { + this.estimatedModuleSize = estimatedModuleSize; + this.count = count; + } + + /// + /// Gets the size of the estimated module. + /// + /// + /// The size of the estimated module. + /// + public float EstimatedModuleSize + { + get + { + return estimatedModuleSize; + } + } + + internal int Count + { + get + { + return count; + } + } + + /* + internal void incrementCount() + { + this.count++; + } + */ + + ///

Determines if this finder pattern "about equals" a finder pattern at the stated + /// position and size -- meaning, it is at nearly the same center with nearly the same size.

+ ///
+ internal bool aboutEquals(float moduleSize, float i, float j) + { + if (Math.Abs(i - Y) <= moduleSize && Math.Abs(j - X) <= moduleSize) + { + float moduleSizeDiff = Math.Abs(moduleSize - estimatedModuleSize); + return moduleSizeDiff <= 1.0f || moduleSizeDiff <= estimatedModuleSize; + + } + return false; + } + + /// + /// Combines this object's current estimate of a finder pattern position and module size + /// with a new estimate. It returns a new {@code FinderPattern} containing a weighted average + /// based on count. + /// + /// The i. + /// The j. + /// New size of the module. + /// + internal FinderPattern combineEstimate(float i, float j, float newModuleSize) + { + int combinedCount = count + 1; + float combinedX = (count * X + j) / combinedCount; + float combinedY = (count * Y + i) / combinedCount; + float combinedModuleSize = (count * estimatedModuleSize + newModuleSize) / combinedCount; + return new FinderPattern(combinedX, combinedY, combinedModuleSize, combinedCount); + } + } +} \ No newline at end of file diff --git a/shadowsocks-csharp/3rd/zxing/qrcode/detector/FinderPatternFinder.cs b/shadowsocks-csharp/3rd/zxing/qrcode/detector/FinderPatternFinder.cs new file mode 100755 index 00000000..7b4c5113 --- /dev/null +++ b/shadowsocks-csharp/3rd/zxing/qrcode/detector/FinderPatternFinder.cs @@ -0,0 +1,808 @@ +/* +* 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 +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +using System; +using System.Collections.Generic; + +using ZXing.Common; + +namespace ZXing.QrCode.Internal +{ + /// + ///

This class attempts to find finder patterns in a QR Code. Finder patterns are the square + /// markers at three corners of a QR Code.

+ /// + ///

This class is thread-safe but not reentrant. Each thread must allocate its own object. + ///

+ /// Sean Owen + public class FinderPatternFinder + { + private const int CENTER_QUORUM = 2; + /// + /// 1 pixel/module times 3 modules/center + /// + protected internal const int MIN_SKIP = 3; + /// + /// support up to version 10 for mobile clients + /// + protected internal const int MAX_MODULES = 57; + private const int INTEGER_MATH_SHIFT = 8; + + private readonly BitMatrix image; + private List possibleCenters; + private bool hasSkipped; + private readonly int[] crossCheckStateCount; + private readonly ResultPointCallback resultPointCallback; + + /// + ///

Creates a finder that will search the image for three finder patterns.

+ ///
+ /// image to search + public FinderPatternFinder(BitMatrix image) + : this(image, null) + { + } + + /// + /// Initializes a new instance of the class. + /// + /// The image. + /// The result point callback. + public FinderPatternFinder(BitMatrix image, ResultPointCallback resultPointCallback) + { + this.image = image; + this.possibleCenters = new List(); + this.crossCheckStateCount = new int[5]; + this.resultPointCallback = resultPointCallback; + } + + /// + /// Gets the image. + /// + virtual protected internal BitMatrix Image + { + get + { + return image; + } + } + + /// + /// Gets the possible centers. + /// + virtual protected internal List PossibleCenters + { + get + { + return possibleCenters; + } + } + + internal virtual FinderPatternInfo find(IDictionary hints) + { + bool tryHarder = hints != null && hints.ContainsKey(DecodeHintType.TRY_HARDER); + bool pureBarcode = hints != null && hints.ContainsKey(DecodeHintType.PURE_BARCODE); + int maxI = image.Height; + int maxJ = image.Width; + // We are looking for black/white/black/white/black modules in + // 1:1:3:1:1 ratio; this tracks the number of such modules seen so far + + // Let's assume that the maximum version QR Code we support takes up 1/4 the height of the + // image, and then account for the center being 3 modules in size. This gives the smallest + // number of pixels the center could be, so skip this often. When trying harder, look for all + // QR versions regardless of how dense they are. + int iSkip = (3 * maxI) / (4 * MAX_MODULES); + if (iSkip < MIN_SKIP || tryHarder) + { + iSkip = MIN_SKIP; + } + + bool done = false; + int[] stateCount = new int[5]; + for (int i = iSkip - 1; i < maxI && !done; i += iSkip) + { + // Get a row of black/white values + stateCount[0] = 0; + stateCount[1] = 0; + stateCount[2] = 0; + stateCount[3] = 0; + stateCount[4] = 0; + int currentState = 0; + for (int j = 0; j < maxJ; j++) + { + if (image[j, i]) + { + // Black pixel + if ((currentState & 1) == 1) + { + // Counting white pixels + currentState++; + } + stateCount[currentState]++; + } + else + { + // White pixel + if ((currentState & 1) == 0) + { + // Counting black pixels + if (currentState == 4) + { + // A winner? + if (foundPatternCross(stateCount)) + { + // Yes + bool confirmed = handlePossibleCenter(stateCount, i, j, pureBarcode); + if (confirmed) + { + // Start examining every other line. Checking each line turned out to be too + // expensive and didn't improve performance. + iSkip = 2; + if (hasSkipped) + { + done = haveMultiplyConfirmedCenters(); + } + else + { + int rowSkip = findRowSkip(); + if (rowSkip > stateCount[2]) + { + // Skip rows between row of lower confirmed center + // and top of presumed third confirmed center + // but back up a bit to get a full chance of detecting + // it, entire width of center of finder pattern + + // Skip by rowSkip, but back off by stateCount[2] (size of last center + // of pattern we saw) to be conservative, and also back off by iSkip which + // is about to be re-added + i += rowSkip - stateCount[2] - iSkip; + j = maxJ - 1; + } + } + } + else + { + stateCount[0] = stateCount[2]; + stateCount[1] = stateCount[3]; + stateCount[2] = stateCount[4]; + stateCount[3] = 1; + stateCount[4] = 0; + currentState = 3; + continue; + } + // Clear state to start looking again + currentState = 0; + stateCount[0] = 0; + stateCount[1] = 0; + stateCount[2] = 0; + stateCount[3] = 0; + stateCount[4] = 0; + } + else + { + // No, shift counts back by two + stateCount[0] = stateCount[2]; + stateCount[1] = stateCount[3]; + stateCount[2] = stateCount[4]; + stateCount[3] = 1; + stateCount[4] = 0; + currentState = 3; + } + } + else + { + stateCount[++currentState]++; + } + } + else + { + // Counting white pixels + stateCount[currentState]++; + } + } + } + if (foundPatternCross(stateCount)) + { + bool confirmed = handlePossibleCenter(stateCount, i, maxJ, pureBarcode); + if (confirmed) + { + iSkip = stateCount[0]; + if (hasSkipped) + { + // Found a third one + done = haveMultiplyConfirmedCenters(); + } + } + } + } + + FinderPattern[] patternInfo = selectBestPatterns(); + if (patternInfo == null) + return null; + + ResultPoint.orderBestPatterns(patternInfo); + + return new FinderPatternInfo(patternInfo); + } + + /// Given a count of black/white/black/white/black pixels just seen and an end position, + /// figures the location of the center of this run. + /// + private static float? centerFromEnd(int[] stateCount, int end) + { + var result = (end - stateCount[4] - stateCount[3]) - stateCount[2] / 2.0f; + if (Single.IsNaN(result)) + return null; + return result; + } + + /// count of black/white/black/white/black pixels just read + /// + /// true iff the proportions of the counts is close enough to the 1/1/3/1/1 ratios + /// used by finder patterns to be considered a match + /// + protected internal static bool foundPatternCross(int[] stateCount) + { + int totalModuleSize = 0; + for (int i = 0; i < 5; i++) + { + int count = stateCount[i]; + if (count == 0) + { + return false; + } + totalModuleSize += count; + } + if (totalModuleSize < 7) + { + return false; + } + int moduleSize = (totalModuleSize << INTEGER_MATH_SHIFT) / 7; + int maxVariance = moduleSize / 2; + // Allow less than 50% variance from 1-1-3-1-1 proportions + return Math.Abs(moduleSize - (stateCount[0] << INTEGER_MATH_SHIFT)) < maxVariance && + Math.Abs(moduleSize - (stateCount[1] << INTEGER_MATH_SHIFT)) < maxVariance && + Math.Abs(3 * moduleSize - (stateCount[2] << INTEGER_MATH_SHIFT)) < 3 * maxVariance && + Math.Abs(moduleSize - (stateCount[3] << INTEGER_MATH_SHIFT)) < maxVariance && + Math.Abs(moduleSize - (stateCount[4] << INTEGER_MATH_SHIFT)) < maxVariance; + } + + private int[] CrossCheckStateCount + { + get + { + crossCheckStateCount[0] = 0; + crossCheckStateCount[1] = 0; + crossCheckStateCount[2] = 0; + crossCheckStateCount[3] = 0; + crossCheckStateCount[4] = 0; + return crossCheckStateCount; + } + } + + /// + /// After a vertical and horizontal scan finds a potential finder pattern, this method + /// "cross-cross-cross-checks" by scanning down diagonally through the center of the possible + /// finder pattern to see if the same proportion is detected. + /// + /// row where a finder pattern was detected + /// center of the section that appears to cross a finder pattern + /// maximum reasonable number of modules that should be observed in any reading state, based on the results of the horizontal scan + /// The original state count total. + /// true if proportions are withing expected limits + private bool crossCheckDiagonal(int startI, int centerJ, int maxCount, int originalStateCountTotal) + { + int maxI = image.Height; + int maxJ = image.Width; + int[] stateCount = CrossCheckStateCount; + + // Start counting up, left from center finding black center mass + int i = 0; + while (startI - i >= 0 && image[centerJ - i, startI - i]) + { + stateCount[2]++; + i++; + } + + if ((startI - i < 0) || (centerJ - i < 0)) + { + return false; + } + + // Continue up, left finding white space + while ((startI - i >= 0) && (centerJ - i >= 0) && !image[centerJ - i, startI - i] && stateCount[1] <= maxCount) + { + stateCount[1]++; + i++; + } + + // If already too many modules in this state or ran off the edge: + if ((startI - i < 0) || (centerJ - i < 0) || stateCount[1] > maxCount) + { + return false; + } + + // Continue up, left finding black border + while ((startI - i >= 0) && (centerJ - i >= 0) && image[centerJ - i, startI - i] && stateCount[0] <= maxCount) + { + stateCount[0]++; + i++; + } + if (stateCount[0] > maxCount) + { + return false; + } + + // Now also count down, right from center + i = 1; + while ((startI + i < maxI) && (centerJ + i < maxJ) && image[centerJ + i, startI + i]) + { + stateCount[2]++; + i++; + } + + // Ran off the edge? + if ((startI + i >= maxI) || (centerJ + i >= maxJ)) + { + return false; + } + + while ((startI + i < maxI) && (centerJ + i < maxJ) && !image[centerJ + i, startI + i] && stateCount[3] < maxCount) + { + stateCount[3]++; + i++; + } + + if ((startI + i >= maxI) || (centerJ + i >= maxJ) || stateCount[3] >= maxCount) + { + return false; + } + + while ((startI + i < maxI) && (centerJ + i < maxJ) && image[centerJ + i, startI + i] && stateCount[4] < maxCount) + { + stateCount[4]++; + i++; + } + + if (stateCount[4] >= maxCount) + { + return false; + } + + // If we found a finder-pattern-like section, but its size is more than 100% different than + // the original, assume it's a false positive + int stateCountTotal = stateCount[0] + stateCount[1] + stateCount[2] + stateCount[3] + stateCount[4]; + return Math.Abs(stateCountTotal - originalStateCountTotal) < 2*originalStateCountTotal && + foundPatternCross(stateCount); + } + + /// + ///

After a horizontal scan finds a potential finder pattern, this method + /// "cross-checks" by scanning down vertically through the center of the possible + /// finder pattern to see if the same proportion is detected.

+ ///
+ /// row where a finder pattern was detected + /// center of the section that appears to cross a finder pattern + /// maximum reasonable number of modules that should be + /// observed in any reading state, based on the results of the horizontal scan + /// The original state count total. + /// + /// vertical center of finder pattern, or null if not found + /// + private float? crossCheckVertical(int startI, int centerJ, int maxCount, int originalStateCountTotal) + { + int maxI = image.Height; + int[] stateCount = CrossCheckStateCount; + + // Start counting up from center + int i = startI; + while (i >= 0 && image[centerJ, i]) + { + stateCount[2]++; + i--; + } + if (i < 0) + { + return null; + } + while (i >= 0 && !image[centerJ, i] && stateCount[1] <= maxCount) + { + stateCount[1]++; + i--; + } + // If already too many modules in this state or ran off the edge: + if (i < 0 || stateCount[1] > maxCount) + { + return null; + } + while (i >= 0 && image[centerJ, i] && stateCount[0] <= maxCount) + { + stateCount[0]++; + i--; + } + if (stateCount[0] > maxCount) + { + return null; + } + + // Now also count down from center + i = startI + 1; + while (i < maxI && image[centerJ, i]) + { + stateCount[2]++; + i++; + } + if (i == maxI) + { + return null; + } + while (i < maxI && !image[centerJ, i] && stateCount[3] < maxCount) + { + stateCount[3]++; + i++; + } + if (i == maxI || stateCount[3] >= maxCount) + { + return null; + } + while (i < maxI && image[centerJ, i] && stateCount[4] < maxCount) + { + stateCount[4]++; + i++; + } + if (stateCount[4] >= maxCount) + { + return null; + } + + // If we found a finder-pattern-like section, but its size is more than 40% different than + // the original, assume it's a false positive + int stateCountTotal = stateCount[0] + stateCount[1] + stateCount[2] + stateCount[3] + stateCount[4]; + if (5 * Math.Abs(stateCountTotal - originalStateCountTotal) >= 2 * originalStateCountTotal) + { + return null; + } + + return foundPatternCross(stateCount) ? centerFromEnd(stateCount, i) : null; + } + + ///

Like {@link #crossCheckVertical(int, int, int, int)}, and in fact is basically identical, + /// except it reads horizontally instead of vertically. This is used to cross-cross + /// check a vertical cross check and locate the real center of the alignment pattern.

+ ///
+ private float? crossCheckHorizontal(int startJ, int centerI, int maxCount, int originalStateCountTotal) + { + int maxJ = image.Width; + int[] stateCount = CrossCheckStateCount; + + int j = startJ; + while (j >= 0 && image[j, centerI]) + { + stateCount[2]++; + j--; + } + if (j < 0) + { + return null; + } + while (j >= 0 && !image[j, centerI] && stateCount[1] <= maxCount) + { + stateCount[1]++; + j--; + } + if (j < 0 || stateCount[1] > maxCount) + { + return null; + } + while (j >= 0 && image[j, centerI] && stateCount[0] <= maxCount) + { + stateCount[0]++; + j--; + } + if (stateCount[0] > maxCount) + { + return null; + } + + j = startJ + 1; + while (j < maxJ && image[j, centerI]) + { + stateCount[2]++; + j++; + } + if (j == maxJ) + { + return null; + } + while (j < maxJ && !image[j, centerI] && stateCount[3] < maxCount) + { + stateCount[3]++; + j++; + } + if (j == maxJ || stateCount[3] >= maxCount) + { + return null; + } + while (j < maxJ && image[j, centerI] && stateCount[4] < maxCount) + { + stateCount[4]++; + j++; + } + if (stateCount[4] >= maxCount) + { + return null; + } + + // If we found a finder-pattern-like section, but its size is significantly different than + // the original, assume it's a false positive + int stateCountTotal = stateCount[0] + stateCount[1] + stateCount[2] + stateCount[3] + stateCount[4]; + if (5 * Math.Abs(stateCountTotal - originalStateCountTotal) >= originalStateCountTotal) + { + return null; + } + + return foundPatternCross(stateCount) ? centerFromEnd(stateCount, j) : null; + } + + /// + ///

This is called when a horizontal scan finds a possible alignment pattern. It will + /// cross check with a vertical scan, and if successful, will, ah, cross-cross-check + /// with another horizontal scan. This is needed primarily to locate the real horizontal + /// center of the pattern in cases of extreme skew. + /// And then we cross-cross-cross check with another diagonal scan.

+ /// If that succeeds the finder pattern location is added to a list that tracks + /// the number of times each location has been nearly-matched as a finder pattern. + /// Each additional find is more evidence that the location is in fact a finder + /// pattern center + ///
+ /// reading state module counts from horizontal scan + /// row where finder pattern may be found + /// end of possible finder pattern in row + /// if set to true [pure barcode]. + /// + /// true if a finder pattern candidate was found this time + /// + protected bool handlePossibleCenter(int[] stateCount, int i, int j, bool pureBarcode) + { + int stateCountTotal = stateCount[0] + stateCount[1] + stateCount[2] + stateCount[3] + + stateCount[4]; + float? centerJ = centerFromEnd(stateCount, j); + if (centerJ == null) + return false; + float? centerI = crossCheckVertical(i, (int)centerJ.Value, stateCount[2], stateCountTotal); + if (centerI != null) + { + // Re-cross check + centerJ = crossCheckHorizontal((int)centerJ.Value, (int)centerI.Value, stateCount[2], stateCountTotal); + if (centerJ != null && + (!pureBarcode || crossCheckDiagonal((int) centerI, (int) centerJ, stateCount[2], stateCountTotal))) + { + float estimatedModuleSize = stateCountTotal / 7.0f; + bool found = false; + for (int index = 0; index < possibleCenters.Count; index++) + { + var center = possibleCenters[index]; + // Look for about the same center and module size: + if (center.aboutEquals(estimatedModuleSize, centerI.Value, centerJ.Value)) + { + possibleCenters.RemoveAt(index); + possibleCenters.Insert(index, center.combineEstimate(centerI.Value, centerJ.Value, estimatedModuleSize)); + + found = true; + break; + } + } + if (!found) + { + var point = new FinderPattern(centerJ.Value, centerI.Value, estimatedModuleSize); + + possibleCenters.Add(point); + if (resultPointCallback != null) + { + + resultPointCallback(point); + } + } + return true; + } + } + return false; + } + + /// number of rows we could safely skip during scanning, based on the first + /// two finder patterns that have been located. In some cases their position will + /// allow us to infer that the third pattern must lie below a certain point farther + /// down in the image. + /// + private int findRowSkip() + { + int max = possibleCenters.Count; + if (max <= 1) + { + return 0; + } + ResultPoint firstConfirmedCenter = null; + foreach (var center in possibleCenters) + { + if (center.Count >= CENTER_QUORUM) + { + if (firstConfirmedCenter == null) + { + firstConfirmedCenter = center; + } + else + { + // We have two confirmed centers + // How far down can we skip before resuming looking for the next + // pattern? In the worst case, only the difference between the + // difference in the x / y coordinates of the two centers. + // This is the case where you find top left last. + hasSkipped = true; + //UPGRADE_WARNING: Data types in Visual C# might be different. Verify the accuracy of narrowing conversions. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1042'" + return (int)(Math.Abs(firstConfirmedCenter.X - center.X) - Math.Abs(firstConfirmedCenter.Y - center.Y)) / 2; + } + } + } + return 0; + } + + /// true iff we have found at least 3 finder patterns that have been detected + /// at least {@link #CENTER_QUORUM} times each, and, the estimated module size of the + /// candidates is "pretty similar" + /// + private bool haveMultiplyConfirmedCenters() + { + int confirmedCount = 0; + float totalModuleSize = 0.0f; + int max = possibleCenters.Count; + foreach (var pattern in possibleCenters) + { + if (pattern.Count >= CENTER_QUORUM) + { + confirmedCount++; + totalModuleSize += pattern.EstimatedModuleSize; + } + } + if (confirmedCount < 3) + { + return false; + } + // OK, we have at least 3 confirmed centers, but, it's possible that one is a "false positive" + // and that we need to keep looking. We detect this by asking if the estimated module sizes + // vary too much. We arbitrarily say that when the total deviation from average exceeds + // 5% of the total module size estimates, it's too much. + float average = totalModuleSize / max; + float totalDeviation = 0.0f; + for (int i = 0; i < max; i++) + { + var pattern = possibleCenters[i]; + totalDeviation += Math.Abs(pattern.EstimatedModuleSize - average); + } + return totalDeviation <= 0.05f * totalModuleSize; + } + + /// the 3 best {@link FinderPattern}s from our list of candidates. The "best" are + /// those that have been detected at least {@link #CENTER_QUORUM} times, and whose module + /// size differs from the average among those patterns the least + /// + private FinderPattern[] selectBestPatterns() + { + int startSize = possibleCenters.Count; + if (startSize < 3) + { + // Couldn't find enough finder patterns + return null; + } + + // Filter outlier possibilities whose module size is too different + if (startSize > 3) + { + // But we can only afford to do so if we have at least 4 possibilities to choose from + float totalModuleSize = 0.0f; + float square = 0.0f; + foreach (var center in possibleCenters) + { + float size = center.EstimatedModuleSize; + totalModuleSize += size; + square += size * size; + } + float average = totalModuleSize / startSize; + float stdDev = (float)Math.Sqrt(square / startSize - average * average); + + possibleCenters.Sort(new FurthestFromAverageComparator(average)); + + float limit = Math.Max(0.2f * average, stdDev); + + for (int i = 0; i < possibleCenters.Count && possibleCenters.Count > 3; i++) + { + FinderPattern pattern = possibleCenters[i]; + if (Math.Abs(pattern.EstimatedModuleSize - average) > limit) + { + possibleCenters.RemoveAt(i); + i--; + } + } + } + + if (possibleCenters.Count > 3) + { + // Throw away all but those first size candidate points we found. + + float totalModuleSize = 0.0f; + foreach (var possibleCenter in possibleCenters) + { + totalModuleSize += possibleCenter.EstimatedModuleSize; + } + + float average = totalModuleSize / possibleCenters.Count; + + possibleCenters.Sort(new CenterComparator(average)); + + //possibleCenters.subList(3, possibleCenters.Count).clear(); + possibleCenters = possibleCenters.GetRange(0, 3); + } + + return new[] + { + possibleCenters[0], + possibleCenters[1], + possibleCenters[2] + }; + } + + /// + /// Orders by furthest from average + /// + private sealed class FurthestFromAverageComparator : IComparer + { + private readonly float average; + + public FurthestFromAverageComparator(float f) + { + average = f; + } + + public int Compare(FinderPattern x, FinderPattern y) + { + float dA = Math.Abs(y.EstimatedModuleSize - average); + float dB = Math.Abs(x.EstimatedModuleSize - average); + return dA < dB ? -1 : dA == dB ? 0 : 1; + } + } + + ///

Orders by {@link FinderPattern#getCount()}, descending.

+ private sealed class CenterComparator : IComparer + { + private readonly float average; + + public CenterComparator(float f) + { + average = f; + } + + public int Compare(FinderPattern x, FinderPattern y) + { + if (y.Count == x.Count) + { + float dA = Math.Abs(y.EstimatedModuleSize - average); + float dB = Math.Abs(x.EstimatedModuleSize - average); + return dA < dB ? 1 : dA == dB ? 0 : -1; + } + return y.Count - x.Count; + } + } + } +} \ No newline at end of file diff --git a/shadowsocks-csharp/3rd/zxing/qrcode/detector/FinderPatternInfo.cs b/shadowsocks-csharp/3rd/zxing/qrcode/detector/FinderPatternInfo.cs new file mode 100755 index 00000000..982b755a --- /dev/null +++ b/shadowsocks-csharp/3rd/zxing/qrcode/detector/FinderPatternInfo.cs @@ -0,0 +1,74 @@ +/* +* 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 +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +namespace ZXing.QrCode.Internal +{ + /// + ///

Encapsulates information about finder patterns in an image, including the location of + /// the three finder patterns, and their estimated module size.

+ ///
+ /// Sean Owen + public sealed class FinderPatternInfo + { + private readonly FinderPattern bottomLeft; + private readonly FinderPattern topLeft; + private readonly FinderPattern topRight; + + /// + /// Initializes a new instance of the class. + /// + /// The pattern centers. + public FinderPatternInfo(FinderPattern[] patternCenters) + { + this.bottomLeft = patternCenters[0]; + this.topLeft = patternCenters[1]; + this.topRight = patternCenters[2]; + } + + /// + /// Gets the bottom left. + /// + public FinderPattern BottomLeft + { + get + { + return bottomLeft; + } + } + + /// + /// Gets the top left. + /// + public FinderPattern TopLeft + { + get + { + return topLeft; + } + } + + /// + /// Gets the top right. + /// + public FinderPattern TopRight + { + get + { + return topRight; + } + } + } +} \ No newline at end of file diff --git a/shadowsocks-csharp/3rd/zxing/BlockPair.cs b/shadowsocks-csharp/3rd/zxing/qrcode/encoder/BlockPair.cs similarity index 100% rename from shadowsocks-csharp/3rd/zxing/BlockPair.cs rename to shadowsocks-csharp/3rd/zxing/qrcode/encoder/BlockPair.cs diff --git a/shadowsocks-csharp/3rd/zxing/ByteMatrix.cs b/shadowsocks-csharp/3rd/zxing/qrcode/encoder/ByteMatrix.cs similarity index 100% rename from shadowsocks-csharp/3rd/zxing/ByteMatrix.cs rename to shadowsocks-csharp/3rd/zxing/qrcode/encoder/ByteMatrix.cs diff --git a/shadowsocks-csharp/3rd/zxing/Encoder.cs b/shadowsocks-csharp/3rd/zxing/qrcode/encoder/Encoder.cs similarity index 67% rename from shadowsocks-csharp/3rd/zxing/Encoder.cs rename to shadowsocks-csharp/3rd/zxing/qrcode/encoder/Encoder.cs index ae9996e9..32826a00 100755 --- a/shadowsocks-csharp/3rd/zxing/Encoder.cs +++ b/shadowsocks-csharp/3rd/zxing/qrcode/encoder/Encoder.cs @@ -30,6 +30,15 @@ namespace ZXing.QrCode.Internal public static class Encoder { + // The original table is defined in the table 5 of JISX0510:2004 (p.19). + private static readonly int[] ALPHANUMERIC_TABLE = { + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 0x00-0x0f + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 0x10-0x1f + 36, -1, -1, -1, 37, 38, -1, -1, -1, -1, 39, 40, -1, 41, 42, 43, // 0x20-0x2f + 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 44, -1, -1, -1, -1, -1, // 0x30-0x3f + -1, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, // 0x40-0x4f + 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, -1, -1, -1, -1, -1, // 0x50-0x5f + }; internal static String DEFAULT_BYTE_MODE_ENCODING = "ISO-8859-1"; @@ -52,15 +61,32 @@ namespace ZXing.QrCode.Internal /// Note that there is no way to encode bytes in MODE_KANJI. We might want to add EncodeWithMode() /// with which clients can specify the encoding mode. For now, we don't need the functionality. ///
- /// text to encode - /// error correction level to use - /// representing the encoded QR code + /// The content. + /// The ec level. public static QRCode encode(String content, ErrorCorrectionLevel ecLevel) + { + return encode(content, ecLevel, null); + } + + /// + /// Encodes the specified content. + /// + /// The content. + /// The ec level. + /// The hints. + /// + public static QRCode encode(String content, + ErrorCorrectionLevel ecLevel, + IDictionary hints) { // Determine what character encoding has been specified by the caller, if any #if !SILVERLIGHT || WINDOWS_PHONE - String encoding = DEFAULT_BYTE_MODE_ENCODING; - //bool generateECI = !DEFAULT_BYTE_MODE_ENCODING.Equals(encoding); + String encoding = hints == null || !hints.ContainsKey(EncodeHintType.CHARACTER_SET) ? null : (String)hints[EncodeHintType.CHARACTER_SET]; + if (encoding == null) + { + encoding = DEFAULT_BYTE_MODE_ENCODING; + } + bool generateECI = !DEFAULT_BYTE_MODE_ENCODING.Equals(encoding); #else // Silverlight supports only UTF-8 and UTF-16 out-of-the-box const string encoding = "UTF-8"; @@ -71,27 +97,12 @@ namespace ZXing.QrCode.Internal // Pick an encoding mode appropriate for the content. Note that this will not attempt to use // multiple modes / segments even if that were more efficient. Twould be nice. - Mode mode = Mode.BYTE; + Mode mode = chooseMode(content, encoding); // This will store the header information, like mode and // length, as well as "header" segments like an ECI segment. BitArray headerBits = new BitArray(); - /* - // Append ECI segment if applicable - if (mode == Mode.BYTE && generateECI) - { - CharacterSetECI eci = CharacterSetECI.getCharacterSetECIByName(encoding); - if (eci != null) - { - var eciIsExplicitDisabled = (hints != null && hints.ContainsKey(EncodeHintType.DISABLE_ECI) ? (bool)hints[EncodeHintType.DISABLE_ECI] : false); - if (!eciIsExplicitDisabled) - { - appendECI(eci, headerBits); - } - } - } - * */ // (With ECI in place,) Write the mode marker appendModeInfo(mode, headerBits); @@ -156,6 +167,106 @@ namespace ZXing.QrCode.Internal return qrCode; } + /// + /// Gets the alphanumeric code. + /// + /// The code. + /// the code point of the table used in alphanumeric mode or + /// -1 if there is no corresponding code in the table. + internal static int getAlphanumericCode(int code) + { + if (code < ALPHANUMERIC_TABLE.Length) + { + return ALPHANUMERIC_TABLE[code]; + } + return -1; + } + + /// + /// Chooses the mode. + /// + /// The content. + /// + public static Mode chooseMode(String content) + { + return chooseMode(content, null); + } + + /// + /// Choose the best mode by examining the content. Note that 'encoding' is used as a hint; + /// if it is Shift_JIS, and the input is only double-byte Kanji, then we return {@link Mode#KANJI}. + /// + /// The content. + /// The encoding. + /// + private static Mode chooseMode(String content, String encoding) + { + if ("Shift_JIS".Equals(encoding)) + { + + // Choose Kanji mode if all input are double-byte characters + return isOnlyDoubleByteKanji(content) ? Mode.KANJI : Mode.BYTE; + } + bool hasNumeric = false; + bool hasAlphanumeric = false; + for (int i = 0; i < content.Length; ++i) + { + char c = content[i]; + if (c >= '0' && c <= '9') + { + hasNumeric = true; + } + else if (getAlphanumericCode(c) != -1) + { + hasAlphanumeric = true; + } + else + { + return Mode.BYTE; + } + } + if (hasAlphanumeric) + { + + return Mode.ALPHANUMERIC; + } + if (hasNumeric) + { + + return Mode.NUMERIC; + } + return Mode.BYTE; + } + + private static bool isOnlyDoubleByteKanji(String content) + { + byte[] bytes; + try + { + bytes = Encoding.GetEncoding("Shift_JIS").GetBytes(content); + } + catch (Exception ) + { + return false; + } + int length = bytes.Length; + if (length % 2 != 0) + { + return false; + } + for (int i = 0; i < length; i += 2) + { + + + int byte1 = bytes[i] & 0xFF; + if ((byte1 < 0x81 || byte1 > 0x9F) && (byte1 < 0xE0 || byte1 > 0xEB)) + { + + return false; + } + } + return true; + } private static int chooseMaskPattern(BitArray bits, ErrorCorrectionLevel ecLevel, @@ -199,7 +310,7 @@ namespace ZXing.QrCode.Internal return version; } } - throw new Exception("Data too big"); + throw new WriterException("Data too big"); } /// @@ -212,7 +323,7 @@ namespace ZXing.QrCode.Internal int capacity = numDataBytes << 3; if (bits.Size > capacity) { - throw new Exception("data bits cannot fit in the QR Code" + bits.Size + " > " + + throw new WriterException("data bits cannot fit in the QR Code" + bits.Size + " > " + capacity); } for (int i = 0; i < 4 && bits.Size < capacity; ++i) @@ -237,7 +348,7 @@ namespace ZXing.QrCode.Internal } if (bits.Size != capacity) { - throw new Exception("Bits size does not equal capacity"); + throw new WriterException("Bits size does not equal capacity"); } } @@ -261,7 +372,7 @@ namespace ZXing.QrCode.Internal { if (blockID >= numRSBlocks) { - throw new Exception("Block ID too large"); + throw new WriterException("Block ID too large"); } // numRsBlocksInGroup2 = 196 % 5 = 1 int numRsBlocksInGroup2 = numTotalBytes % numRSBlocks; @@ -284,13 +395,13 @@ namespace ZXing.QrCode.Internal if (numEcBytesInGroup1 != numEcBytesInGroup2) { - throw new Exception("EC bytes mismatch"); + throw new WriterException("EC bytes mismatch"); } // 5 = 4 + 1. if (numRSBlocks != numRsBlocksInGroup1 + numRsBlocksInGroup2) { - throw new Exception("RS blocks mismatch"); + throw new WriterException("RS blocks mismatch"); } // 196 = (13 + 26) * 4 + (14 + 26) * 1 if (numTotalBytes != @@ -299,7 +410,7 @@ namespace ZXing.QrCode.Internal ((numDataBytesInGroup2 + numEcBytesInGroup2) * numRsBlocksInGroup2)) { - throw new Exception("Total bytes mismatch"); + throw new WriterException("Total bytes mismatch"); } if (blockID < numRsBlocksInGroup1) @@ -335,7 +446,7 @@ namespace ZXing.QrCode.Internal if (bits.SizeInBytes != numDataBytes) { - throw new Exception("Number of bits and data bytes does not match"); + throw new WriterException("Number of bits and data bytes does not match"); } // Step 1. Divide data bytes into blocks and generate error correction bytes for them. We'll @@ -369,7 +480,7 @@ namespace ZXing.QrCode.Internal if (numDataBytes != dataBytesOffset) { - throw new Exception("Data bytes does not match offset"); + throw new WriterException("Data bytes does not match offset"); } BitArray result = new BitArray(); @@ -400,7 +511,7 @@ namespace ZXing.QrCode.Internal } if (numTotalBytes != result.SizeInBytes) { // Should be same. - throw new Exception("Interleaving error: " + numTotalBytes + " and " + + throw new WriterException("Interleaving error: " + numTotalBytes + " and " + result.SizeInBytes + " differ."); } @@ -450,7 +561,7 @@ namespace ZXing.QrCode.Internal int numBits = mode.getCharacterCountBits(version); if (numLetters >= (1 << numBits)) { - throw new Exception(numLetters + " is bigger than " + ((1 << numBits) - 1)); + throw new WriterException(numLetters + " is bigger than " + ((1 << numBits) - 1)); } bits.appendBits(numLetters, numBits); } @@ -467,10 +578,83 @@ namespace ZXing.QrCode.Internal BitArray bits, String encoding) { + if (mode.Equals(Mode.NUMERIC)) + appendNumericBytes(content, bits); + else + if (mode.Equals(Mode.ALPHANUMERIC)) + appendAlphanumericBytes(content, bits); + else if (mode.Equals(Mode.BYTE)) append8BitBytes(content, bits, encoding); + else + if (mode.Equals(Mode.KANJI)) + appendKanjiBytes(content, bits); else - throw new Exception("Invalid mode: " + mode); + throw new WriterException("Invalid mode: " + mode); + } + + internal static void appendNumericBytes(String content, BitArray bits) + { + int length = content.Length; + + int i = 0; + while (i < length) + { + int num1 = content[i] - '0'; + if (i + 2 < length) + { + // Encode three numeric letters in ten bits. + int num2 = content[i + 1] - '0'; + int num3 = content[i + 2] - '0'; + bits.appendBits(num1 * 100 + num2 * 10 + num3, 10); + i += 3; + } + else if (i + 1 < length) + { + // Encode two numeric letters in seven bits. + int num2 = content[i + 1] - '0'; + bits.appendBits(num1 * 10 + num2, 7); + i += 2; + } + else + { + // Encode one numeric letter in four bits. + bits.appendBits(num1, 4); + i++; + } + } + } + + internal static void appendAlphanumericBytes(String content, BitArray bits) + { + int length = content.Length; + + int i = 0; + while (i < length) + { + int code1 = getAlphanumericCode(content[i]); + if (code1 == -1) + { + throw new WriterException(); + } + if (i + 1 < length) + { + int code2 = getAlphanumericCode(content[i + 1]); + if (code2 == -1) + { + throw new WriterException(); + } + // Encode two alphanumeric letters in 11 bits. + bits.appendBits(code1 * 45 + code2, 11); + i += 2; + } + else + { + // Encode one alphanumeric letter in six bits. + bits.appendBits(code1, 6); + i++; + } + } } internal static void append8BitBytes(String content, BitArray bits, String encoding) @@ -504,7 +688,7 @@ namespace ZXing.QrCode.Internal #endif catch (Exception uee) { - throw new Exception(uee.Message, uee); + throw new WriterException(uee.Message, uee); } foreach (byte b in bytes) { @@ -512,6 +696,42 @@ namespace ZXing.QrCode.Internal } } + internal static void appendKanjiBytes(String content, BitArray bits) + { + byte[] bytes; + try + { + bytes = Encoding.GetEncoding("Shift_JIS").GetBytes(content); + } + catch (Exception uee) + { + throw new WriterException(uee.Message, uee); + } + int length = bytes.Length; + for (int i = 0; i < length; i += 2) + { + int byte1 = bytes[i] & 0xFF; + int byte2 = bytes[i + 1] & 0xFF; + int code = (byte1 << 8) | byte2; + int subtracted = -1; + if (code >= 0x8140 && code <= 0x9ffc) + { + + subtracted = code - 0x8140; + } + else if (code >= 0xe040 && code <= 0xebbf) + { + subtracted = code - 0xc140; + } + if (subtracted == -1) + { + + throw new WriterException("Invalid byte sequence"); + } + int encoded = ((subtracted >> 8) * 0xc0) + (subtracted & 0xff); + bits.appendBits(encoded, 13); + } + } /* private static void appendECI(CharacterSetECI eci, BitArray bits) diff --git a/shadowsocks-csharp/3rd/zxing/MaskUtil.cs b/shadowsocks-csharp/3rd/zxing/qrcode/encoder/MaskUtil.cs similarity index 100% rename from shadowsocks-csharp/3rd/zxing/MaskUtil.cs rename to shadowsocks-csharp/3rd/zxing/qrcode/encoder/MaskUtil.cs diff --git a/shadowsocks-csharp/3rd/zxing/MatrixUtil.cs b/shadowsocks-csharp/3rd/zxing/qrcode/encoder/MatrixUtil.cs similarity index 96% rename from shadowsocks-csharp/3rd/zxing/MatrixUtil.cs rename to shadowsocks-csharp/3rd/zxing/qrcode/encoder/MatrixUtil.cs index ffd9c7b4..9fcde4e1 100755 --- a/shadowsocks-csharp/3rd/zxing/MatrixUtil.cs +++ b/shadowsocks-csharp/3rd/zxing/qrcode/encoder/MatrixUtil.cs @@ -14,7 +14,6 @@ * limitations under the License. */ -using System; using ZXing.Common; namespace ZXing.QrCode.Internal @@ -310,7 +309,7 @@ namespace ZXing.QrCode.Internal // All bits should be consumed. if (bitIndex != dataBits.Size) { - throw new Exception("Not all bits consumed: " + bitIndex + '/' + dataBits.Size); + throw new WriterException("Not all bits consumed: " + bitIndex + '/' + dataBits.Size); } } @@ -366,9 +365,6 @@ namespace ZXing.QrCode.Internal /// public static int calculateBCHCode(int value, int poly) { - if (poly == 0) - throw new ArgumentException("0 polynominal", "poly"); - // If poly is "1 1111 0010 0101" (version info poly), msbSetInPoly is 13. We'll subtract 1 // from 13 to make it 12. int msbSetInPoly = findMSBSet(poly); @@ -394,7 +390,7 @@ namespace ZXing.QrCode.Internal { if (!QRCode.isValidMaskPattern(maskPattern)) { - throw new Exception("Invalid mask pattern"); + throw new WriterException("Invalid mask pattern"); } int typeInfo = (ecLevel.Bits << 3) | maskPattern; bits.appendBits(typeInfo, 5); @@ -409,7 +405,7 @@ namespace ZXing.QrCode.Internal if (bits.Size != 15) { // Just in case. - throw new Exception("should not happen but we got: " + bits.Size); + throw new WriterException("should not happen but we got: " + bits.Size); } } @@ -428,7 +424,7 @@ namespace ZXing.QrCode.Internal if (bits.Size != 18) { // Just in case. - throw new Exception("should not happen but we got: " + bits.Size); + throw new WriterException("should not happen but we got: " + bits.Size); } } @@ -472,7 +468,7 @@ namespace ZXing.QrCode.Internal { if (matrix[8, matrix.Height - 8] == 0) { - throw new Exception(); + throw new WriterException(); } matrix[8, matrix.Height - 8] = 1; } @@ -483,7 +479,7 @@ namespace ZXing.QrCode.Internal { if (!isEmpty(matrix[xStart + x, yStart])) { - throw new Exception(); + throw new WriterException(); } matrix[xStart + x, yStart] = 0; } @@ -495,7 +491,7 @@ namespace ZXing.QrCode.Internal { if (!isEmpty(matrix[xStart, yStart + y])) { - throw new Exception(); + throw new WriterException(); } matrix[xStart, yStart + y] = 0; } diff --git a/shadowsocks-csharp/3rd/zxing/QRCode.cs b/shadowsocks-csharp/3rd/zxing/qrcode/encoder/QRCode.cs similarity index 67% rename from shadowsocks-csharp/3rd/zxing/QRCode.cs rename to shadowsocks-csharp/3rd/zxing/qrcode/encoder/QRCode.cs index 123d4f18..90d10bf5 100755 --- a/shadowsocks-csharp/3rd/zxing/QRCode.cs +++ b/shadowsocks-csharp/3rd/zxing/qrcode/encoder/QRCode.cs @@ -21,7 +21,7 @@ namespace ZXing.QrCode.Internal { /// satorux@google.com (Satoru Takabayashi) - creator /// dswitkin@google.com (Daniel Switkin) - ported from C++ - public class QRCode + public sealed class QRCode { /// /// @@ -76,6 +76,40 @@ namespace ZXing.QrCode.Internal /// public ByteMatrix Matrix { get; set; } + /// + /// Returns a that represents this instance. + /// + /// + /// A that represents this instance. + /// + public override String ToString() + { + var result = new StringBuilder(200); + result.Append("<<\n"); + result.Append(" mode: "); + result.Append(Mode); + result.Append("\n ecLevel: "); + result.Append(ECLevel); + result.Append("\n version: "); + if (Version == null) + result.Append("null"); + else + result.Append(Version); + result.Append("\n maskPattern: "); + result.Append(MaskPattern); + if (Matrix == null) + { + result.Append("\n matrix: null\n"); + } + else + { + result.Append("\n matrix:\n"); + result.Append(Matrix.ToString()); + } + result.Append(">>\n"); + return result.ToString(); + } + /// /// Check if "mask_pattern" is valid. /// diff --git a/shadowsocks-csharp/3rd/zxing/qrcode/encoder/QrCodeEncodingOptions.cs b/shadowsocks-csharp/3rd/zxing/qrcode/encoder/QrCodeEncodingOptions.cs new file mode 100755 index 00000000..41e87a08 --- /dev/null +++ b/shadowsocks-csharp/3rd/zxing/qrcode/encoder/QrCodeEncodingOptions.cs @@ -0,0 +1,110 @@ +/* + * Copyright 2012 ZXing.Net 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 + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +using System; + +using ZXing.Common; +using ZXing.QrCode.Internal; + +namespace ZXing.QrCode +{ + /// + /// The class holds the available options for the QrCodeWriter + /// + [Serializable] + public class QrCodeEncodingOptions : EncodingOptions + { + /// + /// Specifies what degree of error correction to use, for example in QR Codes. + /// Type depends on the encoder. For example for QR codes it's type + /// {@link com.google.zxing.qrcode.decoder.ErrorCorrectionLevel ErrorCorrectionLevel}. + /// + public ErrorCorrectionLevel ErrorCorrection + { + get + { + if (Hints.ContainsKey(EncodeHintType.ERROR_CORRECTION)) + { + return (ErrorCorrectionLevel) Hints[EncodeHintType.ERROR_CORRECTION]; + } + return null; + } + set + { + if (value == null) + { + if (Hints.ContainsKey(EncodeHintType.ERROR_CORRECTION)) + Hints.Remove(EncodeHintType.ERROR_CORRECTION); + } + else + { + Hints[EncodeHintType.ERROR_CORRECTION] = value; + } + } + } + + /// + /// Specifies what character encoding to use where applicable (type {@link String}) + /// + public string CharacterSet + { + get + { + if (Hints.ContainsKey(EncodeHintType.CHARACTER_SET)) + { + return (string) Hints[EncodeHintType.CHARACTER_SET]; + } + return null; + } + set + { + if (value == null) + { + if (Hints.ContainsKey(EncodeHintType.CHARACTER_SET)) + Hints.Remove(EncodeHintType.CHARACTER_SET); + } + else + { + Hints[EncodeHintType.CHARACTER_SET] = value; + } + } + } + + /// + /// Explicitly disables ECI segment when generating QR Code + /// That is against the specification of QR Code but some + /// readers have problems if the charset is switched from + /// ISO-8859-1 (default) to UTF-8 with the necessary ECI segment. + /// If you set the property to true you can use UTF-8 encoding + /// and the ECI segment is omitted. + /// + public bool DisableECI + { + get + { + if (Hints.ContainsKey(EncodeHintType.DISABLE_ECI)) + { + return (bool)Hints[EncodeHintType.DISABLE_ECI]; + } + return false; + } + set + { + Hints[EncodeHintType.DISABLE_ECI] = value; + } + } + } +} diff --git a/shadowsocks-csharp/shadowsocks-csharp.csproj b/shadowsocks-csharp/shadowsocks-csharp.csproj index d14d7287..89cb782c 100755 --- a/shadowsocks-csharp/shadowsocks-csharp.csproj +++ b/shadowsocks-csharp/shadowsocks-csharp.csproj @@ -70,20 +70,51 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - - - - - - - - - - - - + + +