Issue 829 C# port of Code 39 encoder

git-svn-id: https://zxing.googlecode.com/svn/trunk@1765 59b500cc-1b3d-0410-9834-0bbf25fbcc57
This commit is contained in:
srowen 2011-05-11 17:18:32 +00:00
parent 0e28aa0e4c
commit 66836c74f9
4 changed files with 86 additions and 4 deletions

View file

@ -22,6 +22,7 @@ David Olivier
Diego Pierotto
drejc83
Eduardo Castillejo (University of Deusto)
Emanuele Aina
Eric Kobrin (Velocitude)
Erik Barbara
Fred Lin (Anobiit)

View file

@ -32,7 +32,7 @@ namespace com.google.zxing.oned
public sealed class Code39Reader:OneDReader
{
private const System.String ALPHABET_STRING = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ-. *$/+%";
internal const System.String ALPHABET_STRING = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ-. *$/+%";
//UPGRADE_NOTE: Final was removed from the declaration of 'ALPHABET '. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1003'"
private static readonly char[] ALPHABET = ALPHABET_STRING.ToCharArray();
@ -41,7 +41,7 @@ namespace com.google.zxing.oned
/// with 1s representing "wide" and 0s representing narrow.
/// </summary>
//UPGRADE_NOTE: Final was removed from the declaration of 'CHARACTER_ENCODINGS'. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1003'"
private static readonly int[] CHARACTER_ENCODINGS = new int[]{0x034, 0x121, 0x061, 0x160, 0x031, 0x130, 0x070, 0x025, 0x124, 0x064, 0x109, 0x049, 0x148, 0x019, 0x118, 0x058, 0x00D, 0x10C, 0x04C, 0x01C, 0x103, 0x043, 0x142, 0x013, 0x112, 0x052, 0x007, 0x106, 0x046, 0x016, 0x181, 0x0C1, 0x1C0, 0x091, 0x190, 0x0D0, 0x085, 0x184, 0x0C4, 0x094, 0x0A8, 0x0A2, 0x08A, 0x02A};
internal static readonly int[] CHARACTER_ENCODINGS = new int[]{0x034, 0x121, 0x061, 0x160, 0x031, 0x130, 0x070, 0x025, 0x124, 0x064, 0x109, 0x049, 0x148, 0x019, 0x118, 0x058, 0x00D, 0x10C, 0x04C, 0x01C, 0x103, 0x043, 0x142, 0x013, 0x112, 0x052, 0x007, 0x106, 0x046, 0x016, 0x181, 0x0C1, 0x1C0, 0x091, 0x190, 0x0D0, 0x085, 0x184, 0x0C4, 0x094, 0x0A8, 0x0A2, 0x08A, 0x02A};
//UPGRADE_NOTE: Final was removed from the declaration of 'ASTERISK_ENCODING '. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1003'"
private static readonly int ASTERISK_ENCODING = CHARACTER_ENCODINGS[39];
@ -389,4 +389,4 @@ namespace com.google.zxing.oned
return decoded.ToString();
}
}
}
}

View file

@ -0,0 +1,80 @@
/*
* Copyright 2011 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;
using BarcodeFormat = com.google.zxing.BarcodeFormat;
using ReaderException = com.google.zxing.ReaderException;
using Result = com.google.zxing.Result;
using ResultPoint = com.google.zxing.ResultPoint;
using ByteMatrix = com.google.zxing.common.ByteMatrix;
namespace com.google.zxing.oned
{
/// <summary> <p>Implements decoding of the EAN-13 format.</p>
///
/// </summary>
/// <author> erik.barbara@gmail.com (Erik Barbara)
/// </author>
/// <author> em@nerd.ocracy.org (Emanuele Aina) - Ported from ZXING Java Source
/// </author>
public sealed class Code39Writer:UPCEANWriter
{
public override ByteMatrix encode(string contents, BarcodeFormat format, int width, int height, Hashtable hints) {
if (format != BarcodeFormat.CODE_39) {
throw new ArgumentException("Can only encode CODE_39, but got " + format);
}
return base.encode(contents, format, width, height, hints);
}
public override sbyte[] encode(string contents) {
int length = contents.Length;
if (length > 80) {
throw new ArgumentException("Requested contents should be less than 80 digits long, but got " + length);
}
int[] widths = new int[9];
int codeWidth = 24 + 1 + length;
for (int i = 0; i < length; i++) {
int indexInString = Code39Reader.ALPHABET_STRING.IndexOf(contents[i]);
toIntArray(Code39Reader.CHARACTER_ENCODINGS[indexInString], widths);
for(int j = 0; j < widths.Length; j++) {
codeWidth += widths[j];
}
}
sbyte[] result = new sbyte[codeWidth];
toIntArray(Code39Reader.CHARACTER_ENCODINGS[39], widths);
int pos = appendPattern(result, 0, widths, 1);
int[] narrowWhite = {1};
pos += appendPattern(result, pos, narrowWhite, 0);
//append next character to bytematrix
for(int i = length-1; i >= 0; i--) {
int indexInString = Code39Reader.ALPHABET_STRING.IndexOf(contents[i]);
toIntArray(Code39Reader.CHARACTER_ENCODINGS[indexInString], widths);
pos += appendPattern(result, pos, widths, 1);
pos += appendPattern(result, pos, narrowWhite, 0);
}
toIntArray(Code39Reader.CHARACTER_ENCODINGS[39], widths);
pos += appendPattern(result, pos, widths, 1);
return result;
}
private static void toIntArray(int a, int[] toReturn) {
for (int i = 0; i < 9; i++) {
int temp = a & (1 << i);
toReturn[i] = (temp == 0) ? 1 : 2;
}
}
}
}

View file

@ -232,6 +232,7 @@
<Compile Include="SupportClass.cs" />
<Compile Include="Writer.cs" />
<Compile Include="WriterException.cs" />
<Compile Include="oned\Code39Writer.cs" />
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<PropertyGroup>
@ -240,4 +241,4 @@
<PostBuildEvent>
</PostBuildEvent>
</PropertyGroup>
</Project>
</Project>