57

I have to print some data on thermal bluetooth printer, I'm doing with this:

String message="abcdef any message 12345";
byte[] send;
send = message.getBytes();
mService.write(send);

It works well for text, but not for images. I think I need to get the byte[] of the image data. I tried getting the data of the image this way:

Bitmap bitmap=BitmapFactory.decodeResource(getResources(), R.drawable.qrcode);
ByteArrayOutputStream stream=new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 90, stream);
byte[] image=stream.toByteArray();

Unfortunately the printer prints a lot of strange characters (approx. 50 cm of paper). I don't know how to print the image.

I would like to try getting the pixels of the bitmap and next converting it to a byte[] and sending it, but i don't know how to do it.

Thanks

UPDATE:

After so much time, i'm doing this: I have a method called print_image(String file), the which gets the path of the image that i want to print:

private void print_image(String file) {
    File fl = new File(file);
    if (fl.exists()) {
        Bitmap bmp = BitmapFactory.decodeFile(file);
        convertBitmap(bmp);
        mService.write(PrinterCommands.SET_LINE_SPACING_24);

        int offset = 0;
        while (offset < bmp.getHeight()) {
            mService.write(PrinterCommands.SELECT_BIT_IMAGE_MODE);
            for (int x = 0; x < bmp.getWidth(); ++x) {

                for (int k = 0; k < 3; ++k) {

                    byte slice = 0;
                    for (int b = 0; b < 8; ++b) {
                        int y = (((offset / 8) + k) * 8) + b;
                        int i = (y * bmp.getWidth()) + x;
                        boolean v = false;
                        if (i < dots.length()) {
                            v = dots.get(i);
                        }
                        slice |= (byte) ((v ? 1 : 0) << (7 - b));
                    }
                    mService.write(slice);
                }
            }
            offset += 24;
            mService.write(PrinterCommands.FEED_LINE);
            mService.write(PrinterCommands.FEED_LINE);          
            mService.write(PrinterCommands.FEED_LINE);
            mService.write(PrinterCommands.FEED_LINE);
            mService.write(PrinterCommands.FEED_LINE);
            mService.write(PrinterCommands.FEED_LINE);
        }
        mService.write(PrinterCommands.SET_LINE_SPACING_30);


    } else {
        Toast.makeText(this, "file doesn't exists", Toast.LENGTH_SHORT)
                .show();
    }
}

I did it based on this post

This is the class PrinterCommands:

public class PrinterCommands {
public static final byte[] INIT = {27, 64};
public static byte[] FEED_LINE = {10};

public static byte[] SELECT_FONT_A = {27, 33, 0};

public static byte[] SET_BAR_CODE_HEIGHT = {29, 104, 100};
public static byte[] PRINT_BAR_CODE_1 = {29, 107, 2};
public static byte[] SEND_NULL_BYTE = {0x00};

public static byte[] SELECT_PRINT_SHEET = {0x1B, 0x63, 0x30, 0x02};
public static byte[] FEED_PAPER_AND_CUT = {0x1D, 0x56, 66, 0x00};

public static byte[] SELECT_CYRILLIC_CHARACTER_CODE_TABLE = {0x1B, 0x74, 0x11};

public static byte[] SELECT_BIT_IMAGE_MODE = {0x1B, 0x2A, 33, -128, 0};
public static byte[] SET_LINE_SPACING_24 = {0x1B, 0x33, 24};
public static byte[] SET_LINE_SPACING_30 = {0x1B, 0x33, 30};

public static byte[] TRANSMIT_DLE_PRINTER_STATUS = {0x10, 0x04, 0x01};
public static byte[] TRANSMIT_DLE_OFFLINE_PRINTER_STATUS = {0x10, 0x04, 0x02};
public static byte[] TRANSMIT_DLE_ERROR_STATUS = {0x10, 0x04, 0x03};
public static byte[] TRANSMIT_DLE_ROLL_PAPER_SENSOR_STATUS = {0x10, 0x04, 0x04};
}

