从QR码创建png图像用于iPhone

6

我一直在使用zxing解码二维码。现在我也能够使用编码器创建包含编码数据的QRCode。

我想知道是否有人知道如何将这个QRCode数据转换为png图像。

5个回答

3
如果您的应用程序有在线访问权限,那么您可以使用类似http://www.tag.cx/qr-codes/这样的东西。
许多用户正在寻找一种在iPhone上进行QR码和其他编码编程的方法。目前尚未将这些功能从Android移植到iPhone中,如此Zxing线程所述:https://groups.google.com/group/zxing/browse_thread/thread/7325ed13cc49122c/aba6f4545c5c3583?lnk=gst&q=encode+to+png+iphone#aba6f4545c5c3583 请参考这个问题以进一步讨论: 在iPhone和Android上生成二维条形码(例如QR码,数据矩阵,PDF417) 您可以使用Phonegap插件here来编码QR条形码。按照说明操作,您应该会成功。
Javascript很简单,直接来源于https://github.com/phonegap/phonegap-plugins/
   window.plugins.barcodeScanner.encode(BarcodeScanner.Encode.TEXT_TYPE, "http://www.nytimes.com", function(success) {
    alert("encode success: " + success);
  }, function(fail) {
    alert("encoding failed: " + fail);
  }, {yesString: "Install"}
);

我需要离线生成图像。 - Chris Baxter
编辑答案,包含 Phonegap 插件,该插件允许以编程方式对 QR 码进行编码。 - Chris Truman

0

编辑 这篇文章有类似的问题,我已经链接到了这个答案。

我在我的一个项目中使用了这个简单的代码来编码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;
}

0

Psytec编码器(http://groups.google.com/group/zxing/msg/27e421daeb510d0f)效果很好。我在生产中使用它。使用类似于libpng的东西将其生成的位数组转换成png应该相对简单。在设备上,我遍历它生成的位数组并直接绘制到CG上下文中。


你能否发布你的代码和/或所需文件的链接,以便完成这个任务?我找到了所有的C/C++文件,但无法成功编译或启动任何东西。 - james
我在iOS上使用的Objective C++代码的框架可在https://gist.github.com/1005365中找到。也许这会有所帮助。但是我恐怕没有将其放入Xcode的说明。它深深地嵌入了我的项目中,因此很难单独提取出来。 - smparkes

0

this Github 链接下载 QR-code 生成器的示例代码,它更简单、更容易。将该示例项目中使用的资源文件添加到您的项目中。

如果您只想在 UIImageview 中显示生成的 QR-code,请直接使用示例项目中的代码

或者

如果您需要带有 QR-code 的 png 文件,则可以从 UIImageview 的图像中获取 NSData 并将其转换为 png 文件并使用它。


0
如果您正在使用PhoneGap并需要离线支持,也许jquery qrcode可以帮助您。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接