Forum Xamarin.Forms

Announcement:

The Xamarin Forums have officially moved to the new Microsoft Q&A experience. Microsoft Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

To create new threads and ask questions head over to Microsoft Q&A for .NET and get involved today.

How to print an image using Bluetooth printer

topbktopbk AUMember ✭✭

I have spent many days to find an answer for this but still couldn't. I have a Xamarin cross platform app and what I need, is to print Barcode from a Bluetooth printer.

I used ZXing.Mobile to generate the barcode as a MemoryStream. It is a dependency injection and it works fine.
MemoryStream stream = new MemoryStream();
stream = (MemoryStream)DependencyService.Get().ConvertImageStream("C442871452");

Now I have a barcode image in Memory Stream. I have implemented another dependency service for printing. It can print text and work well.
await DependencyService.Get().Print("Text");

Now the question is how to print MemoryStream image?

I bought the Bluetooth printer from alibaba.com
https://www.alibaba.com/product-detail/Wireless-barcode-scanner-pos-printer-Black_60672727314.html?spm=a2700.galleryofferlist.normal_offer.d_image.4cd61b55PU0Ij9

Appreciate it if someone can share some codes.

Best Answers

  • AlessandroCaliaroAlessandroCaliaro ITMember ✭✭✭✭✭
    Accepted Answer

    Sorry, these are all info I have. I have not a printer to test.
    BTW, I think GS should be an ESC char, not a string.
    If you take a look to the manual, you see

    So, they are all CHARS.

    char GS = (char) 29;

  • topbktopbk AUMember ✭✭
    Accepted Answer

    Thanks for your help. I have figured it out. here is the working code

    public static string GetBarcodeStr(string barcode, string msg)
    {
    char ESC = (char)27;
    char GS = (char)29;

            string getBarcodeStr;
            getBarcodeStr = ESC + "a" + (char)1; //align center
            getBarcodeStr = getBarcodeStr + GS + "h" + (char)80; //Bardcode Hieght           
            getBarcodeStr = getBarcodeStr + GS + "w" + (char)3; //Barcode Width 1 to 4
            getBarcodeStr = getBarcodeStr + GS + "f" + (char)0; //Font for HRI characters
            getBarcodeStr = getBarcodeStr + GS + "H" + (char)2; //Position of HRI characters
            getBarcodeStr = getBarcodeStr + GS + "k" + (char)69 + (char)barcode.Length; //'Print Barcode Smb 39            
            getBarcodeStr = getBarcodeStr + barcode + (char)0; //'Print Text Under            
            getBarcodeStr = getBarcodeStr + GS + "d" + (char)3;
            getBarcodeStr = getBarcodeStr + msg + "\r\n";
            getBarcodeStr = getBarcodeStr + GS + "@";
            getBarcodeStr = getBarcodeStr + ESC + "a" + (char)0; //align left
    
            return getBarcodeStr;
        }
    

