roblav96 / nativescript-tesseract-ios

MIT License
0 stars 0 forks source link

For inspiration #1

Open EddyVerbruggen opened 8 years ago

EddyVerbruggen commented 8 years ago

Just FYI, in case you didn't see it before: https://github.com/EddyVerbruggen/nativescript-ocr

roblav96 commented 8 years ago

@EddyVerbruggen Hey, thanks! I actually came across your repo after I made this repo, but your example on how to use the delegate was extremely helpful! So helpful, I wrote up a tutorial in the ios-runtime docs on how to create a delegate in typescript. https://github.com/NativeScript/ios-runtime-docs/pull/56/files So I certainly thank you for that.

This repo's only purpose is to install the pod files:

pod 'SSZipArchive'
pod 'GPUImage'
pod 'TesseractOCRiOS', :git => 'https://github.com/gali8/Tesseract-OCR-iOS.git'

so now in my project I can do things like so:

checkTessData() {
    let tessdata: Folder = knownFolders.temp().getFolder('tessdata')
    tessdata.clear().then(() => {
        return this.downloadData()
    }, function(error) {
        throw error
    }).then((path: NSURL) => {
        return this.unzipData(path)
    }).then((dir: string) => {

        let delegate: G8TesseractDelegateImpl = G8TesseractDelegateImpl.new()
        Tesseract.tess = G8Tesseract.new()
        Tesseract.tess.initWithLanguageConfigDictionaryConfigFileNamesCachesRelatedDataPathEngineMode(
            'eng',
            null,
            null,
            '', // empty path cause it loads from Caches directory
            G8OCREngineMode.TesseractOnly
        )
        Tesseract.tess.delegate = delegate

    }).catch(function(error) {
        global.tnsconsole.error('error', error)
    })
}

Now I can download compressed zips for different languages and use GPUImage for blazing fast ORC results.

Thanks again for the help you've provided mate! 🍰

Edit:

Here's the download and unzip methods incase you wanted to know:

    downloadData(): Promise<NSURL> {
        return new Promise(function(resolve, reject) {
            let file: string = 'Archive'
            let config: NSURLSessionConfiguration = NSURLSessionConfiguration.defaultSessionConfiguration()
            let manager: AFURLSessionManager = AFURLSessionManager.new().initWithSessionConfiguration(config)
            let url: NSURL = NSURL.URLWithString(global.ip + ':1331/tessdata/' + file + '.zip')
            let request: NSURLRequest = NSURLRequest.requestWithURL(url)
            let downloadTask: NSURLSessionDownloadTask = manager.downloadTaskWithRequestProgressDestinationCompletionHandler(
                request,
                (progress: NSProgress) => {
                    let value: number = Math.round((progress.completedUnitCount / progress.totalUnitCount) * 50)
                    Tesseract.progSubject.next(value)
                },
                (targetPath: NSURL, response: NSURLResponse): NSURL => {
                    let tessdata: Folder = knownFolders.temp().getFolder('tessdata')
                    let nsurl: NSURL = NSURL.fileURLWithPath(tessdata.path + '/' + file + '.zip')
                    return nsurl
                },
                (response: NSURLResponse, filePath: NSURL, error: NSError) => {
                    Tesseract.progSubject.next(50)
                    if (error) {
                        if (error.description) {
                            global.tnsconsole.error('downloadData > error.description', error.description)
                            reject(error.description)
                        } else {
                            global.tnsconsole.error('downloadData > error', error)
                            reject(error)
                        }
                    } else {
                        resolve(filePath)
                    }
                }
            )
            downloadTask.resume()
        })
    }

    unzipData(filePath: NSURL): Promise<string> {
        return new Promise(function(resolve, reject) {
            let thePath: string = filePath.absoluteString.substring(7)
            let theDir: string = thePath.substring(0, thePath.lastIndexOf('/'))

            SSZipArchive.unzipFileAtPathToDestinationProgressHandlerCompletionHandler(
                thePath,
                theDir,
                function progressHandler(entry: string, zipInfo: unz_file_info, entryNumber: number, total: number) {
                    let value: number = Math.round((entryNumber / total) * 50) + 50
                    Tesseract.progSubject.next(value)
                },
                function completionHandler(path: string, succeeded: boolean, error: NSError) {
                    Tesseract.progSubject.next(100)
                    if (error) {
                        if (error.description) {
                            global.tnsconsole.error('unzipData > error.description', error.description)
                            reject(error.description)
                        } else {
                            global.tnsconsole.error('unzipData > error', error)
                            reject(error)
                        }
                    } else if (succeeded) {
                        resolve(theDir)
                    } else {
                        reject('!succeeded')
                    }
                }
            )
        })
    }