DantSu / ESCPOS-ThermalPrinter-Android

Useful library to help Android developpers to print with (Bluetooth, TCP, USB) ESC/POS thermal printer.
MIT License
1.22k stars 370 forks source link

Int android.graphics.Bitmap.getWidth()' on a Null object reference, but i set bitmap width!! #202

Closed abdogamerpro closed 3 years ago

abdogamerpro commented 3 years ago

I use html to bitmap library, and here is my code:

String html = "<html><body><h3>test</h3></body><html>";
Bitmap bitmap = new Html2Bitmap.Builder().setContext(MainActivity.this).setContent(WebViewContent.html(html)).setBitmapWidth(150).build().getBitmap();
SimpleDateFormat format = new SimpleDateFormat("'on' yyyy-MM-dd 'at' HH:mm:ss");
EscPosPrinter printer = new EscPosPrinter(printerConnection, 203, 48f, 32);
printer.printFormattedText("[C]<img>" + PrinterTextParserImg.bitmapToHexadecimalString(printer, bitmap) + "</img>\n");

But it give me the error . Also I try to remove .BitmapWidth(), same error appers.

DantSu commented 3 years ago

Bitmap bitmap = new Html2Bitmap.Builder().setContext(MainActivity.this).setContent(WebViewContent.html(html)).setBitmapWidth(150).build().getBitmap(); Why do you put new before Html2Bitmap.Builder() ??

bitmap var seems to be null.

abdogamerpro commented 3 years ago

I do like docs says: https://github.com/iZettle/android-html2bitmap And If i delete new this error will show: The method Builder() is undefined for the type Html2Bitmap And I try to put imageview to the bitmap and it works perfectly. Error come from EscPosPrinterSize.java line 89 bitmap.getWidth() return with null!!

DantSu commented 3 years ago

It's very strange way to do a new instance... but whatever It's not the problem here.

getBitmap() return null. Why do you have * in yojr html tag ?? The problem seems to come from html2bitmap.

abdogamerpro commented 3 years ago

I said that I put * here only because github doesn't make it as code. Any solution for this for now? Or anyway to print string as image? Thanks in advance.

DantSu commented 3 years ago

I edit your comment to remove the * in your html. You have to write java after backtit to specify language.

As I said your problem don't come from my library, it's come from Html2Image which return a null value.

DantSu commented 3 years ago

Are you sure to be in the correct place to use MainActivity.this ?

abdogamerpro commented 3 years ago

Yeah iam sure that I put the code in MainActivity And the Html2Bitmap library work perfectly when try to put bitmap to an image, what is wrong?

DantSu commented 3 years ago

the exception is call here :

    /**
     * Convert Bitmap object to ESC/POS image.
     *
     * @param bitmap Instance of Bitmap
     * @return Bytes contain the image in ESC/POS command
     */
    public byte[] bitmapToBytes(Bitmap bitmap) {
        boolean isSizeEdit = false;
        int bitmapWidth = bitmap.getWidth(),
                bitmapHeight = bitmap.getHeight(),
                maxWidth = this.printerWidthPx,
                maxHeight = 256;

        if (bitmapWidth > maxWidth) {
            bitmapHeight = Math.round(((float) bitmapHeight) * ((float) maxWidth) / ((float) bitmapWidth));
            bitmapWidth = maxWidth;
            isSizeEdit = true;
        }
        if (bitmapHeight > maxHeight) {
            bitmapWidth = Math.round(((float) bitmapWidth) * ((float) maxHeight) / ((float) bitmapHeight));
            bitmapHeight = maxHeight;
            isSizeEdit = true;
        }

        if (isSizeEdit) {
            bitmap = Bitmap.createScaledBitmap(bitmap, bitmapWidth, bitmapHeight, true);
        }

        return EscPosPrinterCommands.bitmapToBytes(bitmap);
    }

So here int bitmapWidth = bitmap.getWidth(), instead of a Bitmap instance, you send a null object. And when he try to call .getWidth() on null, the error appear.

So this : new Html2Bitmap.Builder().setContext(MainActivity.this).setContent(WebViewContent.html(html)).setBitmapWidth(150).build().getBitmap(); return a null value.

DantSu commented 3 years ago

Check in your log if you have a warning error.

If you do this, the error still appear ? =>

String html = "<html><body><h3>test</h3></body><html>";
Bitmap bitmap = new Html2Bitmap.Builder().setContext(MainActivity.this).setContent(WebViewContent.html(html)).setBitmapWidth(150).build().getBitmap();
SimpleDateFormat format = new SimpleDateFormat("'on' yyyy-MM-dd 'at' HH:mm:ss");
if(bitmap != null) {
    EscPosPrinter printer = new EscPosPrinter(printerConnection, 203, 48f, 32);
    printer.printFormattedText("[C]<img>" + PrinterTextParserImg.bitmapToHexadecimalString(printer, bitmap) + "</img>\n");
}