export default {

  /**
     * canvas转dataURL：canvas对象、转换格式、图像品质
     */
  canvasToDataURL: function (canvas, format, quality) {
    return canvas.toDataURL(format || 'image/jpeg', quality || 1.0);
  },

  /**
     * DataURL转canvas
     */
  dataURLToCanvas: function (dataurl, cb) {
    var canvas = document.createElement('CANVAS');
    var ctx = canvas.getContext('2d');
    var img = new Image();
    img.onload = function () {
      canvas.width = img.width;
      canvas.height = img.height;
      ctx.drawImage(img, 0, 0);
      cb(canvas);
    };
    img.src = dataurl;
  },

  /**
     * image转canvas：图片地址
     */
  imageToCanvas: function (src, cb) {
    var canvas = document.createElement('CANVAS');
    var ctx = canvas.getContext('2d');
    var img = new Image();
    img.src = src;
    img.onload = function () {
      canvas.width = img.width;
      canvas.height = img.height;
      ctx.drawImage(img, 0, 0);
      cb(canvas);
    };
  },
  /**
     * canvas转image
     */
  canvasToImage: function (canvas) {
    var img = new Image();
    img.src = canvas.toDataURL('image/jpeg', 1.0);
    return img;
  },

  /**
     * File/Blob对象转DataURL
     */
  fileOrBlobToDataURL: function (obj, cb) {
    var a = new FileReader();
    a.readAsDataURL(obj);
    a.onload = function (e) {
      cb(e.target.result);
    };
  },

  /**
     * DataURL转Blob对象
     */
  dataURLToBlob: function (dataurl) {
    var arr = dataurl.split(',');
    var mime = arr[0].match(/:(.*?);/)[1];
    var bstr = atob(arr[1]);
    var n = bstr.length;
    var u8arr = new Uint8Array(n);
    while (n--) {
      u8arr[n] = bstr.charCodeAt(n);
    }
    return new Blob([u8arr], { type: mime });
  },


  /**
     * DataURL转Png Blob对象
     */
  dataURLToPngBlob: function (dataurl) {
    var arr = dataurl.split(',');
    var mime = arr[0].match(/:(.*?);/)[1];
    var bstr = atob(arr[1]);
    var n = bstr.length;
    var u8arr = new Uint8Array(n);
    while (n--) {
      u8arr[n] = bstr.charCodeAt(n);
    }
    return new Blob([u8arr], { type: 'image/png' });
  },

  /**
     * Blob转image
     */
  blobToImage: function (blob, cb) {
    this.fileOrBlobToDataURL(blob, function (dataurl) {
      var img = new Image();
      img.src = dataurl;
      cb(img);
    });
  },

  /**
     * image转Blob
     */
  imageToBlob: function (src, cb) {
    let _self = this;
    this.imageToCanvas(src, function (canvas) {
      cb(_self.dataURLToBlob(_self.canvasToDataURL(canvas)));
    });
  },

  /**
     * Blob转canvas
     */
  BlobToCanvas: function (blob, cb) {
    let _self = this;
    this.fileOrBlobToDataURL(blob, function (dataurl) {
      _self.dataURLToCanvas(dataurl, cb);
    });
  },

  /**
     * canvas转Blob
     */
  canvasToBlob: function (canvas, cb) {
    let _self = this;
    this.cb(_self.dataURLToBlob(_self.canvasToDataURL(canvas)));
  },

  /**
     * image转dataURL
     */
  imageToDataURL: function (src, cb) {
    let _self = this;
    this.imageToCanvas(src, function (canvas) {
      cb(_self.canvasToDataURL(canvas));
    });
  },

  /**
     * dataURL转image，这个不需要转，直接给了src就能用
     */
  dataURLToImage: function (dataurl) {
    var img = new Image();
    img.src = dataurl;
    return img;
  },
  /**
     * dataURL转File
     * @param {Object} dataurl
     * @param {Object} filename
     */
  dataURLtoFile: function (dataurl, filename) {//将base64转换为文件
    var arr = dataurl.split(','), mime = arr[0].match(/:(.*?);/)[1],
      bstr = atob(arr[1]), n = bstr.length, u8arr = new Uint8Array(n);
    while (n--) {
      u8arr[n] = bstr.charCodeAt(n);
    }
    return new File([u8arr], filename, { type: mime });
  },

  /**
   * 这个函数是实现将canvas上面的base64图像转为图像数据流的字符串形式
   */
  dataURItoBlob: function (dataURI) {
    // convert base64/URLEncoded data component to raw binary data held in a string
    var byteString;
    if (dataURI.split(',')[0].indexOf('base64') >= 0)
      byteString = atob(dataURI.split(',')[1]);
    else
      byteString = unescape(dataURI.split(',')[1]);

    // separate out the mime component
    var mimeString = dataURI.split(',')[0].split(':')[1].split(';')[0];

    // write the bytes of the string to a typed array
    var ia = new Uint8Array(byteString.length);
    for (var i = 0; i < byteString.length; i++) {
      ia[i] = byteString.charCodeAt(i);
    }

    return new Blob([ia], { type: mimeString });
  },

};
