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 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/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++) { diff --git a/QRCodeEncoderObjectiveCAtGithub/QRImageView.h b/QRCodeEncoderObjectiveCAtGithub/QRImageView.h new file mode 100644 index 0000000..60f3a8d --- /dev/null +++ b/QRCodeEncoderObjectiveCAtGithub/QRImageView.h @@ -0,0 +1,17 @@ +// +// QRImageView.h +// QRCodeEncoderObjectiveCAtGithub +// +// Created by Shiva Huang on 13/6/18. +// +// + +#import +#import "DataMatrix.h" + +@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 new file mode 100644 index 0000000..76a49aa --- /dev/null +++ b/QRCodeEncoderObjectiveCAtGithub/QRImageView.m @@ -0,0 +1,64 @@ +// +// 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 + self.borderWidth = 4; + } + 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, [self.backgroundColor CGColor]); + CGContextFillRect(context, rect); + + if(self.dataMatrix == nil) + return; + + const int dataDimension = self.dataMatrix.dimension; + 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 + 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