Skip to content

@smparkes /gist:1005365
Created

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
psytec example
// code.h
// -*- Mode: ObjC; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
#include <string>
class CQR_Encode;
@interface Code : UIView {
std::string _uri;
CQR_Encode* _encoder;
size_t _size;
}
- (id)initWithURI:(std::string const&)uri;
@end
// code.mm
// -*- Mode: ObjC; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
#import "code.h"
#include <QR_Encode.h>
#import <QuartzCore/QuartzCore.h>
using std::string;
@implementation Code
- (void)dealloc {
delete _encoder;
[super dealloc];
}
- (id)initWithURI:(string const&)uri {
if ((self = [super initWithFrame:CGRectZero])) {
_uri = uri;
self.backgroundColor = [UIColor whiteColor];
self.layer.masksToBounds = YES;
self.layer.cornerRadius = 5;
_encoder = new CQR_Encode;
const int level = QR_LEVEL_H;
// const int level = QR_LEVEL_Q;
const int version = 0;
const int autoExtent = true;
const int maskingNumber = -1;
// _uri = &_uri[1];
_encoder->EncodeData(level,
version,
autoExtent,
maskingNumber,
_uri.c_str(),
_uri.size());
_size = _encoder->m_nVersion*4+17;
// 35 @ H = 29
// 36 @ H == 33
// 50 @ Q == 29
// cerr << "len " << _uri.size() << endl;
// cerr << "size " << _size << endl;
}
return self;
}
- (void)drawRect:(CGRect)rect {
CGRect frame = self.frame;
size_t size = _size + 2;
float pixelsPerModule = frame.size.width / size;
size_t last_x = (rect.origin.x + rect.size.width)/pixelsPerModule;
size_t last_y = (rect.origin.y + rect.size.height)/pixelsPerModule;
CGContextRef context = UIGraphicsGetCurrentContext();
(void)context;
for(size_t x = rect.origin.x / pixelsPerModule; x < last_x; ++x) {
for(size_t y = rect.origin.y / pixelsPerModule; y < last_y; ++y) {
if (_encoder->m_byModuleData[y][x]) {
CGSize size = CGSizeMake(pixelsPerModule, pixelsPerModule);
CGPoint exact = CGPointMake((x+1)*pixelsPerModule, (y+1)*pixelsPerModule);
CGPoint origin = CGPointMake(int(exact.x), int(exact.y));
size = CGSizeMake(size.width+exact.x-origin.x, size.height+exact.y-origin.y);
size = CGSizeMake(int(size.width+1), int(size.height+1));
CGRect rect;
rect.origin = origin;
rect.size = size;
UIRectFill(rect);
}
}
}
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.