0

我正在尝试在 android studio 的热敏 pos 蓝牙打印机上打印 qr 代码,如图像,打印机已连接 - 蓝牙,我可以将文本和图像打印为 Drawable,但我不能我无法打印图像来自网址,如果有人可以提供帮助,我将不胜感激。

我想将二维码作为图像发送到打印机

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

在此处输入图像描述

我使用此功能(Android Studio)进行打印

  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);

我使用此功能从 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;
    }
}

如果不是myBitSlika,而是用可绘制目录拍照,打印没有问题,(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" +
4

0 回答 0