0

I would like to combine taken pictures of a multi-page document to at least one dataURL, because I need one string of the image Data to send it to an OCR-API.

Therefore I use a canvas based on this example:

Merge two dataURIs to create a single image

But my canvas in the html only shows a small part of one image and also the image Data from the DataURL giving me only this small part.

HTML

<div width=100 height=100 id="canvas" #ocrCanvas>

Sources

var base64Canvas = [
   { src: 'data:image/png;base64,... },
   { src: 'data:image/png;base64,... },
   ...
];

Add canvas

    addCanvas() {

      var base64ImagesArray = this.base64Canvas;
      var canvas = document.createElement('canvas');
      var destination = this.ocrCanvas.nativeElement;

      Promise.all(base64ImagesArray.map(imageObj => add2canvas(canvas, imageObj)))
          .then(
            (imageObj) => {
                   destination.appendChild(canvas);
                   var canvasURL = canvas.toDataURL();
                   this.sendDataGoogleOCR(canvasURL)
                 }
          );

      function add2canvas(canvas, imageObj) {
        //console.log(imageObj);
         return new Promise( (resolve, reject) => {
            if (!imageObj || typeof imageObj != 'object') return reject();
            var img = new Image();
            img.onload = function () {
                canvas.getContext('2d')
                      .drawImage(this, imageObj.x || 0, imageObj.y || 0);
                resolve();
            };
            img.src = imageObj.src;
         });
      }
  }
mm1975
  • 1,583
  • 4
  • 30
  • 51
  • 1
    For a start, you need to set the `width` and `height` attributes of your output canvas. – Kaiido Jun 24 '18 at 23:27
  • Thx, I´ve solved it by dynamically calculate the height and width of the canvas and also the y-position of the images – mm1975 Jun 25 '18 at 10:43

0 Answers0