mirror of
https://gitee.com/akwkevin/aistudio.-wpf.-diagram
synced 2026-04-30 13:13:24 +08:00
项目结构调整
This commit is contained in:
134
Others/zxing.core/zxing.core/multi/ByQuadrantReader.cs
Normal file
134
Others/zxing.core/zxing.core/multi/ByQuadrantReader.cs
Normal file
@@ -0,0 +1,134 @@
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace ZXing.Multi
|
||||
{
|
||||
/// <summary>
|
||||
/// This class attempts to decode a barcode from an image, not by scanning the whole image,
|
||||
/// but by scanning subsets of the image. This is important when there may be multiple barcodes in
|
||||
/// an image, and detecting a barcode may find parts of multiple barcode and fail to decode
|
||||
/// (e.g. QR Codes). Instead this scans the four quadrants of the image -- and also the center
|
||||
/// 'quadrant' to cover the case where a barcode is found in the center.
|
||||
/// </summary>
|
||||
/// <seealso cref="GenericMultipleBarcodeReader" />
|
||||
public sealed class ByQuadrantReader : Reader
|
||||
{
|
||||
private readonly Reader @delegate;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="ByQuadrantReader"/> class.
|
||||
/// </summary>
|
||||
/// <param name="delegate">The @delegate.</param>
|
||||
public ByQuadrantReader(Reader @delegate)
|
||||
{
|
||||
this.@delegate = @delegate;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Locates and decodes a barcode in some format within an image.
|
||||
/// </summary>
|
||||
/// <param name="image">image of barcode to decode</param>
|
||||
/// <returns>
|
||||
/// String which the barcode encodes
|
||||
/// </returns>
|
||||
public Result decode(BinaryBitmap image)
|
||||
{
|
||||
return decode(image, null);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Locates and decodes a barcode in some format within an image. This method also accepts
|
||||
/// hints, each possibly associated to some data, which may help the implementation decode.
|
||||
/// </summary>
|
||||
/// <param name="image">image of barcode to decode</param>
|
||||
/// <param name="hints">passed as a <see cref="IDictionary{TKey, TValue}"/> from <see cref="DecodeHintType"/>
|
||||
/// to arbitrary data. The
|
||||
/// meaning of the data depends upon the hint type. The implementation may or may not do
|
||||
/// anything with these hints.</param>
|
||||
/// <returns>
|
||||
/// String which the barcode encodes
|
||||
/// </returns>
|
||||
public Result decode(BinaryBitmap image, IDictionary<DecodeHintType, object> hints)
|
||||
{
|
||||
int width = image.Width;
|
||||
int height = image.Height;
|
||||
int halfWidth = width / 2;
|
||||
int halfHeight = height / 2;
|
||||
|
||||
// No need to call makeAbsolute as results will be relative to original top left here
|
||||
var result = @delegate.decode(image.crop(0, 0, halfWidth, halfHeight), hints);
|
||||
if (result != null)
|
||||
return result;
|
||||
|
||||
result = @delegate.decode(image.crop(halfWidth, 0, halfWidth, halfHeight), hints);
|
||||
if (result != null)
|
||||
{
|
||||
makeAbsolute(result.ResultPoints, halfWidth, 0);
|
||||
return result;
|
||||
}
|
||||
|
||||
result = @delegate.decode(image.crop(0, halfHeight, halfWidth, halfHeight), hints);
|
||||
if (result != null)
|
||||
{
|
||||
makeAbsolute(result.ResultPoints, 0, halfHeight);
|
||||
return result;
|
||||
}
|
||||
|
||||
result = @delegate.decode(image.crop(halfWidth, halfHeight, halfWidth, halfHeight), hints);
|
||||
if (result != null)
|
||||
{
|
||||
makeAbsolute(result.ResultPoints, halfWidth, halfHeight);
|
||||
return result;
|
||||
}
|
||||
|
||||
int quarterWidth = halfWidth / 2;
|
||||
int quarterHeight = halfHeight / 2;
|
||||
var center = image.crop(quarterWidth, quarterHeight, halfWidth, halfHeight);
|
||||
result = @delegate.decode(center, hints);
|
||||
if (result != null)
|
||||
{
|
||||
makeAbsolute(result.ResultPoints, quarterWidth, quarterHeight);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Resets any internal state the implementation has after a decode, to prepare it
|
||||
/// for reuse.
|
||||
/// </summary>
|
||||
public void reset()
|
||||
{
|
||||
@delegate.reset();
|
||||
}
|
||||
|
||||
private static void makeAbsolute(ResultPoint[] points, int leftOffset, int topOffset)
|
||||
{
|
||||
if (points != null)
|
||||
{
|
||||
for (int i = 0; i < points.Length; i++)
|
||||
{
|
||||
ResultPoint relative = points[i];
|
||||
if (relative != null)
|
||||
{
|
||||
points[i] = new ResultPoint(relative.X + leftOffset, relative.Y + topOffset);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,222 @@
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace ZXing.Multi
|
||||
{
|
||||
/// <summary>
|
||||
/// <p>Attempts to locate multiple barcodes in an image by repeatedly decoding portion of the image.
|
||||
/// After one barcode is found, the areas left, above, right and below the barcode's
|
||||
/// {@link com.google.zxing.ResultPoint}s are scanned, recursively.</p>
|
||||
/// <p>A caller may want to also employ {@link ByQuadrantReader} when attempting to find multiple
|
||||
/// 2D barcodes, like QR Codes, in an image, where the presence of multiple barcodes might prevent
|
||||
/// detecting any one of them.</p>
|
||||
/// <p>That is, instead of passing a {@link Reader} a caller might pass
|
||||
/// <code>new ByQuadrantReader(reader)</code>.</p>
|
||||
/// <author>Sean Owen</author>
|
||||
/// </summary>
|
||||
public sealed class GenericMultipleBarcodeReader : MultipleBarcodeReader, Reader
|
||||
{
|
||||
private const int MIN_DIMENSION_TO_RECUR = 30;
|
||||
private const int MAX_DEPTH = 4;
|
||||
|
||||
private readonly Reader _delegate;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="GenericMultipleBarcodeReader"/> class.
|
||||
/// </summary>
|
||||
/// <param name="delegate">The @delegate.</param>
|
||||
public GenericMultipleBarcodeReader(Reader @delegate)
|
||||
{
|
||||
this._delegate = @delegate;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Decodes the multiple.
|
||||
/// </summary>
|
||||
/// <param name="image">The image.</param>
|
||||
/// <returns></returns>
|
||||
public Result[] decodeMultiple(BinaryBitmap image)
|
||||
{
|
||||
return decodeMultiple(image, null);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Decodes the multiple.
|
||||
/// </summary>
|
||||
/// <param name="image">The image.</param>
|
||||
/// <param name="hints">The hints.</param>
|
||||
/// <returns></returns>
|
||||
public Result[] decodeMultiple(BinaryBitmap image, IDictionary<DecodeHintType, object> hints)
|
||||
{
|
||||
var results = new List<Result>();
|
||||
doDecodeMultiple(image, hints, results, 0, 0, 0);
|
||||
if ((results.Count == 0))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
int numResults = results.Count;
|
||||
Result[] resultArray = new Result[numResults];
|
||||
for (int i = 0; i < numResults; i++)
|
||||
{
|
||||
resultArray[i] = (Result)results[i];
|
||||
}
|
||||
return resultArray;
|
||||
}
|
||||
|
||||
private void doDecodeMultiple(BinaryBitmap image, IDictionary<DecodeHintType, object> hints, IList<Result> results, int xOffset, int yOffset, int currentDepth)
|
||||
{
|
||||
if (currentDepth > MAX_DEPTH)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
Result result = _delegate.decode(image, hints);
|
||||
if (result == null)
|
||||
return;
|
||||
|
||||
bool alreadyFound = false;
|
||||
for (int i = 0; i < results.Count; i++)
|
||||
{
|
||||
Result existingResult = (Result)results[i];
|
||||
if (existingResult.Text.Equals(result.Text))
|
||||
{
|
||||
alreadyFound = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!alreadyFound)
|
||||
{
|
||||
results.Add(translateResultPoints(result, xOffset, yOffset));
|
||||
}
|
||||
|
||||
ResultPoint[] resultPoints = result.ResultPoints;
|
||||
if (resultPoints == null || resultPoints.Length == 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
int width = image.Width;
|
||||
int height = image.Height;
|
||||
float minX = width;
|
||||
float minY = height;
|
||||
float maxX = 0.0f;
|
||||
float maxY = 0.0f;
|
||||
for (int i = 0; i < resultPoints.Length; i++)
|
||||
{
|
||||
ResultPoint point = resultPoints[i];
|
||||
if (point == null)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
float x = point.X;
|
||||
float y = point.Y;
|
||||
if (x < minX)
|
||||
{
|
||||
minX = x;
|
||||
}
|
||||
if (y < minY)
|
||||
{
|
||||
minY = y;
|
||||
}
|
||||
if (x > maxX)
|
||||
{
|
||||
maxX = x;
|
||||
}
|
||||
if (y > maxY)
|
||||
{
|
||||
maxY = y;
|
||||
}
|
||||
}
|
||||
|
||||
// Decode left of barcode
|
||||
if (minX > MIN_DIMENSION_TO_RECUR)
|
||||
{
|
||||
doDecodeMultiple(image.crop(0, 0, (int)minX, height), hints, results, xOffset, yOffset, currentDepth + 1);
|
||||
}
|
||||
// Decode above barcode
|
||||
if (minY > MIN_DIMENSION_TO_RECUR)
|
||||
{
|
||||
doDecodeMultiple(image.crop(0, 0, width, (int)minY), hints, results, xOffset, yOffset, currentDepth + 1);
|
||||
}
|
||||
// Decode right of barcode
|
||||
if (maxX < width - MIN_DIMENSION_TO_RECUR)
|
||||
{
|
||||
doDecodeMultiple(image.crop((int)maxX, 0, width - (int)maxX, height), hints, results, xOffset + (int)maxX, yOffset, currentDepth + 1);
|
||||
}
|
||||
// Decode below barcode
|
||||
if (maxY < height - MIN_DIMENSION_TO_RECUR)
|
||||
{
|
||||
doDecodeMultiple(image.crop(0, (int)maxY, width, height - (int)maxY), hints, results, xOffset, yOffset + (int)maxY, currentDepth + 1);
|
||||
}
|
||||
}
|
||||
|
||||
private static Result translateResultPoints(Result result, int xOffset, int yOffset)
|
||||
{
|
||||
var oldResultPoints = result.ResultPoints;
|
||||
var newResultPoints = new ResultPoint[oldResultPoints.Length];
|
||||
for (int i = 0; i < oldResultPoints.Length; i++)
|
||||
{
|
||||
var oldPoint = oldResultPoints[i];
|
||||
if (oldPoint != null)
|
||||
{
|
||||
newResultPoints[i] = new ResultPoint(oldPoint.X + xOffset, oldPoint.Y + yOffset);
|
||||
}
|
||||
}
|
||||
var newResult = new Result(result.Text, result.RawBytes, result.NumBits, newResultPoints, result.BarcodeFormat);
|
||||
newResult.putAllMetadata(result.ResultMetadata);
|
||||
return newResult;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Locates and decodes a barcode in some format within an image.
|
||||
/// </summary>
|
||||
/// <param name="image">image of barcode to decode</param>
|
||||
/// <returns>
|
||||
/// String which the barcode encodes
|
||||
/// </returns>
|
||||
public Result decode(BinaryBitmap image)
|
||||
{
|
||||
return _delegate.decode(image);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Locates and decodes a barcode in some format within an image. This method also accepts
|
||||
/// hints, each possibly associated to some data, which may help the implementation decode.
|
||||
/// </summary>
|
||||
/// <param name="image">image of barcode to decode</param>
|
||||
/// <param name="hints">passed as a <see cref="IDictionary{TKey, TValue}"/> from <see cref="DecodeHintType"/>
|
||||
/// to arbitrary data. The
|
||||
/// meaning of the data depends upon the hint type. The implementation may or may not do
|
||||
/// anything with these hints.</param>
|
||||
/// <returns>
|
||||
/// String which the barcode encodes
|
||||
/// </returns>
|
||||
public Result decode(BinaryBitmap image, IDictionary<DecodeHintType, object> hints)
|
||||
{
|
||||
return _delegate.decode(image, hints);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Resets any internal state the implementation has after a decode, to prepare it
|
||||
/// for reuse.
|
||||
/// </summary>
|
||||
public void reset()
|
||||
{
|
||||
_delegate.reset();
|
||||
}
|
||||
}
|
||||
}
|
||||
43
Others/zxing.core/zxing.core/multi/MultipleBarcodeReader.cs
Normal file
43
Others/zxing.core/zxing.core/multi/MultipleBarcodeReader.cs
Normal file
@@ -0,0 +1,43 @@
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace ZXing.Multi
|
||||
{
|
||||
/// <summary>
|
||||
/// Implementation of this interface attempt to read several barcodes from one image.
|
||||
/// <author>Sean Owen</author>
|
||||
/// <seealso cref="Reader"/>
|
||||
/// </summary>
|
||||
public interface MultipleBarcodeReader
|
||||
{
|
||||
/// <summary>
|
||||
/// Decodes the multiple.
|
||||
/// </summary>
|
||||
/// <param name="image">The image.</param>
|
||||
/// <returns></returns>
|
||||
Result[] decodeMultiple(BinaryBitmap image);
|
||||
|
||||
/// <summary>
|
||||
/// Decodes the multiple.
|
||||
/// </summary>
|
||||
/// <param name="image">The image.</param>
|
||||
/// <param name="hints">The hints.</param>
|
||||
/// <returns></returns>
|
||||
Result[] decodeMultiple(BinaryBitmap image, IDictionary<DecodeHintType, object> hints);
|
||||
}
|
||||
}
|
||||
154
Others/zxing.core/zxing.core/multi/qrcode/QRCodeMultiReader.cs
Normal file
154
Others/zxing.core/zxing.core/multi/qrcode/QRCodeMultiReader.cs
Normal file
@@ -0,0 +1,154 @@
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
using System.Collections.Generic;
|
||||
|
||||
using ZXing.Common;
|
||||
using ZXing.Multi.QrCode.Internal;
|
||||
using ZXing.QrCode;
|
||||
using ZXing.QrCode.Internal;
|
||||
|
||||
namespace ZXing.Multi.QrCode
|
||||
{
|
||||
/// <summary>
|
||||
/// This implementation can detect and decode multiple QR Codes in an image.
|
||||
/// </summary>
|
||||
public sealed class QRCodeMultiReader : QRCodeReader, MultipleBarcodeReader
|
||||
{
|
||||
private static readonly ResultPoint[] NO_POINTS = new ResultPoint[0];
|
||||
|
||||
/// <summary>
|
||||
/// Decodes the multiple.
|
||||
/// </summary>
|
||||
/// <param name="image">The image.</param>
|
||||
/// <returns></returns>
|
||||
public Result[] decodeMultiple(BinaryBitmap image)
|
||||
{
|
||||
return decodeMultiple(image, null);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Decodes the multiple.
|
||||
/// </summary>
|
||||
/// <param name="image">The image.</param>
|
||||
/// <param name="hints">The hints.</param>
|
||||
/// <returns></returns>
|
||||
public Result[] decodeMultiple(BinaryBitmap image, IDictionary<DecodeHintType, object> hints)
|
||||
{
|
||||
var results = new List<Result>();
|
||||
var detectorResults = new MultiDetector(image.BlackMatrix).detectMulti(hints);
|
||||
foreach (DetectorResult detectorResult in detectorResults)
|
||||
{
|
||||
var decoderResult = getDecoder().decode(detectorResult.Bits, hints);
|
||||
if (decoderResult == null)
|
||||
continue;
|
||||
|
||||
var points = detectorResult.Points;
|
||||
// If the code was mirrored: swap the bottom-left and the top-right points.
|
||||
var data = decoderResult.Other as QRCodeDecoderMetaData;
|
||||
if (data != null)
|
||||
{
|
||||
data.applyMirroredCorrection(points);
|
||||
}
|
||||
var result = new Result(decoderResult.Text, decoderResult.RawBytes, points, BarcodeFormat.QR_CODE);
|
||||
var byteSegments = decoderResult.ByteSegments;
|
||||
if (byteSegments != null)
|
||||
{
|
||||
result.putMetadata(ResultMetadataType.BYTE_SEGMENTS, byteSegments);
|
||||
}
|
||||
var ecLevel = decoderResult.ECLevel;
|
||||
if (ecLevel != null)
|
||||
{
|
||||
result.putMetadata(ResultMetadataType.ERROR_CORRECTION_LEVEL, ecLevel);
|
||||
}
|
||||
if (decoderResult.StructuredAppend)
|
||||
{
|
||||
result.putMetadata(ResultMetadataType.STRUCTURED_APPEND_SEQUENCE, decoderResult.StructuredAppendSequenceNumber);
|
||||
result.putMetadata(ResultMetadataType.STRUCTURED_APPEND_PARITY, decoderResult.StructuredAppendParity);
|
||||
}
|
||||
results.Add(result);
|
||||
}
|
||||
if (results.Count == 0)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
results = ProcessStructuredAppend(results);
|
||||
return results.ToArray();
|
||||
}
|
||||
|
||||
internal static List<Result> ProcessStructuredAppend(List<Result> results)
|
||||
{
|
||||
var newResults = new List<Result>();
|
||||
var saResults = new List<Result>();
|
||||
foreach (var result in results)
|
||||
{
|
||||
if (result.ResultMetadata.ContainsKey(ResultMetadataType.STRUCTURED_APPEND_SEQUENCE))
|
||||
{
|
||||
saResults.Add(result);
|
||||
}
|
||||
else
|
||||
{
|
||||
newResults.Add(result);
|
||||
}
|
||||
}
|
||||
if (saResults.Count == 0)
|
||||
{
|
||||
return results;
|
||||
}
|
||||
// sort and concatenate the SA list items
|
||||
saResults.Sort(SaSequenceSort);
|
||||
var newText = new System.Text.StringBuilder();
|
||||
using (var newRawBytes = new System.IO.MemoryStream())
|
||||
using (var newByteSegment = new System.IO.MemoryStream())
|
||||
{
|
||||
foreach (Result saResult in saResults)
|
||||
{
|
||||
newText.Append(saResult.Text);
|
||||
byte[] saBytes = saResult.RawBytes;
|
||||
newRawBytes.Write(saBytes, 0, saBytes.Length);
|
||||
if (saResult.ResultMetadata.ContainsKey(ResultMetadataType.BYTE_SEGMENTS))
|
||||
{
|
||||
var byteSegments = (IEnumerable<byte[]>) saResult.ResultMetadata[ResultMetadataType.BYTE_SEGMENTS];
|
||||
if (byteSegments != null)
|
||||
{
|
||||
foreach (byte[] segment in byteSegments)
|
||||
{
|
||||
newByteSegment.Write(segment, 0, segment.Length);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Result newResult = new Result(newText.ToString(), newRawBytes.ToArray(), NO_POINTS, BarcodeFormat.QR_CODE);
|
||||
if (newByteSegment.Length > 0)
|
||||
{
|
||||
var byteSegmentList = new List<byte[]>();
|
||||
byteSegmentList.Add(newByteSegment.ToArray());
|
||||
newResult.putMetadata(ResultMetadataType.BYTE_SEGMENTS, byteSegmentList);
|
||||
}
|
||||
newResults.Add(newResult);
|
||||
}
|
||||
return newResults;
|
||||
}
|
||||
|
||||
private static int SaSequenceSort(Result a, Result b)
|
||||
{
|
||||
var aNumber = (int) (a.ResultMetadata[ResultMetadataType.STRUCTURED_APPEND_SEQUENCE]);
|
||||
var bNumber = (int) (b.ResultMetadata[ResultMetadataType.STRUCTURED_APPEND_SEQUENCE]);
|
||||
return aNumber - bNumber;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
using System.Collections.Generic;
|
||||
using ZXing.Common;
|
||||
using ZXing.QrCode.Internal;
|
||||
|
||||
namespace ZXing.Multi.QrCode.Internal
|
||||
{
|
||||
/// <summary>
|
||||
/// <p>Encapsulates logic that can detect one or more QR Codes in an image, even if the QR Code
|
||||
/// is rotated or skewed, or partially obscured.</p>
|
||||
///
|
||||
/// <author>Sean Owen</author>
|
||||
/// <author>Hannes Erven</author>
|
||||
/// </summary>
|
||||
public sealed class MultiDetector : Detector
|
||||
{
|
||||
private static readonly DetectorResult[] EMPTY_DETECTOR_RESULTS = new DetectorResult[0];
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="MultiDetector"/> class.
|
||||
/// </summary>
|
||||
/// <param name="image">The image.</param>
|
||||
public MultiDetector(BitMatrix image)
|
||||
: base(image)
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Detects the multi.
|
||||
/// </summary>
|
||||
/// <param name="hints">The hints.</param>
|
||||
/// <returns></returns>
|
||||
public DetectorResult[] detectMulti(IDictionary<DecodeHintType, object> hints)
|
||||
{
|
||||
var image = Image;
|
||||
var resultPointCallback =
|
||||
hints == null || !hints.ContainsKey(DecodeHintType.NEED_RESULT_POINT_CALLBACK) ? null : (ResultPointCallback)hints[DecodeHintType.NEED_RESULT_POINT_CALLBACK];
|
||||
var finder = new MultiFinderPatternFinder(image, resultPointCallback);
|
||||
var infos = finder.findMulti(hints);
|
||||
|
||||
if (infos.Length == 0)
|
||||
{
|
||||
return EMPTY_DETECTOR_RESULTS;
|
||||
}
|
||||
|
||||
var result = new List<DetectorResult>();
|
||||
foreach (FinderPatternInfo info in infos)
|
||||
{
|
||||
var oneResult = processFinderPatternInfo(info);
|
||||
if (oneResult != null)
|
||||
result.Add(oneResult);
|
||||
}
|
||||
if (result.Count == 0)
|
||||
{
|
||||
return EMPTY_DETECTOR_RESULTS;
|
||||
}
|
||||
else
|
||||
{
|
||||
return result.ToArray();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,335 @@
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using ZXing.Common;
|
||||
using ZXing.QrCode.Internal;
|
||||
|
||||
namespace ZXing.Multi.QrCode.Internal
|
||||
{
|
||||
/// <summary>
|
||||
/// <p>This class attempts to find finder patterns in a QR Code. Finder patterns are the square
|
||||
/// markers at three corners of a QR Code.</p>
|
||||
///
|
||||
/// <p>This class is thread-safe but not reentrant. Each thread must allocate its own object.</p>
|
||||
///
|
||||
/// <p>In contrast to <see cref="FinderPatternFinder" />, this class will return an array of all possible
|
||||
/// QR code locations in the image.</p>
|
||||
///
|
||||
/// <p>Use the TRY_HARDER hint to ask for a more thorough detection.</p>
|
||||
///
|
||||
/// <author>Sean Owen</author>
|
||||
/// <author>Hannes Erven</author>
|
||||
/// </summary>
|
||||
public sealed class MultiFinderPatternFinder : FinderPatternFinder
|
||||
{
|
||||
private static readonly FinderPatternInfo[] EMPTY_RESULT_ARRAY = new FinderPatternInfo[0];
|
||||
|
||||
// TODO MIN_MODULE_COUNT and MAX_MODULE_COUNT would be great hints to ask the user for
|
||||
// since it limits the number of regions to decode
|
||||
|
||||
// max. legal count of modules per QR code edge (177)
|
||||
private static float MAX_MODULE_COUNT_PER_EDGE = 180;
|
||||
|
||||
// min. legal count per modules per QR code edge (11)
|
||||
private static float MIN_MODULE_COUNT_PER_EDGE = 9;
|
||||
|
||||
/// <summary>
|
||||
/// More or less arbitrary cutoff point for determining if two finder patterns might belong
|
||||
/// to the same code if they differ less than DIFF_MODSIZE_CUTOFF_PERCENT percent in their
|
||||
/// estimated modules sizes.
|
||||
/// </summary>
|
||||
private static float DIFF_MODSIZE_CUTOFF_PERCENT = 0.05f;
|
||||
|
||||
/// <summary>
|
||||
/// More or less arbitrary cutoff point for determining if two finder patterns might belong
|
||||
/// to the same code if they differ less than DIFF_MODSIZE_CUTOFF pixels/module in their
|
||||
/// estimated modules sizes.
|
||||
/// </summary>
|
||||
private const float DIFF_MODSIZE_CUTOFF = 0.5f;
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// A comparator that orders FinderPatterns by their estimated module size.
|
||||
/// </summary>
|
||||
private sealed class ModuleSizeComparator : IComparer<FinderPattern>
|
||||
{
|
||||
public int Compare(FinderPattern center1, FinderPattern center2)
|
||||
{
|
||||
float value = center2.EstimatedModuleSize - center1.EstimatedModuleSize;
|
||||
return value < 0.0 ? -1 : value > 0.0 ? 1 : 0;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// <p>Creates a finder that will search the image for three finder patterns.</p>
|
||||
///
|
||||
/// <param name="image">image to search</param>
|
||||
/// <param name="resultPointCallback">callback for result points</param>
|
||||
/// </summary>
|
||||
public MultiFinderPatternFinder(BitMatrix image, ResultPointCallback resultPointCallback)
|
||||
: base(image, resultPointCallback)
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// </summary>
|
||||
/// <returns>the 3 best <see cref="FinderPattern" />s from our list of candidates. The "best" are
|
||||
/// those that have been detected at least CENTER_QUORUM times, and whose module
|
||||
/// size differs from the average among those patterns the least
|
||||
/// </returns>
|
||||
private FinderPattern[][] selectMultipleBestPatterns()
|
||||
{
|
||||
List<FinderPattern> possibleCenters = PossibleCenters;
|
||||
int size = possibleCenters.Count;
|
||||
|
||||
if (size < 3)
|
||||
{
|
||||
// Couldn't find enough finder patterns
|
||||
return null;
|
||||
}
|
||||
|
||||
/*
|
||||
* Begin HE modifications to safely detect multiple codes of equal size
|
||||
*/
|
||||
if (size == 3)
|
||||
{
|
||||
return new FinderPattern[][]
|
||||
{
|
||||
new FinderPattern[]
|
||||
{
|
||||
possibleCenters[0],
|
||||
possibleCenters[1],
|
||||
possibleCenters[2]
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
// Sort by estimated module size to speed up the upcoming checks
|
||||
possibleCenters.Sort(new ModuleSizeComparator());
|
||||
|
||||
/*
|
||||
* Now lets start: build a list of tuples of three finder locations that
|
||||
* - feature similar module sizes
|
||||
* - are placed in a distance so the estimated module count is within the QR specification
|
||||
* - have similar distance between upper left/right and left top/bottom finder patterns
|
||||
* - form a triangle with 90° angle (checked by comparing top right/bottom left distance
|
||||
* with pythagoras)
|
||||
*
|
||||
* Note: we allow each point to be used for more than one code region: this might seem
|
||||
* counterintuitive at first, but the performance penalty is not that big. At this point,
|
||||
* we cannot make a good quality decision whether the three finders actually represent
|
||||
* a QR code, or are just by chance layouted so it looks like there might be a QR code there.
|
||||
* So, if the layout seems right, lets have the decoder try to decode.
|
||||
*/
|
||||
|
||||
List<FinderPattern[]> results = new List<FinderPattern[]>(); // holder for the results
|
||||
|
||||
for (int i1 = 0; i1 < (size - 2); i1++)
|
||||
{
|
||||
FinderPattern p1 = possibleCenters[i1];
|
||||
if (p1 == null)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
for (int i2 = i1 + 1; i2 < (size - 1); i2++)
|
||||
{
|
||||
FinderPattern p2 = possibleCenters[i2];
|
||||
if (p2 == null)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
// Compare the expected module sizes; if they are really off, skip
|
||||
float vModSize12 = (p1.EstimatedModuleSize - p2.EstimatedModuleSize) /
|
||||
Math.Min(p1.EstimatedModuleSize, p2.EstimatedModuleSize);
|
||||
float vModSize12A = Math.Abs(p1.EstimatedModuleSize - p2.EstimatedModuleSize);
|
||||
if (vModSize12A > DIFF_MODSIZE_CUTOFF && vModSize12 >= DIFF_MODSIZE_CUTOFF_PERCENT)
|
||||
{
|
||||
// break, since elements are ordered by the module size deviation there cannot be
|
||||
// any more interesting elements for the given p1.
|
||||
break;
|
||||
}
|
||||
|
||||
for (int i3 = i2 + 1; i3 < size; i3++)
|
||||
{
|
||||
FinderPattern p3 = possibleCenters[i3];
|
||||
if (p3 == null)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
// Compare the expected module sizes; if they are really off, skip
|
||||
float vModSize23 = (p2.EstimatedModuleSize - p3.EstimatedModuleSize) /
|
||||
Math.Min(p2.EstimatedModuleSize, p3.EstimatedModuleSize);
|
||||
float vModSize23A = Math.Abs(p2.EstimatedModuleSize - p3.EstimatedModuleSize);
|
||||
if (vModSize23A > DIFF_MODSIZE_CUTOFF && vModSize23 >= DIFF_MODSIZE_CUTOFF_PERCENT)
|
||||
{
|
||||
// break, since elements are ordered by the module size deviation there cannot be
|
||||
// any more interesting elements for the given p1.
|
||||
break;
|
||||
}
|
||||
|
||||
FinderPattern[] test = {p1, p2, p3};
|
||||
ResultPoint.orderBestPatterns(test);
|
||||
|
||||
// Calculate the distances: a = topleft-bottomleft, b=topleft-topright, c = diagonal
|
||||
FinderPatternInfo info = new FinderPatternInfo(test);
|
||||
float dA = ResultPoint.distance(info.TopLeft, info.BottomLeft);
|
||||
float dC = ResultPoint.distance(info.TopRight, info.BottomLeft);
|
||||
float dB = ResultPoint.distance(info.TopLeft, info.TopRight);
|
||||
|
||||
// Check the sizes
|
||||
float estimatedModuleCount = (dA + dB) / (p1.EstimatedModuleSize * 2.0f);
|
||||
if (estimatedModuleCount > MAX_MODULE_COUNT_PER_EDGE ||
|
||||
estimatedModuleCount < MIN_MODULE_COUNT_PER_EDGE)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
// Calculate the difference of the edge lengths in percent
|
||||
float vABBC = Math.Abs((dA - dB) / Math.Min(dA, dB));
|
||||
if (vABBC >= 0.1f)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
// Calculate the diagonal length by assuming a 90° angle at topleft
|
||||
float dCpy = (float) Math.Sqrt((double) dA * dA + (double) dB * dB);
|
||||
// Compare to the real distance in %
|
||||
float vPyC = Math.Abs((dC - dCpy) / Math.Min(dC, dCpy));
|
||||
|
||||
if (vPyC >= 0.1f)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
// All tests passed!
|
||||
results.Add(test);
|
||||
} // end iterate p3
|
||||
} // end iterate p2
|
||||
} // end iterate p1
|
||||
|
||||
if (results.Count != 0)
|
||||
{
|
||||
return results.ToArray();
|
||||
}
|
||||
|
||||
// Nothing found!
|
||||
return null;
|
||||
}
|
||||
|
||||
public FinderPatternInfo[] findMulti(IDictionary<DecodeHintType, object> hints)
|
||||
{
|
||||
bool tryHarder = hints != null && hints.ContainsKey(DecodeHintType.TRY_HARDER);
|
||||
BitMatrix image = Image;
|
||||
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;
|
||||
}
|
||||
|
||||
int[] stateCount = new int[5];
|
||||
for (int i = iSkip - 1; i < maxI; i += iSkip)
|
||||
{
|
||||
// Get a row of black/white values
|
||||
doClearCounts(stateCount);
|
||||
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) && handlePossibleCenter(stateCount, i, j))
|
||||
{
|
||||
// Yes
|
||||
// Clear state to start looking again
|
||||
currentState = 0;
|
||||
doClearCounts(stateCount);
|
||||
}
|
||||
else
|
||||
{
|
||||
// No, shift counts back by two
|
||||
doShiftCounts2(stateCount);
|
||||
currentState = 3;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
stateCount[++currentState]++;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// Counting white pixels
|
||||
stateCount[currentState]++;
|
||||
}
|
||||
}
|
||||
} // for j=...
|
||||
|
||||
if (foundPatternCross(stateCount))
|
||||
{
|
||||
handlePossibleCenter(stateCount, i, maxJ);
|
||||
} // end if foundPatternCross
|
||||
} // for i=iSkip-1 ...
|
||||
FinderPattern[][] patternInfo = selectMultipleBestPatterns();
|
||||
if (patternInfo == null)
|
||||
return EMPTY_RESULT_ARRAY;
|
||||
List<FinderPatternInfo> result = new List<FinderPatternInfo>();
|
||||
foreach (FinderPattern[] pattern in patternInfo)
|
||||
{
|
||||
ResultPoint.orderBestPatterns(pattern);
|
||||
result.Add(new FinderPatternInfo(pattern));
|
||||
}
|
||||
|
||||
if (result.Count == 0)
|
||||
{
|
||||
return EMPTY_RESULT_ARRAY;
|
||||
}
|
||||
else
|
||||
{
|
||||
return result.ToArray();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user