我必须在热蓝牙打印机上打印一些数据,我正在这样做:
String message="abcdef any message 12345";
byte[] send;
send = message.getBytes();
mService.write(send);
Run Code Online (Sandbox Code Playgroud)
它适用于文本,但不适用于图像.我想我需要获取byte[]
图像数据.我尝试以这种方式获取图像的数据:
Bitmap bitmap=BitmapFactory.decodeResource(getResources(), R.drawable.qrcode);
ByteArrayOutputStream stream=new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 90, stream);
byte[] image=stream.toByteArray();
Run Code Online (Sandbox Code Playgroud)
不幸的是,打印机会打印出许多奇怪的字符(大约50厘米的纸张).我不知道如何打印图像.
我想尝试获取位图的像素,然后将其转换为a byte[]
并发送它,但我不知道该怎么做.
谢谢
更新:
经过这么多时间,我这样做:我有一个名为print_image(String file)的方法,它获取我想要打印的图像的路径:
private void print_image(String file) {
File fl = new File(file);
if (fl.exists()) {
Bitmap bmp = BitmapFactory.decodeFile(file);
convertBitmap(bmp);
mService.write(PrinterCommands.SET_LINE_SPACING_24);
int offset = 0;
while (offset < bmp.getHeight()) {
mService.write(PrinterCommands.SELECT_BIT_IMAGE_MODE);
for (int x = 0; x < bmp.getWidth(); ++x) {
for (int k = 0; k < …
Run Code Online (Sandbox Code Playgroud)