【问题标题】:Recognizing the Text Drawn By using Bezier Paths识别使用贝塞尔路径绘制的文本
【发布时间】:2013-12-30 10:09:21
【问题描述】:

我编写了下面的代码来使用贝塞尔路径绘制文本。
现在我的任务是如何识别使用 Beizer 路径绘制的文本。
例如,如果我画“Apple”,那么我的 Label 将显示“U Have Drawn Apple”。

@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

【问题讨论】:

    标签: ios cocoa-touch ios5 ios4


    【解决方案1】:

    我不确定你是否有兴趣使用开源代码,但你可以试试GLGestureRecognizer

    这是您在stack-Overflow中的问题的一个这样的答案

    【讨论】:

      【解决方案2】:

      检测字母区域的变化:除了字母“i”(小写)外,英文字母都是“连接”的,所以如果一个笔划区域的水平间距超过了 33%第一个笔画的长度(即正在绘制的字符的高度或宽度),我认为可以安全地假设您创建了一个新字母(因此有间隙)。

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

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

      应该不到一天 O:)

      【讨论】:

        猜你喜欢
        • 2017-12-15
        • 1970-01-01
        • 2011-02-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-01-09
        相关资源
        最近更新 更多