Generate and recognize Han Xin Code barcodes in .NET

Blog category: Barcode.NET

May 04, 2020

Han Xin Code is a two-dimensional matrix symbology, which is made up of an array of nominally square modules arranged in an overall square pattern.
Samples of Han Xin Code barcodes

Han Xin Code barcode matrix has four search patterns (A1-A4) positioned at the edges of barcode. Also the barcode matrix has the alignment patterns (B), which are located inside the matrix and allow to perform the recognition of barcode with various distortions, which might appear as result of imperfection in the process of photographing or the surface irregularity on which the barcode is printed or labeled.

Structure of Han Xin Code barcode


Han Xin Code barcode has special feature - it can store text data from GB18030 encoding (2- and 4-byte symbols) in the most compact way.

General features of Han Xin Code:


VintaSoft Barcode .NET SDK supports reading, writing and print quality testing of Han Xin Code (Chinese Sensible Code) barcodes.

Here is C# code that shows how to generate Han Xin Code barcode as raster image:
/// <summary>
/// Generates Han Xin Code barcode as raster image.
/// </summary>
public void GenerateHanXinCodeBarcodeAsRasterImage()
{
    // create the barcode writer
    Vintasoft.Barcode.BarcodeWriter barcodeWriter = new Vintasoft.Barcode.BarcodeWriter();

    // set barcode writer settings
    barcodeWriter.Settings.Barcode = Vintasoft.Barcode.BarcodeType.HanXinCode;
    barcodeWriter.Settings.Value = "012345abcde";

    // get a barcode image
    using (System.Drawing.Image image = barcodeWriter.GetBarcodeAsBitmap())
    {
        // save the barcode image to a file
        image.Save("HanXinCodeBarcode.png");
    }
}


Here is C# code that shows how to generate Han Xin Code barcode in vector form:
/// <summary>
/// Generates Han Xin Code barcode as graphics path.
/// </summary>
public System.Drawing.Drawing2D.GraphicsPath GenerateHanXinCodeBarcodeAsGraphicsPath()
{
    // create the barcode writer
    Vintasoft.Barcode.BarcodeWriter barcodeWriter = new Vintasoft.Barcode.BarcodeWriter();

    // set barcode writer settings
    barcodeWriter.Settings.Barcode = Vintasoft.Barcode.BarcodeType.HanXinCode;
    barcodeWriter.Settings.Value = "012345abcde";

    // return barcode as graphics path
    return barcodeWriter.GetBarcodeAsGraphicsPath();
}


Here is C# code that shows how to generate Han Xin Code barcode as SVG image:
/// <summary>
/// Generates Han Xin Code barcode as SVG image.
/// </summary>
public string GenerateHanXinCodeBarcodeAsSvgImage()
{
    // create the barcode writer
    Vintasoft.Barcode.BarcodeWriter barcodeWriter = new Vintasoft.Barcode.BarcodeWriter();

    // set barcode writer settings
    barcodeWriter.Settings.Barcode = Vintasoft.Barcode.BarcodeType.HanXinCode;
    barcodeWriter.Settings.Value = "012345abcde";

    // return barcode as SVG image
    return barcodeWriter.GetBarcodeAsSvgFile();
}

Here is C# code that shows how to recognize Han Xin Code barcode in image:
/// <summary>
/// Recognizes Han Xin Code barcode in image.
/// </summary>
public void RecognizeHanXinCodeBarcode()
{
    // create barcode reader
    using (Vintasoft.Barcode.BarcodeReader reader = new Vintasoft.Barcode.BarcodeReader())
    {
        // specify that reader must search for Han Xin Code barcodes
        reader.Settings.ScanBarcodeTypes = Vintasoft.Barcode.BarcodeType.HanXinCode;

        // specify that reader must search for horizontal and vertical barcodes only
        reader.Settings.ScanDirection = Vintasoft.Barcode.ScanDirection.Horizontal | Vintasoft.Barcode.ScanDirection.Vertical;

        // read barcodes from image file
        Vintasoft.Barcode.IBarcodeInfo[] barcodeInfos = reader.ReadBarcodes("HanXinCodeBarcode.png");

        // if barcodes are not detected
        if (barcodeInfos.Length == 0)
        {
            Console.WriteLine("Barcodes are not found.");
        }
        // if barcodes are detected
        else
        {
            // get information about recognized barcodes

            Console.WriteLine(string.Format("{0} barcode(s) found:", barcodeInfos.Length));
            Console.WriteLine();
            for (int i = 0; i &lt; barcodeInfos.Length; i++)
            {
                Vintasoft.Barcode.IBarcodeInfo barcodeInfo = barcodeInfos[i];
                Console.WriteLine(string.Format("[{0}:{1}]", i + 1, barcodeInfo.BarcodeType));
                Console.WriteLine(string.Format("Value:      {0}", barcodeInfo.Value));
                Console.WriteLine(string.Format("Region:     {0}", barcodeInfo.Region));
                Console.WriteLine();
            }
        }
    }
}

