带有Firebase ML套件的Nativescript Js OCR

时间:2018-11-13 15:52:53

标签: javascript firebase ocr nativescript firebase-mlkit

我正在尝试使用NativeScript JS和firebase ML套件(在设备上)开发OCR应用程序。我做了下一页的步骤,没有任何问题:

插件文档: https://market.nativescript.org/plugins/nativescript-plugin-firebase#start-up-wiring

但是当我在寻找文本识别部分的javascript文档时,我只发现了所有打字稿的版本(我不知道如何添加。

ML套件文档: https://github.com/EddyVerbruggen/nativescript-plugin-firebase/blob/master/docs/ML_KIT.md

我的home.page.xml中包含以下代码:

<Page 
navigatingTo="onNavigatingTo" 
xmlns:FirebaseMLKitTextRecognition="nativescript-plugin-firebase/mlkit/textrecognition">

  <FirebaseMLKitTextRecognition:MLKitTextRecognition
      class="my-class"
      width="260"
      height="380"
      processEveryNthFrame="3"
      preferFrontCamera="false"
      pause="{{ pause }}"
      scanResult="onTextRecognitionResult" />

</Page>

它确实有效。在这种情况下,我可以看到此home-page.js代码激活了设备上的相机:

const HomeViewModel = require("./home-view-model");

function onNavigatingTo(args) {
    const page = args.object;

    //TODO

    page.bindingContext = new HomeViewModel();
}

exports.onNavigatingTo = onNavigatingTo;

我在MLkit文档中找到了以下代码:

import { MLKitRecognizeTextResult } from "nativescript-plugin-firebase/mlkit/textrecognition";
const firebase = require("nativescript-plugin-firebase");

firebase.mlkit.textrecognition.recognizeTextOnDevice({
  image: imageSource // a NativeScript Image or ImageSource, see the demo for examples
}).then((result: MLKitRecognizeTextResult) => { // just look at this type to see what else is returned
  console.log(result.text ? result.text : "");
}).catch(errorMessage => console.log("ML Kit error: " + errorMessage));

但是该代码是打字稿,并且我有错误:

Successfully synced application org.nativescript.gocr on device ECPBBCF741803200.
ActivityManager: Start proc 16965:org.nativescript.gocr/u0a149 for activity org.nativescript.gocr/com.tns.NativeScriptActivity
ActivityManager: ActivityManagerService,attachApplication,callingPid = 16965
ActivityManager: New app record ProcessRecord{f7395dd 16965:org.nativescript.gocr/u0a149} thread=android.os.BinderProxy@11ce5552 pid=16965
ActivityManager: getContentProviderImpl: from caller=android.app.ApplicationThreadProxy@22b46920 (pid=16965, userId=0) to get content provider settings cpr=ContentProviderRecord{35d191e9 u0 com.android.providers.settings/.SettingsProvider}
D/ActivityThread(16965): hoder:android.app.IActivityManager$ContentProviderHolder@23a23c91,provider,holder.Provider:android.content.ContentProviderProxy@3ae27f6
ActivityManager: getContentProviderImpl: from caller=android.app.ApplicationThreadProxy@dece411 (pid=16965, userId=0) to get content provider com.google.android.gms.phenotype cpr=ContentProviderRecord{16a60ce8 u0 com.google.android.gms/.phenotype.provider.ConfigurationProvider}
D/ActivityThread(16965): hoder:android.app.IActivityManager$ContentProviderHolder@2b62a1df,provider,holder.Provider:android.content.ContentProviderProxy@17cb0e2c
ActivityManager: publishContentProviders: from caller=android.app.ApplicationThreadProxy@11807976 (pid=16965)
JS: firebase.init done
System.err: java.lang.RuntimeException: Unable to start activity ComponentInfo{org.nativescript.gocr/com.tns.NativeScriptActivity}: com.tns.NativeScriptException:
System.err: Calling js method onCreate failed
System.err: Error: Building UI from XML. @file:///app/app-root.xml:1:1
System.err:  > Unexpected token {
System.err: File: "file:///data/data/org.nativescript.gocr/files/app/tns_modules/tns-core-modules/ui/builder/builder.js, line: 244, column: 20
System.err: StackTrace:
System.err:     Frame: function:'ScopeError', file:'file:///data/data/org.nativescript.gocr/files/app/tns_modules/tns-core-modules/utils/debug.js', line: 63, column: 24
System.err:     Frame: function:'SourceError', file:'file:///data/data/org.nativescript.gocr/files/app/tns_modules/tns-core-modules/utils/debug.js', line: 74, column: 23
System.err:     Frame: function:'', file:'file:///data/data/org.nativescript.gocr/files/app/tns_modules/tns-core-modules/ui/builder/builder.js', line: 263, column: 17
System.err:     Frame: function:'', file:'file:///data/data/org.nativescript.gocr/files/app/tns_modules/tns-core-modules/ui/builder/builder.js', line: 244, column: 33
System.err:     Frame: function:'', file:'file:///data/data/org.nativescript.gocr/files/app/tns_modules/tns-core-modules/xml/xml.js', line: 148, column: 13
System.err:     Frame: function:'EasySAXParser.parse', file:'file:///data/data/org.nativescript.gocr/files/app/tns_modules/tns-core-modules/js-libs/easysax/easysax.js', line: 751, column: 23
System.err:     Frame: function:'XmlParser.parse', file:'file:///data/data/org.nativescript.gocr/files/app/tns_modules/tns-core-modules/xml/xml.js', line: 195, column: 22
System.err:     Frame: function:'XmlStringParser.parse', file:'file:///data/data/org.nativescript.gocr/files/app/tns_modules/tns-core-modules/ui/builder/builder.js', line: 250, column: 27
System.err:     Frame: function:'parseInternal', file:'file:///data/data/org.nativescript.gocr/files/app/tns_modules/tns-core-modules/ui/builder/builder.js', line: 212, column: 11
System.err:     Frame: function:'loadInternal', file:'file:///data/data/org.nativescript.gocr/files/app/tns_modules/tns-core-modules/ui/builder/builder.js', line: 117, column: 27
System.err:     Frame: function:'loadPage', file:'file:///data/data/org.nativescript.gocr/files/app/tns_modules/tns-core-modules/ui/builder/builder.js', line: 53, column: 27
System.err:     Frame: function:'', file:'file:///data/data/org.nativescript.gocr/files/app/tns_modules/tns-core-modules/ui/builder/builder.js', line: 72, column: 12
System.err:     Frame: function:'', file:'file:///data/data/org.nativescript.gocr/files/app/tns_modules/tns-core-modules/ui/builder/builder.js', line: 86, column: 20
System.err:     Frame: function:'ActivityCallbacksImplementation.setActivityContent', file:'file:///data/data/org.nativescript.gocr/files/app/tns_modules/tns-core-modules/ui/frame/frame.js', line: 899, column: 50
System.err:     Frame: function:'ActivityCallbacksImplementation.onCreate', file:'file:///data/data/org.nativescript.gocr/files/app/tns_modules/tns-core-modules/ui/frame/frame.js', line: 751, column: 14
System.err:     Frame: function:'NativeScriptActivity.onCreate', file:'file:///data/data/org.nativescript.gocr/files/app/tns_modules/tns-core-modules/ui/frame/activity.js', line: 20, column: 25
System.err:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2641)
System.err:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2721)
System.err:     at android.app.ActivityThread.access$1200(ActivityThread.java:186)
System.err:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1532)
System.err:     at android.os.Handler.dispatchMessage(Handler.java:111)
System.err:     at android.os.Looper.loop(Looper.java:194)
System.err:     at android.app.ActivityThread.main(ActivityThread.java:5931)
System.err:     at java.lang.reflect.Method.invoke(Native Method)
System.err:     at java.lang.reflect.Method.invoke(Method.java:372)
System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:987)
System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:782)
System.err: Caused by: com.tns.NativeScriptException:
System.err: Calling js method onCreate failed
System.err: Error: Building UI from XML. @file:///app/app-root.xml:1:1
System.err:  > Unexpected token {
System.err: File: "file:///data/data/org.nativescript.gocr/files/app/tns_modules/tns-core-modules/ui/builder/builder.js, line: 244, column: 20
System.err: StackTrace:
System.err:     Frame: function:'ScopeError', file:'file:///data/data/org.nativescript.gocr/files/app/tns_modules/tns-core-modules/utils/debug.js', line: 63, column: 24
System.err:     Frame: function:'SourceError', file:'file:///data/data/org.nativescript.gocr/files/app/tns_modules/tns-core-modules/utils/debug.js', line: 74, column: 23
System.err:     Frame: function:'', file:'file:///data/data/org.nativescript.gocr/files/app/tns_modules/tns-core-modules/ui/builder/builder.js', line: 263, column: 17
System.err:     Frame: function:'', file:'file:///data/data/org.nativescript.gocr/files/app/tns_modules/tns-core-modules/ui/builder/builder.js', line: 244, column: 33
System.err:     Frame: function:'', file:'file:///data/data/org.nativescript.gocr/files/app/tns_modules/tns-core-modules/xml/xml.js', line: 148, column: 13
System.err:     Frame: function:'EasySAXParser.parse', file:'file:///data/data/org.nativescript.gocr/files/app/tns_modules/tns-core-modules/js-libs/easysax/easysax.js', line: 751, column: 23
System.err:     Frame: function:'XmlParser.parse', file:'file:///data/data/org.nativescript.gocr/files/app/tns_modules/tns-core-modules/xml/xml.js', line: 195, column: 22
System.err:     Frame: function:'XmlStringParser.parse', file:'file:///data/data/org.nativescript.gocr/files/app/tns_modules/tns-core-modules/ui/builder/builder.js', line: 250, column: 27
System.err:     Frame: function:'parseInternal', file:'file:///data/data/org.nativescript.gocr/files/app/tns_modules/tns-core-modules/ui/builder/builder.js', line: 212, column: 11
System.err:     Frame: function:'loadInternal', file:'file:///data/data/org.nativescript.gocr/files/app/tns_modules/tns-core-modules/ui/builder/builder.js', line: 117, column: 27
System.err:     Frame: function:'loadPage', file:'file:///data/data/org.nativescript.gocr/files/app/tns_modules/tns-core-modules/ui/builder/builder.js', line: 53, column: 27
System.err:     Frame: function:'', file:'file:///data/data/org.nativescript.gocr/files/app/tns_modules/tns-core-modules/ui/builder/builder.js', line: 72, column: 12
System.err:     Frame: function:'', file:'file:///data/data/org.nativescript.gocr/files/app/tns_modules/tns-core-modules/ui/builder/builder.js', line: 86, column: 20
System.err:     Frame: function:'ActivityCallbacksImplementation.setActivityContent', file:'file:///data/data/org.nativescript.gocr/files/app/tns_modules/tns-core-modules/ui/frame/frame.js', line: 899, column: 50
System.err:     Frame: function:'ActivityCallbacksImplementation.onCreate', file:'file:///data/data/org.nativescript.gocr/files/app/tns_modules/tns-core-modules/ui/frame/frame.js', line: 751, column: 14
System.err:     Frame: function:'NativeScriptActivity.onCreate', file:'file:///data/data/org.nativescript.gocr/files/app/tns_modules/tns-core-modules/ui/frame/activity.js', line: 20, column: 25
System.err:     at com.tns.Runtime.callJSMethodNative(Native Method)
System.err:     at com.tns.Runtime.dispatchCallJSMethodNative(Runtime.java:1116)
System.err:     at com.tns.Runtime.callJSMethodImpl(Runtime.java:996)
System.err:     at com.tns.Runtime.callJSMethod(Runtime.java:983)
System.err:     at com.tns.Runtime.callJSMethod(Runtime.java:967)
System.err:     at com.tns.Runtime.callJSMethod(Runtime.java:959)
System.err:     at com.tns.NativeScriptActivity.onCreate(NativeScriptActivity.java:18)
System.err:     at android.app.Activity.performCreate(Activity.java:6151)
System.err:     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1112)
System.err:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2588)
System.err:     ... 10 more
ActivityManager: Process org.nativescript.gocr (pid 16965) has died
ActivityManager: SVC-handleAppDiedLocked: app = ProcessRecord{f7395dd 16965:org.nativescript.gocr/u0a149}, app.pid = 16965
ActivityManager: cleanUpApplicationRecord -- 16965

在哪里可以找到它的javascript版本?或如何将该代码转换为javascript?可能我做错了什么?

1 个答案:

答案 0 :(得分:1)

TypeScript只是JavaScript的超集,如果您消除了大多数应该可以为您提供JavaScript语法的类型。

firebase.mlkit.textrecognition.recognizeTextOnDevice({
  image: imageSource 
}).then((result) => { 
  console.log(result.text ? result.text : "");
}).catch(errorMessage => console.log("ML Kit error: " + errorMessage));

充分利用eslint插件,以便您的IDE可以指导您使用语法。