0

I've been following many SO answers and none of them work. This is one of them, and I think is the most promising (the original answer)

        public static async Task PrintImageFilesAsync(BluetoothSocket _socket, IEnumerable<FileResult> imageFiles)
        { 
            foreach(var file in imageFiles) {
                var path = file.FullPath;
                var imageFile = File.Open(path, FileMode.Open, FileAccess.ReadWrite);
                
                Bitmap originalImage = await BitmapFactory.DecodeStreamAsync(imageFile);
                var strippedImage = StripColor(originalImage); // Strips the image of ALL colour, and re-colourizes it all to black.

                using (var memStream = new MemoryStream()) {
                    int bufferLength = (int)imageFile.Length;
                    byte[] buffer = new byte[bufferLength];

                    await strippedImage.CompressAsync(Bitmap.CompressFormat.Jpeg, 0, memStream); // Writes image data to memStream
                    buffer = memStream.ToArray(); // Writes data in memStream to buffer
                    await imageFile.ReadAsync(buffer, 0, bufferLength); // Modifies the buffer IN PLACE
                    try
                    {
                        await _socket.OutputStream.WriteAsync(buffer, 0, bufferLength); // This part is responsible for printing.
                    }
                    catch (Java.IO.IOException ioEx)
                    {
                        originalImage.Dispose();
                        strippedImage.Dispose();

                        memStream.Close();
                        memStream.Dispose();

                        _socket.Close();
                        _socket.Dispose();

                        CrossToastPopUp.Current.ShowToastError($"{ioEx.Message}\nError: {ioEx}");
                        return;
                    }
                    strippedImage.Dispose();
                    originalImage.Dispose();
                }
                var status = "Print job completed.";
                Debug.WriteLine(status);
                CrossToastPopUp.Current.ShowToastMessage(status);
            }
            _socket.Close();
            _socket.Dispose();
        }

The function then strips the image of all color with StripColor

        private static Task<Bitmap> StripColor(Bitmap image)
        {
            image = image.Copy(Bitmap.Config.Argb8888, true);
            try
            {
                image.EraseColor(0);
            }
            catch (Java.Lang.IllegalStateException isEx)
            {
                Debug.WriteLine($"ERROR:\n\n{isEx.Message}");
                return null;
            }

            return image;
        }

With the current code this is the output when I print an image: https://i.stack.imgur.com/hZjHr.jpg
It just prints out random gibberish. Is there a way to fix this?

YJH16120
  • 419
  • 1
  • 4
  • 15
  • I think the problem is with `buffer = memStream.ToArray();`, at no point in your code are you writing anything to `memStream`. Judging from the linked SO answer, you should probably add `image.Save(memStream, ImageFormat.Jpeg);` before `buffer = memStream.ToArray();` – MindSwipe Mar 22 '21 at 08:50
  • 1
    But in general: It's incredibly complicated to understand what your code is doing! You have `image` and `_image`, as well as a buffer floating around, and it's being passed as a parameter which is unnecessary, so, in general, I think your code could use some cleanup – MindSwipe Mar 22 '21 at 08:53
  • 1
    I think you need more information about the printer itself. Does the printer support printing images? And if yes, in which format? Due to the fact, that you send raw binary data through a serial interface you have to take care, what the printer expects to get. – Oliver Mar 22 '21 at 08:53
  • "in general, I think your code could use some cleanup" I know, I usually do that after it works. – YJH16120 Mar 22 '21 at 08:56
  • @Oliver "Does the printer support printing images?" Yes. "And if yes, in which format?" I have no idea. – YJH16120 Mar 22 '21 at 09:02
  • @MindSwipe "I think the problem is with `buffer = memStream.ToArray();`, at no point in your code are you writing anything to memStream" I updated the question, and replaced `file` with `memStream` in `PrintImage`. The result is still the same. – YJH16120 Mar 22 '21 at 09:03
  • The entire code as it is right now is a mess (no offence), and a lot of it is redundant, as what's actually happening is this `var file = File.Open(path, FileMode.Open, FileAccess.ReadWrite);` then `var buffer = new byte[(int)file.Length];` then `await file.ReadAsync(buffer, 0, buffer.Length);` and finally `await _socket.OutputStream.WriteAsync(buffer, 0, buffer.Length);`. It's incredibly hard to see what's going on, which is why I'm an advocate for "clean as you go". And as you can now clearly see, you're just sending raw bytes from the file to the printer, and none of your other code is ran – MindSwipe Mar 22 '21 at 09:29
  • "The entire code as it is right now is a mess (no offence)" I know its a mess. I can print text when I send bytes to the printer. I'm just assuming that I can do the same with images. "...and none of your other code is ran" I don't really see this though. – YJH16120 Mar 22 '21 at 23:52
  • @MindSwipe I cleaned up the code as per your suggestion – YJH16120 Mar 24 '21 at 02:37
  • @DexYap any update? I'm having the same problem. – devs121 Apr 16 '21 at 22:32
  • @devs121 yes, use this [api](https://github.com/lukevp/ESC-POS-.NET) – YJH16120 Apr 17 '21 at 02:22
  • @DexYap this seems that will solve the problem. I tried it, but I couldn't connect a bluetooth printer. Did you connect in this way? I mean, in example of the api shows `var printer = new SerialPrinter(portName: "COM5", baudRate: 115200);`, but for bluetooth how can I show the Address of it? Can you help me? Thanks for now. – devs121 Apr 20 '21 at 16:39
  • I connected it through classes available on the Android.Bluetooth namespace – YJH16120 Apr 22 '21 at 13:32
  • @DexYap Cool, but when it goes to `printer.Write()` it is referenced by the connection that you do with the printer (my answer above). How can you reference the printer connection by Android.Bluetooth to this `printer.Write()`? (Sorry for so many questions, i'm struggling with it about 5 months to find some solution). – devs121 Apr 22 '21 at 19:48
  • And one more thing: I'm using an Android cellphone to send print to my printer (it's a thermal printer) by Bluetooth. I'm not using any USB cable. Does it work with it that I'm using, right? – devs121 Apr 22 '21 at 19:51
  • Create a new question. It's more appropriate. Take a look at [this](https://github.com/YJH16120/TSC_MobileApp/blob/master/MobileApp/Printing/Printing.cs#L56) and [this](https://github.com/YJH16120/TSC_MobileApp/blob/master/MobileApp/Printing/Printing.cs#L150). And keep note of the imports. – YJH16120 Apr 23 '21 at 00:44
  • @DexYap I'm printing now, but I have a big problem about the height of what I'm printing. I created a question [here](https://stackoverflow.com/questions/67288683/print-image-esc-pos), please take a look. Thanks for now. – devs121 Apr 27 '21 at 18:30

0 Answers0