Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/316.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何使用蓝牙打印机设备从android打印图片_Java_Android_Printing_Bitmap_Bluetooth - Fatal编程技术网

Java 如何使用蓝牙打印机设备从android打印图片

Java 如何使用蓝牙打印机设备从android打印图片,java,android,printing,bitmap,bluetooth,Java,Android,Printing,Bitmap,Bluetooth,我正在开发android蓝牙设备,我想创建一个应用程序,通过蓝牙打印机打印来自android手机的内容。我已经做了编码和测试的文本打印,但我找不到正确的方法来打印图片,例如美国标志。。。。我所做的是将位图转换为ByteArrayOutputStream,但我不知道如何完成。。。 我使用的是android SDK: 将位图转换为ByteArrayOutputStream的代码如下: private int mWidth;// private int mHeight;// private S

我正在开发android蓝牙设备,我想创建一个应用程序,通过蓝牙打印机打印来自android手机的内容。我已经做了编码和测试的文本打印,但我找不到正确的方法来打印图片,例如美国标志。。。。我所做的是将位图转换为ByteArrayOutputStream,但我不知道如何完成。。。 我使用的是android SDK:

将位图转换为ByteArrayOutputStream的代码如下:

private int mWidth;//  
private int mHeight;//  
private String mStatus;//
private BitSet dots;//
private ByteArrayOutputStream mService;//

public void print_image()
    {
        MainActivity m = new MainActivity();

    Bitmap bmp=BitmapFactory.decodeResource(m.getResources(), R.drawable.ic_launcher);
    //ByteArrayOutputStream stream=new ByteArrayOutputStream();

    //bitmap.compress(Bitmap.CompressFormat.PNG, 90, stream);
    //byte[] image=stream.toByteArray();


    //Bitmap bmp = BitmapFactory.decodeFile(file);
    convertBitmap(bmp);

    try {
        mService.write(PrinterCommands.SET_LINE_SPACING_24);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    int offset = 0;
    while (offset < bmp.getHeight()) {
        try {
            mService.write(PrinterCommands.SELECT_BIT_IMAGE_MODE);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        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;
        try {
            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);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

}

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();


        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++;

            }


        }



}
让我们假设上面的代码工作成功,那么我们将如何完成呢

我在谷歌上搜索了很多,但在互联网上没有任何解决方案。。。谁能帮帮我吗

提前谢谢,如果我的英语不好,我很抱歉

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[] SELECT_BIT_IMAGE_MODE = {0x1B, 0x2A, 33, (byte) 255, 3};
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};
}