美文网首页
iOS蓝牙小票打印机 打印图片

iOS蓝牙小票打印机 打印图片

作者: 漫步的老狼 | 来源:发表于2021-06-08 18:10 被阅读0次

直接上代码,亲测可用!(也是网上找的,有的找的是乱码,这个可用)
ps:只能打印黑白图片,图片一定要是白色背景


-(void)printerImage:(UIImage *)image{
    NSData *imageData = [self imageToThermalData:image];
     [self printLongData:imageData];
}
- (void) printLongData:(NSData *)printContent{
    
    NSUInteger i;
    // 打印数据长度
    NSUInteger strLength;
    //打印行数
    NSUInteger cellCount;
    NSUInteger cellMin;
    NSUInteger cellLen;
    
    //数据长度
    strLength = [printContent length];
    if (strLength < 1) {
        return;
    }
    //MAX_CHARACTERISTIC_VALUE_SIZE = 120
    cellCount = (strLength % MAX_CHARACTERISTIC_VALUE_SIZE) ? (strLength/MAX_CHARACTERISTIC_VALUE_SIZE + 1):(strLength/MAX_CHARACTERISTIC_VALUE_SIZE);
    for (i=0; i<cellCount; i++) {
        cellMin = i*MAX_CHARACTERISTIC_VALUE_SIZE;
        if (cellMin + MAX_CHARACTERISTIC_VALUE_SIZE > strLength) {
            cellLen = strLength-cellMin;
        }
        else {
            cellLen = MAX_CHARACTERISTIC_VALUE_SIZE;
        }
        
        NSRange rang = NSMakeRange(cellMin, cellLen);
        //        截取打印数据
        NSData *subData = [printContent subdataWithRange:rang];
        //循环写入数据
        [self.peripheral writeValue:subData forCharacteristic:self.writeCharacteristic type:CBCharacteristicWriteWithResponse];
    }
}

// 参考 http://developer.apple.com/library/mac/#qa/qa1509/_index.html
-(CGContextRef)CreateARGBBitmapContextWithCGImageRef:(CGImageRef)inImage
{
    CGContextRef    context = NULL;
    CGColorSpaceRef colorSpace;
    void *          bitmapData;
    int             bitmapByteCount;
    int             bitmapBytesPerRow;
    
    // Get image width, height. We'll use the entire image.
    size_t pixelsWide = CGImageGetWidth(inImage);
    size_t pixelsHigh = CGImageGetHeight(inImage);
    
    // Declare the number of bytes per row. Each pixel in the bitmap in this
    // example is represented by 4 bytes; 8 bits each of red, green, blue, and
    // alpha.
    bitmapBytesPerRow   = (int)(pixelsWide * 4);
    bitmapByteCount     = (int)(bitmapBytesPerRow * pixelsHigh);
    
    // Use the generic RGB color space.
    colorSpace =CGColorSpaceCreateDeviceRGB();
    if (colorSpace == NULL)
    {
        return NULL;
    }
    
    // Allocate memory for image data. This is the destination in memory
    // where any drawing to the bitmap context will be rendered.
    bitmapData = malloc( bitmapByteCount );
    if (bitmapData == NULL)
    {
        CGColorSpaceRelease( colorSpace );
        return NULL;
    }
    
    // Create the bitmap context. We want pre-multiplied ARGB, 8-bits
    // per component. Regardless of what the source image format is
    // (CMYK, Grayscale, and so on) it will be converted over to the format
    // specified here by CGBitmapContextCreate.
    context = CGBitmapContextCreate (bitmapData,
                                     pixelsWide,
                                     pixelsHigh,
                                     8,      // bits per component
                                     bitmapBytesPerRow,
                                     colorSpace,
                                     kCGImageAlphaPremultipliedFirst);
    if (context == NULL)
    {
        free (bitmapData);
    }
    
    // Make sure and release colorspace before returning
    CGColorSpaceRelease( colorSpace );
    
    return context;
}









typedef enum {
    ALPHA = 0,
    BLUE = 1,
    GREEN = 2,
    RED = 3
} PIXELS;


