从 iPhone 的 QRCode 创建 png 图像

发布于 2024-11-08 18:52:56 字数 110 浏览 0 评论 0原文

我一直在使用 zxing 来解码 QRCodes。我现在还可以让编码器正常工作,并且可以创建包含编码数据的 QRCode。

我问是否有人知道如何将此 QRCOde 数据转换为 png 图像。

I've been using zxing to decode QRCodes. I've also now the got the encoder working and can create the QRCode which contains the encoded data.

I'm asking if anyone knows how to convert this QRCOde data to a png image.

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

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

发布评论

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

评论(5

恬淡成诗 2024-11-15 18:52:56

如果您的应用程序可以在线访问,那么您可以使用类似 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 码、Data Matrix、PDF417)

您可以使用 Phonegap 使用插件 此处。按照说明进行操作,您应该会成功。

Javascript 很简单,直接取自 https://github.com/phonegap/phonegap-plugins/< /a>

   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"}
);

If your app has online access then you can use something like http://www.tag.cx/qr-codes/

Many users are looking for a way to encode QR codes and other codes programmatically on the iPhone. These features have not been ported from Android to iPhone yet as described in this Zxing thread: https://groups.google.com/group/zxing/browse_thread/thread/7325ed13cc49122c/aba6f4545c5c3583?lnk=gst&q=encode+to+png+iphone#aba6f4545c5c3583

Please see this question for further discussion:
Generate 2D bar code (e.g. QR Code, Data Matrix, PDF417) on iPhone and Android

You can use Phonegap to encode a QR barcode using the plugin here. Follow the instructions and you should be successful.

The Javascript is simple, taken straight from 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"}
);
谁的新欢旧爱 2024-11-15 18:52:56

psytec 编码器(http://groups.google.com/group/zxing/msg/27e421daeb510d0f)运行良好。我在生产中使用它。使用像 libpng 这样的东西从它生成的位数组转换为 png 应该是相对简单的。在设备上,我会检查它生成的位数组并直接绘制到 CG 上下文中。

The psytec encoder (http://groups.google.com/group/zxing/msg/27e421daeb510d0f) works well. I use it in production. It should be relatively starightforward to use something like libpng to go from the bit array it produces to a png. On devices, I go through the bit array it produces and draw straight into CG contexts.

若沐 2024-11-15 18:52:56

如果您使用的是phonegap并且需要离线支持,也许 jquery qrcode 可以提供帮助。

If you are using phonegap and needs offline support, perhaps jquery qrcode could help.

请别遗忘我 2024-11-15 18:52:56

这个 Github链接下载QR-code Generator的示例代码,更多更简单、更容易。将示例项目中使用的资源文件添加到您的项目中。

如果您只想显示 UIImageview 中生成的二维码,您可以直接使用示例项目中的代码

,或者

如果您需要带有二维码的 png 文件,则意味着您可以获得 NSData 来自 UIImageview 的图像,并将 NSData 转换为 png 文件并使用它。

Download the sample code for QR-code Generator from this Github link, It is more simpler and easier. Add the resource files which were used in that sample project to your project.

If you want just to show the QR-code generated in a UIImageview, you can directly use the code in the sample project

or

If you need the png file with the QR code means you can get the NSData from the image of the UIImageview and convert the NSData to png file and use it.

浸婚纱 2024-11-15 18:52:56

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

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

Edited There are similar questions of this post, I linked to this answer.

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