2013-12-12 51 views
2

我使用贝塞尔路径编写绘图文本的下面的代码。
现在我的任务是如何识别使用Beizer路径绘制的文本。
例如,如果我画“苹果”,那么我的标签会显示“U有绘制苹果”。通过使用贝塞尔路径识别文本绘制

@implementation NaiveVarWidthView 
{ 
UIBezierPath *path; 
UIImage *incrementalImage; 
CGPoint pts[5]; 
uint ctr; 
} 
- (id)initWithFrame:(CGRect)frame 
{ 
self = [super initWithFrame:frame]; 
if (self) { 
    [self setMultipleTouchEnabled:NO]; 
    path = [UIBezierPath bezierPath]; 
} 
return self; 
} 
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
ctr = 0; 
UITouch *touch = [touches anyObject]; 
pts[0] = [touch locationInView:self]; 
} 
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
UITouch *touch = [touches anyObject]; 
CGPoint p = [touch locationInView:self]; 
ctr++; 
pts[ctr] = p; 
if (ctr == 4) 
{ 
    pts[3] = CGPointMake((pts[2].x + pts[4].x)/2.0, (pts[2].y + pts[4].y)/2.0); 
    [path moveToPoint:pts[0]]; 
    [path addCurveToPoint:pts[3] controlPoint1:pts[1] controlPoint2:pts[2]]; 
    UIGraphicsBeginImageContextWithOptions(self.bounds.size, YES, 0.0); // 
    if (!incrementalImage) 
    { 
     UIBezierPath *rectpath = [UIBezierPath bezierPathWithRect:self.bounds]; 
     [[UIColor whiteColor] setFill]; 
     [rectpath fill]; 
    } 
    [incrementalImage drawAtPoint:CGPointZero]; 
    [[UIColor blackColor] setStroke]; 
    float speed = 0.0; 
    for (int i = 0; i < 3; i++) 
    { 
     float dx = pts[i+1].x - pts[i].x; 
     float dy = pts[i+1].y - pts[i].y; 
     speed += sqrtf(dx * dx + dy * dy); 
    } 
#define FUDGE_FACTOR 100 
    float width = FUDGE_FACTOR/speed; 
    [path setLineWidth:width]; 
    [path stroke]; 
    incrementalImage = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 
    [self setNeedsDisplay]; 
    [path removeAllPoints]; 
    pts[0] = pts[3]; 
    pts[1] = pts[4]; 
    ctr = 1; 
} 
} 
- (void)drawRect:(CGRect)rect 
{ 
    [incrementalImage drawInRect:rect]; 
} 
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    [self setNeedsDisplay]; 
} 
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    [self touchesEnded:touches withEvent:event]; 
} 
@end 

回答

1

我不知道你是否有兴趣在使用开源代码,但你可以给一个尝试将GLGestureRecognizer

这里是一个这样的回答你的问题在stack-Overflow

1

检测在信地区变化:英文字母都“连接”除了字母“i”(小写),所以如果的水平间距笔画区域比第一笔画长度的33%(即画出的字符的高度或宽度)多,我认为假设你已经创建了一个新的字母(因此是空白)是安全的。

检测字母本身:我会制作一个字母的共同特征图表(如绘制它们需要多少笔划,多少条垂直线,多少条水平线,多少个“点”,多少个角度线条,多少条曲线等),直到您可以系统地检测通过整个属性列表唯一地绘制哪个字母。

检测笔划类型:为了检测行程(水平,垂直,对角)只需查看的起始X/Y(的touchesBegan)和结束X/Y(touchesEnded)的方向,并用if语句比较。要检测一个点只是衡量长度与高度相比,它们应该是大致相同的(即一个圆圈)...另一个可接受的方法是touchesBegan和touchesEnded被称为但不touchesMoved被称为(一个水龙头是一个点),以检测笔画是否为起点和终点之间的直线或曲线使用距离公式,并将其与实际绘制的金额(距离)进行比较(您可以使用+= touchesMoved中的可变更改跟踪该实际金额(距离)),超过距离公式的30%我会说假设绘制曲线而不是线条(如字母C)是安全的,您还可以通过跟踪最远的左侧和/或右侧来检测曲线弯曲的方式,或更右点(x-val)并将其与开始或结束x-val点进行比较!

应该少于一天O :)