- (NSData *) imageToThermalData:(UIImage*)image
{
    
    CGImageRef imageRef = image.CGImage;
     // Create a bitmap context to draw the uiimage into
     CGContextRef context = [self CreateARGBBitmapContextWithCGImageRef:imageRef];
     if(!context) {
         return NULL;
     }
     
     size_t width = CGImageGetWidth(imageRef);
     size_t height = CGImageGetHeight(imageRef);
     
     CGRect rect = CGRectMake(0, 0, width, height);
     
     // Draw image into the context to get the raw image data
     CGContextDrawImage(context, rect, imageRef);
     
     // Get a pointer to the data
     uint32_t *bitmapData = (uint32_t *)CGBitmapContextGetData(context);
     
     if(bitmapData) {
         
         uint8_t *m_imageData = (uint8_t *) malloc(width * height/8 + 8*height/8);
         memset(m_imageData, 0, width * height/8 + 8*height/8);
         int result_index = 0;
         
         for(int y = 0; (y + 24) < height;) {
             m_imageData[result_index++] = 27;
             m_imageData[result_index++] = 51;
             m_imageData[result_index++] = 0;
             
             m_imageData[result_index++] = 27;
             m_imageData[result_index++] = 42;
             m_imageData[result_index++] = 33;
             
             m_imageData[result_index++] = width%256;
             m_imageData[result_index++] = width/256;
             for(int x = 0; x < width; x++) {
                 int value = 0;
                 for (int temp_y = 0 ; temp_y < 8; ++temp_y)
                 {
                     uint8_t *rgbaPixel = (uint8_t *) &bitmapData[(y+temp_y) * width + x];
                     uint32_t gray = 0.3 * rgbaPixel[RED] + 0.59 * rgbaPixel[GREEN] + 0.11 * rgbaPixel[BLUE];
                     
                     if (gray < 127)
                     {
                         value += 1<<(7-temp_y)&255;
                     }
                 }
                 m_imageData[result_index++] = value;
                 
                 value = 0;
                 for (int temp_y = 8 ; temp_y < 16; ++temp_y)
                 {
                     uint8_t *rgbaPixel = (uint8_t *) &bitmapData[(y+temp_y) * width + x];
                     uint32_t gray = 0.3 * rgbaPixel[RED] + 0.59 * rgbaPixel[GREEN] + 0.11 * rgbaPixel[BLUE];
                     
                     if (gray < 127)
                     {
                         value += 1<<(7-temp_y%8)&255;
                     }
                     
                 }
                 m_imageData[result_index++] = value;
                 
                 value = 0;
                 for (int temp_y = 16 ; temp_y < 24; ++temp_y)
                 {
                     uint8_t *rgbaPixel = (uint8_t *) &bitmapData[(y+temp_y) * width + x];
                     uint32_t gray = 0.3 * rgbaPixel[RED] + 0.59 * rgbaPixel[GREEN] + 0.11 * rgbaPixel[BLUE];
                     
                     if (gray < 127)
                     {
                         value += 1<<(7-temp_y%8)&255;
                     }
                     
                 }
                 m_imageData[result_index++] = value;
             }
             m_imageData[result_index++] = 13;
             m_imageData[result_index++] = 10;
             y += 24;
         }
         NSMutableData *data = [[NSMutableData alloc] initWithCapacity:0];
         [data appendBytes:m_imageData length:result_index];
         free(bitmapData);
         return data;
         
     } else {
         NSLog(@"Error getting bitmap pixel data\n");
     }
     
     CGContextRelease(context);
     
     return nil ;
}

相关文章

  • 打印机

    iOS开发之蓝牙/Socket链接小票打印机(一)iOS开发之蓝牙/Socket链接小票打印机(二) iOS so...

  • iOS开发 蓝牙打印小票

    要求:手机通过蓝牙连接蓝牙打印机,在手机上点击‘打印’,打印机就打印出小票(小票就跟送外卖的那种)。 设备:BT5...

  • iOS蓝牙小票打印机 打印图片

    直接上代码,亲测可用!(也是网上找的,有的找的是乱码,这个可用)ps:只能打印黑白图片,图片一定要是白色背景

  • iOS蓝牙4.0打印小票功能的实现

    公司业务有涉及到订单模块,客户需要连接蓝牙打印机打印订单小票。所以本文就记录一下iOS蓝牙打印的相关知识以及实际开...

  • iOS CoreBluetooth 的使用讲解

    最近研究了iOS下连接蓝牙打印机,实现打印购物小票的功能,对iOS中BLE 4.0的使用有了一定的了解,这里记录一...

  • iOS Bluetooth 打印小票(一)

    在iOS app中连接蓝牙打印机打印商品小票,在没有电脑只有手机的情况下,感觉非常实用,而且最近经常最近公司正好也...

  • iOS 蓝牙打印小票命令汇总

    在SaaS App中有很多地方需要通过连接蓝牙打印机打印结算及订单小票,而且在结算的时候使用打印机的的频率比较高,...

  • swift 连接 BLE 蓝牙打印机

    swift 蓝牙连接 项目简介 最近公司要用到便携式蓝牙打印机进行打印打印机使用的ECS/POS指令集ios使用的...

  • uniapp实现蓝牙小票打印功能

    最近的一个项目增加了小票蓝牙打印的功能,由于之前对蓝牙打印机了解不多,所以遇到的坑比较多,花了点时间把蓝牙连接、打...

  • iOS 蓝牙打印小票

    前言: 最近做了款蓝牙打印的功能,包含蓝牙自动连接,蓝牙搜索,连接之后进行打印。总结了下知识点,写了一个简单的De...

网友评论

      本文标题:iOS蓝牙小票打印机 打印图片

      本文链接:https://www.haomeiwen.com/subject/rawqeltx.html