离线生成二维码

发布于 2024-11-10 04:37:19 字数 47 浏览 4 评论 0原文

是否有一个 Objective-C 库可以让我离线生成 QRCodes? 谢谢

Is there an objective-c library which will allow me to generate QRCodes offline?
Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

动听の歌 2024-11-17 04:37:19

请参阅:https://github.com/jverkoey/ObjQREncoder#readme

使用

#import <QREncoder/QREncoder.h>

UIImage* image = [QREncoder encode:@"http://www.google.com/"];

See: https://github.com/jverkoey/ObjQREncoder#readme

To use

#import <QREncoder/QREncoder.h>

UIImage* image = [QREncoder encode:@"http://www.google.com/"];
南烟 2024-11-17 04:37:19

在 Mavericks 和 iOS7 中,二维码生成是 Core Image 的一部分。您只需使用 CIQRCodeGenerator 过滤器。在 Github 上,您可以找到一个为 iOS 实现此功能的 。我已经修改了此代码以获得下面的 OS X 兼容代码:

NSString *website = @"http://stackoverflow.com/";
NSData *urlAsData = [website dataUsingEncoding:NSUTF8StringEncoding];

CIFilter *filter = [CIFilter filterWithName:@"CIQRCodeGenerator"];
[filter setDefaults];

[filter setValue: urlAsData forKey:@"inputMessage"];
[filter setValue:@"M" forKey:@"inputCorrectionLevel"];

CIImage *outputImage = [filter valueForKey:kCIOutputImageKey];

如果您想绘制 CIImage,有几种可能性。您可以像这样创建一个 NSImage:

CIContext *context = [[NSGraphicsContext currentContext] CIContext];
CGImageRef cgImage = [context createCGImage:outputImage
                                   fromRect:[outputImage extent]];

NSImage *image = [[NSImage alloc] initWithCGImage:cgImage size:NSZeroSize];

但是该图像通常会比您想要的小得多。我相信二维码中的每个黑点都会变成一个像素。不完全是你想要的。要放大图像而不使其模糊,请执行以下操作:

NSSize largeSize = NSMakeSize(image.size.width * 10, image.size.height * 10);

[image setScalesWhenResized:YES];
NSImage *largeImage = [[NSImage alloc] initWithSize:largeSize];
[largeImage lockFocus];
[image setSize:largeSize];
[[NSGraphicsContext currentContext] setImageInterpolation:NSImageInterpolationNone];
[image drawAtPoint:NSZeroPoint fromRect:CGRectMake(0, 0, largeSize.width, largeSize.height) operation:NSCompositeCopy fraction:1.0];
[largeImage unlockFocus];

largeImage 就是您可以显示的结果图像。

如果您想解码 QR,请使用 AVFoundation,如下所述 博客。不幸的是,目前似乎只有 iOS7 支持。

In Mavericks and iOS7 QR code generation is part of Core Image. You just use the CIQRCodeGenerator filter. On Github you can find a class which implements this for iOS. I've adapted this code to get the OS X compatible code below:

NSString *website = @"http://stackoverflow.com/";
NSData *urlAsData = [website dataUsingEncoding:NSUTF8StringEncoding];

CIFilter *filter = [CIFilter filterWithName:@"CIQRCodeGenerator"];
[filter setDefaults];

[filter setValue: urlAsData forKey:@"inputMessage"];
[filter setValue:@"M" forKey:@"inputCorrectionLevel"];

CIImage *outputImage = [filter valueForKey:kCIOutputImageKey];

If you want to draw the CIImage there are several possibilities. You can create an NSImage like this:

CIContext *context = [[NSGraphicsContext currentContext] CIContext];
CGImageRef cgImage = [context createCGImage:outputImage
                                   fromRect:[outputImage extent]];

NSImage *image = [[NSImage alloc] initWithCGImage:cgImage size:NSZeroSize];

But this image will typicall be much smaller than you want. I believe each black dot in the QR code just becomes one pixel. Not quite what you want. To scale up the image without making it blurry, do this:

NSSize largeSize = NSMakeSize(image.size.width * 10, image.size.height * 10);

[image setScalesWhenResized:YES];
NSImage *largeImage = [[NSImage alloc] initWithSize:largeSize];
[largeImage lockFocus];
[image setSize:largeSize];
[[NSGraphicsContext currentContext] setImageInterpolation:NSImageInterpolationNone];
[image drawAtPoint:NSZeroPoint fromRect:CGRectMake(0, 0, largeSize.width, largeSize.height) operation:NSCompositeCopy fraction:1.0];
[largeImage unlockFocus];

largeImage is then your result image which you can display.

If you want to decode a QR you use AVFoundation as explained in this blog. Unfortunately this seems to only be supported on iOS7 at the moment.

梦明 2024-11-17 04:37:19

只是生成 QR 码的简单且本机的方法:

  • (CIImage *)createQRForString:(NSString *)qrString {

    NSData *stringData = [qrString dataUsingEncoding: NISOLatin1StringEncoding];

    CIFilter *qrFilter = [CIFilter filterWithName:@"CIQRCodeGenerator"];

    [qrFilter setValue:stringData forKey:@"inputMessage"];

    返回qrFilter.outputImage;
    NSData *stringData =

Just a simple and native way of generating the QR code:

  • (CIImage *)createQRForString:(NSString *)qrString {

    NSData *stringData = [qrString dataUsingEncoding: NSISOLatin1StringEncoding];

    CIFilter *qrFilter = [CIFilter filterWithName:@"CIQRCodeGenerator"];

    [qrFilter setValue:stringData forKey:@"inputMessage"];

    return qrFilter.outputImage;
    }

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文