/*
* 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 the result of decoding a barcode within an image.
///
public sealed class Result
{
/// raw text encoded by the barcode, if applicable, otherwise null
public String Text { get; private set; }
/// raw bytes encoded by the barcode, if applicable, otherwise null
public byte[] RawBytes { get; private set; }
///
/// points related to the barcode in the image. These are typically points
/// identifying finder patterns or the corners of the barcode. The exact meaning is
/// specific to the type of barcode that was decoded.
///
public ResultPoint[] ResultPoints { get; private set; }
/// {@link BarcodeFormat} representing the format of the barcode that was decoded
public BarcodeFormat BarcodeFormat { get; private set; }
///
/// {@link Hashtable} mapping {@link ResultMetadataType} keys to values. May be
/// null
. This contains optional metadata about what was detected about the barcode,
/// like orientation.
///
public IDictionary ResultMetadata { get; private set; }
///
/// Gets the timestamp.
///
public long Timestamp { get; private set; }
///
/// Initializes a new instance of the class.
///
/// The text.
/// The raw bytes.
/// The result points.
/// The format.
public Result(String text,
byte[] rawBytes,
ResultPoint[] resultPoints,
BarcodeFormat format)
: this(text, rawBytes, resultPoints, format, DateTime.Now.Ticks)
{
}
///
/// Initializes a new instance of the class.
///
/// The text.
/// The raw bytes.
/// The result points.
/// The format.
/// The timestamp.
public Result(String text, byte[] rawBytes, ResultPoint[] resultPoints, BarcodeFormat format, long timestamp)
{
if (text == null && rawBytes == null)
{
throw new ArgumentException("Text and bytes are null");
}
Text = text;
RawBytes = rawBytes;
ResultPoints = resultPoints;
BarcodeFormat = format;
ResultMetadata = null;
Timestamp = timestamp;
}
///
/// Adds one metadata to the result
///
/// The type.
/// The value.
public void putMetadata(ResultMetadataType type, Object value)
{
if (ResultMetadata == null)
{
ResultMetadata = new Dictionary();
}
ResultMetadata[type] = value;
}
///
/// Adds a list of metadata to the result
///
/// The metadata.
public void putAllMetadata(IDictionary metadata)
{
if (metadata != null)
{
if (ResultMetadata == null)
{
ResultMetadata = metadata;
}
else
{
foreach (var entry in metadata)
ResultMetadata[entry.Key] = entry.Value;
}
}
}
///
/// Adds the result points.
///
/// The new points.
public void addResultPoints(ResultPoint[] newPoints)
{
var oldPoints = ResultPoints;
if (oldPoints == null)
{
ResultPoints = newPoints;
}
else if (newPoints != null && newPoints.Length > 0)
{
var allPoints = new ResultPoint[oldPoints.Length + newPoints.Length];
Array.Copy(oldPoints, 0, allPoints, 0, oldPoints.Length);
Array.Copy(newPoints, 0, allPoints, oldPoints.Length, newPoints.Length);
ResultPoints = allPoints;
}
}
///
/// Returns a that represents this instance.
///
///
/// A that represents this instance.
///
public override String ToString()
{
if (Text == null)
{
return "[" + RawBytes.Length + " bytes]";
}
return Text;
}
}
}