如何在iOS项目中使用C代码(libqrencode)

时间:2011-09-28 18:20:47

标签: iphone objective-c ios static-libraries qr-code

我大约一年前在iOS上开始编写代码,并且已经开始编写Objective C并且设法生成了一些应用程序。我下周参加我的第一个黑客日,并提出了一个涉及二维码的准寻宝理念。我设法找到了ObjC类来处理QR码的解码,但首先没有编码/生成它们。

我找到了git hub上几个客观C框架的链接,但似乎都有突出的问题。然后我在这里找到了这个帖子:

Is there any QR Encoding Library in Cocoa?

这指向了libqrencode一个C'库'的方向,我应该可以在iOS中工作,成为C的超集。

不幸的是没有正式的计算机科学背景我还没有线索从哪里开始在我的项目中可用。任何人都可以解释我需要做的基本步骤,这样我至少可以聪明地使用谷歌。

到目前为止,我已经阅读了有关设置搜索路径和创建静态库的信息,但不知道这是否是正确的路径。

任何帮助表示感兴趣 - 尽量保持简单,我的知识最终会写出基本的VC。

5 个答案:

答案 0 :(得分:3)

Here's a blogpost来自一个创建了包含库和示例代码的工作XCode项目的人。示例代码与@conradev作为答案之一发布的代码相同。我在Xcode 4.3中对此进行了测试,效果很好。

我尝试在我的一个项目中包含原始的lib并且失败了但是我把它包含在这个项目中并且它都没有任何调整,所以我很肯定他没有改变任何设置Xcode项目。只需创建一个新项目,将该项目中的qrencode lib文件拖入其中,粘贴@conradev的代码,它们都按预期工作。

答案 1 :(得分:1)

我建议使用Objective C QR Encoder,这是一个用目标C编写的Apache Licensed QR编码器。

答案 2 :(得分:1)

看看这个libqrencode xcode project。它只给你一个编译过的lib,没有使用的例子。

答案 3 :(得分:1)

以下是您对任何C静态库的一般操作:

  1. 将库包含在XCode项目中,然后转到项目 选择“构建阶段”选项卡,然后打开“链接二进制文件” with libraries“section - 添加你的编译静态libqrencode 图书馆那里。
  2. 转到项目的“构建设置”选项卡并搜索 “header”,然后将路径添加到库保留标题的位置 “标题搜索路径”中的文件。
  3. 现在,您可以将头文件包含在Objective-C文件中,并直接调用库。

    一般的Google搜索起点是“将C库添加到XCode iPhone项目”。这不是CS技能,CS比搜索路径更深奥......

答案 4 :(得分:1)

libqrencode是根据LGPL许可的。为了使您的代码保持专有,它必须以允许用户用修改后的版本(1)替换库的方式链接到libqrencode。这在App Store中是不可能的。您的代码必须根据GPL许可才能使用libqrencode,并且App Store (2)中不允许使用GPL许可的代码。因此,您无法在App Store应用程序中使用libqrencode。

那就是说,这里有一些代码可以在iOS项目中用来与libqrencode接口:

#import <libqrencode/qrencode.h>

void freeRawData(void *info, const void *data, size_t size) {
    free((unsigned char *)data);
}

- (UIImage *)quickResponseImageForString:(NSString *)dataString withDimension:(int)imageWidth {

    QRcode *resultCode = QRcode_encodeString([dataString UTF8String], 0, QR_ECLEVEL_L, QR_MODE_8, 1);

    unsigned char *pixels = (*resultCode).data;
    int width = (*resultCode).width;
    int len = width * width;

    if (imageWidth < width)
        imageWidth = width;

    // Set bit-fiddling variables
    int bytesPerPixel = 4;
    int bitsPerPixel = 8 * bytesPerPixel;
    int bytesPerLine = bytesPerPixel * imageWidth;
    int rawDataSize = bytesPerLine * imageWidth;

    int pixelPerDot = imageWidth / width;
    int offset = (int)((imageWidth - pixelPerDot * width) / 2);

    // Allocate raw image buffer
    unsigned char *rawData = (unsigned char*)malloc(rawDataSize);
    memset(rawData, 0xFF, rawDataSize);

    // Fill raw image buffer with image data from QR code matrix
    int i;
    for (i = 0; i < len; i++) {
        char intensity = (pixels[i] & 1) ? 0 : 0xFF;

        int y = i / width;
        int x = i - (y * width);

        int startX = pixelPerDot * x * bytesPerPixel + (bytesPerPixel * offset);
        int startY = pixelPerDot * y + offset;
        int endX = startX + pixelPerDot * bytesPerPixel;
        int endY = startY + pixelPerDot;

        int my;
        for (my = startY; my < endY; my++) {
            int mx;
            for (mx = startX; mx < endX; mx += bytesPerPixel) {
                rawData[bytesPerLine * my + mx    ] = intensity;    //red
                rawData[bytesPerLine * my + mx + 1] = intensity;    //green
                rawData[bytesPerLine * my + mx + 2] = intensity;    //blue
                rawData[bytesPerLine * my + mx + 3] = 255;          //alpha
            }
        }
    }

    CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, rawData, rawDataSize, (CGDataProviderReleaseDataCallback)&freeRawData);
    CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();
    CGBitmapInfo bitmapInfo = kCGBitmapByteOrderDefault;
    CGColorRenderingIntent renderingIntent = kCGRenderingIntentDefault;
    CGImageRef imageRef = CGImageCreate(imageWidth, imageWidth, 8, bitsPerPixel, bytesPerLine, colorSpaceRef, bitmapInfo, provider, NULL, NO, renderingIntent);

    UIImage *quickResponseImage = [UIImage imageWithCGImage:imageRef];

    CGImageRelease(imageRef);
    CGColorSpaceRelease(colorSpaceRef);
    CGDataProviderRelease(provider);
    QRcode_free(resultCode);

    return quickResponseImage;
}