As is seen in the print_image method I'm calling a method, called convertBitmap, and im sending a bitmap, this is the code:

   public String convertBitmap(Bitmap inputBitmap) {

    mWidth = inputBitmap.getWidth();
    mHeight = inputBitmap.getHeight();

    convertArgbToGrayscale(inputBitmap, mWidth, mHeight);
    mStatus = "ok";
    return mStatus;

}

private void convertArgbToGrayscale(Bitmap bmpOriginal, int width,
        int height) {
    int pixel;
    int k = 0;
    int B = 0, G = 0, R = 0;
    dots = new BitSet();
    try {

        for (int x = 0; x < height; x++) {
            for (int y = 0; y < width; y++) {
                // get one pixel color
                pixel = bmpOriginal.getPixel(y, x);

                // retrieve color of all channels
                R = Color.red(pixel);
                G = Color.green(pixel);
                B = Color.blue(pixel);
                // take conversion up to one single value by calculating
                // pixel intensity.
                R = G = B = (int) (0.299 * R + 0.587 * G + 0.114 * B);
                // set bit into bitset, by calculating the pixel's luma
                if (R < 55) {                       
                    dots.set(k);//this is the bitset that i'm printing
                }
                k++;

            }


        }


    } catch (Exception e) {
        // TODO: handle exception
        Log.e(TAG, e.toString());
    }
}

This is the printer that i'm using, resolution: 8 dots/mm, 576 dots/line

And this is what I like to do (i did it with the same printer, but with an app downloaded from play store) Image that I want to print

This is what i'm getting now My printing trying

Closer: A closer part

Closer2: enter image description here

A little part of the image can be seen, so I think that i'm closer to can print the image...

The image that i'm using is this (576x95):enter image description here

