Click or drag to resize

ZebraBarcodeFactory Class

Factory used to acquire an instance of a font specific barcode object.
Inheritance Hierarchy
SystemObject
  Zebra.Sdk.Card.Graphics.BarcodeZebraBarcodeFactory

Namespace:  Zebra.Sdk.Card.Graphics.Barcode
Assembly:  SdkApi_Card_Desktop (in SdkApi_Card_Desktop.dll) Version: 2.14.1989
Syntax
public class ZebraBarcodeFactory

The ZebraBarcodeFactory type exposes the following members.

Constructors
  NameDescription
Public methodZebraBarcodeFactory
Initializes a new instance of the ZebraBarcodeFactory class
Top
Methods
  NameDescription
Public methodEquals
Determines whether the specified object is equal to the current object.
(Inherited from Object.)
Public methodStatic memberGetCode128
Returns the barcode utilites class for Code128 barcodes.
Public methodStatic memberGetCode39
Returns the barcode utilites class for Code39 barcodes.
Public methodStatic memberGetCodeEAN13
Returns the barcode utilites class for EAN13 barcodes.
Public methodStatic memberGetCodeEAN8
Returns the barcode utilites class for EAN8 barcodes.
Public methodStatic memberGetCodePDF417
Returns the barcode utilites class for EAN13 barcodes.
Public methodGetHashCode
Serves as the default hash function.
(Inherited from Object.)
Public methodStatic memberGetQRCode
Returns the barcode utilites class for QRCode barcodes.
Public methodGetType
Gets the Type of the current instance.
(Inherited from Object.)
Public methodToString
Returns a string that represents the current object.
(Inherited from Object.)
Top
Examples
using System;
using System.Collections.Generic;
using System.Threading;
using Zebra.Sdk.Card.Containers;
using Zebra.Sdk.Card.Enumerations;
using Zebra.Sdk.Card.Graphics;
using Zebra.Sdk.Card.Graphics.Barcode;
using Zebra.Sdk.Card.Job;
using Zebra.Sdk.Card.Printer;
using Zebra.Sdk.Comm;

public class PrintBarcodeExample {

    private const int CARD_FEED_TIMEOUT = 30000;

    public static void Main(string[] args) {
        Connection connection = null;
        ZebraCardPrinter zebraCardPrinter = null;

        try {
            connection = new TcpConnection("1.2.3.4", 9100);
            connection.Open();

            zebraCardPrinter = ZebraCardPrinterFactory.GetInstance(connection);

            List<GraphicsInfo> graphicsData = new List<GraphicsInfo>() {
                DrawGraphics(zebraCardPrinter)
            };

            zebraCardPrinter.SetJobSetting(ZebraCardJobSettingNames.K_OPTIMIZATION_FRONT, "Barcode");

            // Send job
            int jobId = zebraCardPrinter.Print(1, graphicsData);

            // Poll job status
            JobStatusInfo jobStatus = PollJobStatus(jobId, zebraCardPrinter);
            Console.WriteLine($"Job {jobId} completed with status '{jobStatus.PrintStatus}'.");
        } catch (Exception e) {
            Console.WriteLine($"Error printing barcode image: {e.Message}");
        } finally {
            CloseQuietly(connection, zebraCardPrinter);
        }
    }

-    #region Graphics
     /// <exception cref="ConnectionException"></exception>
     /// <exception cref="System.IO.IOException"></exception>
     /// <exception cref="NotSupportedException"></exception>
     /// <exception cref="System.Security.SecurityException"></exception>
     /// <exception cref="UnauthorizedAccessException"></exception>
     /// <exception cref="Zebra.Sdk.Card.Exceptions.ZebraCardException"></exception>
     /// <exception cref="Zebra.Sdk.Device.ZebraIllegalArgumentException"></exception>
     private static GraphicsInfo DrawGraphics(ZebraCardPrinter zebraCardPrinter) {
         // Generate image data
         using (ZebraGraphics graphics = new ZebraCardGraphics(zebraCardPrinter)) {
             graphics.Initialize(0, 0, OrientationType.Landscape, PrintType.MonoK, null);
             DrawQRCode(graphics);
             DrawCode39(graphics);
             DrawCode128(graphics);
             DrawCodePDF417(graphics);
 
             ZebraCardImageI imageData = graphics.CreateImage();
             return AddImage(CardSide.Front, PrintType.MonoK, 0, 0, -1, imageData);
         }
     }
 
     /// <exception cref="Zebra.Sdk.Card.Exceptions.ZebraCardException"></exception>
     /// <exception cref="Zebra.Sdk.Device.ZebraIllegalArgumentException"></exception>
     private static void DrawQRCode(ZebraGraphics graphics) {
         using (QRCodeUtil qrCode = ZebraBarcodeFactory.GetQRCode(graphics)) {
             qrCode.ValueToEncode = "https://www.zebra.com";
             qrCode.DrawBarcode(50, 50, 75, 75);
         }
     }
 
