height too large in Thermal Or POS Printer in Android

1.2k views Asked by At

please any help ... I want to print receipt in a thermal printer using android and if the height of image larger then 350 PX this error show "height is too large"

My Code :

 private void printVoucher(){
        ArrayList<Printable> printables = new ArrayList<>();
    
        Picasso.get()
                .load("Path Image")
                .into(new Target() {
                    @Override
                    public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
                        printables.add(new ImagePrintable.Builder(bitmap).setNewLinesAfter(0).build());
                        //printing.print(printables);
                        Printooth.INSTANCE.printer().print(printables);
                    }

                    @Override
                    public void onBitmapFailed(Exception e, Drawable errorDrawable) {

                    }

                    @Override
                    public void onPrepareLoad(Drawable placeHolderDrawable) {

                    }
                });
    }

Error :

E/decodeBitmapĀ error:  height is too large
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.mirasoft.mirasoftapperp, PID: 20979
    java.lang.NullPointerException
        at com.mazenrashed.printooth.data.DefaultPrintingImagesHelper.getBitmapAsByteArray(DefaultPrintingImagesHelper.kt:8)
        at com.mazenrashed.printooth.data.printable.ImagePrintable.getPrintableByteArray(ImagePrintable.kt:16)
        at com.mazenrashed.printooth.utilities.Printing.printPrintables(Printing.kt:66)
        at com.mazenrashed.printooth.utilities.Printing.access$printPrintables(Printing.kt:13)
        at com.mazenrashed.printooth.utilities.Printing$initDeviceCallback$1.onDeviceConnected(Printing.kt:43)
        at com.mazenrashed.printooth.utilities.Bluetooth$ConnectThread.lambda$run$0$Bluetooth$ConnectThread(Bluetooth.java:324)
        at com.mazenrashed.printooth.utilities.-$$Lambda$Bluetooth$ConnectThread$PrUWb5GXkpw9YbMT2riXHliWDR8.run(Unknown Source:2)
        at android.os.Handler.handleCallback(Handler.java:789)
        at android.os.Handler.dispatchMessage(Handler.java:98)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:6944)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
2

There are 2 answers

0
Ap0piec On

I found a workaround that allowed me to use this printing library by splitting the image into smaller images.

Bitmap bmp1 = Bitmap.createBitmap(screen, 200, 300, 350, 255);
Bitmap bmp2 = Bitmap.createBitmap(screen, 200, 555, 350, 255);
Bitmap bmp3 = Bitmap.createBitmap(screen, 200, 810, 350, 155);

ArrayList<Printable> al = new ArrayList<>();

al.add(new ImagePrintable.Builder(bmp1)
       .setAlignment(DefaultPrinter.Companion.getALIGNMENT_CENTER())
       .build());
al.add(new ImagePrintable.Builder(bmp2)
       .setAlignment(DefaultPrinter.Companion.getALIGNMENT_CENTER())
       .build());
al.add(new ImagePrintable.Builder(bmp3)
       .setAlignment(DefaultPrinter.Companion.getALIGNMENT_CENTER())
       .build());

Printooth.INSTANCE.printer().print(al);

Bitmap.createBitmap is used here to crop the image and it takes arguments: original bitmap, x, y, width, height The image printed just fine even tho I split it up.

0
Sachin Deshapande On

Old version of printooth library allows only 255 px of image height and width. The issue is resolved in newer version-- use updated Printooth library

  implementation 'com.github.mazenrashed:Printooth:1.3.1'