Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions QRCodeEncoderDemo/QRCodeEncoderDemoViewController.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#import <UIKit/UIKit.h>
#import "QREncoder.h"
#import "DataMatrix.h"
#import "QRImageView.h"

@interface QRCodeEncoderDemoViewController : UIViewController {

Expand Down
29 changes: 22 additions & 7 deletions QRCodeEncoderDemo/QRCodeEncoderDemoViewController.mm
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
8 changes: 8 additions & 0 deletions QRCodeEncoderObjectiveCAtGithub.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -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 */; };
Expand Down Expand Up @@ -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 = "<group>"; };
5889BAAE17709DD100C7697A /* QRImageView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = QRImageView.m; sourceTree = "<group>"; };
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; };
Expand Down Expand Up @@ -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;
Expand All @@ -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;
};
Expand Down Expand Up @@ -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;
};
Expand Down
2 changes: 1 addition & 1 deletion QRCodeEncoderObjectiveCAtGithub/DataMatrix.mm
Original file line number Diff line number Diff line change
Expand Up @@ -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; y<self->dim; y++) {
Expand Down
17 changes: 17 additions & 0 deletions QRCodeEncoderObjectiveCAtGithub/QRImageView.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//
// QRImageView.h
// QRCodeEncoderObjectiveCAtGithub
//
// Created by Shiva Huang on 13/6/18.
//
//

#import <UIKit/UIKit.h>
#import "DataMatrix.h"

@interface QRImageView : UIView

@property (strong, nonatomic) DataMatrix *dataMatrix;
@property (assign, nonatomic) unsigned int borderWidth;

@end
64 changes: 64 additions & 0 deletions QRCodeEncoderObjectiveCAtGithub/QRImageView.m
Original file line number Diff line number Diff line change
@@ -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<dataDimension; my++) {
yOffset = yMargin + my * pixelPerDot;

for(int mx=0; mx<dataDimension; mx++) {
if([self.dataMatrix valueAt:mx y:my]) {
CGContextAddRect(context, CGRectMake(xMargin + mx * pixelPerDot, yOffset, pixelPerDot, pixelPerDot));
}
}
}

CGContextFillPath(context);
}

@end