简体   繁体   English

Android - 如何在蓝牙打印机 (X330) 上打印图像(光栅)?

[英]Android - How can I print an image (Raster) on a Bluetooth printer (X330)?

I have the thermal printer X330 and I need to print and image (bitmap) taken from the android Camera, but I can't...I'm always get symbols in the printer.我有热敏打印机 X330,我需要打印和从 android 相机拍摄的图像(位图),但我不能......我总是在打印机中得到符号。

In the documentation said:在文档中说:

Print raster bit image打印光栅位图

  • Hex 1D 76 30 m xL xH yL yH d1...dk六角 1D 76 30 m xL xH yL yH d1...dk
  • 0 ≤ m ≤ 3, 48 ≤ m ≤ 51 0 ≤ m ≤ 3, 48 ≤ m ≤ 51
  • 0 ≤ xL ≤ 255 0 ≤ xL ≤ 255
  • 0 ≤ xH ≤ 255 0 ≤ xH ≤ 255
  • 0 ≤ yL ≤ 255 0 ≤ yL ≤ 255
  • 0 ≤ d ≤255 0≤d≤255
  • k = ( xL + xH × 256) × ( yL + yH × 256) ( k ≠ 0) k = ( xL + xH × 256) × ( yL + yH × 256) ( k ≠ 0)
  • xL, xH, select the number of data bits ( xL+ xH×256) in the horizontal direction for the bit image xL,xH,选择位图在水平方向的数据位数(xL+xH×256)
  • yL, yH, select the number of data bits ( yL+ yH×256) in the vertical direction for the bit image yL,yH,选择位图垂直方向的数据位数(yL+yH×256)
  • If the printing area width set by GS L and GS W is less than the minimum width, the printing area is extended to the minimum width only on the line in question.如果 GS L 和 GS W 设置的打印区域宽度小于最小宽度,则打印区域仅在有问题的行上扩展到最小宽度。 The minimum width means 1 dot in normal (m=0, 48) and double-height (m=2, 50), 2 dots in double-width (m=1, 49) and quadruple (m=3, 51) modes.最小宽度表示正常(m=0, 48)和双倍高度(m=2, 50)下的1个点,双倍(m=1, 49)和四倍(m=3, 51)模式下的2个点.
  • Data outside the printing area is read in and discarded on a dot-by-dot basis打印区域外的数据逐点读入和丢弃
  • The position at which subsequent characters are to be printed for raster bit image is specified by HT (Horizontal Tab), ESC $ (Set absolute print position), ESC \\ ( Set relative print position), and GS L (Set left margin ).光栅位图的后续字符打印位置由 HT(水平制表符)、ESC $(设置绝对打印位置)、ESC \\(设置相对打印位置)和 GS L(设置左边距)指定。 If the position at which subsequent characters are to be printed is not a multiple of 8, print speed may decline如果后续字符要打印的位置不是 8 的倍数,打印速度可能会下降
  • The ESC a (Select justification) setting is also effective on raster bit images ESC a(选择对齐)设置对光栅位图也有效
  • When this command is received during macro definition, the printer ends macro definition, and begins performing this command.当在宏定义过程中接收到该命令时,打印机结束宏定义,并开始执行该命令。 The definition of this command should be cleared这个命令的定义应该被清除
  • d indicates the bit-image data. d 表示位图数据。 Set time a bit to 1 prints a dot and setting it to 0 does not print a dot.将 time 设置为 1 会打印一个点,将其设置为 0 不会打印一个点。

I know that the bitmap need to be converted, but I can't get the correct format.我知道需要转换位图,但我无法获得正确的格式。

Someone can help me!!!有人可以帮助我!!!

Thanks in advance提前致谢

I struggled with this a little while ago and eventually got it working.不久前我为此苦苦挣扎,最终让它发挥作用。 My thermal printer is not the same one that you are using but the principle should remain the same.我的热敏打印机与您使用的打印机不同,但原理应该保持不变。 So my printer works over USB and here is what I send to the printer for the image to be printed.所以我的打印机通过 USB 工作,这是我发送到打印机以打印图像的内容。

byte[] rasterImage = {0x1D, 0x76, 0x30, 0x30, 0x20, 0x00, 0x00, 0x01};

File file = new File("/mnt/sdcard/1.bmp");
int size = (int) file.length();
bitmapdata = new byte[size];
try
{
    BufferedInputStream buf = new BufferedInputStream(new FileInputStream(file));
    buf.read(bitmapdata, 0, bitmapdata.length);
    buf.close();
} catch (FileNotFoundException e)
{

} catch (IOException e)
{

}

connection.bulkTransfer(endpoint, rasterImage, rasterImage.length, TIMEOUT);
connection.bulkTransfer(endpoint, bitmapdata, bitmapdata.length, TIMEOUT);

The rasterImage is basically the setup of the hex data needed to tell the printer than I want to print a raster bit image. rasterImage 基本上是告诉打印机我想要打印光栅位图所需的十六进制数据的设置。 Then I read the photo or image from the internal storage of the device and convert it into a byte[].然后我从设备的内部存储中读取照片或图像并将其转换为字节 []。 Then I send the rasterImage hex data and the bitmapdata to the printer.然后我将 rasterImage 十六进制数据和位图数据发送到打印机。

A few things to note, firstly you need to convert the image you want to print to a 1 bit depth monochrome image, you can use paint to do this (simply save the image from paint as a monochrome bitmap), you need to do this because the printer can only print a dot or no dot, it cannot print layered images etc. Second, you need to make sure that the image resolution is a multiple of 8, the reason for this is due to the xL, xH, yL and yH calculation on my printer, the one works only in bytes and so I have no way to get anything other than a multiple of 8. So my image was 256x256.需要注意的几点,首先你需要将你要打印的图像转换为1位深度的单色图像,你可以使用paint来做到这一点(只需将paint中的图像保存为单色位图),你需要这样做因为打印机只能打印一个点或不打印点,不能打印分层图像等。 其次,您需要确保图像分辨率是8的倍数,这是由于xL、xH、yL和在我的打印机上进行 yH 计算,只能以字节为单位进行计算,因此除了 8 的倍数之外,我无法获得任何其他信息。所以我的图像是 256x256。

Let me know if you have success and if you need more help let me know.如果您成功了,请告诉我,如果您需要更多帮助,请告诉我。

I did not include any USB code, but just use your bluetooth connection to send the data in a similar way.我没有包含任何 USB 代码,只是使用您的蓝牙连接以类似的方式发送数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM