From ece2c6c2688e1f369a2348ea3a8942713cd8f8eb Mon Sep 17 00:00:00 2001 From: Shiva Huang Date: Tue, 18 Jun 2013 22:04:49 +0800 Subject: [PATCH 1/6] Add empty subclass of UIView to project. --- .../project.pbxproj | 8 +++++ QRCodeEncoderObjectiveCAtGithub/QRImageView.h | 13 ++++++++ QRCodeEncoderObjectiveCAtGithub/QRImageView.m | 31 +++++++++++++++++++ 3 files changed, 52 insertions(+) create mode 100644 QRCodeEncoderObjectiveCAtGithub/QRImageView.h create mode 100644 QRCodeEncoderObjectiveCAtGithub/QRImageView.m diff --git a/QRCodeEncoderObjectiveCAtGithub.xcodeproj/project.pbxproj b/QRCodeEncoderObjectiveCAtGithub.xcodeproj/project.pbxproj index d29fc01..0500c98 100644 --- a/QRCodeEncoderObjectiveCAtGithub.xcodeproj/project.pbxproj +++ b/QRCodeEncoderObjectiveCAtGithub.xcodeproj/project.pbxproj @@ -7,6 +7,8 @@ objects = { /* Begin PBXBuildFile section */ + 5889BAAF17709DD100C7697A /* QRImageView.h in Headers */ = {isa = PBXBuildFile; fileRef = 5889BAAD17709DD100C7697A /* QRImageView.h */; }; + 5889BAB017709DD100C7697A /* QRImageView.m in Sources */ = {isa = PBXBuildFile; fileRef = 5889BAAE17709DD100C7697A /* QRImageView.m */; }; 9621844415431DBA00447877 /* UIKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 9621844315431DBA00447877 /* UIKit.framework */; }; 9621844515431DBA00447877 /* Foundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 9672D35A13BD662F0002B0E2 /* Foundation.framework */; }; 9621844715431DBA00447877 /* CoreGraphics.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 9621844615431DBA00447877 /* CoreGraphics.framework */; }; @@ -38,6 +40,8 @@ /* End PBXContainerItemProxy section */ /* Begin PBXFileReference section */ + 5889BAAD17709DD100C7697A /* QRImageView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = QRImageView.h; sourceTree = ""; }; + 5889BAAE17709DD100C7697A /* QRImageView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = QRImageView.m; sourceTree = ""; }; 9621844115431DBA00447877 /* QRCodeEncoderDemo.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = QRCodeEncoderDemo.app; sourceTree = BUILT_PRODUCTS_DIR; }; 9621844315431DBA00447877 /* UIKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = UIKit.framework; path = System/Library/Frameworks/UIKit.framework; sourceTree = SDKROOT; }; 9621844615431DBA00447877 /* CoreGraphics.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = CoreGraphics.framework; path = System/Library/Frameworks/CoreGraphics.framework; sourceTree = SDKROOT; }; @@ -148,6 +152,8 @@ 9672D36813BD665E0002B0E2 /* QREncoder-Prefix.pch */, 9672D36913BD665E0002B0E2 /* QREncoder.h */, 9672D36A13BD665E0002B0E2 /* QREncoder.mm */, + 5889BAAD17709DD100C7697A /* QRImageView.h */, + 5889BAAE17709DD100C7697A /* QRImageView.m */, 9672D35D13BD662F0002B0E2 /* Supporting Files */, ); path = QRCodeEncoderObjectiveCAtGithub; @@ -173,6 +179,7 @@ 9672D36E13BD665E0002B0E2 /* QR_Encode.h in Headers */, 9672D36F13BD665E0002B0E2 /* QREncoder-Prefix.pch in Headers */, BA7984CF1043BDE62FC71866 /* QRCodeEncoderDemoViewController.h in Headers */, + 5889BAAF17709DD100C7697A /* QRImageView.h in Headers */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -270,6 +277,7 @@ 9672D36D13BD665E0002B0E2 /* QR_Encode.cpp in Sources */, 9672D37113BD665E0002B0E2 /* QREncoder.mm in Sources */, BA798313F4E3F133E55601A3 /* QRCodeEncoderDemoViewController.mm in Sources */, + 5889BAB017709DD100C7697A /* QRImageView.m in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/QRCodeEncoderObjectiveCAtGithub/QRImageView.h b/QRCodeEncoderObjectiveCAtGithub/QRImageView.h new file mode 100644 index 0000000..47d20b8 --- /dev/null +++ b/QRCodeEncoderObjectiveCAtGithub/QRImageView.h @@ -0,0 +1,13 @@ +// +// QRImageView.h +// QRCodeEncoderObjectiveCAtGithub +// +// Created by Shiva Huang on 13/6/18. +// +// + +#import + +@interface QRImageView : UIView + +@end diff --git a/QRCodeEncoderObjectiveCAtGithub/QRImageView.m b/QRCodeEncoderObjectiveCAtGithub/QRImageView.m new file mode 100644 index 0000000..2185d1c --- /dev/null +++ b/QRCodeEncoderObjectiveCAtGithub/QRImageView.m @@ -0,0 +1,31 @@ +// +// QRImageView.m +// QRCodeEncoderObjectiveCAtGithub +// +// Created by Shiva Huang on 13/6/18. +// +// + +#import "QRImageView.h" + +@implementation QRImageView + +- (id)initWithFrame:(CGRect)frame +{ + self = [super initWithFrame:frame]; + if (self) { + // Initialization code + } + return self; +} + +/* +// Only override drawRect: if you perform custom drawing. +// An empty implementation adversely affects performance during animation. +- (void)drawRect:(CGRect)rect +{ + // Drawing code +} +*/ + +@end From d0d622b80f4950eaac90a7d50601721a15a23256 Mon Sep 17 00:00:00 2001 From: Shiva Huang Date: Tue, 18 Jun 2013 22:05:44 +0800 Subject: [PATCH 2/6] Draw QR code matrix on view. --- QRCodeEncoderObjectiveCAtGithub/QRImageView.h | 5 +++- QRCodeEncoderObjectiveCAtGithub/QRImageView.m | 29 +++++++++++++++++-- 2 files changed, 31 insertions(+), 3 deletions(-) diff --git a/QRCodeEncoderObjectiveCAtGithub/QRImageView.h b/QRCodeEncoderObjectiveCAtGithub/QRImageView.h index 47d20b8..8248e34 100644 --- a/QRCodeEncoderObjectiveCAtGithub/QRImageView.h +++ b/QRCodeEncoderObjectiveCAtGithub/QRImageView.h @@ -7,7 +7,10 @@ // #import +#import "DataMatrix.h" @interface QRImageView : UIView -@end +@property (strong, nonatomic) DataMatrix *dataMatrix; + +@end \ No newline at end of file diff --git a/QRCodeEncoderObjectiveCAtGithub/QRImageView.m b/QRCodeEncoderObjectiveCAtGithub/QRImageView.m index 2185d1c..d61c463 100644 --- a/QRCodeEncoderObjectiveCAtGithub/QRImageView.m +++ b/QRCodeEncoderObjectiveCAtGithub/QRImageView.m @@ -19,13 +19,38 @@ - (id)initWithFrame:(CGRect)frame return self; } -/* + // Only override drawRect: if you perform custom drawing. // An empty implementation adversely affects performance during animation. - (void)drawRect:(CGRect)rect { + CGContextRef context = UIGraphicsGetCurrentContext(); + + CGContextSetFillColorWithColor(context, [[UIColor whiteColor] CGColor]); + CGContextFillRect(context, rect); + + if(self.dataMatrix == nil) + return; + + const int dataDimension = self.dataMatrix.dimension; + const CGFloat ratio = self.frame.size.width / (dataDimension + 8); + CGFloat yOffset = 0.0; + // Drawing code + + CGContextSetFillColorWithColor(context, [[UIColor blackColor] CGColor]); + + for(int my=0; my Date: Wed, 19 Jun 2013 08:56:36 +0800 Subject: [PATCH 3/6] Draw the image on the center of view. Adapt the image size to view size. Make the border width configurable. --- QRCodeEncoderObjectiveCAtGithub/QRImageView.h | 1 + QRCodeEncoderObjectiveCAtGithub/QRImageView.m | 21 ++++++++++++------- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/QRCodeEncoderObjectiveCAtGithub/QRImageView.h b/QRCodeEncoderObjectiveCAtGithub/QRImageView.h index 8248e34..60f3a8d 100644 --- a/QRCodeEncoderObjectiveCAtGithub/QRImageView.h +++ b/QRCodeEncoderObjectiveCAtGithub/QRImageView.h @@ -12,5 +12,6 @@ @interface QRImageView : UIView @property (strong, nonatomic) DataMatrix *dataMatrix; +@property (assign, nonatomic) unsigned int borderWidth; @end \ No newline at end of file diff --git a/QRCodeEncoderObjectiveCAtGithub/QRImageView.m b/QRCodeEncoderObjectiveCAtGithub/QRImageView.m index d61c463..a3121a6 100644 --- a/QRCodeEncoderObjectiveCAtGithub/QRImageView.m +++ b/QRCodeEncoderObjectiveCAtGithub/QRImageView.m @@ -15,6 +15,7 @@ - (id)initWithFrame:(CGRect)frame self = [super initWithFrame:frame]; if (self) { // Initialization code + self.borderWidth = 4; } return self; } @@ -24,28 +25,32 @@ - (id)initWithFrame:(CGRect)frame // An empty implementation adversely affects performance during animation. - (void)drawRect:(CGRect)rect { - CGContextRef context = UIGraphicsGetCurrentContext(); - - CGContextSetFillColorWithColor(context, [[UIColor whiteColor] CGColor]); - CGContextFillRect(context, rect); - if(self.dataMatrix == nil) return; const int dataDimension = self.dataMatrix.dimension; - const CGFloat ratio = self.frame.size.width / (dataDimension + 8); + const CGFloat imageDimension = MIN(self.frame.size.width, self.frame.size.height); + const CGFloat pixelPerDot = imageDimension / (dataDimension + self.borderWidth * 2); + CGFloat yMargin = (self.frame.size.height - imageDimension) / 2.0; + CGFloat xMargin = (self.frame.size.width - imageDimension) / 2.0; CGFloat yOffset = 0.0; // Drawing code + CGContextRef context = UIGraphicsGetCurrentContext(); + + CGContextSetFillColorWithColor(context, [[UIColor whiteColor] CGColor]); + CGContextFillRect(context, CGRectMake(xMargin, yMargin, imageDimension, imageDimension)); CGContextSetFillColorWithColor(context, [[UIColor blackColor] CGColor]); + yMargin += self.borderWidth * pixelPerDot; + xMargin += self.borderWidth * pixelPerDot; for(int my=0; my Date: Wed, 19 Jun 2013 09:31:31 +0800 Subject: [PATCH 4/6] Fill drawing area with background color. --- QRCodeEncoderObjectiveCAtGithub/QRImageView.m | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/QRCodeEncoderObjectiveCAtGithub/QRImageView.m b/QRCodeEncoderObjectiveCAtGithub/QRImageView.m index a3121a6..76a49aa 100644 --- a/QRCodeEncoderObjectiveCAtGithub/QRImageView.m +++ b/QRCodeEncoderObjectiveCAtGithub/QRImageView.m @@ -25,6 +25,11 @@ - (id)initWithFrame:(CGRect)frame // An empty implementation adversely affects performance during animation. - (void)drawRect:(CGRect)rect { + CGContextRef context = UIGraphicsGetCurrentContext(); + + CGContextSetFillColorWithColor(context, [self.backgroundColor CGColor]); + CGContextFillRect(context, rect); + if(self.dataMatrix == nil) return; @@ -36,8 +41,6 @@ - (void)drawRect:(CGRect)rect CGFloat yOffset = 0.0; // Drawing code - CGContextRef context = UIGraphicsGetCurrentContext(); - CGContextSetFillColorWithColor(context, [[UIColor whiteColor] CGColor]); CGContextFillRect(context, CGRectMake(xMargin, yMargin, imageDimension, imageDimension)); From 1075597687c3643f272b6d8ee1a099cdb2baf459 Mon Sep 17 00:00:00 2001 From: Shiva Huang Date: Wed, 19 Jun 2013 10:40:16 +0800 Subject: [PATCH 5/6] Add simple demo of QRImageView. --- .../QRCodeEncoderDemoViewController.h | 1 + .../QRCodeEncoderDemoViewController.mm | 29 ++++++++++++++----- 2 files changed, 23 insertions(+), 7 deletions(-) diff --git a/QRCodeEncoderDemo/QRCodeEncoderDemoViewController.h b/QRCodeEncoderDemo/QRCodeEncoderDemoViewController.h index 7dd9d28..6aa83f3 100644 --- a/QRCodeEncoderDemo/QRCodeEncoderDemoViewController.h +++ b/QRCodeEncoderDemo/QRCodeEncoderDemoViewController.h @@ -7,6 +7,7 @@ #import #import "QREncoder.h" #import "DataMatrix.h" +#import "QRImageView.h" @interface QRCodeEncoderDemoViewController : UIViewController { diff --git a/QRCodeEncoderDemo/QRCodeEncoderDemoViewController.mm b/QRCodeEncoderDemo/QRCodeEncoderDemoViewController.mm index 1f5abb1..0a16ea1 100644 --- a/QRCodeEncoderDemo/QRCodeEncoderDemoViewController.mm +++ b/QRCodeEncoderDemo/QRCodeEncoderDemoViewController.mm @@ -16,6 +16,7 @@ - (void)viewDidLoad { [super viewDidLoad]; + UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:self.view.frame]; //the qrcode is square. now we make it 250 pixels wide int qrcodeImageDimension = 250; @@ -32,17 +33,31 @@ - (void)viewDidLoad //put the image into the view UIImageView* qrcodeImageView = [[UIImageView alloc] initWithImage:qrcodeImage]; CGRect parentFrame = self.view.frame; - CGRect tabBarFrame = self.tabBarController.tabBar.frame; - //center the image - CGFloat x = (parentFrame.size.width - qrcodeImageDimension) / 2.0; - CGFloat y = (parentFrame.size.height - qrcodeImageDimension - tabBarFrame.size.height) / 2.0; - CGRect qrcodeImageViewFrame = CGRectMake(x, y, qrcodeImageDimension, qrcodeImageDimension); + //put the image on a scroll view + CGFloat margin = (parentFrame.size.width - qrcodeImageDimension) / 2.0; + CGRect qrcodeImageViewFrame = CGRectMake(margin, margin, qrcodeImageDimension, qrcodeImageDimension); [qrcodeImageView setFrame:qrcodeImageViewFrame]; - //and that's it! - [self.view addSubview:qrcodeImageView]; + //you can also show the matrix using QRImageView + CGRect qrcodeImageViewFrame2 = CGRectMake(margin, margin * 2 + qrcodeImageDimension, qrcodeImageDimension, qrcodeImageDimension); + QRImageView* qrcodeImageView2 = [[QRImageView alloc] initWithFrame:qrcodeImageViewFrame2]; + + //you can set the border arounding the QR code to make scanning easier, the default with is 4 dots + qrcodeImageView2.borderWidth = 4; + + //assign the matrix + qrcodeImageView2.dataMatrix = qrMatrix; + + //and that's it! The upper one is a bitmap image shown in UIImageView, the lower one is a QRImageView showing the same data matrix. + [scrollView addSubview:qrcodeImageView]; + [scrollView addSubview:qrcodeImageView2]; + [scrollView setContentSize:CGSizeMake(parentFrame.size.width, qrcodeImageDimension * 2 + margin * 3)]; + [self.view addSubview:scrollView]; + [qrcodeImageView release]; + [qrcodeImageView2 release]; + [scrollView release]; } @end From f11c0e838cfcc01af948216ddbd21de76083b464 Mon Sep 17 00:00:00 2001 From: Shiva Huang Date: Wed, 19 Jun 2013 10:43:28 +0800 Subject: [PATCH 6/6] Fix init warning. --- QRCodeEncoderObjectiveCAtGithub/DataMatrix.mm | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/QRCodeEncoderObjectiveCAtGithub/DataMatrix.mm b/QRCodeEncoderObjectiveCAtGithub/DataMatrix.mm index ac5a631..07be30e 100644 --- a/QRCodeEncoderObjectiveCAtGithub/DataMatrix.mm +++ b/QRCodeEncoderObjectiveCAtGithub/DataMatrix.mm @@ -6,7 +6,7 @@ @implementation DataMatrix - (id)initWith:(int)dimension { - if ([super init]) { + if (self = [super init]) { self->dim = dimension; self->data = (bool**)malloc(sizeof(bool*) * self->dim); for (int y=0; ydim; y++) {