1 Reply Latest reply on Sep 7, 2017 3:59 PM by Robin West

    Printing BMP in Xamarin without SDK

    Kevin Robarge

      I am able to print text in my Xamarin.Android app with no issue, but have no clue on where to start on printing a BMP.  The app captures a signature in PNG format.  I take this and convert it to a BMP, but am lost as to what to do next.  Could someone help complete the code

       

      void PrintSignature(object sender, EventArgs e)

              {

       

                  OutputStream sOut;

                  BluetoothSocket s;

                  BA.CancelDiscovery();

       

                  string addy = "";

                  BufferedWriter outReader = null;

       

                  try

                  {

               

                      var pairedprinters = BA.BondedDevices;

                      foreach (var device in pairedprinters) { addy = device.Address; }

                      BluetoothDevice BD = BA.GetRemoteDevice(addy);

       

                      s = BD.CreateInsecureRfcommSocketToServiceRecord(SerialPortServiceClass_UUID);

                      if (!s.IsConnected)

                      {

                          s.Connect();

                          System.Threading.Thread.Sleep(3000); // <-- WAIT FOR SOCKET 

                      }

                      outReader = new BufferedWriter(new OutputStreamWriter(s.OutputStream));

       

                      String cc = "^XA~TA000~JSN^LT0^MNN^MTD^PON^PMN^LH0,0^JMA^PR5,5~SD10^JUS^LRN^CI0^MMT^PW812^LL500^LS0";

                      cc += $"^FT485,1900^A0I,56,55^FH\\^FDHERE IS THE SIGNATURE:^FS";

       

                  }

                  catch (Java.Lang.Exception eg)

                  {

                      Log.Error("x", $"error: {eg.Message}/n{eg.StackTrace}");

                  }

       

                   Bitmap _bit = BitmapFactory.DecodeFile(signatureImageSavePath);   //Path to PNG file

       

                  ByteArrayOutputStream stream = new ByteArrayOutputStream();

                  int width = _bit.Width / 8;

                  int byteCount = _bit.ByteCount;

                  byte[] buf = new byte[byteCount];

                 //   _bit.CopyPixelsToBuffer()                         //CALL DOES NOT EXIST

                 

                  // Generate a command string and unlock the bitmap bits, NOT SURE IF THIS IS CORRECT

                      cc = string.Format("^FO{0},{1}^GFB,{2},{3},{4},{5}^FS\n",

                      x, y, byteCount, byteCount, _bit.Width,Encoding.);

       

       

                  cc += $"^PQ1,0,1,Y^XZ";

       

       

                  outReader.Write(cc + "\n");

                  outReader.Flush();

                  outReader.Close();

                  s.Close();

                  BA.CancelDiscovery();

       

       

              }

        • Re: Printing BMP in Xamarin without SDK
          Robin West

          Hi Kevin, I think I have it.  There may be a more elegant solution, but this should get you there.

           

          void PrintSignature(object sender, EventArgs e)

                  {

           

                      OutputStream sOut;

                      BluetoothSocket s;

                      BA.CancelDiscovery();

           

                      string addy = "";

                      BufferedWriter outReader = null;

           

                      try

                      {

                 

                          var pairedprinters = BA.BondedDevices;

                          foreach (var device in pairedprinters) { addy = device.Address; }

                          BluetoothDevice BD = BA.GetRemoteDevice(addy);

           

                          s = BD.CreateInsecureRfcommSocketToServiceRecord(SerialPortServiceClass_UUID);

                          if (!s.IsConnected)

                          {

                              s.Connect();

                              System.Threading.Thread.Sleep(3000); // <-- WAIT FOR SOCKET

                          }

                          outReader = new BufferedWriter(new OutputStreamWriter(s.OutputStream));

           

                          String cc = "^XA~TA000~JSN^LT0^MNN^MTD^PON^PMN^LH0,0^JMA^PR5,5~SD10^JUS^LRN^CI0^MMT^PW812^LL500^LS0";

                          cc += $"^FT485,100^A0I,56,55^FH\\^FDHERE IS THE SIGNATURE:^FS";

                          cc += $"^FO20,120^XGR:IMAGE.BMP^FS^PQ1,0,1,Y^XZ";

           

                      }

                      catch (Java.Lang.Exception eg)

                      {

                          Log.Error("x", $"error: {eg.Message}/n{eg.StackTrace}");

                      }

           

                      try

                      {

                          // get saved bitmap image as byte array (must be saved as a bitmap, not PNG or JPG)

                          var imgFile = new Java.IO.File(signatureImageSavePath);

                          var stream = new Java.IO.FileInputStream(imgFile);

                          var bytes = new byte[imgFile.Length()]; stream.Read(bytes);

           

                          //Store image to printer

                          string bitmapHeader = String.Format("~DYR:IMAGE,B,B,{0},,", bytes.Length);

                          outReader.Write(bitmapHeader);

                          s.OutputStream.Write(bytes);

                                     }

                                     catch (Java.IO.Exception eg)

                      {

                          Log.Error("x", $"error: {eg.Message}/n{eg.StackTrace}");

                      }

           

                      outReader.Write(cc + "\n");

                      outReader.Flush();

                      outReader.Close();

                      s.Close();

                      BA.CancelDiscovery();

           

           

                  }