我通过这段代码得到的服务类图像我如何通过图像的功能离子2
this.cameraService.getImage(this.width, this.height, this.quality).subscribe(data => this.image = data, error =>
我会想这个代码传递给getVision之一()函数使我能够使用Google API.May我知道我该怎么做?我试图声明一个字符串变量,并尝试把上面的代码的变量中但它不work.Below是代码
Camera.TS class
export class CameraPage {
width: number;
height: number;
cropper: Cropper;*/
image:string;
width:number = 500;
height:number = 500;
quality:number = 90;
picture:string;
labels: Array<any> = [];
//translation
scanning: Array<any> = [];
choseLang: boolean = false;
loading: boolean = false;
constructor(public navCtrl: NavController, public navParams: NavParams,public testService: TestService,public cameraService: CameraService,public toastCtrl: ToastController) {
}
addPhoto(){ //take picture & return image ***
this.cameraService.getImage(this.width, this.height, this.quality).subscribe(data => this.image = data, error =>
{
this.getVision(this.image);
// Toast errot and return DEFAULT_PHOTO from Constants
this.toast(error);
});
}
toast(message: string) {
let toast = this.toastCtrl.create({
message: message,
duration: 2500,
showCloseButton: false
});
toast.present();
}
getVision(image64:string) {
this.testService.getVisionLabels(image64:string)
.subscribe((sub) => {
this.labels = sub.responses[0].textAnnotations;
this.getText();
});
}
getText() {
this.labels.forEach((label) => {
let translation = {search: label.description, result: ''};
console.log(label.description);
});
}
}
相机服务类
export class CameraService {
public base64Image: string;
constructor(public platform: Platform, public alertCtrl: AlertController, public modalCtrl: ModalController, private http: Http) {
}
getImage(width: number, height: number, quality: number) {
return Observable.create(observer => {
//Set default options for taking an image with the camera
let imageOptions: any = {
quality: quality,
destinationType: Camera.DestinationType.DATA_URL,
sourceType: Camera.PictureSourceType.CAMERA,
encodingType: Camera.EncodingType.JPEG,
correctOrientation: 1,
saveToPhotoAlbum: false,
mediaType: Camera.MediaType.PICTURE,
cameraDirection: 1
};
let selectAlert = this.alertCtrl.create({
title: 'Let\'s add a picture!',
message: "Select how you would like to add the picture",
enableBackdropDismiss: false,
buttons: [{
text: 'Albums',
handler: data => {
//Change sourceType to PHOTOLIBRARY
imageOptions.sourceType = Camera.PictureSourceType.PHOTOLIBRARY;
selectAlert.dismiss();
}
}, {
text: 'Camera',
handler: data => {
selectAlert.dismiss();
}
}]
});
selectAlert.onDidDismiss(() => {
this.getCameraImage(imageOptions).subscribe(image => { //image options are either album or camera**
let cropModal = this.modalCtrl.create(ScannerPage, { "imageBase64": image, "width": 500, "height": 500 });
cropModal.onDidDismiss((croppedImage: any) => {
if (!croppedImage)
observer.error("Canceled while cropping.")
else {
observer.next(croppedImage);
observer.complete();
}
});
cropModal.present();
}, error => observer.error(error));
});
selectAlert.present();
});
}
getCameraImage(options: any) { //get base64 image
return Observable.create(observer => {
this.platform.ready().then(() => {
Camera.getPicture(options).then((imageData: any) => {
// imageData is a base64 encoded string as per options set above
let base64Image: string = "data:image/jpeg;base64," + imageData;
observer.next(base64Image);
observer.complete();
}, error => {
observer.error(error);
});
});
});
}
}
如果我理解正确,服务'this.cameraService.getImage(...)'将返回图像。而这张图片就是你想传递给'getVision()'的东西。如果在服务中出现错误,请执行其他操作,对吗? –
是的,你是对的 –
在这种情况下,我认为你需要将'getVision()'调用从错误回调转换为'subscribe()'。我会回答。 –