1 Reply Latest reply on Jun 19, 2018 1:25 PM by Robin West

    print an image and text to receipt using cpcl in android  MZ320

    mahmoud zahran

      Dear all,
      I tried to print image and text in receipt using Cpcl format like that template. I have an example set logo as a variable and use function to read the image as string but that image in example is CPCL format I tried a lot  to get my logo as a cpcl format but i can't so I need to know how to get my image logo as cpcl format or another way to set image and text like that templete.

      ====================================================

      ! 0 200 200 1050 1

      PW 575

      TONE 0

      SPEED 3

      ON-FEED IGNORE

      NO-PACE

      BAR-SENSE

      BT 0 4 6

      B 128 3 30 120 20 0 ${barcode}

       

      PCX 420 790 ${Logo}

       

      ML 32

      T270 7 0 550 170 ${typeTicket}

      ENDML

       

      ML 32

      T270 7 0 498 170 ${validPeriod}

      ENDML

       

      ML 25

      T270 7 0 446 170 ${park}

      ENDML

       

      ML 32

      T270 7 0 370 170 ${price}

      ENDML

       

      ML 25

      T270 7 0 300 170 ${termsOfUse}

      ENDML

       

       

      PRINT

      =================================

      this is the code used to read image as a string and it works perfectly with the example.

       

      this line how to use !

      result = StringUtils.replace(result, "${Logo}", PrinterUtil.readFormat(context, R.raw.logo));

       

      public static String readFormat(Context context, int formatRes) {

        InputStream is = null;

         try {

        is = context.getResources().openRawResource(formatRes);

         try {

         return readString(is);

        } catch (IOException e) {

         return null;

        }

        } finally {

         if (is != null) {

         try {

        is.close();

        } catch (IOException e) {

        }

        }

        }

      }

        • Re: print an image and text to receipt using cpcl in android  MZ320
          Robin West

          Hi Mahmoud, the easiest way to convert the image to CPCL format is to use the SDK.

          1. First you need to use the connection to create a CPCL ZebraPrinter object.
          2. Start sending your format.
          3. Use the printImage method as an embedded image
          4. Finish sending your format

           

          import java.io.IOException;

           

          import com.zebra.sdk.comm.Connection;

          import com.zebra.sdk.comm.ConnectionException;

          import com.zebra.sdk.comm.TcpConnection;

          import com.zebra.sdk.printer.ZebraPrinter;

          import com.zebra.sdk.printer.ZebraPrinterFactory;

          import com.zebra.sdk.printer.ZebraPrinterLanguageUnknownException;

           

          public class GraphicsUtilExample {

           

               public static void main(String[] args) throws Exception {

                   Connection connection = new TcpConnection("192.168.1.32", TcpConnection.DEFAULT_ZPL_TCP_PORT);

                   try {

                       connection.open();

                       ZebraPrinter printer = ZebraPrinterFactory.getInstance(PrinterLanguage.CPCL, connection);

                   // send the first part of your format

                       int x = 0;

                       int y = 0;

                       int width = 0;

                       int height = 0;

                       printer.printImage("/sdcard/sample.jpg", x, y, width, height, true );

                  // send the end of your format

                   } catch (ConnectionException e) {

                       e.printStackTrace();

                   } catch (ZebraPrinterLanguageUnknownException e) {

                       e.printStackTrace();

                   } catch (IOException e) {

                       e.printStackTrace();

                   } finally {

                       connection.close();

                   }

               }

           

          }