我需要帮助制作适用于Mac的QR码生成器

时间:2014-06-11 19:10:37

标签: xcode macos core-graphics qr-code

我遇到了一个问题,我正在尝试为OS X制作QR生成器函数包含URL或文本的字符串,两个NSColor用于背景颜色,一个NSInterger用于QuietZone和Foreground,两个NSInterger用于宽度和另一个是高度。

另外我正在使用libqrencode框架Official Site

继承了我所做的功能。

+ (NSImage*)QRCodeGenerator:(NSString*)iData andBackgroundColor:(NSColor*)iBackgroundColor andForgroundColor:(NSColor*)iForgroundColour andQuietZone:(NSInteger)iQuietZone andImageWidth:(NSInteger)iwidth andImageHeight:(NSInteger)iheight;

我已经尝试了iPhone教程,并将其改编为适用于Mac的但是失败了,但是我已经修改了很多次代码块,试图让它运行起来。

+ (NSImage*)QRCodeGenerator:(NSString*)iData andBackgroundColor:(NSColor*)iBackgroundColor andForgroundColor:(NSColor*)iForgroundColour andQuietZone:(NSInteger)iQuietZone andImageWidth:(NSInteger)iwidth andImageHeight:(NSInteger)iheight{

NSImage* image = nil;
QRcode *qr = QRcode_encodeString([iData UTF8String], 0, QR_ECLEVEL_L, QR_MODE_8, 1);

int logQRSize = qr->width;
long phyQRSize = logQRSize + (2 * iQuietZone);
long xscale = iwidth / phyQRSize;
long yscale = iheight / phyQRSize;
long widthImagSize = phyQRSize * xscale;
long heightImagSize = phyQRSize * yscale;

if(xscale < 1){
    xscale = 1;
}
if(yscale < 1){
    yscale = 1;
}

    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    CGContextRef ctx = CGBitmapContextCreate(0, xscale, yscale, 8, xscale * 4, colorSpace, kCGBitmapByteOrderDefault);

    CGAffineTransform translateTransform = CGAffineTransformMakeTranslation(0, -xscale);
    CGAffineTransform scaleTransform = CGAffineTransformMakeScale(1, -1);
    CGContextConcatCTM(ctx, CGAffineTransformConcat(translateTransform, scaleTransform));

    //init all pixels to 'Background color'
    CGRect bounds = CGRectMake(0, 0,  widthImagSize, heightImagSize);
    CGContextSetFillColorWithColor(ctx,  iBackgroundColor.CGColor);
    CGContextFillRect(ctx, bounds);

    //set any 'Forground color' pixels
    {
        int x,y;

        CGContextSetFillColorWithColor(ctx, iForgroundColour.CGColor);

        for (y = 0; y < logQRSize; y++){
            for (x = 0; x < logQRSize; x++){
                if(qr->data[(y*logQRSize)+x] & 1){
                    CGContextFillRect(ctx, CGRectMake((iQuietZone + x)*xscale, (iQuietZone + y)*yscale, xscale, yscale));
                }
            }
        }

    }

    CGImageRef qrCGImage = CGBitmapContextCreateImage(ctx);

    // convert CGImage to NSImage
    NSBitmapImageRep* rep;
    rep = [[NSBitmapImageRep alloc] initWithCGImage:qrCGImage];
    NSImage* qrImage = [[NSImage alloc] initWithSize:NSMakeSize(xscale*4, yscale*4)];
    [qrImage addRepresentation:rep];

    // free memory
    CGContextRelease(ctx);
    CGImageRelease(qrCGImage);
    CGColorSpaceRelease(colorSpace);


QRcode_free(qr);

return qrImage; }

教程网站Official Tutorial Site中的原始版本仅以图片形式显示。

enter image description here

我希望我生成的QR代码看起来类似于教程One但在Mac上

Finished QR Code

编辑:下面的代码是教程项目的原始修改版本。

+ (NSImage*)QRCodeGenerator:(NSString*)iData andBackgroundColor:(NSColor*)iBackgroundColor andForgroundColor:(NSColor*)iForgroundColour andQuietZone:(NSInteger)iQuietZone andImageWidth:(NSInteger)iwidth andImageHeight:(NSInteger)iheight{

NSImage* image = nil;
QRcode *qr = QRcode_encodeString([iData UTF8String], 0, QR_ECLEVEL_L, QR_MODE_8, 1);

int logQRSize = qr->width;
long phyQRSize = logQRSize + (2 * iQuietZone);
long xscale = iwidth / phyQRSize;
long yscale = iheight / phyQRSize;
long widthImagSize = phyQRSize * xscale;
long heightImagSize = phyQRSize * yscale;

if(xscale < 1){
    xscale = 1;
}
if(yscale < 1){
    yscale = 1;
}

{

  image = [[NSImage alloc] initWithSize:NSMakeSize(widthImagSize, heightImagSize)];
    [image lockFocus];
    /* drawing code here */
    CGContextRef ctx = (CGContextRef)[[NSGraphicsContext currentContext] graphicsPort];

    //init all pixels to 'Background color'
    CGRect bounds = CGRectMake(0, 0,  widthImagSize, heightImagSize);
    CGContextSetFillColorWithColor(ctx,  iBackgroundColor.CGColor);
    CGContextFillRect(ctx, bounds);

    //set any 'Forground color' pixels
    {
        int x,y;

        CGContextSetFillColorWithColor(ctx, iForgroundColour.CGColor);

        for (y = 0; y < logQRSize; y++){
            for (x = 0; x < logQRSize; x++){
                if(qr->data[(y*logQRSize)+x] & 1){
                    CGContextFillRect(ctx, CGRectMake((iQuietZone + x)*xscale, (iQuietZone + y)*yscale, xscale, yscale));
                }
            }
        }

    }

    //Generate the NSImage
    CGImageRef imgRef = CGBitmapContextCreateImage(ctx);
    image = [[NSImage alloc] initWithCGImage:imgRef size:NSZeroSize];
    CGImageRelease(imgRef);

    [image unlockFocus];

    //[image drawAtPoint:NSMakePoint(0.0, 0.0) fromRect: NSMakeRect(0.0, 0.0, 100.0, 100.0) operation: NSCompositeSourceOver fraction: 1.0];

}

QRcode_free(qr);

return image;
}

请注意,此原始修改代码发送了错误消息

Cannot lock focus on image <NSImage 0x6000002614c0 Size={0, 0} Reps=()>, because it is size zero.

0 个答案:

没有答案