|
using System;
using NUnit.Framework;
using GenCode128;
namespace GenCode128Tests
{
/// <summary>
/// Summary description for Content.
/// </summary>
[TestFixture]
public class ContentTests
{
private const int cSHIFT = 98;
private const int cCODEA = 101;
private const int cCODEB = 100;
[Test]
public void CharRangeTests()
{
Assert.AreEqual(Code128Code.CodeSetAllowed.CodeAorB, Code128Code.CodesetAllowedForChar(66), "Incorrect codeset requirement returned");
Assert.AreEqual(Code128Code.CodeSetAllowed.CodeA, Code128Code.CodesetAllowedForChar(17), "Incorrect codeset requirement returned");
Assert.AreEqual(Code128Code.CodeSetAllowed.CodeB, Code128Code.CodesetAllowedForChar(110), "Incorrect codeset requirement returned");
}
[Test]
public void CharTranslationTests()
{
// in CodeA, thischar Either, nextchar Either
byte thischar = 66;
byte nextchar = 66;
CodeSet currcs = CodeSet.CodeA;
CodeSet origcs = currcs;
int[] resultcodes = Code128Code.CodesForChar(new byte[] { thischar, nextchar }, 0, ref currcs);
Assert.IsNotNull(resultcodes, "No codes returned");
Assert.AreEqual(1, resultcodes.Length, "Incorrect number of codes returned");
Assert.AreEqual(34, resultcodes[0], "Incorrect code returned");
Assert.AreEqual(origcs, currcs, "Incorrect code set returned");
// in CodeA, thischar CodeA, nextchar Either
thischar = 1; // "^A"
nextchar = 66;
currcs = CodeSet.CodeA;
origcs = currcs;
resultcodes = Code128Code.CodesForChar(new byte[] { thischar, nextchar }, 0, ref currcs);
Assert.IsNotNull(resultcodes, "No codes returned");
Assert.AreEqual(1, resultcodes.Length, "Incorrect number of codes returned");
Assert.AreEqual(65, resultcodes[0], "Incorrect code returned");
Assert.AreEqual(origcs, currcs, "Incorrect code set returned");
// in CodeA, thischar CodeB, nextchar Either
thischar = 110; // "n"
nextchar = 66;
currcs = CodeSet.CodeA;
origcs = currcs;
resultcodes = Code128Code.CodesForChar(new byte[] { thischar, nextchar }, 0, ref currcs);
Assert.IsNotNull(resultcodes, "No codes returned");
Assert.AreEqual(2, resultcodes.Length, "Incorrect number of codes returned");
Assert.AreEqual(cSHIFT, resultcodes[0], "Incorrect code returned");
Assert.AreEqual(78, resultcodes[1], "Incorrect code returned");
Assert.AreEqual(origcs, currcs, "Incorrect code set returned");
// in CodeA, thischar Either, nextchar -1
thischar = 66; // "B"
currcs = CodeSet.CodeA;
origcs = currcs;
resultcodes = Code128Code.CodesForChar(new byte[] { thischar }, 0, ref currcs);
Assert.IsNotNull(resultcodes, "No codes returned");
Assert.AreEqual(1, resultcodes.Length, "Incorrect number of codes returned");
Assert.AreEqual(34, resultcodes[0], "Incorrect code returned");
Assert.AreEqual(origcs, currcs, "Incorrect code set returned");
// in CodeA, thischar CodeA, nextchar -1
thischar = 1; // "^A"
currcs = CodeSet.CodeA;
origcs = currcs;
resultcodes = Code128Code.CodesForChar(new byte[] { thischar }, 0, ref currcs);
Assert.IsNotNull(resultcodes, "No codes returned");
Assert.AreEqual(1, resultcodes.Length, "Incorrect number of codes returned");
Assert.AreEqual(65, resultcodes[0], "Incorrect code returned");
Assert.AreEqual(origcs, currcs, "Incorrect code set returned");
// in CodeA, thischar CodeB, nextchar -1
thischar = 110; // "n"
currcs = CodeSet.CodeA;
origcs = currcs;
resultcodes = Code128Code.CodesForChar(new byte[] { thischar }, 0, ref currcs);
Assert.IsNotNull(resultcodes, "No codes returned");
Assert.AreEqual(2, resultcodes.Length, "Incorrect number of codes returned");
Assert.AreEqual(cSHIFT, resultcodes[0], "Incorrect code returned");
Assert.AreEqual(78, resultcodes[1], "Incorrect code returned");
Assert.AreEqual(origcs, currcs, "Incorrect code set returned");
// in CodeA, thischar Either, nextchar CodeA
thischar = 66; // "B"
nextchar = 1;
currcs = CodeSet.CodeA;
origcs = currcs;
resultcodes = Code128Code.CodesForChar(new byte[] { thischar, nextchar }, 0, ref currcs);
Assert.IsNotNull(resultcodes, "No codes returned");
Assert.AreEqual(1, resultcodes.Length, "Incorrect number of codes returned");
Assert.AreEqual(34, resultcodes[0], "Incorrect code returned");
Assert.AreEqual(origcs, currcs, "Incorrect code set returned");
// in CodeA, thischar CodeA, nextchar CodeA
thischar = 1; // "^A"
nextchar = 1;
currcs = CodeSet.CodeA;
origcs = currcs;
resultcodes = Code128Code.CodesForChar(new byte[] { thischar, nextchar }, 0, ref currcs);
Assert.IsNotNull(resultcodes, "No codes returned");
Assert.AreEqual(1, resultcodes.Length, "Incorrect number of codes returned");
Assert.AreEqual(65, resultcodes[0], "Incorrect code returned");
Assert.AreEqual(origcs, currcs, "Incorrect code set returned");
// in CodeA, thischar CodeB, nextchar CodeA
thischar = 110; // "n"
nextchar = 1;
currcs = CodeSet.CodeA;
origcs = currcs;
resultcodes = Code128Code.CodesForChar(new byte[] { thischar, nextchar }, 0, ref currcs);
Assert.IsNotNull(resultcodes, "No codes returned");
Assert.AreEqual(2, resultcodes.Length, "Incorrect number of codes returned");
Assert.AreEqual(cSHIFT, resultcodes[0], "Incorrect code returned");
Assert.AreEqual(78, resultcodes[1], "Incorrect code returned");
Assert.AreEqual(origcs, currcs, "Incorrect code set returned");
// in CodeA, thischar Either, nextchar CodeB
thischar = 66; // "B"
nextchar = 110;
currcs = CodeSet.CodeA;
origcs = currcs;
resultcodes = Code128Code.CodesForChar(new byte[] { thischar, nextchar }, 0, ref currcs);
Assert.IsNotNull(resultcodes, "No codes returned");
Assert.AreEqual(1, resultcodes.Length, "Incorrect number of codes returned");
Assert.AreEqual(34, resultcodes[0], "Incorrect code returned");
Assert.AreEqual(origcs, currcs, "Incorrect code set returned");
// in CodeA, thischar CodeA, nextchar CodeB
thischar = 1; // "^A"
nextchar = 110;
currcs = CodeSet.CodeA;
origcs = currcs;
resultcodes = Code128Code.CodesForChar(new byte[] { thischar, nextchar }, 0, ref currcs);
Assert.IsNotNull(resultcodes, "No codes returned");
Assert.AreEqual(1, resultcodes.Length, "Incorrect number of codes returned");
Assert.AreEqual(65, resultcodes[0], "Incorrect code returned");
Assert.AreEqual(origcs, currcs, "Incorrect code set returned");
// in CodeA, thischar CodeB, nextchar CodeB
thischar = 110; // "n"
nextchar = 110;
currcs = CodeSet.CodeA;
origcs = currcs;
resultcodes = Code128Code.CodesForChar(new byte[] { thischar, nextchar }, 0, ref currcs);
Assert.IsNotNull(resultcodes, "No codes returned");
Assert.AreEqual(2, resultcodes.Length, "Incorrect number of codes returned");
Assert.AreEqual(cCODEB, resultcodes[0], "Incorrect code returned");
Assert.AreEqual(78, resultcodes[1], "Incorrect code returned");
Assert.AreNotEqual(origcs, currcs, "Incorrect code set returned");
// in CodeB, thischar Either, nextchar Either
thischar = 66; // "B"
nextchar = 66;
currcs = CodeSet.CodeB;
origcs = currcs;
resultcodes = Code128Code.CodesForChar(new byte[] { thischar, nextchar }, 0, ref currcs);
Assert.IsNotNull(resultcodes, "No codes returned");
Assert.AreEqual(1, resultcodes.Length, "Incorrect number of codes returned");
Assert.AreEqual(34, resultcodes[0], "Incorrect code returned");
Assert.AreEqual(origcs, currcs, "Incorrect code set returned");
// in CodeB, thischar CodeA, nextchar Either
thischar = 1; // "^A"
nextchar = 66;
currcs = CodeSet.CodeB;
origcs = currcs;
resultcodes = Code128Code.CodesForChar(new byte[] { thischar, nextchar }, 0, ref currcs);
Assert.IsNotNull(resultcodes, "No codes returned");
Assert.AreEqual(2, resultcodes.Length, "Incorrect number of codes returned");
Assert.AreEqual(cSHIFT, resultcodes[0], "Incorrect code returned");
Assert.AreEqual(65, resultcodes[1], "Incorrect code returned");
Assert.AreEqual(origcs, currcs, "Incorrect code set returned");
// in CodeB, thischar CodeB, nextchar Either
thischar = 110; // "n"
nextchar = 66;
currcs = CodeSet.CodeB;
origcs = currcs;
resultcodes = Code128Code.CodesForChar(new byte[] { thischar, nextchar }, 0, ref currcs);
Assert.IsNotNull(resultcodes, "No codes returned");
Assert.AreEqual(1, resultcodes.Length, "Incorrect number of codes returned");
Assert.AreEqual(78, resultcodes[0], "Incorrect code returned");
Assert.AreEqual(origcs, currcs, "Incorrect code set returned");
// in CodeB, thischar Either, nextchar -1
thischar = 66; // "B"
currcs = CodeSet.CodeB;
origcs = currcs;
resultcodes = Code128Code.CodesForChar(new byte[] { thischar }, 0, ref currcs);
Assert.IsNotNull(resultcodes, "No codes returned");
Assert.AreEqual(1, resultcodes.Length, "Incorrect number of codes returned");
Assert.AreEqual(34, resultcodes[0], "Incorrect code returned");
Assert.AreEqual(origcs, currcs, "Incorrect code set returned");
// in CodeB, thischar CodeA, nextchar -1
thischar = 1; // "^A"
currcs = CodeSet.CodeB;
origcs = currcs;
resultcodes = Code128Code.CodesForChar(new byte[] { thischar, nextchar }, 0, ref currcs);
Assert.IsNotNull(resultcodes, "No codes returned");
Assert.AreEqual(2, resultcodes.Length, "Incorrect number of codes returned");
Assert.AreEqual(cSHIFT, resultcodes[0], "Incorrect code returned");
Assert.AreEqual(65, resultcodes[1], "Incorrect code returned");
Assert.AreEqual(origcs, currcs, "Incorrect code set returned");
// in CodeB, thischar CodeB, nextchar -1
thischar = 110; // "n"
currcs = CodeSet.CodeB;
origcs = currcs;
resultcodes = Code128Code.CodesForChar(new byte[] { thischar }, 0, ref currcs);
Assert.IsNotNull(resultcodes, "No codes returned");
Assert.AreEqual(1, resultcodes.Length, "Incorrect number of codes returned");
Assert.AreEqual(78, resultcodes[0], "Incorrect code returned");
Assert.AreEqual(origcs, currcs, "Incorrect code set returned");
// in CodeB, thischar Either, nextchar CodeA
thischar = 66; // "B"
nextchar = 1;
currcs = CodeSet.CodeB;
origcs = currcs;
resultcodes = Code128Code.CodesForChar(new byte[] { thischar, nextchar }, 0, ref currcs);
Assert.IsNotNull(resultcodes, "No codes returned");
Assert.AreEqual(1, resultcodes.Length, "Incorrect number of codes returned");
Assert.AreEqual(34, resultcodes[0], "Incorrect code returned");
Assert.AreEqual(origcs, currcs, "Incorrect code set returned");
// in CodeB, thischar CodeA, nextchar CodeA
thischar = 1; // "^A"
nextchar = 1;
currcs = CodeSet.CodeB;
origcs = currcs;
resultcodes = Code128Code.CodesForChar(new byte[] { thischar, nextchar }, 0, ref currcs);
Assert.IsNotNull(resultcodes, "No codes returned");
Assert.AreEqual(2, resultcodes.Length, "Incorrect number of codes returned");
Assert.AreEqual(cCODEA, resultcodes[0], "Incorrect code returned");
Assert.AreEqual(65, resultcodes[1], "Incorrect code returned");
Assert.AreNotEqual(origcs, currcs, "Incorrect code set returned");
// in CodeB, thischar CodeB, nextchar CodeA
thischar = 110; // "n"
nextchar = 1;
currcs = CodeSet.CodeB;
origcs = currcs;
resultcodes = Code128Code.CodesForChar(new byte[] { thischar, nextchar }, 0, ref currcs);
Assert.IsNotNull(resultcodes, "No codes returned");
Assert.AreEqual(1, resultcodes.Length, "Incorrect number of codes returned");
Assert.AreEqual(78, resultcodes[0], "Incorrect code returned");
Assert.AreEqual(origcs, currcs, "Incorrect code set returned");
// in CodeB, thischar Either, nextchar CodeB
thischar = 66; // "B"
nextchar = 110;
currcs = CodeSet.CodeB;
origcs = currcs;
resultcodes = Code128Code.CodesForChar(new byte[] { thischar, nextchar }, 0, ref currcs);
Assert.IsNotNull(resultcodes, "No codes returned");
Assert.AreEqual(1, resultcodes.Length, "Incorrect number of codes returned");
Assert.AreEqual(34, resultcodes[0], "Incorrect code returned");
Assert.AreEqual(origcs, currcs, "Incorrect code set returned");
// in CodeB, thischar CodeA, nextchar CodeB
thischar = 1; // "^A"
nextchar = 110;
currcs = CodeSet.CodeB;
origcs = currcs;
resultcodes = Code128Code.CodesForChar(new byte[] { thischar, nextchar }, 0, ref currcs);
Assert.IsNotNull(resultcodes, "No codes returned");
Assert.AreEqual(2, resultcodes.Length, "Incorrect number of codes returned");
Assert.AreEqual(cSHIFT, resultcodes[0], "Incorrect code returned");
Assert.AreEqual(65, resultcodes[1], "Incorrect code returned");
Assert.AreEqual(origcs, currcs, "Incorrect code set returned");
// in CodeB, thischar CodeB, nextchar CodeB
thischar = 110; // "n"
nextchar = 110;
currcs = CodeSet.CodeB;
origcs = currcs;
resultcodes = Code128Code.CodesForChar(new byte[] { thischar, nextchar }, 0, ref currcs);
Assert.IsNotNull(resultcodes, "No codes returned");
Assert.AreEqual(1, resultcodes.Length, "Incorrect number of codes returned");
Assert.AreEqual(78, resultcodes[0], "Incorrect code returned");
Assert.AreEqual(origcs, currcs, "Incorrect code set returned");
}
[Test]
public void CharCompatibilityTests()
{
int thischar = 66;
Assert.AreEqual(true, Code128Code.CharCompatibleWithCodeset(thischar, -1, CodeSet.CodeA), "Compat test failed");
thischar = 66; // "B"
Assert.AreEqual(true, Code128Code.CharCompatibleWithCodeset(thischar, -1, CodeSet.CodeB), "Compat test failed");
thischar = 17; // "^Q"
Assert.AreEqual(true, Code128Code.CharCompatibleWithCodeset(thischar, -1, CodeSet.CodeA), "Compat test failed");
thischar = 17; // "^Q"
Assert.AreEqual(false, Code128Code.CharCompatibleWithCodeset(thischar, -1, CodeSet.CodeB), "Compat test failed");
thischar = 110; // "n"
Assert.AreEqual(false, Code128Code.CharCompatibleWithCodeset(thischar, -1, CodeSet.CodeA), "Compat test failed");
thischar = 110; // "n"
Assert.AreEqual(true, Code128Code.CharCompatibleWithCodeset(thischar, -1, CodeSet.CodeB), "Compat test failed");
}
[Test]
public void CharValueTranslationTests()
{
Assert.AreEqual(0, Code128Code.CodeValueForChar(32, CodeSet.CodeA), "Code translation wrong");
Assert.AreEqual(31, Code128Code.CodeValueForChar(63, CodeSet.CodeA), "Code translation wrong");
Assert.AreEqual(32, Code128Code.CodeValueForChar(64, CodeSet.CodeA), "Code translation wrong");
Assert.AreEqual(63, Code128Code.CodeValueForChar(95, CodeSet.CodeA), "Code translation wrong");
Assert.AreEqual(64, Code128Code.CodeValueForChar(96, CodeSet.CodeB), "Code translation wrong");
Assert.AreEqual(64, Code128Code.CodeValueForChar(0, CodeSet.CodeA), "Code translation wrong");
Assert.AreEqual(95, Code128Code.CodeValueForChar(31, CodeSet.CodeA), "Code translation wrong");
}
[Test]
public void FullStringTest()
{
Code128Content content = new Code128Content("BarCode 1");
int[] result = content.Codes;
Assert.AreEqual(12, result.Length, "Wrong number of code values in result");
Assert.AreEqual(104, result[0], "Start code wrong");
Assert.AreEqual(34, result[1], "Code value #1 wrong");
Assert.AreEqual(65, result[2], "Code value #2 wrong");
Assert.AreEqual(82, result[3], "Code value #3 wrong");
Assert.AreEqual(35, result[4], "Code value #4 wrong");
Assert.AreEqual(79, result[5], "Code value #5 wrong");
Assert.AreEqual(68, result[6], "Code value #6 wrong");
Assert.AreEqual(69, result[7], "Code value #7 wrong");
Assert.AreEqual(0, result[8], "Code value #8 wrong");
Assert.AreEqual(17, result[9], "Code value #9 wrong");
Assert.AreEqual(33, result[10], "Checksum wrong");
Assert.AreEqual(106, result[11], "Stop character wrong");
content = new Code128Content("\x11S12345");
result = content.Codes;
Assert.AreEqual(10, result.Length, "Wrong number of code values in result");
}
[Test]
public void KnownBug1()
{
Code128Content content = new Code128Content("55sr ");
int[] result = content.Codes;
Assert.AreEqual(8, result.Length, "Wrong number of code values in result");
Assert.AreEqual(105, result[0], "Start code wrong");
Assert.AreEqual(55, result[1], "Code value #1 wrong");
Assert.AreEqual(100, result[2], "Code value #2 wrong");
Assert.AreEqual(83, result[3], "Code value #3 wrong");
Assert.AreEqual(82, result[4], "Code value #4 wrong");
Assert.AreEqual(0, result[5], "Code value #5 wrong");
Assert.AreEqual(10, result[6], "Checksum wrong");
Assert.AreEqual(106, result[7], "Stop character wrong");
}
[Test]
public void KnownBug2()
{
Code128Content content = new Code128Content("2009 w/");
int[] result = content.Codes;
Assert.AreEqual(9, result.Length, "Wrong number of code values in result");
Assert.AreEqual(105, result[0], "Start code wrong");
Assert.AreEqual(20, result[1], "Code value #1 wrong");
Assert.AreEqual(9, result[2], "Code value #2 wrong");
Assert.AreEqual(100, result[3], "Code value #3 wrong");
Assert.AreEqual(0, result[4], "Code value #4 wrong");
Assert.AreEqual(87, result[5], "Code value #5 wrong");
Assert.AreEqual(15, result[6], "Code value #6 wrong");
Assert.AreEqual(41, result[7], "Checksum wrong");
Assert.AreEqual(106, result[8], "Stop character wrong");
}
[Test]
public void Code3Shorter()
{
Code128Content content = new Code128Content("aa00aa");
int[] result = content.Codes;
Assert.AreEqual(9, result.Length, "Wrong number of code values in result");
Assert.AreEqual(104, result[0], "Start code wrong");
Assert.AreEqual(65, result[1], "Code value #1 wrong");
Assert.AreEqual(65, result[2], "Code value #2 wrong");
Assert.AreEqual(16, result[3], "Code value #3 wrong");
Assert.AreEqual(16, result[4], "Code value #4 wrong");
Assert.AreEqual(65, result[5], "Code value #5 wrong");
Assert.AreEqual(65, result[6], "Code value #6 wrong");
Assert.AreEqual(96, result[7], "Checksum wrong");
Assert.AreEqual(106, result[8], "Stop character wrong");
}
[Test]
public void Code3Shorter2()
{
Code128Content content = new Code128Content("aa0000aa");
int[] result = content.Codes;
Assert.AreEqual(11, result.Length, "Wrong number of code values in result");
Assert.AreEqual(104, result[0], "Start code wrong");
Assert.AreEqual(65, result[1], "Code value #1 wrong");
Assert.AreEqual(65, result[2], "Code value #2 wrong");
Assert.AreEqual(99, result[3], "Code value #3 wrong");
Assert.AreEqual(0, result[4], "Code value #4 wrong");
Assert.AreEqual(0, result[5], "Code value #5 wrong");
Assert.AreEqual(100, result[6], "Code value #6 wrong");
Assert.AreEqual(65, result[7], "Code value #6 wrong");
Assert.AreEqual(65, result[8], "Code value #6 wrong");
Assert.AreEqual(8, result[9], "Checksum wrong");
Assert.AreEqual(106, result[10], "Stop character wrong");
}
[Test]
[ExpectedException(typeof(Exception))]
public void ShouldntBeAbleToDoAnInvalidEncode()
{
//Code A doesn't support lower case... this should throw some form of an exception.
GenCode128.Code128Code.CodeValueForChar(115, CodeSet.CodeA);
}
[Test]
[ExpectedException(typeof(Exception))]
public void ShouldntBeAbleToDoAnInvalidEncode2()
{
//Code B doesn't support non printing characters... this should throw some form of an exception.
GenCode128.Code128Code.CodeValueForChar(16, CodeSet.CodeB);
}
}
}
|
|
|
|
|
Thanks for this excellent article.
Btw, I think I've found a bug. If my barcode is one character length, then I get an IndexOutOfRangeException on line 53:
Code128Code.CodeSetAllowed csa2 = asciiBytes.Length>0 ? Code128Code.CodesetAllowedForChar( asciiBytes[1] ) : Code128Code.CodeSetAllowed.CodeAorB;
If we put this conditional: "asciiBytes.Length>1 " the problem is gone, but I'm not sure if the implementation will be OK.
Thanks.
Gustavo.
|
|
|
|
|
Ouch! You're quite right!
Yes, I'm pretty sure that your proposed solution is the correct one. When the conditional operator using the "1" comparison evaluates false, it would be applying the CodeAorB value, which indicates that we don't care whether it's A or B -- exactly the desired outcome if there's no character for it to represent.
Thanks for taking the time to look into the code to find the solution.
|
|
|
|
|
Hallo i have a question about the lenght ot the code? How many bites can code128 be??
The article is very usefull??
|
|
|
|
|
The length is driven by the practical consideration of what your scanner can read. It's got to have an angle wide enough to view the entire "message" while at a distance that's close enough for it to illuminate the whole thing. This is going to depend on the device you're using, so I would suggest that you just experiment to see what you can get. This library can easily handle anything you'll be able to use in the real world.
|
|
|
|
|
Hi,
First of all: Nice application!
I have tried it, it works very well.
Now I would like to use the printfunction to print the barcode to a label. I am using a Dymo label printer for this. Where should I configure to print the barcode to a label?
|
|
|
|
|
I don't know anything about Dymo label printers, and since the EXE that I included is only intended to be a sample for the barcodes themselves, the printing support is absolutely minimal. In particular, it doesn't even let you select the printer -- it just uses the system's default printer.
You might go to Printers and Faxes in your Control Panel, and set the Dymo device to be the default (you can always switch back later). Then use the application to print and see what happens.
Good luck, but I make no promises.
|
|
|
|
|
You can print to a particular printer by setting the "PrinterName".
<br />
public void PrintToPrinter( string printerName, System.Drawing.Printing.PrintDocument printDocument1 )<br />
{<br />
PrintDialog pd = new PrintDialog();<br />
pd.Document = printDocument1;<br />
pd.PrinterSettings.PrinterName = printerName ;<br />
try<br />
{<br />
pd.Document.Print();<br />
}<br />
catch<br />
{<br />
MessageBox.Show("INVALID PRINTER SPECIFIED");<br />
}<br />
}<br />
http://www.blakerobertson.com
|
|
|
|
|
Just wanted to thank you for your efforts. I am using your application to generate barcodes now.
|
|
|
|
|
I saw
"Error 2 The type or namespace name 'Test' could not be found (are you missing a using directive or an assembly reference?) D:\GenCode128\GenCode128Tests\ContentTest.cs 18 8 GenCode128Tests "
when I compiling your sample code. There was not "NUnit.Framework" in your files. Please help me to solve my problem. I am using Visual 2005.
|
|
|
|
|
That is a good question -- I probably should have noted this in the article.
Solution 1: Just remove the GenCode128Tests project from the solution. They're for testing only, and have no effect on the finished product.
Solution 2: Get NUnit. It's free, and you can download it from http://nunit.org/[^] . It's worth looking at; it might help your code too.
|
|
|
|
|
Hi,
I like this barcode.
However, how can I add in the "Title" at the top of the barcode and the "barcode number" below the barcode?
The "barcode number" is more useful, in-term tat most ppl use it :X then the title
Thanks
-- modified at 1:03 Monday 13th November, 2006
|
|
|
|
|
I finally got some free time after handing over some of my sites to other staff.
I wrote the code to the bottom part of the make image class so I could have the Barcode Number at the bottom, hope these code will help some1...
and i dun think my code is efficent enuf
/*******************************Start of Barcode Number Text*******************************/
Image text = new Bitmap(width, 10);
Font txtFont = new Font("Arial", 8, FontStyle.Regular, GraphicsUnit.Pixel);
using (Graphics txtTool = Graphics.FromImage(text))
{
txtTool.FillRectangle(Brushes.White, 0, 0, width, 10);
int cursor = AddQuietZone ? cQuietWidth * BarWeight : 0;
txtTool.DrawString(InputData.ToUpper(), txtFont, Brushes.Black, cursor, 0);
}
/********************************End of Barcode Number Text********************************/
/*******************************Start of Merging Image*******************************/
Image merged = new Bitmap(width, height + 10);
using (Graphics mergeTool = Graphics.FromImage(merged))
{
mergeTool.DrawImage(myimg, 0, 0);
mergeTool.DrawImage(text, 0, height);
}
/********************************End of Merging Image********************************/
|
|
|
|
|
Cool. Thanks for the contribution. FWIW, what you've done is probably about the same way I'd have approached it. I don't think my code in this library is the optimally efficient, but I tested the performance a bit, and found that even if it might be better, it's still pretty darned fast.
I'm not sure when I'll get around to it, but do you mind if I incorporate your code into the next revision of the library?
|
|
|
|
|
|
Does anyone have a working version of this code that they could possibly send to me please because when I download the zip it will not open, if anyone can post a link or email me the code I would be eternally grateful.
Ian
|
|
|
|
|
Hi,
Thanks for this terrific article!
I have converted your code to VB for an ASP.Net 2.0 project. Only problem is the ASP picturebox does not take an image parameter. It does have an imageurl parameter which works ok if you first write out the barcode image to disk and then plug the path/file ID into the imageurl. Do you know of a way to place the image directly onto a web page? I'd like to avoid the disk I/O.
PS: If you would like a copy of my ASP/VB sample project let me know.
Bill
|
|
|
|
|
That makes sense, as there's no way to put anything but text and (some types of) formatting information "directly on a web page". The web page itself is nothing but HTML. Everything graphic on a page -- banners, etc. -- is acquired by subsequent requests for resources referenced by the main HTML.
So you need a way to return to the browser the image that should be put into that site on the page. I think there are 2 ways to approach this.
- Pre-generate the image when the HTML is being built, so that the request for the image can be trivially handled by retrieving a file that actually exists.
- Encoding barcode content in the image URL without actually generating the image; then generate the image on-the-fly.
I don't like #1 because it forces you to worry about managing the files (e.g., deleting them periodically) as well as worrying about allowing file write permissions to the filesystem.
However, #2 is quite practical. Any reasonable barcode should have a value short enough to easily express as a URL with a GET request. So you always include an image URL of
http://myserver.com/myapp/barcode.aspx?code=abcdef
When your barcode.aspx page is invoked, it just needs to return an image encoding abcdef rather than HTML as you're accustomed. This might sound difficult, but it's pretty easy. A quick search here yields some candidates:
- http://www.codeproject.com/aspnet/aspnet_web_graphics.asp[^]
- http://www.codeproject.com/aspnet/DynamicASPDotNETTextImage.asp[^]
Good luck!
-- modified at 11:37 Thursday 3rd August, 2006
|
|
|
|
|
hi,
I am trying to generate the barcode on a asp.net page.
I am a beginner.
I tried using your code but I was unsuccesful.
Can you please help me out.
thank you
sai
|
|
|
|
|
Hi,
Can you please tell me how to read the barcode image that has been generated from this program?
Thanks in advance..
Regards,
M.Kumar
|
|
|
|
|
That's hugely dependent on the hardware that you're using. In my experience, any barcode reader (whether that's one of Symbol's products or even a CueCat) can handle the Code128 symbology.
Given that the scanner can read and decode the symbols, you've still got to get the data from the scanner into your application. By far the easiest way to do this is to set up the scanner as a "keyboard wedge". Many scanners are actually physically installed in this way: they plug into your PC's PS2 keyboard jack, and your actual keyboard plugs into that connector. Other scanners may have a USB or Serial interface, but these generally come with driver software allowing it to function as if it were a keyboard wedge.
So, assuming that you've got your scanner acting, one way or another, as a keyboard wedge, you don't need to do any programming at all. When the user scans something, it's entered into the computer precisely as if he had typed it on the "real" keyboard. That means that if the cursor is in an edit control, and the user scans a barcode, the value will go into that edit control. This even works in web apps!
You should be aware that scanners can generally be configured to append different characters to the end of what was scanned, usually either nothing, or a Tab, or a Carriage Return. You can use this to your advantage: if they configure it with a Tab, then scanning a value will automatically result in Tabbing into the next field, ready for the next scan.
You can even play games with this. Note that in my article I said that I needed to embed control characters in the barcode. This was so that the scanned barcode would behave as if it were the user typing Ctrl-Q, which happens to be a hotkey to invoke a command on the menu bar. Thus, if your users are away from the keyboard and mouse (or don't even have one!), you can encode commands (e.g., Save, Add, Delete, etc.) into special "command" barcodes. You print out these barcodes and put them on a clipboard or on the wall next to their workstation, and they can save a lot of mousing.
|
|
|
|
|
I am just looking for the C# code that will read the barcode value from the barcode image generated by this program. Basically, I am trying to find out a functionality like my application will generate a barcode based on the current login user and same barcode will retrieve by my application for identifying the user who will send it as fax document along with documents to be uploaded into folder that has been defined for him.
Please get back to me if you know any idea....
|
|
|
|
|
I see. You want a sort of "soft scanner". I've never needed to do this, but Google is our friend . This looks promising: Reading Barcodes from an Image - II
(But the referenced article is for Code39. Presumably you can either enhance it yourself, or find an alternate)
|
|
|
|
|
Hi
I'm using this lib in my projects, and I find it very usefull. But I use long strings in my barcodes (mainly numbers) and I'd like to know if you can complete your code by adding the C encoding ?
Thanks
Dédé
|
|
|
|
|
Your interest is noted. I was thinking about this last night, actually. The actual barcode generation is pretty easy. The more difficult part is the algorithms for deciding which code set to start in, and to look ahead to optimize shifts.
I've pretty much decided that it would have been easier if I'd used a flag-type enum to indicate which codeset(s) could be used. It'll take a little while for me to rewrite it this way.
Unfortunately, I'm not sure when I'll have the opportunity to get back to make this enhancement (since it already works fine for what my project needs).
Is there anyone else out there in CodeProject land that can't use this library because it's lacking this feature?
|
|
|
|
|