Here is C# code that shows how to recognize and test the print quality of Han Xin Code barcode in image:
/// <summary>
/// Recognizes and prints quality of Han Xin Code barcode in image.
/// </summary>
public void ReadBarcodesAndTestBarcodePrintQuality()
{
    // load image with barcode from file
    using (System.Drawing.Image barcodeImage = System.Drawing.Image.FromFile("HanXinCodeBarcode.png"))
    {
        // create the barcode reader
        using (Vintasoft.Barcode.BarcodeReader reader = new Vintasoft.Barcode.BarcodeReader())
        {
            // specify that reader must search for Han Xin Code barcodes
            reader.Settings.ScanBarcodeTypes = Vintasoft.Barcode.BarcodeType.HanXinCode;

            // specify that reader must collect information for quality test
            reader.Settings.CollectTestInformation = true;

            // read barcodes
            Vintasoft.Barcode.IBarcodeInfo[] barcodeInfos = reader.ReadBarcodes(barcodeImage);

            // for each found barcode
            for (int i = 0; i &lt; barcodeInfos.Length; i++)
            {
                Vintasoft.Barcode.IBarcodeInfo barcodeInfo = barcodeInfos[i];

                // print information about recognized barcode

                Console.WriteLine(string.Format("[{0}:{1}]", i + 1, barcodeInfo.BarcodeType));
                Console.WriteLine(string.Format("Value:      {0}", barcodeInfo.Value));
                Console.WriteLine(string.Format("Region:     {0}", barcodeInfo.Region));

                // test barcode print quality in accordance with ISO 15415
                Vintasoft.Barcode.QualityTests.ISO15415QualityTest test = new Vintasoft.Barcode.QualityTests.ISO15415QualityTest();
                test.CalculateGrades((Vintasoft.Barcode.BarcodeInfo.BarcodeInfo2D)barcodeInfo, barcodeImage);

                // print results of barcode print quality test

                Console.WriteLine("ISO15415 print quality test:");
                Console.WriteLine(string.Format("- Decode                         : {0}", GradeToString(test.DecodeGrade)));
                Console.WriteLine(string.Format("- Unused error correction        : {0} ({1}%)", GradeToString(test.UnusedErrorCorrectionGrade), test.UnusedErrorCorrection));
                Console.WriteLine(string.Format("- Symbol contrast                : {0} ({1}%)", GradeToString(test.SymbolContrastGrade), test.SymbolContrast));
                Console.WriteLine(string.Format("- Axial nonuniformity            : {0} ({1})", GradeToString(test.AxialNonuniformityGrade), test.AxialNonuniformity));
                Console.WriteLine(string.Format("- Grid nonuniformity             : {0} ({1} cell)", GradeToString(test.GridNonuniformityGrade), test.GridNonuniformity));
                Console.WriteLine(string.Format("- Modulation                     : {0}", GradeToString(test.ModulationGrade)));
                Console.WriteLine(string.Format("- Reflectance margin             : {0}", GradeToString(test.ReflectanceMarginGrade)));
                Console.WriteLine(string.Format("- Fixed pattern damage           : {0}", GradeToString(test.FixedPatternDamageGrade)));
                Console.WriteLine(string.Format("- Distortion angle (informative) : {0}°", test.DistortionAngle));
                Console.WriteLine();
            }
        }
    }
}

/// <summary>
/// Converts ISO15415 quality grade into string value.
/// </summary>
static string GradeToString(Vintasoft.Barcode.QualityTests.ISO15415QualityGrade grade)
{
    int gradeAsInt = (int)grade;
    return string.Format("{0}({1})", gradeAsInt, grade);
}

Here is screenshot from VintaSoft Barcode Demo that shows result of ISO/IEC 15415 print quality test for Han Xin Code barcode:
Result of ISO/IEC 15415 print quality test for Han Xin Code barcode