簡體   English   中英

將圖像從可繪制文件夾打印到Bluetooth打印機-Android

[英]Print Image from drawable folder to Bluetooth Printer - Android

我正在嘗試使用輸出流將JPEG圖像打印到Zebra藍牙打印機。

我的文字可以正常工作,但無法打印圖像。 我已經在下面包含了我嘗試過的代碼。

編輯:我現在已經創建了一個字節數組,所以我只需要知道如何將字節數組打印到輸出流即可。

        btoutputstream = btsocket.getOutputStream();
        byte[] printformat = {0x1B, 0x21, FONT_TYPE};
        btoutputstream.write(printformat);

        String t_line1 = "! 0 100 100 185 1\r\n";
        String t_line2 = "PCX 80 30\r\n";
        String t_line3 = "PRINT\r\n";

        try {
            Drawable d = ContextCompat.getDrawable(getActivity(), R.drawable.image);
            Bitmap bitmap = ((BitmapDrawable)d).getBitmap();
            ByteArrayOutputStream stream = new ByteArrayOutputStream();
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
            byte[] bitmapdata = stream.toByteArray();

            btoutputstream.write(t_line1.getBytes());
            btoutputstream.write(t_line2.getBytes());

            btoutputstream.write(bitmapdata);
            btoutputstream.write(t_line3.getBytes());
        }catch(Exception e) {

        }
        btoutputstream.flush();

您可以使用以下命令打印位圖: PrintPic

try {
    Drawable d = ContextCompat.getDrawable(getActivity(), R.drawable.image);
    Bitmap bitmap = ((BitmapDrawable)d).getBitmap();
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    PrintPic printPic = PrintPic.getInstance();
    printPic.init(bitmap);
    byte[] bitmapdata = printPic.printDraw();

    btoutputstream.write(t_line1.getBytes());
    btoutputstream.write(t_line2.getBytes());
    btoutputstream.write(bitmapdata);
    btoutputstream.write(t_line3.getBytes());
} catch (Exception e) {
}
btoutputstream.flush();

要打印圖像,請使用PrintHelper 如果您需要文本和圖像作為文檔,請使用PrintManager

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM