ZebraBarcodeFactory Class |
Namespace: Zebra.Sdk.Card.Graphics.Barcode
The ZebraBarcodeFactory type exposes the following members.
Name | Description | |
---|---|---|
ZebraBarcodeFactory | Initializes a new instance of the ZebraBarcodeFactory class |
Name | Description | |
---|---|---|
Equals | Determines whether the specified object is equal to the current object. (Inherited from Object.) | |
GetCode128 |
Returns the barcode utilites class for Code128 barcodes.
| |
GetCode39 |
Returns the barcode utilites class for Code39 barcodes.
| |
GetCodeEAN13 |
Returns the barcode utilites class for EAN13 barcodes.
| |
GetCodeEAN8 |
Returns the barcode utilites class for EAN8 barcodes.
| |
GetCodePDF417 |
Returns the barcode utilites class for EAN13 barcodes.
| |
GetHashCode | Serves as the default hash function. (Inherited from Object.) | |
GetQRCode |
Returns the barcode utilites class for QRCode barcodes.
| |
GetType | Gets the Type of the current instance. (Inherited from Object.) | |
ToString | Returns a string that represents the current object. (Inherited from Object.) |
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 }