从可绘制文件夹打印图像到蓝牙打印机 - Android

时间:2017-04-27 08:11:19

标签: android image printing bluetooth

我正在尝试使用输出流将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();

2 个答案:

答案 0 :(得分:0)

要打印图像,请使用PrintHelper。如果您需要文本和图片作为文档,请使用PrintManager

答案 1 :(得分:0)

您可以使用以下方式打印位图: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();