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

直接上代码,亲测可用!(也是网上找的,有的找的是乱码,这个可用)
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 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蓝牙小票打印机 打印图片)