首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >用android热打印机打印阿拉伯字符

用android热打印机打印阿拉伯字符
EN

Stack Overflow用户
提问于 2021-04-03 13:32:29
回答 3查看 1.3K关注 0票数 1

打印机是GoojPRT便携式打印机PT-210 (热打印机)

相同的代码在另一台热感打印机POS上工作,但在这台打印机上不为阿拉伯字符工作,英文字符很好,但阿拉伯字符显示为中文字符。

尝试添加编码为字符集“UTF-8”的字符,而不对阿拉伯字符使用打印代码:

代码语言:javascript
运行
复制
Button btnPrint=(Button)findViewById(R.id.btnPrint);
        btnPrint.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                Thread t = new Thread() {
                    public void run() {
                        try {
                            OutputStream os = mBluetoothSocket
                                    .getOutputStream();
                            BILL = "ENGLISH" + "\n";
                            BILL =  BILL + "العربية" + "\n";
                            BILL = BILL + "---------------" + "\n";
                            
                            os.write(BILL.getBytes( ));
                        } catch (Exception e) {

                        }
                    }
                };
                t.start();
            }
        });

扫描打印机:

代码语言:javascript
运行
复制
Button btnScan = (Button) findViewById(R.id.btnScan);
        btnScan.setOnClickListener(new View.OnClickListener() {
            public void onClick(View mView) {
                mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
                if (mBluetoothAdapter == null) {
                    Toast.makeText(ActivityTest.this, "Error", Toast.LENGTH_SHORT).show();
                } else {
                    if (!mBluetoothAdapter.isEnabled()) {
                        Intent enableBtIntent = new Intent(
                                BluetoothAdapter.ACTION_REQUEST_ENABLE);
                        startActivityForResult(enableBtIntent,
                                REQUEST_ENABLE_BT);
                    } else {
                        ListPairedDevices();
                        Intent connectIntent = new Intent(ActivityTest.this,
                                DeviceListActivity.class);
                        startActivityForResult(connectIntent,
                                REQUEST_CONNECT_DEVICE);
                    }
                }
            }
        });

印刷样品

我需要打印文本而不是位图或图像。

EN

Stack Overflow用户

发布于 2021-11-18 14:58:43

我也遇到了同样的问题,经过两天的搜索,我发现,打印像阿拉伯语这样的多语种文本的简单方法是在画布上绘制它,并将它打印成一个正常的图像,如下所示:

代码语言:javascript
运行
复制
    public Bitmap getMultiLangTextAsImage(String text, Paint.Align align, float textSize, Typeface typeface)  {


    Paint paint = new Paint();

    paint.setAntiAlias(true);
    paint.setColor(Color.BLACK);
    paint.setTextSize(textSize);
    if (typeface != null) paint.setTypeface(typeface);

    // A real printlabel width (pixel)
    float xWidth = 385;

    // A height per text line (pixel)
    float xHeight = textSize + 5;

    // it can be changed if the align's value is CENTER or RIGHT
    float xPos = 0f;

    // If the original string data's length is over the width of print label,
    // or '\n' character included,
    // it will be increased per line gerneating.
    float yPos = 27f;

    // If the original string data's length is over the width of print label,
    // or '\n' character included,
    // each lines splitted from the original string are added in this list
    // 'PrintData' class has 3 members, x, y, and splitted string data.
    List<PrintData> printDataList = new ArrayList<PrintData>();

    // if '\n' character included in the original string
    String[] tmpSplitList = text.split("\\n");
    for (int i = 0; i <= tmpSplitList.length - 1; i++) {
        String tmpString = tmpSplitList[i];

        // calculate a width in each split string item.
        float widthOfString = paint.measureText(tmpString);

        // If the each split string item's length is over the width of print label,
        if (widthOfString > xWidth) {
            String lastString = tmpString;
            while (!lastString.isEmpty()) {

                String tmpSubString = "";

                // retrieve repeatedly until each split string item's length is
                // under the width of print label
                while (widthOfString > xWidth) {
                    if (tmpSubString.isEmpty())
                        tmpSubString = lastString.substring(0, lastString.length() - 1);
                    else
                        tmpSubString = tmpSubString.substring(0, tmpSubString.length() - 1);

                    widthOfString = paint.measureText(tmpSubString);
                }

                // this each split string item is finally done.
                if (tmpSubString.isEmpty()) {
                    // this last string to print is need to adjust align
                    if (align == Paint.Align.CENTER) {
                        if (widthOfString < xWidth) {
                            xPos = ((xWidth - widthOfString) / 2);
                        }
                    } else if (align == Paint.Align.RIGHT) {
                        if (widthOfString < xWidth) {
                            xPos = xWidth - widthOfString;
                        }
                    }
                    printDataList.add(new PrintData(xPos, yPos, lastString));
                    lastString = "";
                } else {
                    // When this logic is reached out here, it means,
                    // it's not necessary to calculate the x position
                    // 'cause this string line's width is almost the same
                    // with the width of print label
                    printDataList.add(new PrintData(0f, yPos, tmpSubString));

                    // It means line is needed to increase
                    yPos += 27;
                    xHeight += 30;

                    lastString = lastString.replaceFirst(tmpSubString, "");
                    widthOfString = paint.measureText(lastString);
                }
            }
        } else {
            // This split string item's length is
            // under the width of print label already at first.
            if (align == Paint.Align.CENTER) {
                if (widthOfString < xWidth) {
                    xPos = ((xWidth - widthOfString) / 2);
                }
            } else if (align == Paint.Align.RIGHT) {
                if (widthOfString < xWidth) {
                    xPos = xWidth - widthOfString;
                }
            }
            printDataList.add(new PrintData(xPos, yPos, tmpString));
        }

        if (i != tmpSplitList.length - 1) {
            // It means the line is needed to increase
            yPos += 27;
            xHeight += 30;
        }
    }

    // If you want to print the text bold
    //paint.setTypeface(Typeface.create(null as String?, Typeface.BOLD))

    // create bitmap by calculated width and height as upper.
    Bitmap bm = Bitmap.createBitmap((int) xWidth, (int) xHeight, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bm);
    canvas.drawColor(Color.WHITE);

    for (PrintData tmpItem : printDataList)
        canvas.drawText(tmpItem.text, tmpItem.xPos, tmpItem.yPos, paint);


    return bm;
}

static class PrintData {
    float xPos;
    float yPos;
    String text;

    public PrintData(float xPos, float yPos, String text) {
        this.xPos = xPos;
        this.yPos = yPos;
        this.text = text;
    }

    public float getxPos() {
        return xPos;
    }

    public void setxPos(float xPos) {
        this.xPos = xPos;
    }

    public float getyPos() {
        return yPos;
    }

    public void setyPos(float yPos) {
        this.yPos = yPos;
    }

    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }
}

如果需要更多详细信息,请检查

票数 1
EN
查看全部 3 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66931746

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档