     /// <exception cref="Zebra.Sdk.Card.Exceptions.ZebraCardException"></exception>
     /// <exception cref="Zebra.Sdk.Device.ZebraIllegalArgumentException"></exception>
     private static void DrawCode39(ZebraGraphics graphics) {
         using (Code39Util code39 = ZebraBarcodeFactory.GetCode39(graphics)) {
             code39.ValueToEncode = "1234567890";
             code39.QuietZoneWidth = 0;
             code39.DrawBarcode(50, 175, 400, 75);
         }
     }
 
     /// <exception cref="Zebra.Sdk.Card.Exceptions.ZebraCardException"></exception>
     /// <exception cref="Zebra.Sdk.Device.ZebraIllegalArgumentException"></exception>
     private static void DrawCode128(ZebraGraphics graphics) {
         using (Code128Util code128 = ZebraBarcodeFactory.GetCode128(graphics)) {
             code128.ValueToEncode = "Code128 Test";
             code128.QuietZoneWidth = 0;
             code128.DrawBarcode(50, 325, 0, 0);
         }
     }
 
     /// <exception cref="Zebra.Sdk.Card.Exceptions.ZebraCardException"></exception>
     /// <exception cref="Zebra.Sdk.Device.ZebraIllegalArgumentException"></exception>
     private static void DrawCodePDF417(ZebraGraphics graphics) {
         using (CodePDF417Util codePdf417 = ZebraBarcodeFactory.GetCodePDF417(graphics)) {
             codePdf417.ValueToEncode = "Zebra Technologies";
             codePdf417.QuietZoneWidth = 0;
             codePdf417.DrawBarcode(50, 475, 300, 100);
         }
     }
 
     private static GraphicsInfo AddImage(CardSide side, PrintType printType, int xOffset, int yOffset, int fillColor, ZebraCardImageI zebraCardImage) {
         return new GraphicsInfo {
             Side = side,
             PrintType = printType,
             GraphicType = zebraCardImage != null ? GraphicType.BMP : GraphicType.NA,
             XOffset = xOffset,
             YOffset = yOffset,
             FillColor = fillColor,
             Opacity = 0,
             Overprint = false,
             GraphicData = zebraCardImage ?? null
         };
     }
     #endregion Graphics

-    #region JobStatus
     /// <exception cref="ArgumentException"></exception>
     /// <exception cref="ConnectionException"></exception>
     /// <exception cref="System.IO.IOException"></exception>
     /// <exception cref="OverflowException"></exception>
     /// <exception cref="Zebra.Sdk.Settings.SettingsException"></exception>
     /// <exception cref="Zebra.Sdk.Card.Exceptions.ZebraCardException"></exception>
     private static JobStatusInfo PollJobStatus(int jobId, ZebraCardPrinter zebraCardPrinter) {
         JobStatusInfo jobStatusInfo = new JobStatusInfo();
         bool isFeeding = false;
 
         long start = Math.Abs(Environment.TickCount);
         while (true) {
             jobStatusInfo = zebraCardPrinter.GetJobStatus(jobId);
 
             if (!isFeeding) {
                 start = Math.Abs(Environment.TickCount);
             }
 
             isFeeding = jobStatusInfo.CardPosition.Contains("feeding");
 
             string alarmDesc = jobStatusInfo.AlarmInfo.Value > 0 ? $" ({jobStatusInfo.AlarmInfo.Description})" : "";
             string errorDesc = jobStatusInfo.ErrorInfo.Value > 0 ? $" ({jobStatusInfo.ErrorInfo.Description})" : "";
 
             Console.WriteLine($"Job {jobId}: status:{jobStatusInfo.PrintStatus}, position:{jobStatusInfo.CardPosition}, alarm:{jobStatusInfo.AlarmInfo.Value}{alarmDesc}, error:{jobStatusInfo.ErrorInfo.Value}{errorDesc}");
 
             if (jobStatusInfo.PrintStatus.Contains("done_ok")) {
                 break;
             } else if (jobStatusInfo.PrintStatus.Contains("error") || jobStatusInfo.PrintStatus.Contains("cancelled")) {
                 Console.WriteLine($"The job encountered an error [{jobStatusInfo.ErrorInfo.Description}] and was cancelled.");
                 break;
             } else if (jobStatusInfo.ErrorInfo.Value > 0) {
                 Console.WriteLine($"The job encountered an error [{jobStatusInfo.ErrorInfo.Description}] and was cancelled.");
                 zebraCardPrinter.Cancel(jobId);
             } else if (jobStatusInfo.PrintStatus.Contains("in_progress") && isFeeding) {
                 if (Math.Abs(Environment.TickCount) > start + CARD_FEED_TIMEOUT) {
                     Console.WriteLine("The job timed out waiting for a card and was cancelled.");
                     zebraCardPrinter.Cancel(jobId);
                 }
             }
 
             Thread.Sleep(1000);
         }
         return jobStatusInfo;
     }
     #endregion JobStatus

-    #region CleanUp
     private static void CloseQuietly(Connection connection, ZebraCardPrinter zebraCardPrinter) {
         try {
             if (zebraCardPrinter != null) {
                 zebraCardPrinter.Destroy();
             }
         } catch { }
 
         try {
             if (connection != null) {
                 connection.Close();
             }
         } catch { }
     }
     #endregion CleanUp
}
See Also