android相册拍照剪切上传封装,安卓,图片裁剪上传真机测试好使,打包后显示:无法保存裁剪的图像...

var IMAGE_UNSPECIFIED = "image/*";

var PHOTOZOOM = 2; // 获取完图片返回key

var PHOTOLAT = 1; // 剪裁完毕后返回key

var main;

var Intent;

var MediaStore;

var File;

var Uri;

var wt;

var task;

var outPutPath;//图片路径

//临时的服务区位置

// 上传文件

function upload(fullurl) {

if(!fullurl){

return;

}

document.getElementById('image-list').innerHTML = ''%20+%20fullurl%20+%20'';

wt = plus.nativeUI.showWaiting();

task = plus.uploader.createUpload(GV['URL']['AJAX'] + 'huodongtupian.php', //服务端的URL

{

method: "POST"

},

function(t, status) { //上传完成

if (status == 200) {

//到时候把t.responseText解析成json就好

var dt = JSON.parse(t.responseText);

if (dt.error == 0) {

// sdk.obj('authorimg').src=dt.data.avatar;

// mui.fire(plus.webview.getWebviewById('person/index.html'),'selfCallBack',null);

wt.close();

gid('fenmian').value = dt.files;

plus.nativeUI.toast('上传成功');

}

} else {

plus.nativeUI.toast('上传失败');

wt.close();

}

}

);

task.addFile(fullurl, {

key: 'avatar'

});

// task.addData("user_logo",global.getStorage('user_logo'));

task.start();

}

// 从相册中选择图片,并裁剪

function galleryImg() {

var intent = new Intent(Intent.ACTION_PICK, null);

intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, IMAGE_UNSPECIFIED);

console.log(intent);

main.startActivityForResult(intent, PHOTOZOOM);

//

main.onActivityResult = function(requestCode, resultCode, data) {

if (PHOTOZOOM == requestCode) {

//输出的文件目录

outPutPath = plus.io.convertLocalFileSystemURL("_downloads/images/5"+parseInt(Math.random()*(99999-10000+1)+1000)+".jpg");

var file = new File(outPutPath);

// 输出目录uri

var outPutUri = Uri.fromFile(file);

plus.android.importClass(data);

var uri = data.getData();

console.log("uri:" + uri);

var cropIntent = new Intent("com.android.camera.action.CROP");

cropIntent.setDataAndType(uri, IMAGE_UNSPECIFIED);

// 截图完毕后 输出目录

cropIntent.putExtra(MediaStore.EXTRA_OUTPUT, outPutUri);

cropIntent.putExtra("crop", "true");

// aspectX aspectY 是宽高的比例

cropIntent.putExtra("aspectX", 40);

cropIntent.putExtra("aspectY", 55);

// outputX outputY 是裁剪图片宽高

cropIntent.putExtra("outputX", 400);

cropIntent.putExtra("outputY", 550);

cropIntent.putExtra("return-data", true);

main.startActivityForResult(cropIntent, PHOTOLAT);

} else if (requestCode == PHOTOLAT) {

// 判断 剪裁完后的图片输出是否存在

// var _file = new File(outPutPath);

// sdk.obj('authorimg').src=outPutPath;

// console.log(outPutPath);

//这里是否存在的判断还是需要的

// var a = _file.exists();

//查看本地是否有缓存图片 进行删除

// _file.remove( function ( entry ) {

// plus.console.log( "Remove succeeded" );

// }, function ( e ) {

// alert( e.message );

// });

//压缩图片

// plus.zip.compressImage({

// src:outPutPath,

// dst:"_doc/header.jpg",

// quality:20

// },

// function() {

// alert("Compress success!");

// },function(error) {

// alert("Compress error!");

// });

//上传头像->然后进行处理,上传完毕之后 需要删除头像图片

// 判断 剪裁完后的图片输出是否存在

var _file = new File(outPutPath);

var a = _file.exists();

if(a){

upload(outPutPath);

}else{

mui.toast('上传失败')

}

}

};

}

//拍照获取

function getImage() {

//开始拍照

var cmr = plus.camera.getCamera();

cmr.captureImage(function(p) {

//拍照成功调用的函数

plus.io.resolveLocalFileSystemURL(p, function(entry) {

// upload(entry.toLocalURL());

// plus.zip.compressImage({

// src:entry.toLocalURL(),

// dst:"_doc/a.jpg",

// clip:{top:"25%",left:"25%",width:"50%",height:"50%"} // 裁剪图片中心区域

// },

// function() {

// alert("Compress success!");

// },function(error) {

// alert("Compress error!");

// });

//裁剪

outPutPath = plus.io.convertLocalFileSystemURL("_downloads/images/5"+parseInt(Math.random()*(99999-10000+1)+1000)+".jpg");

var file = new File(outPutPath);

// 输出目录uri

var outPutUri = Uri.fromFile(file);

//拍照回来的绝对路径

var path = entry.toLocalURL();

//去掉\\或者file://

path = path.replace("\\", "");

path = path.replace("file://", "");

var _file = new File(path);

var a = _file.exists();

// alert(a);

//绝对路径转uri

var curCropUri = Uri.fromFile(new File(path));

console.log("uri:" + curCropUri);

//裁切

var cropIntent = new Intent("com.android.camera.action.CROP");

cropIntent.setDataAndType(curCropUri, IMAGE_UNSPECIFIED);

// 截图完毕后 输出目录

cropIntent.putExtra(MediaStore.EXTRA_OUTPUT, outPutUri);

cropIntent.putExtra("crop", "true");

// aspectX aspectY 是宽高的比例

cropIntent.putExtra("aspectX", 40);

cropIntent.putExtra("aspectY", 55);

// outputX outputY 是裁剪图片宽高

cropIntent.putExtra("outputX", 64);

cropIntent.putExtra("outputY", 64);

cropIntent.putExtra("return-data", true);

main.startActivityForResult(cropIntent, 1);

main.onActivityResult = function(requestCode, resultCode, data) {

//裁剪成功图片上传

// upload(outPutPath);

// // 判断 剪裁完后的图片输出是否存在

var _file2 = new File(outPutPath);

console.log(outPutPath);

var a2 = _file2.exists();

// alert(a2);

if(a2){

upload(outPutPath);

}else{

mui.toast('上传失败')

}

};

//上传

}, function(e) {

console.log("读取拍照文件错误:" + e.message);

});

}, function(e) {

console.log("失败:" + e.message);

}, {

filename: "_doc/camera/",

index: 1

});

}

//图片点击处理

function showActionSheet() {

// alert(outPutPath);

var bts = [{

title: "本地相册",

style: "destructive"

}, {

title: "拍照"

}]; //暂时不支持{title:"拍照"}

plus.nativeUI.actionSheet({

cancel: "取消",

buttons: bts

},

function(e) {

if (e.index == 1) {

//1 本地相册,包括裁剪

galleryImg();

} else if (e.index == 2) {

//2 拍照

getImage();

} else {

//3 取消

}

}

);

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值