Create png image from QRCode for iPhone

2019-02-22 07:50发布

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.

5条回答
神经病院院长
2楼-- · 2019-02-22 08:31

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

查看更多
时光不老,我们不散
3楼-- · 2019-02-22 08:34

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"}
);
查看更多
叛逆
4楼-- · 2019-02-22 08:34

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.

查看更多
淡お忘
5楼-- · 2019-02-22 08:34

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.

查看更多
Root(大扎)
6楼-- · 2019-02-22 08:41

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;
}
查看更多
登录 后发表回答