Изображение принтера с ESC / POS в Android Java с BlueTooth - PullRequest
0 голосов
/ 04 января 2019

Мне нужно напечатать изображение BitMap, в BlueTooth Thermal 58mm принтер через Android, я уже смог напечатать, но код, который я получаю, прыгает на одну строку после каждого напечатанного блока байтов, поэтому, когда я заканчиваю изображение, оно получает несколько пустых строк, обрезая изображение.

Мне удалось распечатать больше пропусков строки, я не нашел решения.

 public void printImage(Bitmap bitmap) {

    int width = bitmap.getWidth();
    int height = bitmap.getHeight();

    final byte[] controlByte = {(byte) (0x00ff & width), (byte) ((0xff00 & width) >> 8)};
    int[] pixels = new int[width * height];

    bitmap.getPixels(pixels, 0, width, 0, 0, width, height);

    final int BAND_HEIGHT = 24;

    // Bands of pixels are sent that are 8 pixels high.  Iterate through bitmap
    // 24 rows of pixels at a time, capturing bytes representing vertical slices 1 pixel wide.
    // Each bit indicates if the pixel at that position in the slice should be dark or not.
    for (int row = 0; row < height; row += BAND_HEIGHT) {

        ByteArrayOutputStream imageData = getOutputStream();

        // Need to send these two sets of bytes at the beginning of each row.
        writeToPrinterBuffer(imageData, PRINTER_SELECT_BIT_IMAGE_MODE);
        writeToPrinterBuffer(imageData, controlByte); //Gerado Acima.

        // Columns, unlike rows, are one at a time.
        for (int col = 0; col < width; col++) {

            byte[] bandBytes = {0x0, 0x0, 0x0};

            // Ugh, the nesting of forloops.  For each starting row/col position, evaluate
            // each pixel in a column, or "band", 24 pixels high.  Convert into 3 bytes.
            for (int rowOffset = 0; rowOffset < 8; rowOffset++) {

                // Because the printer only maintains correct height/width ratio
                // at the highest density, where it takes 24 bit-deep slices, process
                // a 24-bit-deep slice as 3 bytes.
                int[] pixelSlice = new int[3];
                int pixel2Row = row + rowOffset + 8;
                int pixel3Row = row + rowOffset + 16;

                // If we go past the bottom of the image, just send white pixels so the printer
                // doesn't do anything.  Everything still needs to be sent in sets of 3 rows.
                pixelSlice[0] = bitmap.getPixel(col, row + rowOffset);
                pixelSlice[1] = (pixel2Row >= bitmap.getHeight()) ?
                        Color.WHITE : bitmap.getPixel(col, pixel2Row);
                pixelSlice[2] = (pixel3Row >= bitmap.getHeight()) ?
                        Color.WHITE : bitmap.getPixel(col, pixel3Row);

                boolean[] isDark = {pixelSlice[0] == Color.BLACK,
                        pixelSlice[1] == Color.BLACK,
                        pixelSlice[2] == Color.BLACK};

                // Towing that fine line between "should I forloop or not".  This will only
                // ever be 3 elements deep.
                if (isDark[0]) bandBytes[0] |= 1 << (7 - rowOffset);
                if (isDark[1]) bandBytes[1] |= 1 << (7 - rowOffset);
                if (isDark[2]) bandBytes[2] |= 1 << (7 - rowOffset);
            }
            writeToPrinterBuffer(imageData, bandBytes); //imprime dados
        }
        addLineFeed(imageData, 0); //pula linha
        //addCarrierReturn(imageData);
        print(imageData);
    }
}

private void addLineFeed(ByteArrayOutputStream printerBuffer, int numLines) {
    try {
        if (numLines <= 1) {
            printerBuffer.write(BYTE_LF);
        } else {
            printerBuffer.write(PRINTER_PRINT_AND_FEED);
            printerBuffer.write(numLines);
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}

private void writeToPrinterBuffer(ByteArrayOutputStream printerBuffer, byte[] command) {
    try {
        printerBuffer.write(command);
    } catch (IOException e) {
        Log.d(TAG, "IO Exception while writing printer data to buffer.", e);
    }
}

private void print(ByteArrayOutputStream output) {
    try {
        btOutputStream.write(output.toByteArray());
    } catch (IOException e) {
        Log.d(TAG, "IO Exception while printing.", e);
    }
}

Он печатает, перебрасывая строку в каждый набор из 24 пикселей (что дает 3 байта из 8), в конце он имеет команду пропустить строку, без печати печати, уже пытался заменить на несколько других и сделал не работает.

Пример изображения (QR-код)

...