Generating GS1 DataMatrix using ZXing.Net - c#

What I need
Is to generate a working GS1 DataMatrix, using this test content:
(240)1234567890(10)AA12345(11)123456(21)1(96)1234567
Steps
I've downloaded the nuget package from here:
and
I've created a console app that uses this code:
private static void DoGs1DataMatrixStuff()
{
var writer = new BarcodeWriter
{
Format = BarcodeFormat.DATA_MATRIX
};
writer
.Write("(240)1234567890(10)AA12345(11)123456(21)1(96)1234567")
.Save(#"C:\Temp\barcode.png");
}
There's no obvious specific GS1_DataMatrix format I can use ...
that gives me
which if read by a scanner app on my smartphone, gives the literal content that I originally presented, not with the FNC1 formatting that I expect for GS1:
(240)1234567890(10)AA12345(11)123456(21)1(96)1234567
while it should be
2401234567890 10AA12345 11123456211 961234567
From another source (not a source I can use) I got this barcode:
Using my smartphone app this reads into the correct data.
Question
How can I recreate this working GS1 datamatrix, using ZXing.Net?
also see
this link, Chris Bahns raises the same concern I have, but his request didn't get a working answer.

You have to use a formatted string with ASCII character 29 (GS - Group Separator):
< GS >2401234567890< GS >10AA12345< GS >11123456211< GS >961234567
(replace the "< GS >" with ASCII 29)
ZXing.Net supports the GS symbol with the ASCII encoder since version 0.15. It replaces the ASCII 29 value with the FNC1 codeword (232) in the resulting datamatrix image.
That's only a low level support. There is no built in class or something similar which understands AI (application identifiers) with fixed or variable length (similar to the result parser classes for vCards, vEvent, ISBN, ...).

Related

Convert in Another Format With Character Encoding

I am using Oracle 10g database for a C# application. The issue is, in the database, the NVARCHAR column doesn't save other languages except English. As NVARCHAR supports Unicode, this should be working. But instead I've tried a simple method using a tutorial as follows:
Encoding ascii = Encoding.ASCII;
Encoding unicode = Encoding.Unicode;
//Convert the string into a byte[].
byte[] unicodeBytes = ascii.GetBytes("আমার সোনার বাংলা!"); //Text to show
//Perform the conversion from one encoding to the other.
byte[] asciiBytes = Encoding.Convert(ascii, unicode, unicodeBytes);
char[] asciiChars = new char[ascii.GetCharCount(asciiBytes, 0, asciiBytes.Length)];
ascii.GetChars(asciiBytes, 0, asciiBytes.Length, asciiChars, 0);
string asciiString = new string(asciiChars);
Console.WriteLine(asciiString);
Console.ReadKey();
May seems silly but was expecting if I can show the text in the console app with the above format. Now it shows question (??????) marks. Any way I can show the text and save it at least in any other format, so I can retrieve and show it appropriately in the front-end.
If you can use unicode (and you should, hey its 2018), then it would be best to avoid Bijoy altogether. Process and store everything that is a string, as System.String in .NET and as NVARCHAR in Oracle.
The Windows console can handle unicode without any problems, if we observe two important prerequisites that the documentation clearly states:
Support for Unicode [...] requires a font that has the glyphs needed to render that character. To successfully display Unicode characters to the console, the console font must be set to a [...] font such as Consolas or Lucida Console
This is something you must ensure in Windows setting, independently from your .NET application.
The second prerequisite, emphasis mine:
[...] Console class supports UTF-8 encoding [...] Beginning with the .NET Framework 4.5, the Console class also supports UTF-16 encoding [...] To display Unicode characters to the console. you set the OutputEncoding property to either UTF8Encoding or UnicodeEncoding.
What the documentation does not say, is that none of the fonts that can be selected from the properties menu of the console window will normally contain glyphs of all alphabets in the world. If you needed right-to-left capability as for example with Hebrew or Arabic, you're out of luck.
If the program is running a Windows version without the east asian fonts preinstalled, follow this tutorial to install the Bangla LanguageInterfacePack (KB3180030).
Then apply this answer to our problem as follows:
open the windows registry editor
navigate to HKLM\Software\Microsoft\WindowsNT\CurrentVersion\Console\TrueTypeFont
create a new string value, assign an available key like "000", and the value "Bangla Medium"
reboot the PC
Now set the console font to "Bangla", using the window menu of the console, last menu item "Properties", second tab "Font".
Finally get rid of all that encoding back and forth, and simply write:
using System;
using System.Text;
namespace so49851713
{
class Program
{
public static void Main()
{
var mbb = "\u263Aআমার সোনার বাংলা!";
/* prepare console (once per process) */
Console.OutputEncoding = UTF8Encoding.UTF8;
Console.WriteLine(mbb);
Console.ReadLine();
}
}
}

How to encode RFID label for Zebra Printer using C# and .NET?

Problem
I am using Visual Studio, C#, and .NET. I am also using ZPL commands to print hello world to a Zebra ZD500R. I have been currently unsuccessful in figuring out how to encode an RFID label using the above. I know a SDK for java exists, but my environment is a strict Microsoft stack.
The RFID label that is printed has VOID printed on the entire label. Something like VOIDVOIDVOIDVOIDVOIDVOID. I have included the updated code below.
Code
Here is the following code I have currently written after studying Zebra documentation and sample code:
public static void PrintLabels()
{
try
{
string ipAddress = "127.0.0.1";
int port = 1337;
// ZPL Command(s)
string ZPLString =
"CT~~CD,~CC^~CT~" +
"^XA~TA000~JSN^LT0^MNW^MTT^PON^PMN^LH0,0^JMA^PR4,4~SD10^JUS^LRN^CI0^XZ" +
"^XA" +
"^MMT" +
"^PW300" +
"^LL0526" +
"^LS0" +
"^FT267,433^A0I,42,40^FH\\^FDHello World^FS" +
"^RS8" +
"^RFW,H^FD1234^FS" +
"^PQ1,0,1,Y^XZ";
// Open connection
System.Net.Sockets.TcpClient client = new System.Net.Sockets.TcpClient();
client.Connect(ipAddress, port);
// Write ZPL String to connection
System.IO.StreamWriter writer =
new System.IO.StreamWriter(client.GetStream());
writer.Write(ZPLString);
writer.Flush();
// Close Connection
writer.Close();
client.Close();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
This is a rudimentary example, but does the trick by printing hello world. As far as I know, I was thinking the RFID encoding aspect should be a new line in the string ZPLString. Unfortunately, when I looked into the Zebra programming book I did not see any ZPL commands to confirm this thought.
Attempts
I have done the following in an attempt to solve this problem.
RTFM for Zebra ZPL found here
Downloaded the Zebra Label designer. No encoding function found.
Browsed the ZPL sample code.
Checked stack overflow for relevant questions. Closest I found was this or this, but still not my use case.
Any advice, tips, or comments would be deeply appreciated.
Some notes -
Check out page 381 (table 18) for a list of commands compatible with that printer.
Pages 400 and 401 contain some writing examples. The first one is:
^XA
^RS8
^RFW,A^FD00 my data^FS
^XZ
Let's dissect this. ^XA (start) and ^XZ (end). Plus two others: ^RS and ^RF.
^RS – Set Up RFID Parameters
UHF Printers
0 = None
1 = EPC Class 0
2 = EPC Class 0 Plus
3 = EPC Class 1 64-bit
4 = EPC Class 1 96-bit
5 = UCODE EPC 1.19
6 = Impinj Class 0 Plus
7 = ISO 18000-06A
8 = EPC Class 1, Generation 2 (Gen 2)
9 = ISO 18000-06B
Not sure which transponder you're using. Adjust accordingly.
Then there's ^RF: Read or Write RFID Format.
In this example, they're doing a write operation in ASCII (^RFW,A)
Next, we see
^FD00 my data^FS
Which is the begin and end marker for data.
Another example, this time writing hex:
^XA
^RFW,H^FD1234^FS
^XZ
You see the ,H used, instead of ,A. Again, we see ^FD and ^FS delimiting data.
In Summary...
I'm guessing that you want something like this (let's assume you're writing hex):
^XA
^FO10,10
^A0,100,150
^FDHello, World!^FS
^RS8
^RFW,H^FD1234^FS
^XZ
So, in c#:
string ZPLString = "^XA^FO10,10^A0,100,150^FDHello, World!^FS^RS8^RFW,H^FD1234^FS^XZ";
You MAY need to throw in a ^RS and go off of the above chart.
When sending the command string, you don't need to add in any extra line feeds or CR's.
std_disclaimer: I have only used ZPL to print, not for RFID encoding. Make sure that you have the correct transponders for this unit. According to docs, it supports tags compatible with UHF EPC Gen 2 V1.2/
ISO 18000-6C.
I would test out your ZPL using Zebra's utilities first. If it works, then start playing in c#. Just trying to limit the number of variables at play ;)

C# Web scraper copying text

I have a web scraper written in C# for extracting data. I want to copy text from the web browser control and paste it into a Word file programmatically. When I try to extract rich text box content using its ID and InnerText, the text contains encoded characters like %2c.
I need to get the text with all formatting but I can't find any way. I have tried Encoding, HTTPUtility.UrlDecode, SendKeys and elem.InvokeMember() without success.
How can I programmatically copy and paste text from web browser control preserving formatting?
Here is the sample data to extract:
Description
The Advance Concepts Engineering team designs and develops new vehicles which will meet future regulatory requirements and customer competitive requirements. A qualified candidate will be responsible for the total vehicle packaging. The candidate will identify and resolve adaptation and packaging issues as the vehicle moves toward production. They will lead cross functional team meetings working with Systems & Components, Advance Manufacturing, Service, etc. to ensure that the solutions are optimized for all stages of the vehicle's life.
HtmlElement elem = wb.Document.GetElementById("ctl00_contplhDynamic_txtDescrContentHiddenTextarea");
if (elem == null) return;
elem.InvokeMember("Click");
//elem.InvokeMember("Select All");
//elem.InvokeMember("Copy");
SendKeys.SendWait("^a");
SendKeys.SendWait("^c");
Clipboard.Clear();
elem.Focus();
elem.InvokeMember("Right Click");
elem.InvokeMember("Select All");
elem.InvokeMember("Copy");
Clipboard.SetText(elem.InnerText);
string clipbrdText = Clipboard.GetText();
string data = elem.InnerText;
richTextBox1.Text = data;
string temp = System.Web.HttpUtility.UrlDecode(data);
Encoding iso = Encoding.GetEncoding("windows-1252");
Encoding utf8 = Encoding.UTF8;
byte[] utfBytes = utf8.GetBytes(data);
byte[] isoBytes = Encoding.Convert(utf8, iso, utfBytes);
string msg = iso.GetString(isoBytes);
The text with "%2c" etc has been encoded. If you are getting the content of a web page, you are decoding the HTML, not the URL. You can use HttpUtility.HtmlDecode, or if you are using .NET 4.0 or above you can also use WebUtility.HtmlDecode - this is available within the System.Net namespace.
You should note that Word does not use HTML for its formatting, so you won't be able to paste HTML tags and expect it to recognise them. i.e. <strong>Description</strong> will not result in bold text if you type that into Word.
EDIT:
It looks like you are mixing two different ways to copy the text in the code you pasted - both SendKeys.SendWait("^c"); and elem.InvokeMember("Copy");. I presume both of these methods work?
I think the problem you are having lies in the way you are getting the text. I see you're using Clipboard.GetText() to get the text. Try specifying that it is formatted text using Clipboard.GetText(TextDataFormat.Rtf) or Clipboard.GetText(TextDataFormat.Html). This should hopefully copy the string preserving the formatting.

Read UNIX encoded file with C#

I have c# program we use to replace some Values with others, to be used after as parameters. Like 'NAME1' replaced with &1, 'NAME2' with &2, and so on.
The problem is that the data to modify is on a text file encoded on UNIX, and special characters like í, which even on memory, gets read as a square(Invalid char). Due specifications that are out of my control, the file can't be changed and have no other choice than read it like that.
I have tryed to read with most of the 130 Encodings c# offers me with:
EncodingInfo[] info = System.Text.Encoding.GetEncodings();
string text;
for (int a = 0; a < info.Length; ++a)
{
text = File.ReadAllText(fn, info[a].GetEncoding());
File.WriteAllText(fn + a, text, info[a].GetEncoding());
}
fn is the file path to read. Have checked all the made files(like 130), no one of them writes properly the í so im out of ideas and im unable to find anything on internet.
SOLUTION:
Looks like finally this code made the work to get the text properly, also, had to fix the same encoder for the Writing part:
System.Text.Encoding encoding = System.Text.Encoding.GetEncodings()[41].GetEncoding();
String text = File.ReadAllText(fn, encoding); // get file text
// DO ALL THE STUFF I HAD TO
File.WriteAllText(fn, text, encoding) System.Text.Encoding.GetEncodings()[115].GetEncoding(); //Latin 9 (ISO)
/* ALL THIS ENCODINGS WORKED APARENTLY FOR ME WITH ALL WEIRD CHARS I WAS ABLE TO WRITE :P
System.Text.Encoding.GetEncodings()[108].GetEncoding(); //Baltic (ISO)
System.Text.Encoding.GetEncodings()[107].GetEncoding(); //Latin 3 (ISO)
System.Text.Encoding.GetEncodings()[106].GetEncoding(); //Central European (ISO)
System.Text.Encoding.GetEncodings()[105].GetEncoding(); //Western European (ISO)
System.Text.Encoding.GetEncodings()[49].GetEncoding(); //Vietnamese (Windows)
System.Text.Encoding.GetEncodings()[45].GetEncoding(); //Turkish (Windows)
System.Text.Encoding.GetEncodings()[41].GetEncoding(); //Central European (Windows) <-- Used this one
*/
Thank you very much for your help
Noman(1)
you have to get the proper encoding format. try
use file -i. That will output MIME-type information for the file,
which will also include the character-set encoding. I found a
man-page for it, too :)
Or try enca
It can guess and even convert between encodings. Just look at
the man page.
If you have the proper encoding format, look for a way to apply it to your file reading.
Quotes: How to find encoding of a file in Unix via script(s)

Which library to use to extract text from images?

I am writing a program that when given an image of a low level math problem (e.g. 98*13) should be able to output the answer. The numbers would be black, and the background white. Not a captcha, just an image of a math problem.
The math problems would only have two numbers and one operator, and that operator would only be +, -, *, or /.
Obviously, I know how to do the calculating ;) I'm just not sure how to go about getting the text from the image.
A free library would be ideal... although If I have to write the code myself I could probably manage.
For extract words from image, I use the most accurate open source OCR engine: Tesseract. Available here or directly in your packages NuGet.
And this is my function in C#, which extract words from image passed in sourceFilePath. Set EngineMode to TesseractAndCube; it detect more word than the other options.
var path = "YourSolutionDirectoryPath";
using (var engine = new TesseractEngine(path + Path.DirectorySeparatorChar + "tessdata", "fra", EngineMode.TesseractAndCube))
{
using (var img = Pix.LoadFromFile(sourceFilePath))
{
using (var page = engine.Process(img))
{
var text = page.GetText();
// text variable contains a string with all words found
}
}
}
I hope that helps.
Try this post regarding using the C++ Google Tessaract OCR lib in C#
OCR with the Tesseract interface
You need OCR. There is the free Tesseract library from Google, but it's C code. You could use in a C++/CLI project and access via .NET.
This article gives some information on recognizing numbers (for Sudoku, but your problem is similar)
http://sudokugrab.blogspot.com/2009/07/how-does-it-all-work.html
you can use Microsoft Office Document Imaging (Interop.MODI.dll) in visaul studio and extract text of pictures
Document modiDocument = new Document();
modiDocument.Create(filePath);
modiDocument.OCR(MiLANGUAGES.miLANG_ENGLISH);
MODI.Image modiImage = (modiDocument.Images[0] as MODI.Image);
string extractedText = modiImage.Layout.Text;
modiDocument.Close();
return extractedText;
IronOCR is free for development and testing. The default English language pack should do a good job of reading this, but you may also want to consider using a custom Tesseract language pack written specifically for equations.
See https://ironsoftware.com/csharp/ocr/languages/#custom-language-example
using IronOcr;
var Ocr = new IronTesseract();
Ocr.UseCustomTesseractLanguageFile("languages/equ.traineddata");
using (var Input = new OcrInput(#"images\equation.png"))
{
var Result = Ocr.Read(Input);
Console.WriteLine(Result.Text);
}
Disclaimer: I work for Iron Software.

Categories

Resources