Я пытаюсь нарисовать масштабное фото на холсте. Тем не менее, холст не будет обновляться после того, как я использую объект камеры, чтобы сделать фотографию. Вот мой код камеры:
public openCamera() {
// do we have permission to access the camera?
this.androidPermissions.checkPermission(this.androidPermissions.PERMISSION.CAMERA).then(
result => console.log('openCamera > has camera permission: ', result.hasPermission),
err => this.androidPermissions.requestPermission(this.androidPermissions.PERMISSION.CAMERA)
);
this.androidPermissions.requestPermissions([this.androidPermissions.PERMISSION.CAMERA, this.androidPermissions.PERMISSION.GET_ACCOUNTS]);
const options: CameraOptions = {
quality: 100,
correctOrientation: true,
destinationType: this.camera.DestinationType.FILE_URI,
encodingType: this.camera.EncodingType.JPEG,
mediaType: this.camera.MediaType.PICTURE
}
let file_ext: string = null;
if (options.encodingType == EncodingType.JPEG) {
file_ext = "jpg";
console.log("openCamera > file_ext: ", file_ext);
}
else if (options.encodingType == EncodingType.PNG) {
file_ext = "png";
console.log("openCamera > file_ext: ", file_ext);
}
else {
this.presentAlert("Warning", "Unsupported image encoding type.");
return;
}
// photo already taken?
if (this.photo == null) {
this.photo = new Photo();
this.photo.id = Guid.create().toString();
this.photo.file_extension = file_ext;
}
// image filename: image_id.jpg
const dt: Date = new Date();
let dt_day: string = dt.getDate().toString();
dt_day = dt_day.length == 1 ? "0" + dt_day : dt_day;
let dt_month: string = (dt.getMonth() + 1).toString();
dt_month = dt_month.length == 1 ? "0" + dt_month : dt_month;
const dt_string: string = dt_day + "/" +
dt_month + "/" +
dt.getFullYear() + ", " +
dt.getHours() + ":" +
dt.getMinutes() + ":" +
dt.getSeconds() + "." +
dt.getMilliseconds();
if (this.photo.id == null || this.photo.id == '' || this.photo.id.length == 0) {
this.presentAlert("Error", "image id not set");
return;
}
else if (this.user.id == null || this.user.id == Guid.EMPTY || this.user.id == '' || this.user.id.length == 0) {
this.presentAlert("Error", "user.id not set");
return;
}
else if (dt_string == null || dt_string == '' || dt_string.length == 0) {
this.presentAlert("Error", "dt_string not set");
return;
}
this.photo.taken = dt_string;
const fileName: string = this.photo.id + ".jpg";
this.camera.getPicture(options).then((imageData) => {
// delete previous image
this.filePath.resolveNativePath(imageData)
.then((path) => {
let imagePath = path.substr(0, path.lastIndexOf("/") + 1);
let imageName = path.substring(path.lastIndexOf("/") + 1, path.length);
this.file.moveFile(imagePath, imageName, this.file.dataDirectory, fileName)
.then(newFile => {
this.ngZone.run(() => this.info = "Tap 'Upload' to upload photo");
this.db.addOrUpdatePhoto(this.photo)
.then(() => this.updateCanvas());
})
.catch(err => {
console.log("openCamera: ", err);
})
})
.catch((err) => {
console.log("openCamera: ", err);
})
})
.catch((err) => {
console.log("openCamera: ", err);
});
}
В процессе отладки я написал метод проверки того, что фотография действительно существует после того, как камера сохранила изображение:
public exists() {
try {
this.file.listDir(this.file.dataDirectory, '')
.then(files => {
for (let i = 0; i < files.length; i++) {
if (files[i].name.includes(this.photo.id)) {
files[i].getMetadata((metadata) => {
this.presentAlert("image found", "filename: " + files[i].name + ", size: " + metadata.size + " bytes")
.then(() => this.updateCanvas());
});
}
}
});
}
catch { }
}
ИЯ заметил, что отображение ионного оповещения перед вызовом updateCanvas приводит к правильному отображению фотографии на холсте. Кто-нибудь знает, что проблема может быть здесь?
Кроме того, не уверен, если это уместно, но я использую cordova-plugin-ionic-webview 4.1.1