我通過這段代碼得到的服務類圖像我如何通過圖像的功能離子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()'。我會回答。 –