Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to extract text from a CAPTCHA image in Objective-C for a Mac OS X Application?

I am developing one Mac OS X Application which has a concept of reading/extracting text from a CAPTCHA image. I searched on google and got an API named "DeathByCaptcha", which does exactly what I want to do but this API is not for Mac OS X, this API is available for .Net/C/PHP/Python etc..

'DeathByCaptcha' can be found on the web here. I found this Stack Overflow post while searching but this does not read a CAPTCHA image, it just reads a simple image and convert into text.

Please help me extract text From a CAPTCHA image using Objective-C for a Mac OS X Application.

like image 447
NSExpression Avatar asked Feb 11 '13 10:02

NSExpression


2 Answers

After looking at the API, turns out you first need to build the library.

  1. Open a terminal, navigate to the dbc_api_v4_2_c folder and type

    make

You should get a new folder called lib containing the libdeathbycaptcha.so library file.

  1. Now, create a new Xcode project, include the library in your resources, add the deathbycaptcha.h header to your project.

  2. Then you can use the code provided in the example, but only the code that is not for windows. For instance :

    void *lib = dlopen("./libdeathbycaptcha.so", RTLD_LAZY);
    if (!lib) {
        fprintf(stderr, "dlopen(): %s\n", dlerror());
        exit(EXIT_FAILURE);
    }
    
    dbc_init = (void *)GetProcAddress(lib, "dbc_init");
    dbc_close = (void *)GetProcAddress(lib, "dbc_close");
    dbc_get_balance = (void *)GetProcAddress(lib, "dbc_get_balance");
    dbc_decode_file = (void *)GetProcAddress(lib, "dbc_decode_file");
    dbc_report = (void *)GetProcAddress(lib, "dbc_report");
    dbc_close_captcha = (void *)GetProcAddress(lib, "dbc_close_captcha");
    

It should compile fine. Don't forget to include the following headers:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <dlfcn.h> // not in the docs, but required for dlsym
#include "deathbycaptcha.h"

In addition, you may also need to install the Xcode command line tools (in Xcode preferences) if you haven't already done so.

like image 116
Jean Avatar answered Sep 22 '22 22:09

Jean


Hi all, I wrote below code to solve my captcha issue and it worked fantastic for me.

Code For Reading Text From Captcha Image

You will need ASIHTTPRequest API to send HTTP Request on web and get response for the below code.

Import respective classes of ASIHTTPRequest API into your class where you are going to do process for fetching text from captcha image.

Now use below method and call to solve catpcha .

-(NSString *)CaptchaToText 
{
    // Captcha Image Url in NSData object
    NSData *urlData=[NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://oi47.tinypic.com/357hyea.jpg"]];

    // base64 image data
    NSString *imageBase64Data = [ASIHTTPRequest base64forData:urlData ];

    //NSString *imageBase64Data =[self base64EncodedString:urlData];
    NSLog(@"imageBase64Data =%@\n\n",imageBase64Data);

    // prefix - base64 image data
    NSString *prefixBase64Data = [NSString stringWithFormat:@"base64:%@",imageBase64Data];
    NSLog(@"prefixBase64Data Value = %@\n\n",prefixBase64Data);

    ASIHTTPRequest *req=[ASIHTTPRequest requestWithURL:[NSURL URLWithString:@"http://oi47.tinypic.com/357hyea.jpg"]];

    [req startSynchronous];
    NSError *err=[req error];

    if(!err){
        NSString *respo=[req responseString];
        NSLog(@"respo= %@",respo);

        NSURL *url = [NSURL URLWithString:@"http://api.dbcapi.me/api/captcha"];

        ASIFormDataRequest *request=[ASIFormDataRequest requestWithURL:url];

        [request setPostValue:@"YourDeathByCaptchaUserName" forKey:@"username"];
        [request setPostValue:@"YourDeathByCaptchaPassword!@#$" forKey:@"password"];
        [request setPostValue:prefixBase64Data forKey:@"captchafile"];

        [request setRequestMethod:@"POST"];
        [request startSynchronous];

        NSError *error=[request error];

        if(!error){

            // Here we will get "Text" data from Captcha Image in "response"
            NSString *response=[request responseString];
            NSLog(@"response= %@",response);
            }
        else{
            NSLog(@"error= %@",error);
            }

        }
    else{
        NSLog(@"err= %@",err);
        }

    return response;
}

That's it. DONE

like image 1
NSExpression Avatar answered Sep 20 '22 22:09

NSExpression