經過了一些研究發現V9版本的model-driven app已經有了很大的提升,可以實現很多可能,比如本篇要分享的在app端調用相機拍照的功能。
我在Ribbon上加了個按鈕,按鈕上綁定了名叫CapturePhoto的Event
看下CapturePhoto中的代碼,
function CapturePhoto(clientContext) {
debugger;
var client = Xrm.Utility.getGlobalContext().client;
if (client.getClient() == 'Mobile') {
var imageOptions = {
allowEdit: true,
height: 250,
width: 400,
preferFrontCamera: true,
quality: 100
};
Xrm.Device.captureImage(imageOptions).then(
function success(result) {
// perform operations on the captured image
var data =
{
"entityimage": result.fileContent
}
var alertStrings = { confirmButtonLabel: 'Yes', text: 'fileName:'+result.fileName+'fileSize:'+result.fileSize };
var alertOptions = { height: 120, width: 260 };
Xrm.Navigation.openAlertDialog(alertStrings, alertOptions);
// Xrm.WebApi.updateRecord("ubr_lost", clientContext.data.entity.getId().replace('{', '').replace('}', ''), data).then(
// function success(result) {
// var alertStrings = { confirmButtonLabel: 'Yes', text: 'Lost Image updated.' };
// var alertOptions = { height: 120, width: 260 };
// Xrm.Navigation.openAlertDialog(alertStrings, alertOptions).then(
// function success() {
// // perform operations on alert dialog close
// },
// function (error) {
// console.log(error.message);
// // handle error conditions
// }
// );
// },
// function (error) {
// Xrm.Utility.alertDialog("Error while updating Account Image : " + error.message, null);
// // handle error conditions
// }
// );
},
function (error) {
console.log(error.message);
// handle error conditions
}
);
}
}
這裏僅作爲演示,我把獲取到的照片的基本信息彈框出來了,當然獲取到照片後可以實現很多功能場景,比如我代碼中屏蔽掉的那段把照片作爲記錄的entityimage更新到記錄中,也可以調用OCR的服務接口來進行照片識別,比如名片、證件等的識別。