public
Created

psytec example

  • Download Gist
gistfile1.txt
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
// 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

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.