12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091 |
- (function() {
- var pickSuccess;
- // 弹出选择照片方式
- function showActionSheet() {
- plus.nativeUI.actionSheet({
- cancel: "取消",
- buttons: [{
- title: "从相册选择"
- }, {
- title: "拍照"
- }]
- },
- function(event) {
- if (event.index == 1) {
- getGallery();
- } else if (event.index == 2) {
- getCamera();
- }
- }
- );
- };
- //获取相册
- function getGallery() {
- plus.gallery.pick(function(cbFile) { //scb:SuccessCallBack
- handlePickSucc(cbFile);
- }, function(ecb) { //ecb:ErrorCallBack
- }, {
- filename: "_doc/gallery/",
- filter: "image"
- });
- }
-
- function handlePickSucc(srcUrl) {
- var lastIdx = srcUrl.lastIndexOf("/"),
- imgName = srcUrl;
- if(lastIdx>-1) {
- imgName = srcUrl.slice(lastIdx+1);
- }
- plus.zip.compressImage({
- src: srcUrl,
- dst: "_doc/compressImg/"+imgName,
- quality: 80,
- overwrite: true
- }, function(succ) {
- var urlSucc = succ.target;
- // var size = succ.size;
- // var width = succ.width;
- // var height = succ.height;
- if(pickSuccess && $.isFunction(pickSuccess)) {
- pickSuccess(urlSucc);
- }
-
- }, function(err) {
- mui.toast("压缩失败: " + err.message);
- });
-
- }
-
- // 调用系统摄像头
- function getCamera() {
- var cmr = plus.camera.getCamera();
- cmr.captureImage(function(path) {
- /**
- * 拍照成功后,图片本保存在本地,这时候我们需要调用本地文件
- * http://www.html5plus.org/doc/zh_cn/io.html#plus.io.resolveLocalFileSystemURL
- */
- plus.io.resolveLocalFileSystemURL(path, function(entry) {
- /*
- * 将获取目录路径转换为本地路径URL地址
- * http://www.html5plus.org/doc/zh_cn/io.html#plus.io.DirectoryEntry.toLocalURL
- */
- var fileUrl = entry.toLocalURL();
- console.log(fileUrl);
- handlePickSucc(fileUrl);
- });
- }, function(error) {
- if(error.code==11 && error.message == "null") {
- plus.nativeUI.toast("您尚未授权拍照权限,无法使用拍照功能。");
- }
- // mui.toast(error.message)
- }, {
- filename: "_doc/camera/",
- index: 1 //ios指定主摄像头
- });
- }
-
- window.getAutoRecCompressImageLocalPath = function(cb) {
- pickSuccess = cb;
- showActionSheet();
- }
- })();
|