0

I use ionic3 to make the mobile app , and install cordova-plugin-datecs-printer

When I use the sample code to print image, can't print image

here is my code:

function printMyImage() {
      var image = new Image();
      image.onload = function() {
          var canvas = document.createElement('canvas');
          canvas.height = 50;
          canvas.width = 50;
          var context = canvas.getContext('2d');
          context.drawImage(image, 0, 0);
          var imageData = canvas.toDataURL('image/jpeg').replace(/^data:image\/(png|jpg|jpeg);base64,/, ""); //remove mimetype
          window.DatecsPrinter.printImage(
              imageData, //base64
              canvas.width, 
              canvas.height, 
              1, 
              function() {
                printMyBarcode();
              },
              function(Error) {
                  alert(JSON.stringify(Error));
              }
          )
      };
      image.src = 'assets/imgs/Logo2.jpg';
    }
Stack Programmer
  • 679
  • 6
  • 18
uokivan
  • 1
  • 2

1 Answers1

0

Thee problem is that you're drawing an image without data in it, you must fill the src of the image first using image.src = 'assets/imgs/Logo2.jpg'; then after that check if the image is loaded using image.onload() and perform whatever you need like printing the image

Bellal Mohamed
  • 67
  • 1
  • 12
  • Thank you for this code snippet, which might provide some limited, immediate help. A proper explanation would greatly improve its long-term value by showing why this is a good solution to the problem, and would make it more useful to future readers with other, similar questions. Please edit your answer to add some explanation, including the assumptions you've made. – Murray Foxcroft Feb 18 '19 at 13:33