Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Arabic language error #106

Closed
zerox321 opened this issue Feb 17, 2021 · 10 comments
Closed

Arabic language error #106

zerox321 opened this issue Feb 17, 2021 · 10 comments

Comments

@zerox321
Copy link

i'm just trying to print arabic content with xprinter xpp810
i used that Code new EscPosCharsetEncoding("UTF-8", 16) , or windows-1256
and the result isn't correct

@Ali-Alshikh-Jasem-1996
Copy link

I am facing the same problem, some comments said to convert text to image and print it, if your printer doesn't support utf-8 encoding

@zerox321
Copy link
Author

But when i convert to image it's scale type got error

@Ali-Alshikh-Jasem-1996
Copy link

Ali-Alshikh-Jasem-1996 commented Feb 22, 2021

this is my code it's run successfully:

Bitmap bitmap = getBitmap("سبحان الله");

private Bitmap getBitmap(String text) {
        if (text == null)
            text = getString(R.string.custom_amount);
        Paint paint = new Paint();
        paint.setTextSize(28);

        Rect bounds = new Rect();
        paint.getTextBounds(text, 0, text.length(), bounds);
        int width = bounds.width();
        int height = bounds.height();

        Bitmap bitmap = Bitmap.createBitmap(width*2, height, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        canvas.drawColor(Color.WHITE);
        canvas.drawText(text, 20, 20, paint);
        return bitmap;
    }

@DantSu
Copy link
Owner

DantSu commented Feb 22, 2021

Read this issue to split image to keep full size :
#70

@zerox321
Copy link
Author

zerox321 commented Mar 2, 2021

Thank you Eng DantSu ,
ae6f8077-c17e-4023-8972-165f96beb948

@zerox321 zerox321 closed this as completed Mar 2, 2021
@amalnafia
Copy link

Eng

can you add code that you used please

@zerox321
Copy link
Author

/**
* Asynchronous printing
*/
public AsyncEscPosPrinter getAsyncEscPosPrinter(View containerView ,DeviceConnection printerConnection) {

    AsyncEscPosPrinter printer = new AsyncEscPosPrinter(printerConnection, printerDpi, getPrinterWidthMM(), printerNbrCharactersPerLine);

 

    Bitmap bm = PrinterUtil.screenshot(containerView);
    if (bm == null) {
        startAction();
        return null;
    }

    int width = bm.getWidth(), height = bm.getHeight();

    StringBuilder textToPrint = new StringBuilder();

    for (int y = 0; y < height; y += 256) {
        Bitmap bitmap = Bitmap.createBitmap(bm, 0, y, width, (y + 256 >= height) ? height - y : 256);
        textToPrint.append("[C]<img>").append(PrinterTextParserImg.bitmapToHexadecimalString(printer, bitmap)).append("</img>\n");
    }
    return printer.setTextToPrint(textToPrint.toString());

}

@zerox321
Copy link
Author

first step
i try to convert my constraint layout to bitmap -> Bitmap bm = PrinterUtil.screenshot(containerView);
public static Bitmap screenshot(View view) {

    int getMeasuredWidth = view.getMeasuredWidth();
    int getMeasuredHeight = view.getMeasuredHeight();
    if (getMeasuredWidth == 0 || getMeasuredHeight == 0) return null;
    view.setDrawingCacheEnabled(true);
    view.buildDrawingCache();
    Bitmap bitmap = Bitmap.createBitmap(view.getMeasuredWidth(),
            view.getMeasuredHeight(), Bitmap.Config.ARGB_8888);

    Canvas canvas = new Canvas(bitmap);
    Paint paint = new Paint();
    int iHeight = bitmap.getHeight();
    canvas.drawBitmap(bitmap, 0, iHeight, paint);
    view.draw(canvas);
    return bitmap;
}

and i try to split view using this for ->
for (int y = 0; y < height; y += 256) {
Bitmap bitmap = Bitmap.createBitmap(bm, 0, y, width, (y + 256 >= height) ? height - y : 256);
textToPrint.append("[C]").append(PrinterTextParserImg.bitmapToHexadecimalString(printer, bitmap)).append("\n");
}

@zerox321
Copy link
Author

last note -> containerView must have white background not transparent

@laithbzour
Copy link

@amalnafia
try this you need to convert text to bitmap image .. if image too height then after convert to bitmap split image to chunk

/// split image function
private void splitImage(Bitmap bitmap,SplitBitmapImage spliter) {
Bitmap bmpScale = Bitmap.createScaledBitmap(bitmap, bitmap.getWidth(), bitmap.getHeight(), false);
int rows = bmpScale.getHeight() / 70 + 1;
int cols = 1;
try {
int chunkHeight = bitmap.getHeight() / rows + 1;
int chunkWidth = bitmap.getWidth() / cols;
Bitmap scaledBitmap = Bitmap.createScaledBitmap(bitmap, bitmap.getWidth(), bitmap.getHeight(), false);
int yCoord = 0;
int var8 = 0;

        for(int var9 = rows; var8 < var9; ++var8) {
            int xCoord = 0;
            int var11 = 0;

            for(int var12 = cols; var11 < var12; ++var11) {
                Bitmap bmp = Bitmap.createBitmap(scaledBitmap, xCoord, yCoord, chunkWidth, chunkHeight);

                if (bmp != null && spliter != null) {
                    spliter.bitmpaSpliter(bmp);
                }

                xCoord += chunkWidth;
            }

            yCoord += chunkHeight;
        }
    } catch (Exception var14) {
        var14.printStackTrace();
    }

}

////////////. print function
new Thread(() -> {
try {
EscPosPrinter printer = new EscPosPrinter(MyBluetoothPrintersConnections.selectFirstPairedOne(), 203, 48f, 32);
StringBuilder textToPrint = new StringBuilder();

            splitImage(yourBitmap, text->{
                    textToPrint.append("[C]<img>" + PrinterTextParserImg.bitmapToHexadecimalString(printer, text) + "</img>\n"+
                            "[L]\n");

            });
            textToPrint.append("[C]--------------------------------\n");
            printer.printFormattedTextAndCut(textToPrint.toString());


        } catch (EscPosConnectionException e) {
            e.printStackTrace();
        } catch (EscPosEncodingException e) {
            e.printStackTrace();
        } catch (EscPosBarcodeException e) {
            e.printStackTrace();
        } catch (EscPosParserException e) {
            e.printStackTrace();
        }

}).start();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants