生成二维码

发布于 2024-11-10 13:07:46 字数 49 浏览 5 评论 0原文

我需要使用 Objective-C 生成 QRCodes。是否有可用的库/框架?谢谢

I need to generate QRCodes using objective-c. Is there a library/framework already available for that?Thanks

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

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

发布评论

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

评论(4

ゞ记忆︶ㄣ 2024-11-17 13:07:46

我在我的一个项目中使用了这个简单的代码来编码 QRCode。您可以使用:

//
//  QRCodeGeneratorViewController.h
//  
//
//  Created by Jhoney Lopes on 28/09/14.
//  Copyright (c) 2014 Jhoney Lopes. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface QRCodeGeneratorViewController : UIViewController

@end

实施.m

//
//  QRCodeGeneratorViewController.m
// 
//
//  Created by Jhoney Lopes on 28/09/14.
//  Copyright (c) 2014 Jhoney Lopes. All rights reserved.
//

#import "QRCodeGeneratorViewController.h"

@interface QRCodeGeneratorViewController ()
@property (strong, nonatomic) IBOutlet UIImageView *qrImageView;
@end

@implementation QRCodeGeneratorViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    [self generateTheQRCodeImageFromDataBaseInfo:@"TEXT-WHAT-YOU-WANT-TO-CONVERT-IN-QRCODE"];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - QR Code Generator

- (CIImage *)createQRForString:(NSString *)qrString
{
    // Need to convert the string to a UTF-8 encoded NSData object
    NSData *stringData = [qrString dataUsingEncoding:NSUTF8StringEncoding];

    // Create the filter
    CIFilter *qrFilter = [CIFilter filterWithName:@"CIQRCodeGenerator"];
    // Set the message content and error-correction level
    [qrFilter setValue:stringData forKey:@"inputMessage"];
    [qrFilter setValue:@"H" forKey:@"inputCorrectionLevel"];

    // Send the image back
    return qrFilter.outputImage;
}

- (UIImage *)createNonInterpolatedUIImageFromCIImage:(CIImage *)image withScale:(CGFloat)scale
{
    // Render the CIImage into a CGImage
    CGImageRef cgImage = [[CIContext contextWithOptions:nil] createCGImage:image fromRect:image.extent];

    // Now we'll rescale using CoreGraphics
    UIGraphicsBeginImageContext(CGSizeMake(image.extent.size.width * scale, image.extent.size.width * scale));
    CGContextRef context = UIGraphicsGetCurrentContext();
    // We don't want to interpolate (since we've got a pixel-correct image)
    CGContextSetInterpolationQuality(context, kCGInterpolationNone);
    CGContextDrawImage(context, CGContextGetClipBoundingBox(context), cgImage);
    // Get the image out
    UIImage *scaledImage = UIGraphicsGetImageFromCurrentImageContext();
    // Tidy up
    UIGraphicsEndImageContext();
    CGImageRelease(cgImage);
    return scaledImage;
}

- (void)generateTheQRCodeImageFromDataBaseInfo:(NSString *)jsonString {

    // Get the string
    NSString *stringToEncode = jsonString;

    // Generate the image
    CIImage *qrCode = [self createQRForString:stringToEncode];

    // Convert to an UIImage
    UIImage *qrCodeImg = [self createNonInterpolatedUIImageFromCIImage:qrCode withScale:2*[[UIScreen mainScreen] scale]];

    // And push the image on to the screen
    self.qrImageView.image = qrCodeImg;
}

I used this simple code to encode a QRCode in one of my projects. You can use:

//
//  QRCodeGeneratorViewController.h
//  
//
//  Created by Jhoney Lopes on 28/09/14.
//  Copyright (c) 2014 Jhoney Lopes. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface QRCodeGeneratorViewController : UIViewController

@end

Implementation .m

//
//  QRCodeGeneratorViewController.m
// 
//
//  Created by Jhoney Lopes on 28/09/14.
//  Copyright (c) 2014 Jhoney Lopes. All rights reserved.
//

#import "QRCodeGeneratorViewController.h"

@interface QRCodeGeneratorViewController ()
@property (strong, nonatomic) IBOutlet UIImageView *qrImageView;
@end

@implementation QRCodeGeneratorViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    [self generateTheQRCodeImageFromDataBaseInfo:@"TEXT-WHAT-YOU-WANT-TO-CONVERT-IN-QRCODE"];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - QR Code Generator

- (CIImage *)createQRForString:(NSString *)qrString
{
    // Need to convert the string to a UTF-8 encoded NSData object
    NSData *stringData = [qrString dataUsingEncoding:NSUTF8StringEncoding];

    // Create the filter
    CIFilter *qrFilter = [CIFilter filterWithName:@"CIQRCodeGenerator"];
    // Set the message content and error-correction level
    [qrFilter setValue:stringData forKey:@"inputMessage"];
    [qrFilter setValue:@"H" forKey:@"inputCorrectionLevel"];

    // Send the image back
    return qrFilter.outputImage;
}

- (UIImage *)createNonInterpolatedUIImageFromCIImage:(CIImage *)image withScale:(CGFloat)scale
{
    // Render the CIImage into a CGImage
    CGImageRef cgImage = [[CIContext contextWithOptions:nil] createCGImage:image fromRect:image.extent];

    // Now we'll rescale using CoreGraphics
    UIGraphicsBeginImageContext(CGSizeMake(image.extent.size.width * scale, image.extent.size.width * scale));
    CGContextRef context = UIGraphicsGetCurrentContext();
    // We don't want to interpolate (since we've got a pixel-correct image)
    CGContextSetInterpolationQuality(context, kCGInterpolationNone);
    CGContextDrawImage(context, CGContextGetClipBoundingBox(context), cgImage);
    // Get the image out
    UIImage *scaledImage = UIGraphicsGetImageFromCurrentImageContext();
    // Tidy up
    UIGraphicsEndImageContext();
    CGImageRelease(cgImage);
    return scaledImage;
}

- (void)generateTheQRCodeImageFromDataBaseInfo:(NSString *)jsonString {

    // Get the string
    NSString *stringToEncode = jsonString;

    // Generate the image
    CIImage *qrCode = [self createQRForString:stringToEncode];

    // Convert to an UIImage
    UIImage *qrCodeImg = [self createNonInterpolatedUIImageFromCIImage:qrCode withScale:2*[[UIScreen mainScreen] scale]];

    // And push the image on to the screen
    self.qrImageView.image = qrCodeImg;
}
念﹏祤嫣 2024-11-17 13:07:46

查看 http://code .google.com/p/simplehttp/source/browse/trunk/qrencode/qrencode.c?r=78

如果您可以从 Objective-C 调用 C 例程,那么您就准备好了。

Take a look at http://code.google.com/p/simplehttp/source/browse/trunk/qrencode/qrencode.c?r=78.

If you can call C-routines from objective-C, then you are set.

情话墙 2024-11-17 13:07:46

zxing 具有适用于 iPhone 的 QR 模块。

zxing has QR module for an iPhone.

小矜持 2024-11-17 13:07:46

如果您正在寻找 Objective-C 的编码器,我刚刚从 Psytec 实现中移植了一个。看看 https://github.com/myang-git /QR-Code-Encoder-for-Objective-C。它很新,所以请让我知道任何评论。

If you are looking for an encoder for Objective-C, I just ported one from the Psytec implementation. Take a look at https://github.com/myang-git/QR-Code-Encoder-for-Objective-C. It's quite new so let me know of any comments.

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