srehanuddin / Cordova-Plugin-Bluetooth-Printer

A cordova plugin for bluetooth printer for android platform
103 stars 116 forks source link

Error Print! bad base-64 #47

Open EnriqueV opened 7 years ago

EnriqueV commented 7 years ago

Hi I have this error: Error Print! bad base-64 I use that print image method in this plugin, this is my code: function printerDocument(printParam) {

    var tes='<div><img src="'+printParam.logo+'"/></div><br>';

    BTPrinter.connect(function (data) {

        console.log("Connected...");
        console.log(data);
        BTPrinter.print(function (data) {
            console.log("Success");
            console.log(data)
            BTPrinter.disconnect(function (data) {
                console.log("Success disconnected");
                console.log(data)
            }, function (err) {
                console.log("Error");
                console.log(err)
            }, "WOOSIM");
        }, function (err) {
            console.log("Error Print!");
            console.log(err)
        },tes);
    }, function (err) {
        console.log("Error Connecting!!");
        console.log(err);
    }, "WOOSIM");

printParam.logo is a base64 image

CesarBalzer commented 7 years ago

You are passing a div, it is certainly not a valid base64 image, just pass the image printParam.logo I hope I have helped

EnriqueV commented 7 years ago

hi, i'm trying printParam.logowith print method, but i have this error: Attempt to invoke virtual method 'int android.graphics.Bitmap.getWidth()' on a null object reference

CesarBalzer commented 7 years ago

You have to pass the converted image to base64, without any tags like div, img or others. Use only the parameter you say is a base64 string. The print function you're using, this @EnriqueV will never print. @asiddeen and @EnriqueV here is an example of the function that I am printing the image and the debugged text. Pastebin

alfiaqil commented 7 years ago

i have this image string "data:image/png;base64,iVBORw0KGgoA..." what must i send to print image?

i tried passing whole code, no success i tried passing "iVBORw0KGgo...", no success

i get no error form my console log, but print output not quite what i wanted it just print random special character

EnriqueV commented 7 years ago

Hi @alfiaqil Verify that string is a correct base64 encode, in my case I solve this problem convert string base64 to image file in java code try this:

BitmapFactory.Options options = new BitmapFactory.Options(); options.inScaled = true; int x =0; int y = 0; Bitmap bm = BitmapFactory.decodeFile("file path", options); int width= bm.getWidth(); int height= bm.getHeight(); Bitmap newBitmap= Bitmap.createScaledBitmap(bm,625,height,true);