您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

H5调用相机拍照并压缩图片

2024/6/10 21:18:24发布49次查看
这次给大家带来h5调用相机拍照并压缩图片,h5调用相机拍照并压缩图片的注意事项有哪些,下面就是实战案例,一起来看一下。
整理文档,搜刮出一个h5调用相机拍照并压缩图片的实例代码,稍微整理精简一下做下分享。
背景
最近要做一个h5的页面,主要功能就是调用相机拍照或者是相册选图并且把照片压缩转base64之后上传到后台服务器,然后服务器返回识别结果。
前端的主要功能点有:
h5如何调用相机
图片如何压缩
图片转base64
h5调用相机/相册
调用相机最简单的方法就是使用input file[camera]属性:
<input type="file" capture=camera accept="image/*">//相机 <input type="file" accept="image/*">//相册
这个种方法兼容性还是有问题的,在iphone手机上可以正常打开相机,但是在安卓手机上点击之后是相机、图库、文件管理器等混合选择项。在网上搜了很多都没有什么好的解决办法,只能继续往下写了。。。
图片压缩
图片压缩就要用到filereader和<canvas>了。
filereader对象允许web应用程序异步读取存储在计算机上的文件的内容,使用file或blob对象指定要读取的文件或数据。
<canvas>是一个可以使用脚本在其中绘制图形的html元素,可以绘制图形和简单的动画。
图片压缩要压缩图片的分辨率和质量,分辨率压缩我这里是设置了图片的最大边为800,另一边按照图片原有比例缩放,也可以设置图片整体的缩放比例。
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;//dataurl通过filereader获取
然后就是压缩图片的质量了,这里设置压缩了80%,如果设置太小图片就失真了。动态创建<canvas>标签然后压缩图片:
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获取拍照方向信息。
//file通过input标签获取 exif.getdata(file,function(){   orientation=exif.gettag(file,'orientation'); });
下面给出每个orientation值对应到iphone手机拍照的含义:
orientation描述
3 iphone横屏拍摄,此时home键在左侧,图片相对于原始位置旋转了180度
6 iphone竖屏拍摄,此时home键在下方(正常拿手机的方向),图片相对于原始位置逆时针旋转可90度
8 iphone竖屏拍摄,此时home键在上方,图片相对于原始位置顺时针旋转了90度
获取图片的方向信息之后,根据获取到的值作相应的旋转操作。
switch (orientation) {   case 6:   case 8:     cvs.width = height;     cvs.height = width;     break; } var context=cvs.getcontext(2d); switch(orientation){   //iphone横屏拍摄,此时home键在左侧   case 3:   // 180度向左旋转   context.translate(width, height);   context.rotate(math.pi);   break;   //iphone竖屏拍摄,此时home键在下方(正常拿手机的方向)   case 6:   context.rotate(0.5 * math.pi);   context.translate(0, -height);   break;   //iphone竖屏拍摄,此时home键在上方   case 8:   // 逆时针旋转90度   context.rotate(-0.5 * math.pi);   context.translate(-width, 0);   break; }
然后再上传图片,发现在ios下图片已经正常了。
下面给出完整代码:
$('input[type=file]').change(function(e) {   var file = this.files[0];   var mime_type=file.type;   var orientation=0;   if (file && /^image\//i.test(file.type)) {     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);         //解决ios图片旋转问题          switch(orientation){           //iphone横屏拍摄,此时home键在左侧           case 3:           // 180度向左旋转           context.translate(width, height);           context.rotate(math.pi);           break;           //iphone竖屏拍摄,此时home键在下方(正常拿手机的方向)           case 6:           context.rotate(0.5 * math.pi);           context.translate(0, -height);           break;           //iphone竖屏拍摄,此时home键在上方           case 8:           // 逆时针旋转90度           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(只能识别图片!)   } });
相信看了本文案例你已经掌握了方法,更多精彩请关注其它相关文章!
推荐阅读:
h5中history模式的使用详解
localstorage和sessionstorage使用记录
以上就是h5调用相机拍照并压缩图片的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product