本文摘自PHP中文网,作者不言,侵删。
这篇文章主要介绍了H5调用相机拍照并压缩图片的实例代码,内容挺不错的,现在分享给大家,也给大家做个参考。整理文档,搜刮出一个H5调用相机拍照并压缩图片的实例代码,稍微整理精简一下做下分享。
背景
最近要做一个h5的页面,主要功能就是调用相机拍照或者是相册选图并且把照片压缩转base64之后上传到后台服务器,然后服务器返回识别结果。
前端的主要功能点有:
H5如何调用相机
图片如何压缩
图片转base64
H5调用相机/相册
调用相机最简单的方法就是使用input file[camera]属性:
1 2 | < input type = "file" capture = camera accept = "image/*" >//相机
< input type = "file" accept = "image/*" >//相册
|
这个种方法兼容性还是有问题的,在iphone手机上可以正常打开相机,但是在安卓手机上点击之后是相机、图库、文件管理器等混合选择项。在网上搜了很多都没有什么好的解决办法,只能继续往下写了。。。
图片压缩
图片压缩就要用到FileReader
和<canvas>
了。
FileReader对象允许Web应用程序异步读取存储在计算机上的文件的内容,使用File或Blob对象指定要读取的文件或数据。
<canvas>是一个可以使用脚本在其中绘制图形的HTML元素,可以绘制图形和简单的动画。
图片压缩要压缩图片的分辨率和质量,分辨率压缩我这里是设置了图片的最大边为800,另一边按照图片原有比例缩放,也可以设置图片整体的缩放比例。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | var MAX_WH=800;
var image= new Image();
image.onload= function () {
if (image.height > MAX_WH) {
image.width *= MAX_WH/ image.height;
image.height = MAX_WH;
}
if (image.width > MAX_WH) {
image.height *= MAX_WH/ image.width;
image.width = MAX_WH;
}
}
image.src=dataURL;
|
然后就是压缩图片的质量了,这里设置压缩了80%,如果设置太小图片就失真了。动态创建<canvas>标签然后压缩图片:
1 2 3 4 5 6 7 | var quality=80;
var cvs = document.createElement( 'canvas' );
cvs.width = image.width;
cvs.heigh = image.height;
var context=cvs.getContext( "2d" );
context.drawImage(image, 0, 0,image.width, image.height);
dataURL = cvs.toDataURL( 'image/jpeg' , quality/100);
|
然后就是上传到服务器并展示服务器的结果啦,然而事情并没有那么顺利。。。ios手机拍照压缩之后图片莫名的旋转了,继续解决问题。
解决IOS图片旋转
首先引用exif.js,通过EXIF.getData和EXIF.getTag获取拍照方向信息。
1 2 3 4 | EXIF.getData(file, function (){
orientation=EXIF.getTag(file, 'Orientation' );
});
|
下面给出每个orientation值对应到iphone手机拍照的含义:
orientation | 描述 |
---|
3 | iphone横屏拍摄,此时home键在左侧,图片相对于原始位置旋转了180度 |
6 | iphone竖屏拍摄,此时home键在下方(正常拿手机的方向),图片相对于原始位置逆时针旋转可90度 |
8 | iphone竖屏拍摄,此时home键在上方,图片相对于原始位置顺时针旋转了90度 |
获取图片的方向信息之后,根据获取到的值作相应的旋转操作。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | switch (orientation) {
case 6:
case 8:
cvs.width = height;
cvs.height = width;
break ;
}
var context=cvs.getContext( "2d" );
switch (orientation){
case 3:
context.translate(width, height);
context.rotate(Math.PI);
break ;
case 6:
context.rotate(0.5 * Math.PI);
context.translate(0, -height);
break ;
case 8:
context.rotate(-0.5 * Math.PI);
context.translate(-width, 0);
break ;
}
|
然后再上传图片,发现在IOS下图片已经正常了。
下面给出完整代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 | $( 'input[type=file]' ).change( function (e) {
var file = this .files[0];
var mime_type=file.type;
var orientation=0;
if (file && /^image\
EXIF.getData(file, function (){
orientation=EXIF.getTag(file, 'Orientation' );
});
var reader = new FileReader();
reader.onloadend = function () {
var width,height;
var MAX_WH=800;
var image= new Image();
image.onload= function () {
if (image.height > MAX_WH) {
image.width *= MAX_WH / image.height;
image.height = MAX_WH;
}
if (image.width > MAX_WH) {
image.height *= MAX_WH / image.width;
image.width = MAX_WH;
}
var quality=80;
var cvs = document.createElement( 'canvas' );
cvs.width = width = image.width;
cvs.height =height = image.height;
switch (orientation) {
case 6:
case 8:
cvs.width = height;
cvs.height = width;
break ;
}
var context=cvs.getContext( "2d" );
switch (orientation){
case 3:
context.translate(width, height);
context.rotate(Math.PI);
break ;
case 6:
context.rotate(0.5 * Math.PI);
context.translate(0, -height);
break ;
case 8:
context.rotate(-0.5 * Math.PI);
context.translate(-width, 0);
break ;
}
context.drawImage(image, 0, 0,image.width, image.height);
dataURL = cvs.toDataURL( 'image/jpeg' , quality/100);
...
}
image.src=dataURL;
};
reader.readAsDataURL(file);
} else {
alert( "只能识别图片!" )
}
});
|
以上就是本文的全部内容,希望对大家的学习有所帮助,更多相关内容请关注PHP中文网!
相关推荐:
关于H5新属性audio音频和video视频的控制解析
Angular下H5多张上传图片的方法
以上就是关于H5调用相机拍照并压缩图片的代码的详细内容,更多文章请关注木庄网络博客!
相关阅读 >>
关于H5调用相机拍照并压缩图片的代码
更多相关阅读请进入《H5调用相机拍照》频道 >>
人民邮电出版社
本书对 Vue.js 3 技术细节的分析非常可靠,对于需要深入理解 Vue.js 3 的用户会有很大的帮助。——尤雨溪,Vue.js作者
转载请注明出处:木庄网络博客 » 关于H5调用相机拍照并压缩图片的代码