Fork me on GitHub

基于Vue的移动端图片裁剪组件

  最近项目上要做一个车牌识别的功能。本来以为很简单,只需要将图片扔给后台就可以了,但是经测试后识别率只有20-40%。因此产品建议拍摄图片后,可以对图片进行拖拽和缩放,然后裁剪车牌部分上传给后台来提高识别率。刚开始的话还是百度了一下看看有没有现成的组件,但是找来找去都没有找到一个合适的,还好这个功能不是很着急,因此自己周末就在家里研究一下。

  Demo地址:https://vivialex.github.io/demo/imageClipper/index.html

  下载地址:https://github.com/vivialex/vue-imageClipper

  因为移动端是用vue,所以就写成了一个vue组件,下面就说说自己的一些实现思路(本人技术有限,各位大神请体谅。另外展示的代码不一定是某个功能的完整代码),先看看效果:

     

 

  一、组件的初始化参数

  1、图片img(url或者base64 data-url)

  2、截图的宽clipperImgWidth

  3、截图的高clipperImgHeight

 1 props: {
 2     img: String, //url或dataUrl
 3     clipperImgWidth: {
 4         type: Number,
 5         default: 500
 6     },
 7     clipperImgHeight: {
 8         type: Number,
 9         default: 200
10     }
11 }
参数

  二、布局

  在Z轴方向看主要是由4层组成。第1层是一个占满整个容器的canvas(称cCanvas);第2层是一个有透明度的遮罩层;第3层是裁剪的区域(示例图中的白色方框),里面包含一个与裁剪区域大小相等的canvas(称pCanvas);第4层是一个透明层gesture-mask,用作绑定touchstart,touchmove,touchend事件。其中两个canvas都会加载同一张图片,只是起始坐标不一样。为什么需要两个canvas?因为想做出当手指离开屏幕时,裁剪区域外的部分表面会有一个遮罩层的效果,这样能突出裁剪区域的内容。

 1 <div class="cut-container" ref="cut">
 2     <canvas ref="canvas"></canvas>
 3 
 4     <!-- 裁剪部分 -->
 5     <div class="cut-part">
 6         <div class="pCanvas-container">
 7             <canvas ref="pCanvas"></canvas>
 8         </div>
 9     </div>
