我用 Cordova 相机插件拍照 camera.getPicture() 接口(interface)
并获取 base64 图像我如何将此 base64 图像向右旋转 90 并获取新的 base64 字符串以保存 我的代码:
Camera.getPicture(options).then(function(imageURI) {
var img = new Image();
img.src = "data:image/png;base64," + imageURI;
var rotate = false;
if (img.width > img.height) {
rotate = true;
}
uploadImage(imageURI, activeIndex, rotate);
}, function(err) {
console.log(err);
$ionicLoading.hide();
});
图片旋转功能
var rotateImage = function (base64ImageSrc) {
var canvas = document.createElement('canvas');
var img = new Image();
img.src = base64ImageSrc;
var context = canvas.getContext('2d');
// translate context to center of canvas
context.translate(canvas.width / 2, canvas.height / 2);
// rotate 45 degrees clockwise
context.rotate(Math.PI / 4);
console.log(canvas.toDataURL());
var ImageUrl = canvas.toDataURL();
ImageUrl = ImageUrl.split("data:image/png;base64,");
ImageUrl = ImageUrl[1];
console.log(ImageUrl);
return ImageUrl;
};
可以使用 Canvas 实现图像旋转,
function rotateBase64Image() {
var canvas = document.createElement('canvas');
var ctx = canvas.getContext("2d");
var image = new Image();
image.src = base64data;
image.onload = function() {
ctx.translate(image.width, image.height);
ctx.rotate(180 * Math.PI / 180);
ctx.drawImage(image, 0, 0);
window.eval(""+callback+"('"+canvas.toDataURL()+"')");
};
}
base64data:包含base64编码的照片图像[原始图像]的字符串
canvas.toDataURL():旋转图像
关于javascript - Cordova 如何从相机旋转图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32242151/
欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) | Powered by Discuz! X3.4 |