Search code examples
androidandroid-studioposprinter

Android studio - PrinterThermal/ESCPOS - image from url


I'm trying to print qr code like image, on a thermal pos bluetooth printer in android studio,the printer is connected - bluetooth, I can print the text and image as Drawable, but I can't I can't print the image from url, I would be grateful if someone can help.

i want to send qr code as image to printer

URL := 'http://www.examples.com/code_1.png';

enter image description here

I use this function (Android studio), for printing

  try {
              myBitSlika = getBitmapFromURL("https://examles.com/temp/code_nn.png") ;
        } catch (Exception e) {
            Log.e("APP", "url image::", e);
        }

  BluetoothConnection connection = BluetoothPrintersConnections.selectFirstPaired();
      if (connection != null) 
             {
             EscPosPrinter printer = new EscPosPrinter(connection, 203, 48f, 32);
   final String text = ""[C]<img>" + PrinterTextParserImg.bitmapToHexadecimalString(printer,myBitSlika )+ "</img>\n"  +
                            "[L]\n" +
                            "[L]" + df.format(new Date()) + "\n" +
                            "[C]================================\n" +
                            "[L]<b>Effective Java</b>\n" +
                            "[L]    1 pcs[R]" + nf.format(25000) + "\n" +
                            "[L]<b>Headfirst Android Development</b>\n" +
                            "[L]    1 pcs[R]" + nf.format(45000) + "\n" +
                            "[L]<b>The Martian</b>\n" +
                            "[L]    1 pcs[R]" + nf.format(20000) + "\n" +
                            "[C]--------------------------------\n" +
                            "[L]TOTAL[R]" + nf.format(90000) + "\n" +
                            "[L]DISCOUNT 15%[R]" + nf.format(13500) + "\n" +
                            "[L]TAX 10%[R]" + nf.format(7650) + "\n" +
                            "[L]<b>GRAND TOTAL[R]" + nf.format(84150) + "</b>\n" +
                            "[C]--------------------------------\n" +
                            "[C]<barcode type='ean13' height='10'>202105160005</barcode>\n" +
                            "[C]--------------------------------\n" +
                            "[C]Thanks For Shopping\n" +
                            "[C]https://kodejava.org\n" +
                            "[L]\n" +
                            "[L]<qrcode>https://kodejava.org</qrcode>\n";
    
                    printer.printFormattedText(text);

I use this feature to retrieve an image from the internet

public static Bitmap getBitmapFromURL(String src) {
    try {
        URL url = new URL(src);
        HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
        connection.setDoInput(true);
        connection.connect();
        InputStream input = connection.getInputStream();
        Bitmap myBitmap = BitmapFactory.decodeStream(input);
        return myBitmap;
    } catch (IOException e) {
        Log.e("APP", "save image", e);
        return null;
    }
}

if instead of myBitSlika, I put it to take a picture with drawable directory, print without problems, (PrinterTextParserImg.bitmapToHexadecimalString(printer,this.getApplicationContext().getResources().getDrawableForDensity(R.drawable.code_232,DisplayMetrics.DENSITY_LOW, getTheme()))

final String text = "[C]<img>" + PrinterTextParserImg.bitmapToHexadecimalString(printer,this.getApplicationContext().getResources().getDrawableForDensity(R.drawable.code_232,DisplayMetrics.DENSITY_LOW, getTheme())) + "</img>\n" +

Solution

  • QR Code <qrcode></qrcode> tag allows you to print a QR code. Inside the tag you need to write the QR code data.

    <qrcode>http://www.developpeur-web.dantsu.com/</qrcode> : Prints a QR code with a width and height of 20 millimeters. <qrcode size='25'>123456789</qrcode> : Prints a QR code with a width and height of 25 millimeters. ⚠ WARNING ⚠ : This tag has several constraints :

    A line that contains <qrcode></qrcode> can have only one alignment tag and it must be at the beginning of the line. <qrcode> must be directly preceded by nothing or an alignment tag ([L][C][R]). </qrcode> must be directly followed by a new line \n. You can't write text on a line that contains <qrcode></qrcode>.

    Check this out for more info:https://github.com/DantSu/ESCPOS-ThermalPrinter-Android/tree/1a5b3436526ee2f3b5603a2e72bd12ada94f8e7a#qr-code