关于html5中如何调用相机拍照并且压缩图片的示例详解


当前第2页 返回上一页

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\//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("只能识别图片!")

  }

});

以上就是关于html5中如何调用相机拍照并且压缩图片的示例详解的详细内容,更多文章请关注木庄网络博客

返回前面的内容

相关阅读 >>

如何选择web前端模板引擎(推荐)

详解HTML5图片上传支持图片预览压缩及进度显示兼容ie6及标准浏览器

HTML5边玩边学(十)-俄罗斯方块之控制界面篇(源码)

HTML5存储页面或应用程序的私有自定义数据的data-* 属性

HTML5 虚拟键盘出现挡住输入框怎么办

什么是 websocket?深入理解HTML5中websocket

HTML5怎样做出图片转圈的动画效果

HTML5中你不知道的5个新功能

怎样用h5的webgl实现3d虚拟机房的漫游动画

HTML5应用-生日快乐动画的实现

更多相关阅读请进入《HTML5》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...