首页 > 作文

H5调用相机拍照并压缩图片的实例代码

更新时间:2023-04-06 12:01:47 阅读: 评论:0

整理文档,搜刮出一个h5调用相机拍照并压缩图片的实例代码,稍微整理精简一下做下分享。

背景

最近要做一个h5的页面,主要功能就是调用相机拍照或者是相册选图并且把照片压缩转ba64之后上传到后台服务器,然后服务器返回识别结果。

前端的主要功能点有:

h5如何调用相机图片如何压缩图片转ba64

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.he房屋出租人ight *= 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('imtry用法age/jpeg', quality/100);

然后就是上传到服务器并展示服务器的结果啦,然而事情并没有那么顺利。。。ios手机拍照压缩之后图片莫名的旋转了,继续解决问题。

解决ios图片旋转

首先引用,通过exif.getdata和exif.gettag获取拍照方向信息。

//file通过input标签获取exif.getdata(file,function(){  orientation=exif.gettag(file,'orientation');});

下面给出每个orientation值对应到iphone手机拍照的含义:

orientation描述3iphone横屏拍摄,此时home键在左侧,图片相对于原始位置旋转了180度6iphone竖屏拍摄,此时home键在下方(正常拿手机的方向),图片相对于原始位置逆时针旋转可90度8iphone竖屏拍摄,此时home键在上方,图片相对于原始位置顺时针旋转了90度

获取图片的方向信息之后,根据获取到的值作相应的旋转操作。

switch (orientation) {  ca 6:  ca 8:    cvs.width = height;    cvs.height = width;    break;}var context=cvs.getcontext("2d");switch(orientation){  //iphone横屏拍摄,此时home键在左侧  ca 3:  // 180度向左旋转  context.translate(width, height);  context.rotate(math.pi);  break;  //iphone竖屏拍摄,此时home键在下方(正常拿手机的方向)  ca 6:  context.rotate(0.5 * math.pi);  context.translate(0, -height);  break;  //iphone竖屏拍摄,此时home键在上方  ca 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) {          ca 6:          ca 8:            cvs.width = height;            cvs.height = width;            break;        }        var context=cvs.getcontext("2d");        //解决ios图片旋转问题         switch(orientation){          //iphone横屏拍摄,此时home键在左侧          ca 3:          // 180度向左旋转          context.translate(width, height);          context.rotate(math.pi);          break;          //iphone竖屏拍摄,此时home键在下方(正常拿手机的方向)          ca 6:          context.rotate(0.5 * math.pi);          context.translate(0, -height);          break;          //iphone竖屏拍摄,此时home键在上方          ca 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);  }el{    alert("只能识别图片!")  }});

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持www.887551.com。

本文发布于:2023-04-06 12:01:45,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/zuowen/b7a146e5ee98efe4b8bd4980030be340.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

本文word下载地址:H5调用相机拍照并压缩图片的实例代码.doc

本文 PDF 下载地址:H5调用相机拍照并压缩图片的实例代码.pdf

标签:图片   缩放   相机   逆时针
相关文章
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图