And this is the converted image (i'm converting it with the upper code): converted image

Inverted

So, the answer is: what I'm doing wrong?, I think that the error is in this command:

  public static byte[] SELECT_BIT_IMAGE_MODE = {0x1B, 0x2A, 33, -128, 0};

But, how can I calculate the correct values for my image?, thanks

Leonardo Sapuy
  • 2,600
  • 2
  • 23
  • 30
  • Yeah, i send it of the same way that 1st snipplet `mService.write(image)`, the printer has a SDK but it don't compile, so I don't know how to do it. – Leonardo Sapuy Jan 25 '13 at 20:54
  • If you have the source for the SDK you can look at how they do it. – dutt Jan 25 '13 at 20:55
  • Now I decompile it, they use a method to return the byte[] array, but it don't works, I implemented that method but when I run the app on the tablet, it dies, generate a force close error... `` – Leonardo Sapuy Jan 25 '13 at 21:09
  • I just have the .jar library, this is the SDK proportioned by the printer... But i'm not using this because it don't works for me, it don't compiles... I get no error in the stack trace... When I try to print a image, it prints normally, but not the image, print some stranger characters... – Leonardo Sapuy Apr 30 '13 at 13:56
  • @nlsbshtr I recommend the Bixolon SPP-R200. However we're not using the official SDK to print, but rather we implemented our own pairing and printing procedures. – AgentKnopf May 01 '13 at 14:55
  • @LeonardoSapuy Its tough giving suggestions without the SDK, but what we do to print images: We convert them to Monochrome bitmaps and during conversion we write each converted pixel to a byte array. The max size of the byte array is determined by: the printer's maximum printing width * bitmap.getHeight() / 8 . For the monochrome conversion we turned each pixel black whose RGB color was < 128 and to white, if the RGB color was >= 128 – AgentKnopf May 01 '13 at 14:58
  • However the Bixolon SDK for instance let's you pass in images without having to convert them to monochrome first, as the SDK will handle that. However for some unfathomable reason it was not possible to simply print an in-memory image (the SDK simply did not offer this option), so we hat to send the bytes to the printer ourselves. – AgentKnopf May 01 '13 at 15:01
  • I'm converting the image into a monochrome bmp, doing what do yo say... But i need to get that BMP and print it... Do yo know how to do it?, or could giveme an example using the bixolon SDK, i'm using a REGO printer, but his SDK don't works – Leonardo Sapuy May 02 '13 at 20:39
  • @LeonardoSapuy Hi, Can you please help me out here: http://stackoverflow.com/questions/16982485/printing-an-image-using-a-bluetooth-thermal-printer-from-an-android-app I have a similar problem :( – beerBear Jun 07 '13 at 11:18
  • No, sorry, if you want I could send you the test project – Leonardo Sapuy Oct 29 '13 at 21:56
  • Hi Leonardo Sapuy can you share how you fix problem? – pavol.franek Jul 01 '14 at 13:29
  • can you share the whole code please – Hala.M Sep 21 '20 at 17:02
  • Useful link: https://developer.android.com/training/printing/photos#image – Sumit Shukla Apr 21 '21 at 05:41

8 Answers8

26

I solve it converting Bitmap to Byte array. Remember that your image must be black & white format.

For full source code: https://github.com/imrankst1221/Thermal-Printer-in-Android

enter image description here

 public void printPhoto() {
        try {
            Bitmap bmp = BitmapFactory.decodeResource(getResources(),
                    R.drawable.img);
            if(bmp!=null){
                byte[] command = Utils.decodeBitmap(bmp);
                printText(command);
            }else{
                Log.e("Print Photo error", "the file isn't exists");
            }
        } catch (Exception e) {
            e.printStackTrace();
            Log.e("PrintTools", "the file isn't exists");
        }
    }
Md Imran Choudhury
  • 9,343
  • 4
  • 62
  • 60
  • 5
    Why does it has limitation to width and height? – mr5 May 30 '18 at 15:25
  • 2
    Is it possible to print an image wider than 255 px? – daneejela Jul 24 '18 at 13:31
  • @daneejela answer is no, but if your selected printer support then it works. – Md Imran Choudhury Nov 05 '18 at 03:42
  • what should I do is only paste your code? then It will work? Because what should i change the **Utils** since it has a red error? @MdImranChoudhury – Leon Zaii Nov 07 '18 at 02:43
  • The Utils is run for this device module. So if you are using a different version then maybe you need to change. – Md Imran Choudhury Nov 07 '18 at 03:32
  • Nice job this code works perfect except that u increase the target SDK of your app to 28 then you cannot get connected to Bluetooth print any thing. So please test with target SDK 28 and tell me the result. Most of my customers are complaining about not being able to print via Bluetooth after they upgraded to or bought an Android 9 device (Target SDK 28). – saeed khalafinejad Jun 05 '19 at 05:44
  • How to print that image in center align? I try follow your code but my image is printed in left side – Kharisma Jul 21 '19 at 04:32
  • 1
    Hello @MdImranChoudhury some time decodeBitmap(bmp) return null – Deepak yogi Sep 26 '19 at 07:35
  • You are the angel, it works perfectly. Thanks!! – Sam Chen Feb 16 '21 at 16:59
  • ~You started with `ESC *` but then went on to solve it with `GS v 0`. I haven't tested the former but the latter gives me a tiny image with a distorted first left column. – vesperto Jun 14 '21 at 10:49
  • @MdImranChoudhury is any example to print from image url? – Rembulan Moon Jul 23 '21 at 09:31
  • @RembulanMoon first you need to read the image from the URL as a Bitmap then you can call the print method with Bitmap data. – Md Imran Choudhury Jul 25 '21 at 09:50
  • public void printPhoto(String imgUrl) { try { try { URL url = new URL(imgUrl); Bitmap bmp = BitmapFactory.decodeStream(url.openConnection().getInputStream()); if (bmp != null) { byte[] command = Utils.decodeBitmap(bmp); outputStream.write(PrinterCommands.ESC_ALIGN_CENTER); printText(command); ... i have tried this code, but it prints black rectangle only @MdImranChoudhury – Rembulan Moon Jul 26 '21 at 02:52
  • @MdImranChoudhury is there any way to increase the size of the QR image? – Arnold Brown Aug 18 '21 at 08:44
13

I also tried this and I got to my own solution and I think I figured out how the SELECT_BIT_IMAGE_MODE command works.

The command public static byte[] SELECT_BIT_IMAGE_MODE = {0x1B, 0x2A, 33, 255, 3}; in the class PrinterCommands is the POS Command for image printing.

The first two are pretty standard, the next three determine the mode and the dimensions of the image to be printed. For the sake of this solution, let's just assume that the second element (33, we are indexed zero) is always 33.

The last two elements of that byte[] refers to the Width (in pixels) property of the image that you want to print, element 3 is sometimes referred to as nL and element 4 is sometimes referred to as nH. They are actually both referring to the Width, nL is the Low Byte while nH is the High Byte. This means that we can have at the most an image with a width of 1111 1111 1111 1111b (binary) which is 65535d (decimal), though I haven't tried it yet. If nL or nH aren't set to the proper values, then there will be trash characters printed along with the image.

Somehow, Android docs tells us that the limits of the value for a byte in a byte array is -128 and +127, when I tried to put in 255, Eclipse asked me to cast it to Byte.

Anyway, going back to nL and nW, for your case, you have an image with width 576, if we convert 576 to Binary, we get two bytes which would go like:

0000 0010 0100 0000

In this case, the Low Byte is 0100 0000 while the High Byte is 0000 0010. Convert it back to decimal and we get nL = 64 and nH = 2.

In my case, I printed an image that has width of 330px, converting 330 to binary we get:

0000 0001 0100 1010

In this case now, the Low Byte is 0100 1010 and the High Byte is 0000 0001. Converting to decimal, we get nL = 74 and nH = 1.

For more information, look at these documentation/tutorials:

Star Asia Mobile Printer Documentation

ECS-POS programming guide - really extensive

Another Documentation

The expanded version of the code above, with more explanation

Explanation of the code above

Hope these helps.

Razgriz
  • 7,179
  • 17
  • 78
  • 150
  • 2
    Thanks for your answer, I use this code to initialize my priter: `public static byte[] SELECT_BIT_IMAGE_MODE = { 0x1B, 0x2A, 33, (byte) 255, 3 };` and it works alright for me – Leonardo Sapuy Jul 22 '13 at 13:28
  • This answer also help me. I printet on bluetooth QRcode 150x150 on recaipt and at least work. It is printet on left side. My questions is how to center printet image if is possible ? – Vranilac Dec 19 '19 at 14:22
  • {0x1B,0x33,0x00} load this command before you print the bitmap to center the image @Vranilac – Peter Jan 05 '20 at 09:15
12

Solved!, I was doing a wrong printer initializing... The corect way is:

 public static byte[] SELECT_BIT_IMAGE_MODE = {0x1B, 0x2A, 33, 255, 3};

So, by this way the image is printed completely fine

Leonardo Sapuy
  • 2,600
  • 2
  • 23
  • 30
  • Hi i implemented above code and replaced it with your solution too but still i get some strange characters printed on paper. Please help. Thanks in advance. – Ankit Jun 24 '13 at 13:27
  • That would be great, if you can help me. – Ankit Jul 04 '13 at 13:28
  • Sorry, I wrote wrong my mail, is this: leonardo.sapuy@hotmail.com – Leonardo Sapuy Jul 11 '13 at 17:57
  • I figured it out, please refer to my answer for an expounded version. – Razgriz Jul 22 '13 at 08:17
  • @Leonardo Sapuy i really need your little help...................please email me usmizpassion@gmail.com – user2768215 Jan 02 '14 at 09:54
  • @LeonardoSapuy if i set height upto 255 in height section then `Bitmap bmpGrayscale = Bitmap.CreateBitmap(OriginalBitmap.Width, 228, Bitmap.Config.Argb8888);` then `byte[] command = Utils.DecodeBitmap(bmpGrayscale);` DecodeBitmap method return `null` – Deepak yogi Sep 27 '19 at 06:09
  • HI, @LeonardoSapuy can you please tell me what is mService in your original code. – sups May 15 '20 at 10:54
9

EDIT: Update based on reading your question: https://stackoverflow.com/questions/16597789/print-bitmap-on-esc-pos-printer-java

I will assume that the printer you are printing to is the same as above, i.e. the Rego Thermal Printer. This, as you note, support the ESC/POS Page Description Language.


Printers interpret data streamed to them as either a marked up document (in a similar way to how browser interpret HTML). In some cases, the printer literally runs the document as a program (eg PostScript). Link: Page Description Languages.

Common languages are:

You need to read the specifications for your printer to determine which language to use - if you need to support any printer, then you have a very large job ahead of you :(

In ESC/POS, you will need to use the GS v 0 command (documented on p33). You do this by sending the the characters 0x1D7630 across the serial link, followed by a set of arguments:

ASCII:       Gs   v  0 
Decimal:     29 118 48 m xL xH yL yH [d]k 
Hexadecimal: 1D  76 30 m xL xH yL yH [d]k 

Parameter definitions:

  • m:
    • 0,48: normal mode (1:1 scale)
    • 1,49: double-width
    • 2,50: double-height
    • 3,51: double-width + double-height
  • xL, xH specifies (xL + xH × 256) bytes in horizontal direction for the bit image.
  • yL, yH specifies (yL + yH × 256) dots in vertical direction for the bit image.
  • [d]k specifies the bit image data (raster format).
  • k indicates the number of bit image data. k is an explanation parameter; therefore, it does not need to be transmitted.

Notes:

  • When data [d]k is 1 specifies a bit printed to 1 and not printed to 0.
  • If a raster bit image exceeds one line of print area, the excess data is not printed.
  • This command executes paper feed for amount needed for printing the bit image regardless of the settings by ESC 2 or ESC 3.
  • After printing the bit image, this command sets the print position to the beginning of the line, and clears up the buffer.
  • When this command is executed, the data is transmitted and printed synchronously. So no other printing command is required.

There are several more extensive expositions:


Unfortunately there is no printer API in Android. If you feel strongly about this, do follow these issues:

Community
  • 1
  • 1
Andrew Alcock
  • 19,401
  • 4
  • 42
  • 60
  • Thanks for your answer... Just right now I'm printing a little part of the image, my printer is ESC/POS commands compatible, and has a 576 dost/line resolution... I found [this](http://android-essential-devtopics.blogspot.com/2013/02/sending-bit-image-to-epson-printer.html), is a java traduction for the Nicholas Piasecki's post... I implemented that source in my project, so now i'm printing a little part if my image... But anyway it can't print the complete image. I think that is a bad command use, I'm using this command for initialize the printer in the Graphic mode: `0x1B, 0x2A, 33, -128, 0` – Leonardo Sapuy May 23 '13 at 14:05
  • But I think that I'm doing it wrong... My image has a 576 x 96 pixels resolution, could you tell me if I'm doing right the printer init, or what I have to change in the code?? – Leonardo Sapuy May 23 '13 at 14:06
  • 2
    I'm about to go to bed, but it would be very useful if you could give a more comprehensive description of what is and is not happening. For example, how many lines of output are produced? Is the full width printed on each line? Are the lines printed pixel perfect or wrong? Can you show a hex-dump of the data sent to the printer? – Andrew Alcock May 23 '13 at 14:18
  • Hi Andrew, I edited the answer, so there you can see what exactly I'm doing, thanks in advanced – Leonardo Sapuy May 30 '13 at 16:01
  • Can't get what you are saying.. The answer needs edit. Can't understand well. – Mohammed Shareef C Sep 09 '17 at 02:54
  • @AndrewAlcock I know this is very old and you probably forgot about this, but if you still remember or somehow can still understand your answer, could you give me an example `m` parameter? or the whole print command like: `1D7630data...` – mr5 Jul 01 '18 at 13:55
  • Hi, I am Using intermec pb51 ESC/P printer language, please share the How to change font, which font support Romania character. – Mani Nov 16 '19 at 09:41
3

I am new to ESC/POS and am struggling with it. I came across this page which seems to have some useful functions: http://code.taobao.org/p/printer/src/trunk/prtest/src/com/enjar/plugins/PrintTools_58mm.java It's in Chinese though, but might be worth going through. If anybody figures it out, I would like to get enlightened too...

just some guy
  • 524
  • 1
  • 4
  • 18
  • Thanks, these code could be useful for print images in a BT printer, I improved my code for image printing, so if you need some tips I could help you. – Leonardo Sapuy Nov 14 '13 at 14:27
2

I know for evolute and AMDL bluetooth printers.First Read the protocol defination document of the printer that tells you what specific bytes you need for the device-

public void connect() throws Exception 
{

    BluetoothDevice printer = BluetoothAdapter.getDefaultAdapter().getRemoteDevice(connParams);

    Method m = printer.getClass().getMethod("createInsecureRfcommSocket",new Class[] { int.class });
    sock = (BluetoothSocket)m.invoke(printer, Integer.valueOf(1));
    sock.connect();
    os=sock.getOutputStream();
    in=sock.getInputStream();

}

After connecting through the above code you get the outputstream of the socket.Then convert your image to the corresponding byte through the tool provided with the printer you get something like

public byte[] Packet1={
        (byte)0X8A,(byte)0XC6,(byte)0X94,(byte)0XF4,(byte)0X0B,(byte)0X5E,(byte)0X30,(byte)0X25,(byte)
        0X01,(byte)0X5E,(byte)0X04,(byte)0X24,(byte)0X01,(byte)0X0C,(byte)0X5E,(byte)0X03,(byte)0X24,(byte)0X01,(byte)0X08,(byte)0X5E,(byte)0X27,(byte)0X25,(byte)
        0X01,(byte)0X5E,(byte)0X04,(byte)0X24,(byte)0X05,(byte)0X0C,(byte)0X00,(byte)0X60,(byte)0X00,(byte)0X18,(byte)0X5E,(byte)0X27,(byte)0X25,(byte)
        0X01,(byte)0X5E,(byte)0X03,(byte)0X24,(byte)0X06,(byte)0X30,(byte)0X1E,(byte)0X10,(byte)0X60,(byte)0X00,(byte)0X18,(byte)0X5E,(byte)0X27,(byte)0X25,(byte)
        0X01,(byte)0X5E,(byte)0X03,(byte)0X24,(byte)0X06,(byte)0X70,(byte)0X3F,(byte)0X18,(byte)0XF0,(byte)0X00,(byte)0X3E,(byte)0X5E,(byte)0X27,(byte)0X25,(byte)
        0X01,(byte)0X5E,(byte)0X03,(byte)0X24,(byte)0X06,(byte)0X70,(byte)0X3C,(byte)0X39,(byte)0XF1,(byte)0X80,(byte)0X3E,(byte)0X5E,(byte)0X27,(byte)0X25,(byte)
        0X01,(byte)0X5E,(byte)0X03,(byte)0X24,(byte)0X06,(byte)0XF8,(byte)0X7C,(byte)0X9F,(byte)0XF1,(byte)0X80,(byte)0X7F,(byte)0X5E,(byte)0X27,(byte)0X25,(byte)
        0X01,(byte)0X5E,(byte)0X03,(byte)0X24,(byte)0X06,(byte)0XF9,(byte)0X9E,(byte)0X1C,(byte)0XFF,(byte)0XC2,(byte)0X7E,(byte)0X5E,(byte)0X27,(byte)0X25,(byte)
        0X01,(byte)0X5E,(byte)0X03,(byte)0X24,(byte)0X06,(byte)0XF9,(byte)0X9E,(byte)0X1C,(byte)0XE7,(byte)0XE2,(byte)0X7E,(byte)0X5E,(byte)0X27,(byte)0X25,(byte)
        0X01,(byte)0X5E,(byte)0X03,(byte)0X24,(byte)0X06,(byte)0XFB,(byte)0X1E,(byte)0X1C,(byte)0XFF,(byte)0XE7,(byte)0XBE,(byte)0X5E,(byte)0X27,(byte)0X25,(byte)
        0X01,(byte)0X5E,(byte)0X03,(byte)0X24,(byte)0X06,(byte)0X7B,(byte)0X16,(byte)0X1C,(byte)0XFF,(byte)0XDF,(byte)0X3E,(byte)0X5E,(byte)0X27,(byte)0X25,(byte)
        0X01,(byte)0X5E,(byte)0X03,(byte)0X24,(byte)0X06,(byte)0X71,(byte)0X12,(byte)0X1C,(byte)0XE7,(byte)0XF7,(byte)0X34,(byte)0X5E,(byte)0X27,(byte)0X25,(byte)
        0X01,(byte)0X5E,(byte)0X03,(byte)0X24,(byte)0X06,(byte)0X51,(byte)0X12,(byte)0X1C,(byte)0XF7,(byte)0XF7,(byte)0X24,(byte)0X5E,(byte)0X27,(byte)0X25,(byte)
        0X01,(byte)0X5E,(byte)0X03,(byte)0X24,(byte)0X06,(byte)0X49,(byte)0X12,(byte)0X1C,(byte)0XFF,(byte)0XF3,(byte)0X24,(byte)0X5E,(byte)0X27,(byte)0X25,(byte)
        0X01,(byte)0X5E,(byte)0X03,(byte)0X24,(byte)0X06,(byte)0X49,(byte)0X12,(byte)0X3F,(byte)0XFD,(byte)0XF3,(byte)0X24,(byte)0X5E,(byte)0X27,(byte)0X25,(byte)
        0X01,(byte)0X5E,(byte)0X03,(byte)0X24,(byte)0X06,(byte)0X49,(byte)0X96,(byte)0X3F,(byte)0XFC,(byte)0XF3,(byte)0X24,(byte)0X5E,(byte)0X27,(byte)0X25,(byte)
        0X01,(byte)0X5E,(byte)0X03,(byte)0X24,(byte)0X05,(byte)0X49,(byte)0X80,(byte)0X00,(byte)0X08,(byte)0X10,(byte)0X5E,(byte)0X28,(byte)0X25,(byte)
        0X01,(byte)0X5E,(byte)0X30,(byte)0X25,(byte)
        0X01,(byte)0X5E,(byte)0X03,(byte)0X24,(byte)0X06,(byte)0XE0,(byte)0X74,(byte)0XA9,(byte)0X33,(byte)0X23,(byte)0X26,(byte)0X5E,(byte)0X27,(byte)0X25,(byte)0X04
        };

where 8A is starting byte C6 is mode byte (different for smart card , swipe and fingerprint) , 94 is font byte and last byte 04 is the end byte telling the hardware that this is end of the packet.Depending on size of the image you get several of these packets of length 256 byte (most printer).Write them to the outputStream.

os.write(Packet1)
mihirjoshi
  • 12,161
  • 7
  • 47
  • 78
  • I'm now converting the image to a monochrome bitmap, so I'm creating a bitset the which contain 1's & 0's (it represents black or white), But the image isn't printing alright, just prints a little part of the image... – Leonardo Sapuy May 30 '13 at 14:00
  • you need to convert the image into its hex representation then cast those hex data to byte. Device only understands byte format.Also give me the link of the market app through which you are getting the print.See if I can make anything out of it. – mihirjoshi May 31 '13 at 17:06
  • @mjosh Hi can you please have a look at my question: http://stackoverflow.com/questions/16982485/printing-an-image-using-a-bluetooth-thermal-printer-from-an-android-app – beerBear Jun 07 '13 at 11:06
0

This works for me:

BitmapFactory.Options options = new BitmapFactory.Options();
options.inTargetDensity = 200;
options.inDensity = 200;
Bitmap bmp = BitmapFactory.decodeResource(context.getResources(), img, options);

Before using the BitmapFactory.Options, I was able to print only 60X60 image size, now i can print images of greater size as well.

Pawel Veselov
  • 3,996
  • 7
  • 44
  • 62
-6

use this code:

public static void print(Context context) {

    String examplePath = "file:///sdcard/dcim/Camera/20111210_181524.jpg";

    Intent sendIntent = new Intent(Intent.ACTION_SEND);
    sendIntent.setType("image/jpeg");
    sendIntent.putExtra(Intent.EXTRA_SUBJECT, "Photo");
    sendIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse(examplePath));
    sendIntent.putExtra(Intent.EXTRA_TEXT, "Enjoy the photo");
    context.startActivity(Intent.createChooser(sendIntent, "Email:"));
}
Taldroid
  • 382
  • 1
  • 8