我怎样才能在Android的蓝牙打印机打印图像? [英] How can I print an image on a Bluetooth printer in Android?

查看:180
本文介绍了我怎样才能在Android的蓝牙打印机打印图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要打印热敏蓝牙打印机的一些数据,我做这个:

I have to print some data on thermal bluetooth printer, I'm doing with this:

String message="abcdef any message 12345";
byte[] send;
send = message.getBytes();
mService.write(send);

有可以很好地用于文本,但不适合的图像。我想我需要获得的图像数据的字节[] 。我试图获取图像的数据是这样的:

It works well for text, but not for images. I think I need to get the byte[] of the image data. I tried getting the data of the image this way:

Bitmap bitmap=BitmapFactory.decodeResource(getResources(), R.drawable.qrcode);
ByteArrayOutputStream stream=new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 90, stream);
byte[] image=stream.toByteArray();

不幸的是,打印机打印了很多奇怪的字符(约50厘米纸)。我不知道如何打印图像。

Unfortunately the printer prints a lot of strange characters (approx. 50 cm of paper). I don't know how to print the image.

我想尝试让位图的像素和明年将其转换为字节[] 并发送它,但我不知道该怎么办呢。

I would like to try getting the pixels of the bitmap and next converting it to a byte[] and sending it, but i don't know how to do it.

感谢

更新:

在那么多的时间,我这样做:我有一个名为print_image(字符串的文件)的方法,在其中获得的图像,我想打印的路径:

After so much time, i'm doing this: I have a method called print_image(String file), the which gets the path of the image that i want to print:

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 < 3; ++k) {

                    byte slice = 0;
                    for (int b = 0; b < 8; ++b) {
                        int y = (((offset / 8) + k) * 8) + b;
                        int i = (y * bmp.getWidth()) + x;
                        boolean v = false;
                        if (i < dots.length()) {
                            v = dots.get(i);
                        }
                        slice |= (byte) ((v ? 1 : 0) << (7 - b));
                    }
                    mService.write(slice);
                }
            }
            offset += 24;
            mService.write(PrinterCommands.FEED_LINE);
            mService.write(PrinterCommands.FEED_LINE);          
            mService.write(PrinterCommands.FEED_LINE);
            mService.write(PrinterCommands.FEED_LINE);
            mService.write(PrinterCommands.FEED_LINE);
            mService.write(PrinterCommands.FEED_LINE);
        }
        mService.write(PrinterCommands.SET_LINE_SPACING_30);


    } else {
        Toast.makeText(this, "file doesn't exists", Toast.LENGTH_SHORT)
                .show();
    }
}

我当时就在此基础上<一个href="http://android-essential-devtopics.blogspot.com/2013/02/sending-bit-image-to-epson-printer.html">post

这是类PrinterCommands:

This is the class PrinterCommands:

public class PrinterCommands {
public static final byte[] INIT = {27, 64};
public static byte[] FEED_LINE = {10};

public static byte[] SELECT_FONT_A = {27, 33, 0};

public static byte[] SET_BAR_CODE_HEIGHT = {29, 104, 100};
public static byte[] PRINT_BAR_CODE_1 = {29, 107, 2};
public static byte[] SEND_NULL_BYTE = {0x00};

public static byte[] SELECT_PRINT_SHEET = {0x1B, 0x63, 0x30, 0x02};
public static byte[] FEED_PAPER_AND_CUT = {0x1D, 0x56, 66, 0x00};

public static byte[] SELECT_CYRILLIC_CHARACTER_CODE_TABLE = {0x1B, 0x74, 0x11};

public static byte[] SELECT_BIT_IMAGE_MODE = {0x1B, 0x2A, 33, -128, 0};
public static byte[] SET_LINE_SPACING_24 = {0x1B, 0x33, 24};
public static byte[] SET_LINE_SPACING_30 = {0x1B, 0x33, 30};

public static byte[] TRANSMIT_DLE_PRINTER_STATUS = {0x10, 0x04, 0x01};
public static byte[] TRANSMIT_DLE_OFFLINE_PRINTER_STATUS = {0x10, 0x04, 0x02};
public static byte[] TRANSMIT_DLE_ERROR_STATUS = {0x10, 0x04, 0x03};
public static byte[] TRANSMIT_DLE_ROLL_PAPER_SENSOR_STATUS = {0x10, 0x04, 0x04};
}

正如看到的print_image方法我打电话的方法,叫做convertBitmap,和IM发送的位图,这是code:

As is seen in the print_image method I'm calling a method, called convertBitmap, and im sending a bitmap, this is the code:

   public String convertBitmap(Bitmap inputBitmap) {

    mWidth = inputBitmap.getWidth();
    mHeight = inputBitmap.getHeight();

    convertArgbToGrayscale(inputBitmap, mWidth, mHeight);
    mStatus = "ok";
    return mStatus;

}

private void convertArgbToGrayscale(Bitmap bmpOriginal, int width,
        int height) {
    int pixel;
    int k = 0;
    int B = 0, G = 0, R = 0;
    dots = new BitSet();
    try {

        for (int x = 0; x < height; x++) {
            for (int y = 0; y < width; y++) {
                // get one pixel color
                pixel = bmpOriginal.getPixel(y, x);

                // retrieve color of all channels
                R = Color.red(pixel);
                G = Color.green(pixel);
                B = Color.blue(pixel);
                // take conversion up to one single value by calculating
                // pixel intensity.
                R = G = B = (int) (0.299 * R + 0.587 * G + 0.114 * B);
                // set bit into bitset, by calculating the pixel's luma
                if (R < 55) {                       
                    dots.set(k);//this is the bitset that i'm printing
                }
                k++;

            }


        }


    } catch (Exception e) {
        // TODO: handle exception
        Log.e(TAG, e.toString());
    }
}

这是我使用的打印机,分辨率:8点/毫米,576点/行

This is the printer that i'm using, resolution: 8 dots/mm, 576 dots/line

这是我喜欢做的事(我做到了与同一台打印机,但是从游戏商店下载的应用程序)

And this is what I like to do (i did it with the same printer, but with an app downloaded from play store)

这就是我得到现在

This is what i'm getting now

更紧密:

Closer:

Closer2:

Closer2:

图像的一小部分就可以看出,所以我认为我更接近可打印图像...

A little part of the image can be seen, so I think that i'm closer to can print the image...

这是我使用的形象是这样的(576x95):

The image that i'm using is this (576x95):

这是转换后的图像(我将它与上code):

And this is the converted image (i'm converting it with the upper code):

那么,答案是:我在做什么错了?我认为错误在此命令:

So, the answer is: what I'm doing wrong?, I think that the error is in this command:

  public static byte[] SELECT_BIT_IMAGE_MODE = {0x1B, 0x2A, 33, -128, 0};

但是,我怎么能计算出正确的值,我的形象?谢谢

But, how can I calculate the correct values for my image?, thanks

推荐答案

解决!我是做了错误的打印机初始化...的corect方法是:

Solved!, I was doing a wrong printer initializing... The corect way is:

 public static byte[] SELECT_BIT_IMAGE_MODE = {0x1B, 0x2A, 33, 255, 3};

因此​​,通过这种方式,图像被打印完全细

So, by this way the image is printed completely fine

这篇关于我怎样才能在Android的蓝牙打印机打印图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