简体   繁体   English

JAVA ESC Pos将NV图形上传到打印机

[英]JAVA ESC Pos upload NV graphic to printer

Using Esc/Pos, I want to upload a bitmap image to the NV graphics memory on a printer. 使用Esc / Pos,我想将位图图像上传到打印机上的NV图形内存。

I'm using GS ( L / GS 8 L <Function 67> of the Esc/Pos manual. 我正在使用GS ( L / GS 8 L Esc / Pos手册的GS ( L / GS 8 L <Function 67>

I'm able to use <Function 65> and <Function 66> to delete all or one of the graphics. 我可以使用<Function 65><Function 66>删除所有或其中一个图形。

I know I'm missing something when adding the Bitmap to the function. 我知道在将Bitmap添加到函数时我遗漏了一些东西。

Heres is my command string including the Bitmap. Heres是我的命令字符串,包括Bitmap。 The bitmapString has the file header and info header of the Bitmap removed (first 62 bytes)(DataOffset).: bitmapString删除了Bitmap的文件头和info头(前62个字节)(DataOffset):

String bitmapString = new String(bitmapBytes, Charsets.US_ASCII);
bitmapString = bitmapString.substring(DataOffset, bitmapStringSize);
String commandString = "";

int commandLength = (bitmapStringSize.length) + 11;
    pL = commandLength % 256;
    if (commandLength < 256) {
        pH = 0;
    } else {
        pH = (commandLength - pL) / 256;
    }

    xL = bitmapWidth % 256;
    if (bitmapWidth < 256) {
        xH = 0;
    } else {
        xH = (bitmapWidth - (bitmapWidth % 256)) / 256;
    }

    yL = bitmapHeight % 256;
    if (bitmapHeight < 256) {
        yH = 0;
    } else {
        yH = (bitmapHeight - (bitmapHeight % 256)) / 256;
    }

    commandString
            += Utils.H("1B")// 27
            + Utils.H("40") // 64
            + Utils.H("1B") // 27
            + Utils.H("3D") // 61
            + Utils.H("01") // 1

            + Utils.H("1D") // GS = 29
            + Utils.H("28") // ( = 40
            + Utils.H("4C") // L = 76

            + Utils.D(pL)   // pL
            + Utils.D(pH)   // pH

            + Utils.H("30") // m = 48
            + Utils.H("43") // fn = 67
            + Utils.H("30") // a = 48

            + Utils.H(KC1)  // kc1
            + Utils.H(KC2)  // kc2

            + Utils.H("01") // b = 1

            + Utils.D(xL)   // xL
            + Utils.D(xH)   // xH
            + Utils.D(yL)   // yL
            + Utils.D(yH)   // yH

            + Utils.H("31");// c = 49

    commandString += bitmapString;

I'm using ePOS-Print.jar to open and write to the printer: 我正在使用ePOS-Print.jar打开并写入打印机:

EpsonIo epsonio = new EpsonIo();
byte[] commandBytes = commandString.getBytes(Charsets.US_ASCII);

        epsonio.open(DevType.BLUETOOTH, MAC, null, ESCPosService.this);

        while (n > 0) {

            epsonio.write(commandBytes, i, n > bufferSize ? bufferSize : n, SEND_TIMEOUT);

            Thread.sleep(450);

            i += bufferSize;
            n -= bufferSize;
        }

But when I print the graphic it is distorted: 但是当我打印图形时它会被扭曲:

在此输入图像描述

I've solved the issue. 我已经解决了这个问题。

The bitmap bytes being sent over had to be decoded using the method below. 发送的位图字节必须使用以下方法解码。 The method converts the bitmap pixels into monochrome bytes. 该方法将位图像素转换为单色字节。 1's or 0's. 1或0。

Hope this help's someone if the future! 如果未来,希望这有帮助的人!

public static byte[] decodeBitmap(byte[] bitmapBytes) {

    Bitmap bmp = BitmapFactory.decodeByteArray(bitmapBytes, 0, bitmapBytes.length);

    int zeroCount = bmp.getWidth() % 8;
    String zeroStr = "";
    if (zeroCount > 0) {
        for (int i = 0; i < (8 - zeroCount); i++) {
            zeroStr = zeroStr + "0";
        }
    }

    List<String> list = new ArrayList<>();
    for (int i = 0; i < bmp.getHeight(); i++) {
        StringBuilder sb = new StringBuilder();
        for (int j = 0; j < bmp.getWidth(); j++) {
            int color = bmp.getPixel(j, i);

            int r = (color >> 16) & 0xff;
            int g = (color >> 8) & 0xff;
            int b = color & 0xff;

            // if color close to white,bit='0', else bit='1'
            if (r > 160 && g > 160 && b > 160)
                sb.append("0");
            else
                sb.append("1");
        }
        if (zeroCount > 0) {
            sb.append(zeroStr);
        }

        list.add(sb.toString());
    }

    List<String> bmpHexList = binaryListToHexStringList(list);
    List<String> commandList = new ArrayList<>();
    commandList.addAll(bmpHexList);

    return hexListToBytes(commandList);
}

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

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