You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

Result.cs 5.8 kB

10 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /*
  2. * Copyright 2007 ZXing authors
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. using System;
  17. using System.Collections.Generic;
  18. namespace ZXing
  19. {
  20. /// <summary>
  21. /// Encapsulates the result of decoding a barcode within an image.
  22. /// </summary>
  23. public sealed class Result
  24. {
  25. /// <returns>raw text encoded by the barcode, if applicable, otherwise <code>null</code></returns>
  26. public String Text { get; private set; }
  27. /// <returns>raw bytes encoded by the barcode, if applicable, otherwise <code>null</code></returns>
  28. public byte[] RawBytes { get; private set; }
  29. /// <returns>
  30. /// points related to the barcode in the image. These are typically points
  31. /// identifying finder patterns or the corners of the barcode. The exact meaning is
  32. /// specific to the type of barcode that was decoded.
  33. /// </returns>
  34. public ResultPoint[] ResultPoints { get; private set; }
  35. /// <returns>{@link BarcodeFormat} representing the format of the barcode that was decoded</returns>
  36. public BarcodeFormat BarcodeFormat { get; private set; }
  37. /// <returns>
  38. /// {@link Hashtable} mapping {@link ResultMetadataType} keys to values. May be
  39. /// <code>null</code>. This contains optional metadata about what was detected about the barcode,
  40. /// like orientation.
  41. /// </returns>
  42. public IDictionary<ResultMetadataType, object> ResultMetadata { get; private set; }
  43. /// <summary>
  44. /// Gets the timestamp.
  45. /// </summary>
  46. public long Timestamp { get; private set; }
  47. /// <summary>
  48. /// Initializes a new instance of the <see cref="Result"/> class.
  49. /// </summary>
  50. /// <param name="text">The text.</param>
  51. /// <param name="rawBytes">The raw bytes.</param>
  52. /// <param name="resultPoints">The result points.</param>
  53. /// <param name="format">The format.</param>
  54. public Result(String text,
  55. byte[] rawBytes,
  56. ResultPoint[] resultPoints,
  57. BarcodeFormat format)
  58. : this(text, rawBytes, resultPoints, format, DateTime.Now.Ticks)
  59. {
  60. }
  61. /// <summary>
  62. /// Initializes a new instance of the <see cref="Result"/> class.
  63. /// </summary>
  64. /// <param name="text">The text.</param>
  65. /// <param name="rawBytes">The raw bytes.</param>
  66. /// <param name="resultPoints">The result points.</param>
  67. /// <param name="format">The format.</param>
  68. /// <param name="timestamp">The timestamp.</param>
  69. public Result(String text, byte[] rawBytes, ResultPoint[] resultPoints, BarcodeFormat format, long timestamp)
  70. {
  71. if (text == null && rawBytes == null)
  72. {
  73. throw new ArgumentException("Text and bytes are null");
  74. }
  75. Text = text;
  76. RawBytes = rawBytes;
  77. ResultPoints = resultPoints;
  78. BarcodeFormat = format;
  79. ResultMetadata = null;
  80. Timestamp = timestamp;
  81. }
  82. /// <summary>
  83. /// Adds one metadata to the result
  84. /// </summary>
  85. /// <param name="type">The type.</param>
  86. /// <param name="value">The value.</param>
  87. public void putMetadata(ResultMetadataType type, Object value)
  88. {
  89. if (ResultMetadata == null)
  90. {
  91. ResultMetadata = new Dictionary<ResultMetadataType, object>();
  92. }
  93. ResultMetadata[type] = value;
  94. }
  95. /// <summary>
  96. /// Adds a list of metadata to the result
  97. /// </summary>
  98. /// <param name="metadata">The metadata.</param>
  99. public void putAllMetadata(IDictionary<ResultMetadataType, object> metadata)
  100. {
  101. if (metadata != null)
  102. {
  103. if (ResultMetadata == null)
  104. {
  105. ResultMetadata = metadata;
  106. }
  107. else
  108. {
  109. foreach (var entry in metadata)
  110. ResultMetadata[entry.Key] = entry.Value;
  111. }
  112. }
  113. }
  114. /// <summary>
  115. /// Adds the result points.
  116. /// </summary>
  117. /// <param name="newPoints">The new points.</param>
  118. public void addResultPoints(ResultPoint[] newPoints)
  119. {
  120. var oldPoints = ResultPoints;
  121. if (oldPoints == null)
  122. {
  123. ResultPoints = newPoints;
  124. }
  125. else if (newPoints != null && newPoints.Length > 0)
  126. {
  127. var allPoints = new ResultPoint[oldPoints.Length + newPoints.Length];
  128. Array.Copy(oldPoints, 0, allPoints, 0, oldPoints.Length);
  129. Array.Copy(newPoints, 0, allPoints, oldPoints.Length, newPoints.Length);
  130. ResultPoints = allPoints;
  131. }
  132. }
  133. /// <summary>
  134. /// Returns a <see cref="System.String"/> that represents this instance.
  135. /// </summary>
  136. /// <returns>
  137. /// A <see cref="System.String"/> that represents this instance.
  138. /// </returns>
  139. public override String ToString()
  140. {
  141. if (Text == null)
  142. {
  143. return "[" + RawBytes.Length + " bytes]";
  144. }
  145. return Text;
  146. }
  147. }
  148. }