我一直在使用zxing解码二维码。现在我也能够使用编码器创建包含编码数据的QRCode。
我想知道是否有人知道如何将这个QRCode数据转换为png图像。
我一直在使用zxing解码二维码。现在我也能够使用编码器创建包含编码数据的QRCode。
我想知道是否有人知道如何将这个QRCode数据转换为png图像。
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"}
);
编辑 这篇文章有类似的问题,我已经链接到了这个答案。
我在我的一个项目中使用了这个简单的代码来编码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;
}
Psytec编码器(http://groups.google.com/group/zxing/msg/27e421daeb510d0f)效果很好。我在生产中使用它。使用类似于libpng的东西将其生成的位数组转换成png应该相对简单。在设备上,我遍历它生成的位数组并直接绘制到CG上下文中。
从 this Github 链接下载 QR-code 生成器的示例代码,它更简单、更容易。将该示例项目中使用的资源文件添加到您的项目中。
如果您只想在 UIImageview
中显示生成的 QR-code,请直接使用示例项目中的代码
或者
如果您需要带有 QR-code 的 png 文件,则可以从 UIImageview
的图像中获取 NSData
并将其转换为 png 文件并使用它。