ESC POS打印图像问题

时间:2018-03-20 11:50:12

标签: objective-c thermal-printer escpos

我正在使用ESC POS打印机。使用下面的代码我可以打印图像,但问题是图像打印不正确。您可以在下图中看到。请查看我的代码,让我知道问题的确切位置。

- (void) btnPrintPicture{

UIImage * img = [UIImage imageNamed:@"download.png"];

int width = img.size.width;
int height = img.size.height;

unsigned char * binaryImageData = malloc(width * height);
unsigned char * data = malloc(height * (8 + width / 8));

unsigned char * grayData = [self convertImageToGray:img];

format_K_threshold(grayData, width, height, binaryImageData);
eachLinePixToCmd(binaryImageData, width, height, 0, data);

NSMutableArray *dataArray = [NSMutableArray new];
int splitBytes = 100;

NSData *comData = [[NSData alloc] initWithBytes:(const void *)data length:(height * (8+width/8))];

for(int i = 0;  i < comData.length ; i=i+splitBytes){

    NSData *subData = nil;

    if((i+splitBytes)>comData.length){
        subData = [comData subdataWithRange:NSMakeRange(i, (comData.length-i))];
    }else{
        subData = [comData subdataWithRange:NSMakeRange(i, splitBytes)];
    }

    [dataArray addObject:subData];
}

[dataArray enumerateObjectsUsingBlock:^(NSData  *obj, NSUInteger idx, BOOL * _Nonnull stop) {
    [self.discoveredPeripheral writeValue:obj forCharacteristic:self.discoveredCharacteristic type:CBCharacteristicWriteWithResponse];
}];

free(grayData);
free(binaryImageData);
free(data);
}

此方法用于将图像转换为灰度。

-(unsigned char *)convertImageToGray:(UIImage *)i
{


   int kRed = 1;
    int kGreen = 2;
    int kBlue = 4;

int colors = kGreen | kBlue | kRed;
int m_width = i.size.width;
int m_height = i.size.height;

uint32_t *rgbImage = (uint32_t *) malloc(m_width * m_height * sizeof(uint32_t));
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef context = CGBitmapContextCreate(rgbImage, m_width, m_height, 8, m_width * 4, colorSpace, kCGBitmapByteOrder32Little | kCGImageAlphaNoneSkipLast);
CGContextSetInterpolationQuality(context, kCGInterpolationHigh);
CGContextSetShouldAntialias(context, NO);
CGContextDrawImage(context, CGRectMake(0, 0, m_width, m_height), [i CGImage]);
CGContextRelease(context);
CGColorSpaceRelease(colorSpace);

// now convert to grayscale
uint8_t *m_imageData = (uint8_t *) malloc(m_width * m_height);
for(int y = 0; y < m_height; y++) {
    for(int x = 0; x < m_width; x++) {
        uint32_t rgbPixel=rgbImage[y*m_width+x];
        uint32_t sum=0,count=0;
        if (colors & kRed) {sum += (rgbPixel>>24)&255; count++;}
        if (colors & kGreen) {sum += (rgbPixel>>16)&255; count++;}
        if (colors & kBlue) {sum += (rgbPixel>>8)&255; count++;}
        m_imageData[y*m_width+x]=sum/count;
    }
}
free(rgbImage);

return m_imageData;}


void format_K_threshold(unsigned char * orgpixels, int xsize, int ysize, unsigned char * despixels) {

int graytotal = 0;
int k = 0;

int i;
int j;
int gray;
for(i = 0; i < ysize; ++i) {
    for(j = 0; j < xsize; ++j) {
        gray = orgpixels[k] & 255;
        graytotal += gray;
        ++k;
    }
}

int grayave = graytotal / ysize / xsize;
k = 0;

for(i = 0; i < ysize; ++i) {
    for(j = 0; j < xsize; ++j) {
        gray = orgpixels[k] & 255;
        if(gray > grayave) {
            despixels[k] = 0;
        } else {
            despixels[k] = 1;
        }

        ++k;
    }
}
}

此方法使用ESC命令打印图像。

void eachLinePixToCmd(unsigned char * src, int nWidth, int nHeight, int nMode, unsigned char * data) {


int p0[] = { 0, 0x80 };
int p1[] = { 0, 0x40 };
int p2[] = { 0, 0x20 };
int p3[] = { 0, 0x10 };
int p4[] = { 0, 0x08 };
int p5[] = { 0, 0x04 };
int p6[] = { 0, 0x02 };

int nBytesPerLine = nWidth / 8;

int offset = 0;
int k = 0;
for (int i = 0; i < nHeight; i++) {
    offset = i * (8 + nBytesPerLine);
    data[offset + 0] = 0x1d;
    data[offset + 1] = 0x76;
    data[offset + 2] = 0x30;
    data[offset + 3] = (unsigned char) (nMode & 0x01);
    data[offset + 4] = (unsigned char) (nBytesPerLine % 0xff);
    data[offset + 5] = (unsigned char) (nBytesPerLine / 0xff);
    data[offset + 6] = 0x01;
    data[offset + 7] = 0x00;
    for (int j = 0; j < nBytesPerLine; j++) {
        data[offset + 8 + j] = (unsigned char) (p0[src[k]] + p1[src[k + 1]] + p2[src[k + 2]] + p3[src[k + 3]] + p4[src[k + 4]] + p5[src[k + 5]] + p6[src[k + 6]] + src[k + 7]);
        k = k + 8;
    }
}
}

enter image description here

提前致谢。

1 个答案:

答案 0 :(得分:1)

基于在a very similar bug的不同项目中修复this change,我猜你的图片宽度不能被8整除。

如果图像宽度不能被8整除,则此行将在每行上删除nWidth % 8像素,从而导致向右倾斜。

int nBytesPerLine = nWidth / 8;

相反,你应该用零填充:

int nBytesPerLine = (nWidth + 7) / 8;

您的data变量也需要增长以匹配,它也有同样的问题。

最后,您要为每一行发出GS v 0命令,这不是很有效。您可以为整个图像发出一次,并指定高度。在同一个项目中,C示例为here