Answers

  • AlessandroCaliaroAlessandroCaliaro ITMember ✭✭✭✭✭
    edited January 17
    It seems to use esc/pos. Take a look to esc/pos programming language and you should find a way to print an image

    Maybe https://stackoverflow.com/a/14099717/4399386
  • topbktopbk AUMember ✭✭

    I have already go through that but didn't work. This required system.drawing bitmap but Android returns from dependency service is Android graphic bitmap. So I couldn't use it on a common project.

    I tried to write this on Android but the Android Graphic bitmap does not work below part on GetBitmapData function.

    var luminance = (int)(color.R * 0.3 + color.G * 0.59 + color.B * 0.11);

    Is anyone having a working sample?

  • topbktopbk AUMember ✭✭

    My ultimate goal is to print a barcode, not an image. My printer support Code 39, Code 128, and a few other fonts. Is there any other way that I can send the barcode as a string?

  • AlessandroCaliaroAlessandroCaliaro ITMember ✭✭✭✭✭
    Yes, there are esc pos commands to print a barcode. Take a look to internet to search an esc pos command manual
  • topbktopbk AUMember ✭✭

    last few days I'm searching for it. Couldn't find any working solution.

  • topbktopbk AUMember ✭✭

    Here is the code I'm trying

    string ESC = Convert.ToString((char)27);
    string GS = Convert.ToString((char)29);
    string center = ESC + "a" + (char)1; //align center
    string left = ESC + "a" + (char)0; //align left
    string bold_on = ESC + "E" + (char)1; //turn on bold mode
    string bold_off = ESC + "E" + (char)0; //turn off bold mode
    string cut = ESC + "d" + (char)1 + GS + "V" + (char)66; //add 1 extra line before partial cut

    string initp = ESC + (char)64; //initialize printer
    string buffer = ""; //store all the data that want to be printed
    string QrData = "LianaAliBlogspotMy"; //data to be print in QR code

    buffer += center;
    buffer += "This text is align center!\n"; //to enter or add newline: \n
    buffer += left;
    buffer += "This text is align left!\n"; //align left is already set as default if not specif

    //Print QRCode
    Encoding m_encoding = Encoding.GetEncoding("iso-8859-1"); //set encoding for QRCode
    int store_len = (QrData).Length + 3;
    byte store_pL = (byte)(store_len % 256);
    byte store_pH = (byte)(store_len / 256);
    buffer += initp; //initialize printer
    buffer += m_encoding.GetString(new byte[] { 29, 40, 107, 4, 0, 49, 65, 50, 0 });
    buffer += m_encoding.GetString(new byte[] { 29, 40, 107, 3, 0, 49, 67, 8 });
    buffer += m_encoding.GetString(new byte[] { 29, 40, 107, 3, 0, 49, 69, 48 });
    buffer += m_encoding.GetString(new byte[] { 29, 40, 107, store_pL, store_pH, 49, 80, 48 });
    buffer += QrData;
    buffer += m_encoding.GetString(new byte[] { 29, 40, 107, 3, 0, 49, 81, 48 });

    await DependencyService.Get().Print(buffer, "");

    It works for aligning the font but does not work for Barcode.

  • AlessandroCaliaroAlessandroCaliaro ITMember ✭✭✭✭✭

    I have this old list of commands

    and this is a manual
    https://www.sewoo.eu/wordpress/n/ESCPOS_command_manual.pdf
    I think GS k is your command

  • topbktopbk AUMember ✭✭

    Appreciate your help.

    I was trying in another function. Below is the code. But still didn't work. Did I do something not right?

    public byte[] BarcodeGenrate(string BarcodeNO)
    {
    string GS = "\u001D";
    string ESC = Convert.ToString((char)27);
    string COMMAND = "";
    COMMAND += GS + "H" + 2; //Select print position of HRI characters:
    COMMAND = GS + "h" + 50;//Set barcode height
    COMMAND = GS + "w" + 3;//Set barcode height
    COMMAND += GS + "f" + 1; //Select font for HRI characters:
    COMMAND += GS + "k" + 4 + BarcodeNO + 0; //Print barcode: (A) format, barcode system = CODE39
    var result = System.Text.Encoding.Unicode.GetBytes(COMMAND);
    return result;
    }

  • AlessandroCaliaroAlessandroCaliaro ITMember ✭✭✭✭✭
    Accepted Answer

    Sorry, these are all info I have. I have not a printer to test.
    BTW, I think GS should be an ESC char, not a string.
    If you take a look to the manual, you see

    So, they are all CHARS.

    char GS = (char) 29;

  • topbktopbk AUMember ✭✭
    Accepted Answer

    Thanks for your help. I have figured it out. here is the working code

    public static string GetBarcodeStr(string barcode, string msg)
    {
    char ESC = (char)27;
    char GS = (char)29;

            string getBarcodeStr;
            getBarcodeStr = ESC + "a" + (char)1; //align center
            getBarcodeStr = getBarcodeStr + GS + "h" + (char)80; //Bardcode Hieght           
            getBarcodeStr = getBarcodeStr + GS + "w" + (char)3; //Barcode Width 1 to 4
            getBarcodeStr = getBarcodeStr + GS + "f" + (char)0; //Font for HRI characters
            getBarcodeStr = getBarcodeStr + GS + "H" + (char)2; //Position of HRI characters
            getBarcodeStr = getBarcodeStr + GS + "k" + (char)69 + (char)barcode.Length; //'Print Barcode Smb 39            
            getBarcodeStr = getBarcodeStr + barcode + (char)0; //'Print Text Under            
            getBarcodeStr = getBarcodeStr + GS + "d" + (char)3;
            getBarcodeStr = getBarcodeStr + msg + "\r\n";
            getBarcodeStr = getBarcodeStr + GS + "@";
            getBarcodeStr = getBarcodeStr + ESC + "a" + (char)0; //align left
    
            return getBarcodeStr;
        }
    
Sign In or Register to comment.