@@ -11,16 +11,16 @@ var ImageReader = function () {
11
11
/**
12
12
* 读取图片数据
13
13
*
14
- * @param {HTMLInputElement } file
14
+ * @param {Blob } file
15
15
* @param {int } width
16
16
* @param {int } height
17
17
* @returns {Promise }
18
18
*/
19
19
ImageReader . prototype . read = function ( file , width , height ) {
20
- this . width = width
21
- this . height = height
22
- this . reader . readAsDataURL ( file . files [ 0 ] )
23
20
return new Promise ( ( function ( resolve ) {
21
+ this . width = width
22
+ this . height = height
23
+ this . reader . readAsDataURL ( file )
24
24
this . resolve = resolve
25
25
} ) . bind ( this ) )
26
26
}
@@ -32,29 +32,18 @@ ImageReader.prototype.load = function () {
32
32
for ( var y = 0 ; y < this . width ; y += 1 ) {
33
33
data . push ( [ ] )
34
34
for ( var x = 0 ; x < this . height ; x += 1 ) {
35
- data [ y ] . push ( this . toRGB ( this . canvas . getImageData ( x , y , 1 , 1 ) . data ) )
35
+ data [ y ] . push ( this . toRGBA ( this . canvas . getImageData ( x , y , 1 , 1 ) . data ) )
36
36
}
37
37
}
38
38
this . resolve ( data )
39
39
}
40
40
41
- /**
42
- * 十进制转十六进制,如果不足两位则加前导零
43
- *
44
- * @param {int } dec
45
- * @returns {string }
46
- */
47
- ImageReader . prototype . toHex = function ( dec ) {
48
- var hex = dec . toString ( 16 )
49
- return hex . length == 2 ? hex : '0' + hex
50
- }
51
-
52
41
/**
53
42
* RGBA 数组转十六进制
54
43
*
55
44
* @param {CanvasPixelArray } pixel
56
45
* @returns {string }
57
46
*/
58
- ImageReader . prototype . toRGB = function ( pixel ) {
59
- return '# ' + this . toHex ( pixel [ 0 ] ) + this . toHex ( pixel [ 1 ] ) + this . toHex ( pixel [ 2 ] )
47
+ ImageReader . prototype . toRGBA = function ( pixel ) {
48
+ return 'rgba( ' + pixel [ 0 ] + ',' + pixel [ 1 ] + ',' + pixel [ 2 ] + ',' + pixel [ 2 ] + ')'
60
49
}
0 commit comments