6 Replies Latest reply on Sep 10, 2015 3:04 PM by Beverly Vinson

    Print photo within text on iMZ320 with iOS SDK (Objective C)

    Nguyen Doan

      Hello everybody,

       

      I did ask this problem in other thread that I asked about print text. But I think it should be move to new thread.

      I couldn't print receipt with function

      -(BOOL)printReportAsOneJobUsingNSString:(id<NSObject,ZebraPrinter>)printer withString:(NSMutableString*)fullLabel {
          /*
           Sending large amounts of data in a single write command can overflow the NSStream buffers which are the underlying mechanism used by the SDK to communicate with the printers.
           This method shows one way to break up large strings into smaller chunks to send to the printer
           */
          NSError *error = nil;
         
          long blockSize = 256;
          long totalSize = fullLabel.length;
          long bytesRemaining = totalSize;
         
          while (bytesRemaining > 0) {
              long bytesToSend = MIN(blockSize, bytesRemaining);
              NSRange range = NSMakeRange(0, bytesToSend);
             
              NSString *partialLabel = [fullLabel substringWithRange:range];
             
              [[printer getToolsUtil] sendCommand:partialLabel error:&error];
             
              bytesRemaining -= bytesToSend;
             
              [fullLabel deleteCharactersInRange:range];
             
          }
         
          return (error == nil);
      }
      

       

      But now I need to attach one photo to receipt link bellow

      Painting_7.png

      I did use print image code bellow

      if (_printImage) {
              id<GraphicsUtil, NSObject> graphicsUtil = [printer getGraphicsUtil];
              dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
                  NSError *error = nil;
                  [graphicsUtil printImage:[_printImage CGImage] atX:_posX atY:_posYwithWidth:_printImage.size.width withHeight:_printImage.size.height andIsInsideFormat:NOerror:&error];
                 
              });
          }
      


      But I didn't know where I can place this code, I did try to put it in printReportAsOneJobUsingNSString function, next to while loop (send text data), but no photo wasn't printed.


      Can anybody help me to solve this problem.


      Thank you so much !

        • Re: Print photo within text on iMZ320 with iOS SDK (Objective C)
          Robin West

          Hi Nguyen,

          Is the image a logo or is it dynamic like a signature?  You have different options if you have the ability to convert the file ahead of time as in a logo.  I'm assuming, based on the fact that you are using an iMZ and printing receipts that you are in line mode.  Line-mode is very limited in what it can do so the printImage function is not going to work. 

          If it's a static logo, I recommend converting the image to a pcx file format using GIMP or some other image manipulation tool.  You can then, in your app, send this file to be stored to the printer before you print.  You would create your receipt string and add the recall graphic command in where you wan the image printed.  If you named the image file as it's stored in the printer something like IMAGE.PCX, the receipt data would look something like this:

           

          ! U1 JOURNAL

          ! U1 SETLP 4 0 47

                                        Some text

          ! 0 200 200 500 1

          PCX 0 0 !<IMAGE.PCX

          JOURNAL

          PRINT

          ! U1 JOURNAL

          ! U1 SETLP 4 0 47

                                       Some text

                                        Some text


          If the image is dynamic like a signature you can try to find something to convert the image in iOS, but I doubt one exists.  I recommend switching print languages to ZPL.  You can then use storeImage or printImage functions. 

          Robin

            • Re: Re: Print photo within text on iMZ320 with iOS SDK (Objective C)
              Nguyen Doan

              Hello Robin West,

               

              Thank you for reply.

              I'm using print ZPL language, and my image is dynamic.

              I did by bellow step but not success:

              1. Print first part text

                  long blockSize = 256;  
                  long totalSize = fullLabel.length;  
                  long bytesRemaining = totalSize;  
                   
                  while (bytesRemaining > 0) {  
                      long bytesToSend = MIN(blockSize, bytesRemaining);  
                      NSRange range = NSMakeRange(0, bytesToSend);  
                       
                      NSString *partialLabel = [firstText substringWithRange:range];  
                       
                      [[printer getToolsUtil] sendCommand:partialLabel error:&error];  
                       
                      bytesRemaining -= bytesToSend;  
                       
                      [firstText deleteCharactersInRange:range];  
                       
                  }  
              

               

              2. Print image

              if (_printImage) {  
                      id<GraphicsUtil, NSObject> graphicsUtil = [printer getGraphicsUtil];  
                      dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{  
                          NSError *error = nil;  
                          [graphicsUtil printImage:[_printImage CGImage] atX:_posX atY:_posYwithWidth:_printImage.size.width withHeight:_printImage.size.height andIsInsideFormat:YES error:&error];  
                           
                      });  
                  }
              

               

              I used "andIsInsideFormat:YES", I don't know if it's right

              3. Print second part text, same as with step 1

                  long blockSize = 256;  
                  long totalSize = fullLabel.length;  
                  long bytesRemaining = totalSize;  
                   
                  while (bytesRemaining > 0) {  
                      long bytesToSend = MIN(blockSize, bytesRemaining);  
                      NSRange range = NSMakeRange(0, bytesToSend);  
                       
                      NSString *partialLabel = [firstText substringWithRange:range];  
                       
                      [[printer getToolsUtil] sendCommand:partialLabel error:&error];  
                       
                      bytesRemaining -= bytesToSend;  
                       
                      [firstText deleteCharactersInRange:range];  
                       
                  }  
              


              It only print all part of texts, but no image.

              Can you help, please !

                • Re: Re: Re: Print photo within text on iMZ320 with iOS SDK (Objective C)
                  Robin West

                  Hi Nguyen,

                  You do want to use andIsInsideFormat:YES for what you are doing.  We don't see any specific issues at the moment.  Are you getting any errors?  Can you try putting in an error parameter and checking it?

                   

                   


                  if (_printImage) {
                              id<GraphicsUtil, NSObject> graphicsUtil = [printer getGraphicsUtil];
                              dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
                                  NSError *error = nil;
                                  BOOL success = [graphicsUtil printImage:[_printImage CGImage] atX:_posX atY:_posYwithWidth:_printImage.size.width withHeight:_printImage.size.height andIsInsideFormat:YES error:&error];
                                  if (error != nil || success == NO) {
                                  UIAlertView *errorAlert = [[UIAlertView alloc] initWithTitle:@"Error" message:[error localizedDescription] delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
                                  [errorAlert show];
                                  [errorAlert release];
                              }
                            });
                          }

                    • Re: Re: Re: Re: Print photo within text on iMZ320 with iOS SDK (Objective C)
                      Nguyen Doan

                      Thank you for reply, Robin.

                       

                      I did test but no error pop-up but nothing picture was printed.

                      I really don't understand about andIsInsideFormat:YES, I thought it mean I print a picture with text format.

                      The code I did use:

                       

                      -(BOOL)printReportAsOneJobUsingNSString:(id<NSObject,ZebraPrinter>)printer withString:(NSMutableString*)fullLabel {
                          /*
                          Sending large amounts of data in a single write command can overflow the NSStream buffers which are the underlying mechanism used by the SDK to communicate with the printers.
                          This method shows one way to break up large strings into smaller chunks to send to the printer
                          */
                         
                          _printString1 = [[NSMutableString alloc] initWithString:@"^XA^PON^PW610^MNN^LL1430^LH0,20" \
                                         
                                          @"^FX ---1" \
                                          @"^FO70,40^GB470,100,3^FS" \
                                          @"^A0,N,50" \
                                          @"^FO80,60^FB450,1,1,C,0^FDVIOLATION^FS" \
                                          @"^A0,N,20" \
                                          @"^FO75,110^FB460,1,1,C,0^FDYOU ARE IN VIOLATION OF PARKING RULES/LAWS^FS" \
                                         
                                          @"^CFN,20" \
                                         
                                          @"^FX ---2" \
                                          @"^FO70,160^FB235,1,1,C,0^FDCITATION ID#^FS" \
                                          @"^FO305,160^FB235,1,1,C,0^FDOTHER ID#^FS" \
                                          @"^FO70,190^FB235,1,1,C,0^FDinternal1111^FS" \
                                          @"^FO305,190^FB235,1,1,C,0^FDother^FS" \
                                          @"^FO305,160^GB1,50,1^FS" \
                                          @"^FO55,220^GB500,1,1^FS" \
                                         
                                          @"^FX ---3" \
                                          @"^FO40,230^FB530,1,1,C,0^FDSPECIFIC VIOLATION(S)^FS" \
                                          @"^FO70,260^FDBLOCKING DRIVER1^FS" \
                                          @"^FO70,290^FDBLOCKING DRIVER2^FS" \
                                          @"^FO55,320^GB500,1,1^FS" \
                                         
                                          @"^FX ---4" \
                                          @"^FO40,340^FB530,1,1,C,0^FDVEHICLE INFORMATION^FS" \
                                          @"^FO70,370^FB150,1,1,R,0^FDMAKE:^FS" \
                                          @"^FO230,370^FB300,1,1,A,0^FDXYZ^FS" \
                                          @"^FO70,400^FB150,1,1,R,0^FDMODE:^FS" \
                                          @"^FO230,400^FB300,1,1,A,0^FDXYZ^FS" \
                                          @"^FO70,430^FB150,1,1,R,0^FDPLATE:^FS" \
                                          @"^FO230,430^FB300,1,1,A,0^FDXYZ^FS" \
                                          @"^FO70,460^FB150,1,1,R,0^FDCOLOR:^FS" \
                                          @"^FO230,460^FB300,1,1,A,0^FDXYZ^FS" \
                                          @"^FO70,490^FB150,1,1,R,0^FDSTATE:^FS" \
                                          @"^FO230,490^FB300,1,1,A,0^FDXYZ^FS" \
                                          @"^FO70,520^FB150,1,1,R,0^FDEXPIRATION:^FS" \
                                          @"^FO230,520^FB300,1,1,A,0^FDXYZXYZXYZXYZ^FS" \
                                          @"^FO55,550^GB500,1,1^FS" \
                                         
                                          @"^FX ---5" \
                                          @"^FO40,570^FB530,1,1,C,0^FDPROPERTY^FS" \
                                          @"^FO70,600^FB460,1,1,C,0^FDDestiny Software, Inc^FS" \
                                          @"^FO55,630^GB500,1,1^FS" \
                                         
                                          @"^FX ---6" \
                                          @"^FO40,650^FB530,1,1,C,0^FDLOCATION / COMMENTS^FS" \
                                          @"^FO70,680^FB470,7,1,B,0^FDNote for testing.^FS" \
                                          @"^FO55,900^GB500,1,1^FS"];
                         
                          NSError *error = nil;
                      
                      
                          long blockSize = 256;
                          long totalSize = _printString1.length;
                          long bytesRemaining = totalSize;
                         
                          while (bytesRemaining > 0) {
                              long bytesToSend = MIN(blockSize, bytesRemaining);
                              NSRange range = NSMakeRange(0, bytesToSend);
                             
                              NSString *partialLabel = [_printString1 substringWithRange:range];
                             
                              [[printer getToolsUtil] sendCommand:partialLabel error:&error];
                             
                              bytesRemaining -= bytesToSend;
                             
                              [_printString1 deleteCharactersInRange:range];
                             
                          }
                         
                          if (_printImage) {
                              _lblStatus.text = @"Status: Send picture to printer.";
                             
                              id<GraphicsUtil, NSObject> graphicsUtil = [printer getGraphicsUtil];
                             
                              dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
                                  NSError *error = nil;
                                  BOOL success = [graphicsUtil printImage:[_printImage CGImage] atX:_posX atY:_posY withWidth:_printImage.size.width withHeight:_printImage.size.height andIsInsideFormat:NO error:&error];
                                  if (error != nil || success == NO) {
                                      UIAlertView *errorAlert = [[UIAlertView alloc] initWithTitle:@"Error" message:[error localizedDescription] delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
                                      [errorAlert show];
                                  } else {
                                      _lblStatus.text = @"Status: Picture OK.";
                                  }
                                 
                              });
                             
                          }
                         
                          _printString2 = [[NSMutableString alloc] initWithString:@"^FX ---7" \
                                          @"^FO40,920^FB530,1,1,C,0^FDVEHICLE STATUS^FS" \
                                          @"^FO40,950^FB530,1,1,C,0^FDNOTIFIED MANAGER^FS" \
                                          @"^FO55,980^GB500,1,1^FS" \
                                         
                                          @"^FX ---8" \
                                          @"^FO40,1000^FB530,1,1,C,0^FDTOW INFORMATION^FS" \
                                          @"^FO70,1030^FB150,1,1,R,0^FDTOW COMPANY:^FS" \
                                          @"^FO230,1060^FB300,1,1,A,0^FDXYZ^FS" \
                                          @"^FO70,1090^FB150,1,1,R,0^FDTOW PHONE #:^FS" \
                                          @"^FO230,1120^FB300,1,1,A,0^FDXYZ^FS" \
                                          @"^FO70,1150^FB150,1,1,R,0^FDCLEARED:^FS" \
                                          @"^FO230,1180^FB300,1,1,A,0^FDXYZ^FS" \
                                          @"^FO70,1210^FB150,1,1,R,0^FDAGENCY:^FS" \
                                          @"^FO230,1240^FB300,1,1,A,0^FDXYZ^FS" \
                                         
                                          @"^FX ---9" \
                                          @"^FO50,1260^GB510,80,1^FS" \
                                          @"^FO70,1290^FB470,1,1,C,0^FDDATE / TIME OF VIOLATION^FS" \
                                          @"^FO80,1320^FB450,1,1,C,0^FD2015-08-01 20:16:30^FS" \
                                         
                                          @"^FX ---10" \
                                          @"^FO80,1350^FB450,1,1,C,0^FDOFFICER: JOHN FLORES^FS" \
                                          @"^FO80,1380^FB450,1,1,C,0^FDDPS TESTING ACCOUNT^FS" \
                                          @"^FO80,1410^FB450,1,1,C,0^FD(054) 3-861-925^FS" \
                                          @"^XZ"];
                         
                          blockSize = 256;
                          totalSize = _printString2.length;
                          bytesRemaining = totalSize;
                         
                          while (bytesRemaining > 0) {
                              long bytesToSend = MIN(blockSize, bytesRemaining);
                              NSRange range = NSMakeRange(0, bytesToSend);
                             
                              NSString *partialLabel = [_printString2 substringWithRange:range];
                             
                              [[printer getToolsUtil] sendCommand:partialLabel error:&error];
                             
                              bytesRemaining -= bytesToSend;
                             
                              [_printString2 deleteCharactersInRange:range];
                             
                          }
                         
                          return (error == nil);
                      }
                      

                       

                      The printer activated and an 18 in strip of paper came out. There is very little printer on the paper and the letters are very small.

                      Please give me some advice!