6 Replies Latest reply on Jun 19, 2018 6:50 AM by Rajesh Mohan

    How to print image in CPCL from Xamarin forms - iOS Platform.

    Rajesh Mohan

      Hi,

       

      This is regarding printing of images to Zebra printer from Xamarin Forms in iOS platform.

       

      1. model of Zebra printer - Zebra ZQ520.

       

      2. Language used - CPCL

       

      I am creating a CPCL file with the data to be printed. The data is extracted from SQLite on iPhone and saved in the CPCL file. Finally giving the command to print the cpcl file works fine and the data is printed.

       

      My issue is how to embed the image in this cpcl file. The image is saved as a blob in SQLite database. I need to retrieve this blob and convert it to an image file and then specify the path of the image in the CPCL file. For some reason, the image never prints. Have gone through all the threads regarding printing of image but none of them work. I have even specified the language to use as CPCL.

       

      Can you please assist how to achieve the printing of the image in CPCL. I have the following queries.

       

      1. What format should the image be saved as on the device.

      2. What is the syntax to embed the image in the CPCL file.

      3. Is there any other way to embed the image in the CPCL file and print it along with the data.

       

      Thanks,

       

      Rajesh.

        • Re: How to print image in CPCL from Xamarin forms - iOS Platform.
          Robin West

          Hi Rajesh,

          The image should be a CGImageRef generated from a PNG, JPG, or BMP type of file.  You can then use the PrintImage() method to convert the image to the correct printer image type and print.  You can embed the image by setting InsideFormat in the parameters to True.  You will have to make sure the PrinterLanguage when you create the ZebraPrinter object is set to CPCL or the image conversion won't work.

          Does this make sense?

          • Re: How to print image in CPCL from Xamarin forms - iOS Platform.
            Rajesh Mohan

            Madam,

             

            I followed all the steps as explained by you along with some sample code in the Zebra Forums related to creating a CGImage.

             

            My code is as below.

             

            Code in the PCL project.

             

            Bluetooth Connectivity

             

            public void btn_signprint_clicked(Object sender, EventArgs e)
                    {
                        new Task(new Action(() => {
                            //This will make the call to the function private IFieldDescription[] PrintFormatOnPrinter(string fileNameOnPrinter, string[] mydata)
                            StartBluetoothDiscovery();
                        })).Start();
                    }

                    private void StartBluetoothDiscovery()
                    {

                        IDiscoveryEventHandler bthandler = DiscoveryHandlerFactory.Current.GetInstance();
                        bthandler.OnFoundPrinter += DiscoveryHandler_OnFoundPrinter;
                        connetionType = ConnectionType.Bluetooth;
                        System.Diagnostics.Debug.WriteLine("Starting Bluetooth Discovery");

                        DependencyService.Get<IPrinterDiscovery>().FindBluetoothPrinters(bthandler);
                    }

                    private void DiscoveryHandler_OnFoundPrinter(object sender, IDiscoveredPrinter discoveredPrinter)
                    {

                        //Debug.WriteLine("Found Printer:" + discoveredPrinter.ToString());
                        myPrinter = discoveredPrinter;

                        new Task(new Action(() => {

                            PrintSignature();
                            //StartCitationPrint();

                        })).Start();

                    }

                    public void PrintSignature()
                    {
                        IConnection connection = null;
                        try
                        {
                            connection = myPrinter.Connection;
                            connection.Open();

                            connection.MaxTimeoutForRead = 5000;

                            connection.TimeToWaitForMoreData = 1000;

                          
                            IZebraPrinter printer = ZebraPrinterFactory.Current.GetInstance(PrinterLanguage.CPCL, connection);
                            if ((!CheckPrinterLanguage(connection)) || (!PreCheckPrinterStatus(printer)))
                            {
                                resetPage();
                                return;
                            }

                            //GetSignaturePrint(connection);

                            DependencyService.Get<ISignature>().GetPrint(connection);

                            connection.Close();

                        }
                        catch (Exception ex)
                        {

                        }

                     }

             

            Code in the iOS project

             

            public CoreGraphics.CGImage GetImage(string filename)
                    {
                         CoreGraphics.CGImage image = null;


                             //creating the CGImage  (I found this code from this link Image Conversion Error on iOS printing a PNG file using IZebraPrinter.PrintImage )
                            image = new CoreGraphics.CGImage(500, 130, 1, 1, 400, CoreGraphics.CGColorSpace.CreateDeviceGray(), CoreGraphics.CGBitmapFlags.First, CoreGraphics.CGDataProvider.FromFile(filename), null, false, CoreGraphics.CGColorRenderingIntent.Default);

                            return image;
                    }


                    public void GetPrint(IConnection connection)
                    {
                        var folder = DependencyService.Get<ISignature>().GetFilePath();

                        var imgPath = folder + "/Signature.png";


                        try{
                            //first define the image in CGImage format
                            CoreGraphics.CGImage getImage = GetImage(imgPath);

             

                            //specify the language to CPCL   
                            IZebraPrinter printer = ZebraPrinterFactory.Current.GetInstance(PrinterLanguage.CPCL, connection);

             

                           //print the image
                            printer.PrintImage(getImage,120, 0, 500, 130, true);

                        }

                        catch(Exception ex)
                        {
                           
                        }
             

            }

             

            When executing this code it throws an error <Error>: CGImageCreate: invalid image bits/pixel: 1.

             

            Kindly let me know how to fix this error. Is the code correct in order to creating a CGImage from a PNG image so that it can be printed from iPhone to Zebra ZQ520.

             

            Thanks,

             

            Rajesh.

              • Re: How to print image in CPCL from Xamarin forms - iOS Platform.
                Steven Si

                I would suggest to try to print out the image alone first. Once we are able to print out the image, then we can start putting CPCL and image together. Attached is a very simple Xamarin project that I used to demonstrate the image printing. It's a project based on Xamarin's Phoneword example. There are only three files related to the image printing.

                 

                Under Phoneword:

                                GraphicsTest.cs

                                IFileUtility.cs

                 

                Under Phoneword.iOS

                                FileUtilityImplementation.cs

                 

                Please replace the printer's serial number (after BT:) with yours in GraphicsTest.cs, as iOS use serial number to make Bluetooth connection.

                 

                connection = ConnectionBuilder.Current.Build("BT:XXRBJ152101196");
                

                 

                Once the Phoneword is delployed on an iPhone, press on “Translate” button (ignore the naming of the button, as this project is derived from the hello world project.),  then printGraphicFromPathTest() in GraphicsTest.cs is invoked. The a Zebra logo image is printed out from the printer. Hope this helps.

                  • Re: How to print image in CPCL from Xamarin forms - iOS Platform.
                    Rajesh Mohan

                    Hi Steven,

                    Thanks a lot for your help and sample project.

                     

                    Our flow is :-

                     

                    We need our image to be of the size W-400, H-100.

                    We have data in 2 text files and the signature is called between these 2 text files. All the text data is printed correctly.

                     

                    We are able to print out the image correctly using the code provided by you.

                     

                    But when implementing the same in our application we found that there is a huge blank space after the images

                     

                    Kindly let us know how to remove the blank space  after the images. The extra space is marked in red in the attached image.

                     

                    Our code in the application

                     

                    Image Print-

                     

                    private async void OfficerSignature(IConnection printerConnection)

                            {

                                try

                                {

                                    printerConnection.Open();

                     

                                    IZebraPrinter printer = ZebraPrinterFactory.Current.GetInstance(PrinterLanguage.CPCL, printerConnection);

                     

                                    string pathImg = DependencyService.Get<IGraphics>().GetFullPath("OffSign-Resized.jpeg");

                     

                                    printer.PrintImage(pathImg, 120, 0, 0, 0, true);              

                     

                                    printerConnection.Close();

                     

                                    bool delSign = DependencyService.Get<ISignature>().delSignatureResized(pathImg);

                     

                                    StartPrintForCiteTwo();

                                  

                                }

                                catch (Exception ex)

                                {

                                    Debug.WriteLine(ex);

                                }

                            }

                     

                     

                    Get Full Path-

                     

                     

                    public string GetFullPath(string fileName)

                            {

                               var pictures = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);

                     

                                string withFile = pictures + "/" + fileName;

                     

                                return withFile;

                            }

                     

                    Would be glad if you can assist in removing the extra space after the signatures.

                     

                    Thanks,

                     

                    Rajesh.

                      • Re: How to print image in CPCL from Xamarin forms - iOS Platform.
                        Steven Si
                        IZebraPrinter printer = ZebraPrinterFactory.Current.GetInstance(PrinterLanguage.CPCL, printerConnection);

                        The above indicates that we want to print the image through CPCL.

                         

                        printer.PrintImage(pathImg, 120, 0, 0, 0, true);

                        This indicates that we want the image as part of the CPCL format. Therefore, we should consider the first part of text + image + the second part of text as a whole in CPCL. The code below, for example, splits the CPCL into two parts. Then the code prints part1, followed by the image, then followed by part.

                                        connection = ConnectionBuilder.Current.Build("BT:XXXXJ134101571"); // Specifying printer's SN # here
                                        connection.Open();
                        
                                        String cpcl_part1 = "! 0 200 200 350 1" + "\r\n" +
                                            "T 7 0 30 30 This text has no(default left) justification." + "\r\n" +
                                            "RIGHT" + "\r\n" +
                                            "T 7 0 30 80 This text has right justification." + "\r\n" +
                                            "CENTER" + "\r\n" +
                                            "T 7 0 30 130 This text has center justification." + "\r\n";
                                        
                                        String cpcl_part2 = "LEFT" + "\r\n" +
                                            "T 7 0 30 180 This text has left justification." + "\r\n" +
                                            "CENTER 576" + "\r\n" +
                                            "T 7 0 30 240 This text has center justification," + "\r\n" +
                                            "T 7 0 30 264 but uses 576 as the range." + "\r\n" +
                                            "PRINT" + "\r\n";
                        
                                        connection.Write(GetBytes(cpcl_part1));
                        
                                        IZebraPrinter printer = ZebraPrinterFactory.Current.GetInstance(PrinterLanguage.CPCL, connection);
                        
                                        object image = DependencyService.Get<IFileUtility>().GetCGImage("zebra_logo2.png");
                                        printer.PrintImage(image, 0, 100, 100, 100, true);
                        
                                       connection.Write(GetBytes(cpcl_part2));
                        
                                
                        
                        
                                // A utility function
                                protected static byte[] GetBytes(string str)
                                {
                                    byte[] bytes = new byte[str.Length];
                                    bytes = Encoding.UTF8.GetBytes(str);
                                    return bytes;
                                }
                        

                         

                        The result looks like below without the trailing space. The trailing space in your case might be a result that the CPCL was not ended properly in the second part of text.

                        cpcl_image.jpg

                  • Re: How to print image in CPCL from Xamarin forms - iOS Platform.
                    Rajesh Mohan

                    Hi Steven,

                     

                    We tested the printing with the help of the sample code that you had given earlier.

                     

                    In spite of ending the CPCL we are still getting the same output.

                     

                    Attaching our code and the signature image as well as the photo taken of the printout.

                     

                    private void GetPrintForCiteOne(IConnection printerConnection)

                            {

                                try

                                {

                                    printerConnection.Open();

                     

                     

                                     string cite = "! UF ECITE1.FMT\r\n50140024\r\nX\r\n\r\n\r\n6/21/10 \r\n1043\r\nFRIDAY\r\n86589496\r\n\r\nTEST  TEST\r\n\r\nTRANSIENT\r\nSAN JOSE\r\nCA\r\n95113\r\n\r\n\r\nA0000000\r\nCA\r\n\r\n30\r\n6/6/1980\r\nF\r\nBRN\r\nBRN\r\n510\r\n177\r\nA\r\n\r\n\r\nX\r\nB345\r\nCA\r\n2006\r\nAUDI\r\n100\r\n4D\r\nBRO\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\nX\r\n\r\n14601.1(A)CVC\r\nFAIL TO APPEAR OR PAY FINE\r\nM\r\n\r\n\r\nX\r\nHS-12689(C )\r\nMINOR IN POSSESSION OF FIREWORKS/DEVICES\r\nM\r\n\r\n\r\nX\r\nPC-417(C )\r\nEXHIBIT/DRAW DEADLY WEAPON FRONT OF POLICE OFR\r\nM\r\n\r\n\r\nX\r\nSJ-11.104.010A\r\nABANDONED/INOPERATIVE VEHICLE - NUISANCE\r\nM\r\n\r\n\r\nX\r\nVC-23226B\r\nPASSENGER ALLOW ALCOHOLIC BEVERAGE IN PASSENGER AREA\r\nI\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\nA5\r\nTEST TEST\r\nTRANSIENT\r\nSAN JOSE\r\nCA\r\n95113\r\n\r\nX";

                     

                                    printerConnection.Write(GetBytes(cite));//it accepts as bytes only so converting string to byte

                     

                                    IZebraPrinter printer = ZebraPrinterFactory.Current.GetInstance(PrinterLanguage.CPCL, printerConnection);

                     

                                    string pathImg = DependencyService.Get<IGraphics>().GetFullPath("OffSign-Resized.jpeg");

                     

                                    printer.PrintImage(pathImg, 120, 0, 0, 0, true);  

                     

                                    string citeText = "! UF ECITE2.FMT\r\nRUBEN\r\n002298\r\n6/15/18\r\n\r\n\r\n\r\n";

                     

                                    printerConnection.Write(GetBytes(citeText));

                     

                                    printerConnection.Close();

                     

                                }

                                catch (Exception ex)

                                {

                                    Debug.WriteLine(ex);

                                }

                            }

                     

                    Kindly help us to troubleshoot this problem so that the space between the text and the image is removed. Our code is the same as yours but still the problem of spacing persists.

                     

                    Thanks,

                     

                    Rajesh.