Browse Source

works on iOS Safari

pull/34/head
Kevin Kwok 8 years ago
parent
commit
2b141f8aa5
  1. 3
      examples/file-input/demo.html
  2. 2
      src/browser/index.js
  3. 20
      src/browser/lang.js
  4. 3
      src/common/worker.js

3
examples/file-input/demo.html

@ -122,7 +122,10 @@ function recognizeFile(file){
<option value='ukr' > Ukrainian </option> <option value='ukr' > Ukrainian </option>
<option value='vie' > Vietnamese </option> <option value='vie' > Vietnamese </option>
</select> </select>
<button onclick="recognizeFile('../node/cosmic.png')">Sample Image</button>
<input type="file" onchange="recognizeFile(window.lastFile=this.files[0])"> <input type="file" onchange="recognizeFile(window.lastFile=this.files[0])">
<div id="log"></div> <div id="log"></div>

2
src/browser/index.js

@ -6,7 +6,7 @@ var defaultOptions = {
if (process.env.NODE_ENV === "development") { if (process.env.NODE_ENV === "development") {
console.debug('Using Development Configuration') console.debug('Using Development Configuration')
defaultOptions.workerPath = location.protocol + '//' + location.host + '/dist/worker.dev.js' defaultOptions.workerPath = location.protocol + '//' + location.host + '/dist/worker.dev.js?nocache=' + Math.random().toString(36).slice(3)
}else{ }else{
var version = require('../../package.json').version; var version = require('../../package.json').version;
defaultOptions.workerPath = 'https://cdn.rawgit.com/naptha/tesseract.js/' + version + '/dist/worker.js' defaultOptions.workerPath = 'https://cdn.rawgit.com/naptha/tesseract.js/' + version + '/dist/worker.js'

20
src/browser/lang.js

@ -1,11 +1,18 @@
const leveljs = require('level-js') const leveljs = require('level-js')
var db = typeof indexedDB === 'undefined' ? { open: (_, cb) => cb(true) } : leveljs('./tessdata2')
// something about trying to store these language files in indexedDB
// causes iOS Safari to crash
var iOS = /iPad|iPhone|iPod/.test(navigator.userAgent);
var noIDB = typeof indexedDB === 'undefined' || iOS;
var db = noIDB ? { open: (_, cb) => cb(true) } : leveljs('./tessdata2')
var langdata = require('../common/langdata.json') var langdata = require('../common/langdata.json')
module.exports = function getLanguageData(req, res, cb){ module.exports = function getLanguageData(req, res, cb){
var lang = req.options.lang; var lang = req.options.lang;
function saveDataFile(data){ function saveDataFile(data){
db.put(lang, data, err => console.log('cached', lang, err)) db.put(lang, data, err => console.log('cached', lang, err))
cb(data) cb(data)
@ -45,15 +52,20 @@ function fetchLanguageData(req, res, cb){
xhr.onload = e => { xhr.onload = e => {
if (!(xhr.status == 200 || (xhr.status == 0 && xhr.response))) return res.reject('Error downloading language ' + url); if (!(xhr.status == 200 || (xhr.status == 0 && xhr.response))) return res.reject('Error downloading language ' + url);
res.progress({ status: 'unzipping ' + langfile }) res.progress({ status: 'unzipping ' + langfile, progress: 0 })
// in case the gzips are already ungzipped or extra gzipped // in case the gzips are already ungzipped or extra gzipped
var response = new Uint8Array(xhr.response) var response = new Uint8Array(xhr.response)
try { try {
while(response[0] == 0x1f && response[1] == 0x8b) response = ungzip(response); var n = 2;
while(response[0] == 0x1f && response[1] == 0x8b){
response = ungzip(response);
res.progress({ status: 'unzipping ' + langfile, progress: 1 - 1 / (n++) })
}
} catch (err) { } catch (err) {
return res.reject('Error unzipping language file ' + langfile + '\n' + err.message) return res.reject('Error unzipping language file ' + langfile + '\n' + err.message)
} }
res.progress({ status: 'unzipping ' + langfile, progress: 1 })
cb(response) cb(response)
} }

3
src/common/worker.js

@ -85,9 +85,10 @@ function loadLanguage(req, res, cb){
if(lang in Module._loadedLanguages) return cb(); if(lang in Module._loadedLanguages) return cb();
adapter.getLanguageData(req, res, function(data){ adapter.getLanguageData(req, res, function(data){
res.progress({ status: 'loading ' + lang + '.traineddata', progress: 0 })
Module.FS_createDataFile('tessdata', lang + ".traineddata", data, true, false); Module.FS_createDataFile('tessdata', lang + ".traineddata", data, true, false);
res.progress({ status: 'loading ' + lang + '.traineddata', progress: 1 })
Module._loadedLanguages[lang] = true; Module._loadedLanguages[lang] = true;
res.progress({ status: 'loading ' + lang + '.traineddata', progress: 1 })
cb() cb()
}) })
} }

Loading…
Cancel
Save