Recognizing the Text Drawn By using Bezier Paths

767 views Asked by At

I Write the Below Code for Drawing text by Using Bezier Paths.
Now here my task is How We Recognize the text which is Drawn by using Beizer Paths.
for example if I Draw "Apple" then my Label will show "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
2

There are 2 answers

0
Albert Renshaw On

Detecting change in letter regions: English letters are all "connected" except for the letter "i" (lowercase), so if horizontal spacing of a region of strokes is more than say 33% of the length of the first stroke (i.e. height or width of the character being drawn), I think it's safe to assume you have created a new letter (hence the gap).

Detecting Letters themselves: I would make a chart of common characteristics of letters (like how many strokes it takes to draw them, how many vertical lines, how many horizontal lines, how many "dots", how many angled lines, how many curves, etc.) until you can systematically detect which letter is drawn uniquely by going through the whole property list.

Detecting stroke types: To detect the direction of a stroke (horizontal, vertical, diagonal) simply check the starting X/Y (touchesBegan) and the ending X/Y (touchesEnded) and compare with if-statements. To detect a dot just measure length compared to height, they should be about the same (i.e. a circle) ... another acceptable approach is a touchesBegan and a touchesEnded being called but not touchesMoved being called (a tap is a dot), to detect if a stroke is a line or a curve use distance formula between the starting and ending points and compare it to the actual amount (distance) drawn (which you can keep track of with += variable changes in touchesMoved) if the actual amount drawn is more than 30% that of the distance formula I would say it's safe to assume a curve was drawn instead of a line (like in the letter C), you can also detect which way a curve bends by keeping track of the furthest left and/or further right points (x-val) and comparing it to the starting or ending x-val point!

Should take less than a day O:)

0
Balram Tiwari On

I am not sure whether you would be interested in using open source code, but you can give a try to the GLGestureRecognizer.

Here is one such answer to your question in stack-Overflow