10 
11     <!-- 底部操作栏 -->
12     <div class="action-bar">
13         <button class="btn-cancel" @click="_cancel">取消</button>
14         <button class="btn-ok" @click="_cut">确认</button>
15     </div>
16 
17     <!-- 背景遮罩 -->
18     <div class="mask" :class="{opacity: maskShow}"></div>
19 
20     <!-- 手势操作层 -->
21     <div class="gesture-mask" ref="gesture"></div>
22 </div>
html结构

  三、初始化canvas

  canvas绘制的图片在hdpi显示屏上会出现模糊,具体原因这里不作分析,可以参考下这里。我这里的做法是让canvaswidthheight为其css width/heightdevicePixelRatio倍以及调用canvas api时所传入的参数都要乘以window.devicePixelRatio。最后还要记录一下两个canvas坐标原点的x, y差值(originXDifforiginYDiff)。如下

 1 _ratio(size) {
 2     return parseInt(window.devicePixelRatio * size);
 3 },
 4 _initCanvas() {
 5     let $canvas = this.$refs.canvas,
 6         $pCanvas = this.$refs.pCanvas,
 7         clipperClientRect = this.$refs.clipper.getBoundingClientRect(),
 8         clipperWidth = parseInt(this.clipperImgWidth / window.devicePixelRatio),
 9         clipperHeight = parseInt(this.clipperImgHeight / window.devicePixelRatio);
10 
11     this.ctx = $canvas.getContext('2d');
12     this.pCtx = $pCanvas.getContext('2d');
13 
14     //判断clipperWidth与clipperHeight有没有超过容器值
15     if (clipperWidth < 0 || clipperWidth > clipperClientRect.width) {
16         clipperWidth = 250
17     }
18 
19     if (clipperHeight < 0 || clipperHeight > clipperClientRect.height) {
20         clipperHeight = 100
21     }
22 
23     //因为canvas在手机上会被放大,因此里面的内容会模糊,这里根据手机的devicePixelRatio来放大canvas,然后再通过设置css来收缩,因此关于canvas的所有值或坐标都要乘以devicePixelRatio
24     $canvas.style.width = clipperClientRect.width + 'px';
25     $canvas.style.height = clipperClientRect.height + 'px';
26     $canvas.width = this._ratio(clipperClientRect.width);
27     $canvas.height = this._ratio(clipperClientRect.height);
28 
29     $pCanvas.style.width = clipperWidth + 'px';
30     $pCanvas.style.height = clipperHeight + 'px';
31     $pCanvas.width = this._ratio(clipperWidth);
32     $pCanvas.height = this._ratio(clipperHeight);
33 
34     //计算两个canvas原点的x y差值
35     let cClientRect = $canvas.getBoundingClientRect(),
36         pClientRect = $pCanvas.getBoundingClientRect();
37 
38     this.originXDiff = pClientRect.left - cClientRect.left;
39     this.originYDiff = pClientRect.top - cClientRect.top;
40     this.cWidth = cClientRect.width;
41     this.cHeight = cClientRect.height;
42 }
初始化canvas

   四、加载图片

  加载图片比较简单,首先是创建一个Image对象并监听器onload事件(因为加载的图片有可能是跨域的,因此要设置其crossOrigin属性为Anonymous,然后服务器上要设置Access-Control-Allow-Origin响应头)。加载的图片如果宽高大于容器的宽高,要对其进行缩小处理。最后垂直水平居中显示()(这里注意的是要保存图片绘制前的宽高值,因为日后缩放图片是以该值为基础再乘以缩放倍率,这里取imgStartWidthimgStartHeight)如下

 1 _loadImg() {
 2     if (this.imgLoading || this.loadImgQueue.length === 0) {
 3         return;
 4     }
 5 
 6     let img = this.loadImgQueue.shift();
 7 
 8     if (!img) {
 9         return;
10     }
11 
12     let $img = new Image(),
13         onLoad = e => {
14             $img.removeEventListener('load', onLoad, false);
15             this.$img = $img;
16             this.imgLoaded = true;
17             this.imgLoading = false;
18 
19             this._initImg($img.width, $img.height);
20             this.$emit('loadSuccess', e);
21             this.$emit('loadComplete', e);
22             this._loadImg();
23         },
24         onError = e => {
25             $img.removeEventListener('error', onError, false);
26             this.$img = $img = null;
27             this.imgLoading = false;
28 
29             this.$emit('loadError', e);
30             this.$emit('loadComplete', e);
31             this._loadImg();
32         };
33 
34     this.$emit('beforeLoad');
35     this.imgLoading = true;
36     this.imgLoaded = false;
37     $img.src = this.img;
38     $img.crossOrigin = 'Anonymous'; //因为canvas toDataUrl不能操作未经允许的跨域图片,这需要服务器设置Access-Control-Allow-Origin头
39     $img.addEventListener('load', onLoad, false);
40     $img.addEventListener('error', onError, false);
41 }
42 _initImg(w, h) {
43     let eW = null,
44         eH = null,
45         maxW = this.cWidth,
46         maxH = this.cHeight - this.actionBarHeight;
47 
48     //如果图片的宽高都少于容器的宽高,则不做处理
49     if (w <= maxW && h <= maxH) {
50         eW = w;
51         eH = h;
52     } else if (w > maxW && h <= maxH) {
53         eW = maxW;
54         eH = parseInt(h / w * maxW);
55     } else if (w <= maxW && h > maxH) {
56         eW = parseInt(w / h * maxH);
57         eH = maxH;
58     } else {
59         //判断是横图还是竖图
60         if (h > w) {
61             eW = parseInt(w / h * maxH);
62             eH = maxH;
63         } else {
64             eW = maxW;
65             eH = parseInt(h / w * maxW);
66         }
67     }
68 
69     if (eW <= maxW && eH <= maxH) {
70         //记录其初始化的宽高,日后的缩放功能以此值为基础
71         this.imgStartWidth = eW;
72         this.imgStartHeight = eH;
73         this._drawImage((maxW - eW) / 2, (maxH - eH) / 2, eW, eH);
74     } else {
75         this._initImg(eW, eH);
76     }
77 }
加载图片

   五、绘制图片

  下面的_drawImage有四个参数,分别是图片对应cCanvas的x,y坐标以及图片目前的宽高w,h。函数首先会清空两个canvas的内容,方法是重新设置canvas的宽高。然后更新组件实例中对应的值,最后再调用两个canvas的drawImage去绘制图片。对于pCanvas来说,其绘制的图片坐标值为x,y减去对应的originXDifforiginYDiff(其实相当于切换坐标系显示而已,因此只需要减去两个坐标系原点的x,y差值即可)。看看代码

 1 _drawImage(x, y, w, h) {
 2     this._clearCanvas();
 3     this.imgX = parseInt(x);
 4     this.imgY = parseInt(y);
 5     this.imgCurrentWidth = parseInt(w);
 6     this.imgCurrentHeight = parseInt(h);
 7 
 8     //更新canvas
 9     this.ctx.drawImage(this.$img, this._ratio(x), this._ratio(y), this._ratio(w), this._ratio(h));
10 
11     //更新pCanvas,只需要减去两个canvas坐标原点对应的差值即可
12     this.pCtx.drawImage(this.$img, this._ratio(x - this.originXDiff), this._ratio(y - this.originYDiff), this._ratio(w), this._ratio(h));
13 },
14 _clearCanvas() {
15     let $canvas = this.$refs.canvas,
16         $pCanvas = this.$refs.pCanvas;
17 
18     $canvas.width = $canvas.width;
19     $canvas.height = $canvas.height;
20     $pCanvas.width = $pCanvas.width;
21     $pCanvas.height = $pCanvas.height;
22 }
绘制图片

   六、移动图片

  移动图片实现非常简单,首先给gesture-mask绑定touchstart,touchmove,touchend事件,下面分别介绍这三个事件的内容

  首先定义四个变量scx, scy(手指的起始坐标),iXiY(图片目前的坐标,相对于cCanvas)。

  1、touchstart

    方法很简单,就是获取touches[0]的pageX,pageY来更新scxscy以及更新iXiY

  2、touchmove

    获取touches[0]的pageX,声明变量f1x存放,移动后的x坐标等于iX + f1x - scx,y坐标同理,最后调用_drawImage来更新图片。

  看看代码吧

 1 _initEvent() {
 2     let $gesture = this.$refs.gesture,
 3         scx = 0,
 4         scy = 0;
 5 
 6     let iX = this.imgX,
 7         iY = this.imgY;
 8 
 9     $gesture.addEventListener('touchstart', e => {
10         if (!this.imgLoaded) {
11             return;
12         }
13 
14         let finger = e.touches[0];
15             scx = finger.pageX;
16             scy = finger.pageY;
17             iX = this.imgX;
18             iY = this.imgY;    
19     }, false);
20     $gesture.addEventListener('touchmove', e => {
21         e.preventDefault();
22 
23         if (!this.imgLoaded) {
24             return;
25         }
26 
27         let f1x = e.touches[0].pageX,
28             f1y = e.touches[0].pageY;
29             this._drawImage(iX + f1x - scx, iY + f1y - scy, this.imgCurrentWidth, this.imgCurrentHeight);
30     }, false);
31 }            
移动图片

   七、缩放图片(这里不作特别说明的坐标都是相对于cCanvas坐标系)

  绘制缩放后的图片无非需要4个参数,缩放后图片左上角的坐标以及宽高。求宽高相对好办,宽高等于imgStartWidth * 缩放比率与imgstartHeight * 缩放倍率(imgStartWidth ,imgstartHeight 上文第四节有提到)。接下来就是求缩放倍率的问题了,首先在touchstart事件上求取两手指间的距离d1;然后在touchmove事件上继续求取两手指间的距离d2当前缩放倍率= 初始缩放倍率 + (d2-d1) / 步长(例如每60px算0.1),touchend事件上让初始缩放倍率=当前缩放倍率

  至于如何求取缩放后图片左上角的坐标值,在草稿纸上画来画去,画了很久......终于有点眉目。首先要找到一个缩放中心(这里做法是取双指的中点坐标,但是这个坐标必须要位于图片上,如果不在图片上,则取图片上离该中点坐标最近的点),然后存在下面这个等式

  (缩放中心x坐标 - 缩放后图片左上角x坐标)/ 缩放后图片的宽度 = (缩放中心x坐标 - 缩放前图片左上角x坐标)/ 缩放前图片的宽度;(y坐标同理)

  接下来看看下面这个例子(在visio找了很久都没有画坐标系的功能,所以只能手工画了)

  

  绿色框是一张10*5的图片,蓝色框是宽高放大两倍后的图片20*10,根据上面的公式推算的x2 = sx - w2(sx - x1) / w1,y2 = sy - h2(sy - y1) / h1

  坚持...继续看看代码吧

  1 _initEvent() {
  2     let $gesture = this.$refs.gesture,
  3         cClientRect = this.$refs.canvas.getBoundingClientRect(),
  4         scx = 0, //对于单手操作是移动的起点坐标,对于缩放是图片距离两手指的中点最近的图标。
  5         scy = 0,
  6         fingers = {}; //记录当前有多少只手指在触控屏幕
  7 
  8     //one finger
  9     let iX = this.imgX,
 10         iY = this.imgY;
 11 
 12     //two finger
 13     let figureDistance = 0,
 14         pinchScale = this.imgScale;
 15 
 16     $gesture.addEventListener('touchstart', e => {
 17         if (!this.imgLoaded) {
 18             return;
 19         }
 20 
 21         if (e.touches.length === 1) {
 22             let finger = e.touches[0];
 23 
 24             scx = finger.pageX;
 25             scy = finger.pageY;
 26             iX = this.imgX;
 27             iY = this.imgY;
 28             fingers[finger.identifier] = finger;
 29         } else if (e.touches.length === 2) {
 30             let finger1 = e.touches[0],
 31                 finger2 = e.touches[1],
 32                 f1x = finger1.pageX - cClientRect.left,
 33                 f1y = finger1.pageY - cClientRect.top,
 34                 f2x = finger2.pageX - cClientRect.left,
 35                 f2y = finger2.pageY - cClientRect.top;
 36 
 37             scx = parseInt((f1x + f2x) / 2);
 38             scy = parseInt((f1y + f2y) / 2);
 39             figureDistance = this._pointDistance(f1x, f1y, f2x, f2y);
 40             fingers[finger1.identifier] = finger1;
 41             fingers[finger2.identifier] = finger2;
 42 
 43             //判断变换中点是否在图片中,如果不是则去离图片最近的点
 44             if (scx < this.imgX) {
 45                 scx = this.imgX;
 46             }
 47             if (scx > this.imgX + this.imgCurrentWidth) {
 48                 scx = this.imgX + this.imgCurrentHeight;
 49             }
 50             if (scy < this.imgY) {
 51                 scy = this.imgY;
 52             }
 53             if (scy > this.imgY + this.imgCurrentHeight) {
 54                 scy = this.imgY + this.imgCurrentHeight;
 55             }
 56         }
 57     }, false);
 58     $gesture.addEventListener('touchmove', e => {
 59         e.preventDefault();
 60 
 61         if (!this.imgLoaded) {
 62             return;
 63         }
 64 
 65         this.maskShowTimer && clearTimeout(this.maskShowTimer);
 66         this.maskShow = false;
 67 
 68         if (e.touches.length === 1) {
 69             let f1x = e.touches[0].pageX,
 70                 f1y = e.touches[0].pageY;
 71             this._drawImage(iX + f1x - scx, iY + f1y - scy, this.imgCurrentWidth, this.imgCurrentHeight);
 72         } else if (e.touches.length === 2) {
 73             let finger1 = e.touches[0],
 74                 finger2 = e.touches[1],
 75                 f1x = finger1.pageX - cClientRect.left,
 76                 f1y = finger1.pageY - cClientRect.top,
 77                 f2x = finger2.pageX - cClientRect.left,
 78                 f2y = finger2.pageY - cClientRect.top,
 79                 newFigureDistance = this._pointDistance(f1x, f1y, f2x, f2y),
 80                 scale = this.imgScale + parseFloat(((newFigureDistance - figureDistance) / this.imgScaleStep).toFixed(1));
 81 
 82             fingers[finger1.identifier] = finger1;
 83             fingers[finger2.identifier] = finger2;
 84 
 85             if (scale !== pinchScale) {
 86                 //目前缩放的最小比例是1,最大是5
 87                 if (scale < this.imgMinScale) {
 88                     scale = this.imgMinScale;
 89                 } else if (scale > this.imgMaxScale) {
 90                     scale = this.imgMaxScale;
 91                 }
 92 
 93                 pinchScale = scale;
 94                 this._scale(scx, scy, scale);
 95             }
 96         }
 97     }, false);
 98     $gesture.addEventListener('touchend', e => {
 99         if (!this.imgLoaded) {
100             return;
101         }
102 
103         this.imgScale = pinchScale;
104 
105         //从finger删除已经离开的手指
106         let touches = Array.prototype.slice.call(e.changedTouches, 0);
107 
108         touches.forEach(item => {
109             delete fingers[item.identifier];
110         });
111 
112         //迭代fingers,如果存在finger则更新scx,scy,iX,iY,因为可能缩放后立即单指拖动
113         let i,
114             fingerArr = [];
115 
116         for(i in fingers) {
117             if (fingers.hasOwnProperty(i)) {
118                 fingerArr.push(fingers[i]);
119             }
120         }
121 
122         if (fingerArr.length > 0) {
123             scx = fingerArr[0].pageX;
124             scy = fingerArr[0].pageY;
125             iX = this.imgX;
126             iY = this.imgY;
127         } else {
128             this.maskShowTimer = setTimeout(() => {
129                 this.maskShow = true;
130             }, 300);
131         }
132 
133         //做边界值检测
134         let x = this.imgX,
135             y = this.imgY,
136             pClientRect = this.$refs.pCanvas.getBoundingClientRect();
137 
138         if (x > pClientRect.left + pClientRect.width) {
139             x = pClientRect.left
140         } else if (x + this.imgCurrentWidth < pClientRect.left) {
141             x = pClientRect.left + pClientRect.width - this.imgCurrentWidth;
142         }
143 
144         if (y > pClientRect.top + pClientRect.height) {
145             y = pClientRect.top;
146         } else if (y + this.imgCurrentHeight < pClientRect.top) {
147             y = pClientRect.top + pClientRect.height - this.imgCurrentHeight;
148         }
149 
150         if (this.imgX !== x || this.imgY !== y) {
151             this._drawImage(x, y, this.imgCurrentWidth, this.imgCurrentHeight);
152         }
153     });
154 },
155 _scale(x, y, scale) {
156     let newPicWidth = parseInt(this.imgStartWidth * scale),
157         newPicHeight = parseInt(this.imgStartHeight * scale),
158         newIX = parseInt(x - newPicWidth * (x - this.imgX) / this.imgCurrentWidth),
159         newIY = parseInt(y - newPicHeight * (y - this.imgY) / this.imgCurrentHeight);
160 
161     this._drawImage(newIX, newIY, newPicWidth, newPicHeight);
162 },
163 _pointDistance(x1, y1, x2, y2) {
164     return parseInt(Math.sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2)));
165 }
166 
167 缩放图片
缩放图片

  说明一下fingers是干嘛的,是用来记录当前有多少只手指在屏幕上触摸。可能会出现这种情况,双指缩放后,其中一只手指移出显示屏,而另外一个手指在显示屏上移动。针对这种情况,要在touchend事件上根据e.changedTouches来移除fingers里已经离开显示屏的finger,如果此时fingers里只剩下一个finger,则更新scxscyiXiY为移动图片做初始化准备。

  八、裁剪图片

  这里很简单,就调用pCanvastoDataURL方法就可以了

 1 _clipper() {
 2     let imgData = null;
 3 
 4     try {
 5         imgData = this.$refs.pCanvas.toDataURL();
 6     } catch (e) {
 7         console.error('请在response header加上Access-Control-Allow-Origin,否则canvas无法裁剪未经许可的跨域图片');
 8     }
 9     this.$emit('sure', imgData);
10 }
裁剪图片

   总结

  上面只是列出了一些自己认为比较关键的点, 如果有兴趣的,可以到我的github上下载源码看看。

  本人前端菜鸟一枚,欢迎各位大神的建议与指导,交流可用QQ:594580652或发邮件到此QQ邮箱。

posted @ 2017-11-03 11:06  Alex_Zheng  阅读(17525)  评论(0编辑  收藏  举报