1

在我的 Ionic 应用程序中,我允许用户打开相机,为文本拍照并使用 OCR API(Tesseract)将其转换为文本(有效)。

我首先检查我是否从桌面使用该应用程序,如果是,我使用文件上传而不是打开相机,并且效果很好。

当我在模拟器或真实设备上运行 android 后尝试相机时,相机会打开,我可以拍照但显示错误(在模拟器中显示,在真实设备中不显示,但在登录中存在安卓工作室):

    cordova.fireDocumentEvent is not a function
TypeError: cordova.fireDocumentEvent is not a function
    at <anonymous>:1:9

我不使用cordova,而是使用电容器,所以我不知道为什么会这样。

这是来自 Android Studio 日志的错误:

E/Capacitor/Plugin/Console: {}
V/Capacitor: callback: -1, pluginId: Console, methodName: log, methodData: {"level":"error","message":"{}"}
E/Capacitor/Plugin/Console: {}
E/Capacitor: JavaScript Error: {"type":"js.error","error":{"message":"Uncaught TypeError: cordova.fireDocumentEvent is not a function","url":"http://localhost/users","line":1,"col":9,"errorObject":"{}"}}
D/EGL_emulation: eglMakeCurrent: 0xc73b15a0: ver 3 0 (tinfo 0xcbb8b7e0)

V/Capacitor: callback: -1, pluginId: Console, methodName: log, methodData: {"level":"error","message":"ERROR {\"isTrusted\":true}"}
E/Capacitor/Plugin/Console: ERROR {"isTrusted":true}

这是组件 HTML:

<div>
  <ion-button color="secondary" (click)="onPickImage()" *ngIf="!selectedImage" expand="block">
    <ion-icon name="camera" slot="start"></ion-icon>
    <ion-label>Open Camera</ion-label>
  </ion-button>
</div>
<input
  type="file"
  *ngIf="usePicker"
  #filepicker
  (change)="onFileChosen($event)"
/>
<ion-button
    (click)="recognizeImage()"
    expand="block"
    [disabled]="!selectedImage"
    color="tertiary"
    >Recognize Text</ion-button
  >
<ion-card *ngIf="imageText">
    <ion-card-header>Image Text</ion-card-header>
    <ion-card-content>
      ID: {{ this.id }}, Name: {{ this.name }}
    </ion-card-content>
  </ion-card>

这是组件 TS 文件:

import {
  Component,
  OnInit,
  Output,
  EventEmitter,
  ElementRef,
  ViewChild
} from '@angular/core';
import {
  Plugins,
  Capacitor,
  CameraSource,
  CameraResultType
} from '@capacitor/core';
import { Platform, LoadingController } from '@ionic/angular';
import * as Tesseract from 'tesseract.js';
import { UsersService } from '../users.service';

@Component({
  selector: 'app-image-picker',
  templateUrl: './image-picker.component.html',
  styleUrls: ['./image-picker.component.scss']
})
export class ImagePickerComponent implements OnInit {
  @ViewChild('filepicker') filepickerRef: ElementRef<HTMLInputElement>;
  @Output() imagePick = new EventEmitter<string | File>();
  selectedImage: string;
  usePicker = false;
  imageText: string;
  id: string;
  name: string;

  constructor(
    private platform: Platform,
    private loadingCtrl: LoadingController,
    private usersService: UsersService
  ) {}

  ngOnInit() {
    console.log('Mobile:', this.platform.is('mobile'));
    console.log('Hybrid:', this.platform.is('hybrid'));
    console.log('ios:', this.platform.is('ios'));
    console.log('Android:', this.platform.is('android'));
    console.log('Desktop:', this.platform.is('desktop'));
    if (
      (this.platform.is('mobile') && !this.platform.is('hybrid')) ||
      this.platform.is('desktop')
    ) {
      this.usePicker = true;
    }
  }

  onPickImage() {
    if (!Capacitor.isPluginAvailable('Camera') || this.usePicker) {
      this.filepickerRef.nativeElement.click();
      return;
    }
    Plugins.Camera.getPhoto({
      quality: 50,
      source: CameraSource.Camera,
      correctOrientation: true,
      width: 200,
      resultType: CameraResultType.Base64
    })
      .then(image => {
        this.selectedImage = image.base64String;
        this.imagePick.emit(image.base64String);
      })
      .catch(error => {
        console.log(error);
        return false;
      });
  }

  onFileChosen(event: Event) {
    const pickedFile = (event.target as HTMLInputElement).files[0];
    if (!pickedFile) {
      return;
    }
    const fr = new FileReader();
    fr.onload = () => {
      const dataUrl = fr.result.toString();
      this.selectedImage = dataUrl;
      this.imagePick.emit(pickedFile);
    };
    fr.readAsDataURL(pickedFile);
  }

  recognizeImage() {
    this.loadingCtrl
      .create({
        keyboardClose: true,
        message: 'Analysing Sticker...'
      })
      .then(loadingEl => {
        loadingEl.present();
        Tesseract.recognize(this.selectedImage)
          .progress(message => {
            console.log(message);
          })
          .catch(error => {
            console.error(error);
            loadingEl.dismiss();
          })
          .then(result => {
            this.imageText = result.text;
            console.log(this.imageText);
          })
          .finally(resultOrError => {
            const splitString = this.imageText.split(/\n/ig);
            console.log(splitString);
            this.id = splitString[1];
            this.name = splitString[0];
            this.usersService.formIdFromOCR = splitString[1];
            this.usersService.formNameFromOCR = splitString[0];
            loadingEl.dismiss();
          });
      });
  }
}

请问有什么帮助吗?

4

1 回答 1

0

这是 beta 22 上的错误,更新到 beta 24

于 2019-05-15T08:08:45.350 回答