我正在开发cordova Android 应用程序。其中我必须通过cordova捕获插件捕获图像并将其上传到服务器。我面临的问题是,上传高质量图像时需要花费太多时间。我该如何解决这个问题?
这是客户端 Controller 。
navigator.camera.getPicture($scope.onPhotoDataGlrySuccess, $scope.onGlryFail, {
quality: 50,
destinationType: Camera.DestinationType.DATA_URL,
targetWidth: 200,
});
$scope.onPhotoDataGlrySuccess = function(imageData) {
//var image = document.getElementById('myImage');
$scope.imgsrc = imageData;
$scope.imgname = Date.now();
$scope.dataObj = {
img: $scope.imgsrc,
id: $scope.userid
}
dbServices.addImageToUserProfile($scope.dataObj).then(function(response) {
//$scope.img = 'data:image/jpeg;base64,' + response.data[0].user_image;
if(response.data[0].user_image){
$scope.img = 'data:image/jpeg;base64,' + response.data[0].user_image;
}
})
}
这是客户服务
this.addImageToUserProfile = function (data) {
return $http.post('/addimagetoProfile', {
img : data.img,
id : data.id
}).then(function(response){
return response;
})
}
这是服务器 Controller
app.post('/addimagetoProfile', function(req, res) {
connection.query('UPDATE user SET user_image="' + req.body.img + '" WHERE id= ' + req.body.id + '', function(err, result) {
if (!err) {
//res.json(rows);
connection.query('select id,name,city,expertise,user_image from user where id = ' + req.body.id + '', function(err, rows, fields) {
if (!err) {
res.json(rows);
} else {
console.log(err);
}
})
} else {
console.log(err);
}
})
})
即使将质量设置为 50(在捕获 Controller 中),它也会花费太多时间。 如何最大限度地缩短上传时间,以便将上传时间增加到 80 左右,从而上传质量更好的图像。
将 base64 更改为图像文件,然后使用 angular-file-upload 上传到服务器可以解决此问题吗?如果是这样,那么我们如何将 base64 转换为图像文件。
最佳答案
我通过以下方式使用了此功能。 实际上发送 base-64 编码的图像字符串总是比上传文件到服务器更快。我建议您使用 base-64 编码。
navigator.camera.getPicture(onSuccess, onFail, { quality: 20,correctOrientation : true,targetWidth: 700,
targetHeight: 700,
destinationType: Camera.DestinationType.DATA_URL
});
function onSuccess(imageData) {
myImage = imageData; // variable my image will contain base-64 encoded image
}
质量、目标宽度和高度参数是可选的,您可以更改它们以满足您的要求
关于android - Base64 图像上传到服务器花费太多时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32330107/