Closed abdogamerpro closed 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.
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!!
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.
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.
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.
Are you sure to be in the correct place to use MainActivity.this ?
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?
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.
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");
}
I use html to bitmap library, and here is my code:
But it give me the error . Also I try to remove .BitmapWidth(), same error appers.