Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Specify the result of decoding (QR, Datamatrix, AZTEC and MaxiCode) as byte[] #442

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions Source/lib/Result.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ public sealed class Result
/// <returns>raw text encoded by the barcode, if applicable, otherwise <code>null</code></returns>
public String Text { get; private set; }

/// <returns>raw text binary representation, if applicable, otherwise <code>null</code></returns>
public byte[] Data { get; private set; }

/// <returns>raw bytes encoded by the barcode, if applicable, otherwise <code>null</code></returns>
public byte[] RawBytes { get; private set; }

Expand Down Expand Up @@ -72,6 +75,20 @@ public Result(String text,
{
}

/// <summary>
/// Initizalizes a new instance of the <see cref="Result"/> class.
/// </summary>
/// <param name="text"></param>
/// <param name="data"></param>
/// <param name="rawBytes"></param>
/// <param name="resultPoints"></param>
/// <param name="format"></param>
public Result(String text, byte[] data, byte[] rawBytes, ResultPoint[] resultPoints, BarcodeFormat format)
: this(text, rawBytes, resultPoints, format)
{
Data = data;
}

/// <summary>
/// Initializes a new instance of the <see cref="Result"/> class.
/// </summary>
Expand All @@ -89,6 +106,26 @@ public Result(String text,
{
}

/// <summary>
/// Initializes a new instance of the <see cref="Result"/> class.
/// </summary>
/// <param name="text"></param>
/// <param name="data"></param>
/// <param name="rawBytes"></param>
/// <param name="numBits"></param>
/// <param name="resultPoints"></param>
/// <param name="format"></param>
public Result(String text,
byte[] data,
byte[] rawBytes,
int numBits,
ResultPoint[] resultPoints,
BarcodeFormat format)
: this(text,rawBytes, numBits, resultPoints, format)
{
this.Data = data;
}

/// <summary>
/// Initializes a new instance of the <see cref="Result"/> class.
/// </summary>
Expand All @@ -112,12 +149,18 @@ public Result(String text, byte[] rawBytes, ResultPoint[] resultPoints, BarcodeF
/// <param name="format">The format.</param>
/// <param name="timestamp">The timestamp.</param>
public Result(String text, byte[] rawBytes, int numBits, ResultPoint[] resultPoints, BarcodeFormat format, long timestamp)
: this(text, null, rawBytes, numBits, resultPoints, format, timestamp)
{
}

public Result(String text, byte[] data, byte[] rawBytes, int numBits, ResultPoint[] resultPoints, BarcodeFormat format, long timestamp)
{
if (text == null && rawBytes == null)
{
throw new ArgumentException("Text and bytes are null");
}
Text = text;
Data = data;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this could be a good place to add the default behavior UTF8.GetBytes(text) if data == null

RawBytes = rawBytes;
NumBits = numBits;
ResultPoints = resultPoints;
Expand Down
2 changes: 1 addition & 1 deletion Source/lib/aztec/AztecReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public Result decode(BinaryBitmap image, IDictionary<DecodeHintType, object> hin
}
}

var result = new Result(decoderResult.Text, decoderResult.RawBytes, decoderResult.NumBits, points, BarcodeFormat.AZTEC);
var result = new Result(decoderResult.Text, decoderResult.Data, decoderResult.RawBytes, decoderResult.NumBits, points, BarcodeFormat.AZTEC);

IList<byte[]> byteSegments = decoderResult.ByteSegments;
if (byteSegments != null)
Expand Down
19 changes: 12 additions & 7 deletions Source/lib/aztec/decoder/Decoder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@

using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

using ZXing.Common;
using ZXing.Common.ReedSolomon;

Expand Down Expand Up @@ -111,13 +111,14 @@ public DecoderResult decode(AztecDetectorResult detectorResult)
if (correctedBits == null)
return null;

var result = getEncodedData(correctedBits.correctBits);
byte[] encodedResult;
var result = getEncodedData(correctedBits.correctBits, out encodedResult);
if (result == null)
return null;

var rawBytes = convertBoolArrayToByteArray(correctedBits.correctBits);

var decoderResult = new DecoderResult(rawBytes, correctedBits.correctBits.Length, result, null, String.Format("{0}", correctedBits.ecLevel));
var decoderResult = new DecoderResult(rawBytes, correctedBits.correctBits.Length, encodedResult, result, null, String.Format("{0}", correctedBits.ecLevel));
return decoderResult;
}

Expand All @@ -126,17 +127,17 @@ public DecoderResult decode(AztecDetectorResult detectorResult)
/// </summary>
/// <param name="correctedBits"></param>
/// <returns></returns>
public static String highLevelDecode(bool[] correctedBits)
public static String highLevelDecode(bool[] correctedBits, out byte[] encodedResult)
{
return getEncodedData(correctedBits);
return getEncodedData(correctedBits, out encodedResult);
}

/// <summary>
/// Gets the string encoded in the aztec code bits
/// </summary>
/// <param name="correctedBits">The corrected bits.</param>
/// <returns>the decoded string</returns>
private static String getEncodedData(bool[] correctedBits)
private static String getEncodedData(bool[] correctedBits, out byte[] encodedResult)
{
var endIndex = correctedBits.Length;
var latchTable = Table.UPPER; // table most recently latched to
Expand All @@ -148,6 +149,7 @@ private static String getEncodedData(bool[] correctedBits)
// Final decoded string result
// (correctedBits-5) / 4 is an upper bound on the size (all-digit result)
var result = new StringBuilder((correctedBits.Length - 5) / 4);
var encodedResultStream = new MemoryStream();

// Intermediary buffer of decoded bytes, which is decoded into a string and flushed
// when character encoding changes (ECI) or input ends.
Expand Down Expand Up @@ -212,12 +214,14 @@ private static String getEncodedData(bool[] correctedBits)
{
var byteArray = decodedBytes.ToArray();
result.Append(encoding.GetString(byteArray, 0, byteArray.Length));
encodedResultStream.Write(byteArray, 0, byteArray.Length);
decodedBytes.SetLength(0);
}
switch (n)
{
case 0:
result.Append((char)29); // translate FNC1 as ASCII 29
encodedResultStream.WriteByte((byte)29);
break;
case 7:
throw new FormatException("FLG(7) is reserved and illegal");
Expand Down Expand Up @@ -270,7 +274,6 @@ private static String getEncodedData(bool[] correctedBits)
#if (PORTABLE || NETSTANDARD1_0 || NETSTANDARD1_1 || NETFX_CORE)
var b = StringUtils.PLATFORM_DEFAULT_ENCODING_T.GetBytes(str);
#else

var b = Encoding.ASCII.GetBytes(str);
#endif
decodedBytes.Write(b, 0, b.Length);
Expand All @@ -285,7 +288,9 @@ private static String getEncodedData(bool[] correctedBits)
{
var byteArray = decodedBytes.ToArray();
result.Append(encoding.GetString(byteArray, 0, byteArray.Length));
encodedResultStream.Write(byteArray, 0, byteArray.Length);
}
encodedResult = encodedResultStream.ToArray();
}
return result.ToString();
}
Expand Down
89 changes: 87 additions & 2 deletions Source/lib/common/DecoderResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ public sealed class DecoderResult
/// </summary>
public String Text { get; private set; }

/// <summary>
/// bytes representing the encoded text field
/// </summary>
public byte[] Data { get; private set; }

/// <summary>
/// list of byte segments in the result, or null if not applicable
/// </summary>
Expand Down Expand Up @@ -101,7 +106,21 @@ public bool StructuredAppend
public DecoderResult(byte[] rawBytes, String text, List<byte[]> byteSegments, String ecLevel)
: this(rawBytes, text, byteSegments, ecLevel, -1, -1, 0)
{


}

/// <summary>
/// initilizing constructor
/// </summary>
/// <param name="rawBytes"></param>
/// <param name="data"></param>
/// <param name="text"></param>
/// <param name="byteSegments"></param>
/// <param name="ecLevel"></param>
public DecoderResult(byte[] rawBytes, byte[] data, String text, List<byte[]> byteSegments, String ecLevel)
: this(rawBytes, text, byteSegments, ecLevel)
{
Data = data;
}

/// <summary>
Expand All @@ -116,6 +135,22 @@ public DecoderResult(byte[] rawBytes, String text, IList<byte[]> byteSegments, S
: this(rawBytes, text, byteSegments, ecLevel, -1, -1, symbologyModifier)
{
}

/// <summary>
/// initializing constructor
/// </summary>
/// <param name="rawBytes"></param>
/// <param name="data"></param>
/// <param name="text"></param>
/// <param name="byteSegments"></param>
/// <param name="ecLevel"></param>
/// <param name="symbologyModifier"></param>
public DecoderResult(byte[] rawBytes, byte[] data, String text, IList<byte[]> byteSegments, String ecLevel, int symbologyModifier)
: this(rawBytes, text, byteSegments, ecLevel, -1, -1, symbologyModifier)
{
this.Data = data;
}

/// <summary>
/// initializing constructor
/// </summary>
Expand All @@ -133,7 +168,7 @@ public DecoderResult(byte[] rawBytes,
int saParity)
: this(rawBytes, text, byteSegments, ecLevel, saSequence, saParity, 0)
{

}

/// <summary>
Expand All @@ -151,6 +186,23 @@ public DecoderResult(byte[] rawBytes, String text, IList<byte[]> byteSegments, S
{
}

/// <summary>
/// initializing constructor
/// </summary>
/// <param name="rawBytes"></param>
/// <param name="data"></param>
/// <param name="text"></param>
/// <param name="byteSegments"></param>
/// <param name="ecLevel"></param>
/// <param name="saSequence"></param>
/// <param name="saParity"></param>
/// <param name="symbologyModifier"></param>
public DecoderResult(byte[] rawBytes, byte[] data, String text, IList<byte[]> byteSegments, String ecLevel, int saSequence, int saParity, int symbologyModifier)
: this(rawBytes, text, byteSegments, ecLevel, saSequence, saParity, symbologyModifier)
{
Data = data;
}

/// <summary>
/// initializing constructor
/// </summary>
Expand All @@ -164,6 +216,22 @@ public DecoderResult(byte[] rawBytes, int numBits, String text, IList<byte[]> by
{
}

/// <summary>
/// initializing constructor
/// </summary>
/// <param name="rawBytes"></param>
/// <param name="numBits"></param>
/// <param name="data"></param>
/// <param name="text"></param>
/// <param name="byteSegments"></param>
/// <param name="ecLevel"></param>
public DecoderResult(byte[] rawBytes, int numBits, byte[] data, String text, IList<byte[]> byteSegments, String ecLevel)
: this(rawBytes, numBits, text, byteSegments, ecLevel)
{
this.Data = data;
}


/// <summary>
/// initializing constructor
/// </summary>
Expand All @@ -176,12 +244,29 @@ public DecoderResult(byte[] rawBytes, int numBits, String text, IList<byte[]> by
/// <param name="saParity"></param>
/// <param name="symbologyModifier"></param>
public DecoderResult(byte[] rawBytes, int numBits, String text, IList<byte[]> byteSegments, String ecLevel, int saSequence, int saParity, int symbologyModifier)
: this(rawBytes, null, numBits, text, byteSegments, ecLevel, saSequence, saParity, symbologyModifier)
{
}

/// <summary>
/// initializing constructor
/// </summary>
/// <param name="rawBytes"></param>
/// <param name="numBits"></param>
/// <param name="text"></param>
/// <param name="byteSegments"></param>
/// <param name="ecLevel"></param>
/// <param name="saSequence"></param>
/// <param name="saParity"></param>
/// <param name="symbologyModifier"></param>
public DecoderResult(byte[] rawBytes, byte[] data, int numBits, String text, IList<byte[]> byteSegments, String ecLevel, int saSequence, int saParity, int symbologyModifier)
{
if (rawBytes == null && text == null)
{
throw new ArgumentException();
}
RawBytes = rawBytes;
Data = data;
NumBits = numBits;
Text = text;
ByteSegments = byteSegments;
Expand Down
2 changes: 1 addition & 1 deletion Source/lib/datamatrix/DataMatrixReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public Result decode(BinaryBitmap image, IDictionary<DecodeHintType, object> hin
if (decoderResult == null)
return null;

Result result = new Result(decoderResult.Text, decoderResult.RawBytes, points,
Result result = new Result(decoderResult.Text, decoderResult.Data, decoderResult.RawBytes, points,
BarcodeFormat.DATA_MATRIX);
IList<byte[]> byteSegments = decoderResult.ByteSegments;
if (byteSegments != null)
Expand Down
Loading