在我的Ionic应用程序中,我允许用户打开相机,拍摄文字图片并使用OCR API(Tesseract)将其转换为文字(有效)。
我首先要检查是否从桌面使用该应用程序,如果是,我使用文件上传而不是打开相机,效果很好。
当我在仿真器或真实设备上运行android之后尝试使用相机时,相机会打开,我可以拍照,但显示错误(在Emulator中显示,在真实设备中则不是,但它存在于Android Studio中的日志):
cordova.fireDocumentEvent is not a function
TypeError: cordova.fireDocumentEvent is not a function
at <anonymous>:1:9
我不使用科尔多瓦而是电容器,所以我不确定为什么要得到它。
这是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();
});
});
}
}
请帮忙吗?
答案 0 :(得分:0)
这是Beta 22的错误,更新为Beta 24