在android热敏打印机中打印阿拉伯字符

2024-02-01

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

相同的代码在另一台热敏打印机 POS 上有效,但在这台打印机上不适用于阿拉伯字符 英文字符很好,但阿拉伯字符显示为中文字符

尝试添加编码为字符集“UTF-8”并且不适用于阿拉伯字符 打印代码:

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();
            }
        });

扫描打印机:

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);
                    }
                }
            }
        });

印刷样品

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


我也遇到了同样的问题,经过两天的搜索,我发现打印阿拉伯语等多语言文本的简单方法是将其绘制在画布上并将其打印为普通图像,如下所示:

    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;
    }
}

如果您想了解更多详细信息,请检查this https://github.com/yagoubgrine/AndroidPosBluetoothPrinter

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在android热敏打印机中打印阿拉伯字符 的相关文章

  • 如何使用 ProGuard 将所有方法保留在类中

    我使用 ProGuard 来优化我的 Android 应用程序 然而 对于 Android 仪器测试 我需要一些 但不是全部 类来保留所有成员 我尝试了各种方法 最后一个是 keepclassmembers public class com
  • 强制用户在 Android 中的 EditText 中输入内容

    我的活动中有几个编辑文本 我希望我的用户在提交表单之前正确输入 我该怎么做 我还有旋转器和 RadioGroup 按钮 你可以加验证在提交按钮上单击 private boolean validateFields int yourDesire
  • 使用 android AudioTrack 在左或右扬声器中播放声音

    我正在应用程序中的 AudioTrack 的帮助下播放声音 但我想在特定扬声器 耳朵中播放声音 即左扬声器或右扬声器或两个扬声器 以下代码是我用来播放声音的 private AudioTrack generateTone double fr
  • 安卓无法玩ogg

    有人知道这是什么意思吗 ogg使用phonegap is Media播放 它使用MediaPlayer 05 26 15 41 50 007 1160 3631 E AudioFlinger no more track names avai
  • 在 Anko DSL 中创建自定义 View/ViewGroup 类

    我想创建一个自定义视图 它只是一些 Android 视图的包装 我考虑创建一个自定义 ViewGroup 来管理其子视图的布局 但我不需要这么复杂 我基本上想做的是 class MainActivity verticalLayout tex
  • ProGuard 无法与 Windows 中使用的 Joda Time 一起编译

    实际上 这是一个答案 而不是一个问题 我确实在 Windows 上花了很多时间 使用 JodaTime 并使用 ProGuard 将其包含在我的 Android 项目中 混淆器配置 libraryjars C Users Reto Docu
  • 毕加索动画加载图像

    我有以下代码在毕加索中加载图像 使用可绘制的占位符在图像下载时显示 不过 我想要的是一个动画旋转进度条样式的旋转器 它可以在图像加载时不断地旋转 就像我在大多数专业应用程序中看到的那样 毕加索似乎不支持这一点 只支持静态图像可绘制 有没有办
  • FileNotFoundException:/存储/模拟/0/Android

    我尝试这个文件写入器 读取器代码段进行测试 File file new File Environment getExternalStorageDirectory LM lm lisdat 01 txt FileOutputStream ou
  • Android Studio更新到1.5后Gradle错误

    今天我已将 Android Studio 更新到 v1 5 我的 libgdx 项目在这次更新之前运行良好 现在我收到此错误消息 Error Unable to load class org gradle mvn3 org sonatype
  • 如何防止布局的方向改变,而不是整个屏幕/活动的方向改变

    我需要一个子布局 可以是任何布局 例如FrameLayout or RelativeLayout 忽略方向变化并始终保持横向 但不是它的父级或任何其他兄弟布局 视图 它们应该相应地改变它们的方向 因此 我不能使用setRequestedOr
  • finish() 完成活动但它仍然在后台

    我有一个关于 android studio 中活动的 finish 方法的问题 我有这个简单的代码 public class MainActivity extends AppCompatActivity Override protected
  • Web 视图未在 Android 中加载本地 html 文件

    I am integrating html in android I have created a web view But i am not able load local html page Surprisingly web view
  • 无法获取 Facebook 传入请求

    我正在尝试在我的 Facebook android 游戏应用程序中实现发送数据并接受该数据 我正在关注https developers facebook com docs android send requests notification
  • 如何以编程方式启动 ssh 服务器 android,以及如何获取连接到设备的用户名和密码

    我正在开发像这样的应用程序sshdroid 我想在 Android 操作系统上打开 ssh 连接 并且我想从电脑连接应用程序 我使用了 JSCH lib 但是这个lib用于将android连接到pc 我的要求是pc到android 任何人都
  • Vimeo 视频在 Android 6 设备上停止播放

    我正在尝试在我的应用程序中播放 Vimeo 的视频 问题是在 Android 6 设备上 视频会在一定时间后停止播放 在 API 较低的设备上一切正常 时间取决于质量 对于下面提供的网址的视频 播放一定分钟 1 到 3 视频质量有多低 播放
  • Android - 9 补丁

    我正在尝试使用 9 块图片创建一个新的微调器背景 我尝试了很多方法来获得完美的图像 但都失败了 s Here is my 9 patch 当我用Draw 9 patch模拟时 内容看起来不错 但是带有箭头的部分没有显示 或者当它显示时 这部
  • FCM onMessageReceived 应用程序运行时返回空白消息和标题

    正如您在标题中所写 当应用程序关闭时 它运行良好 并且onMessageReceived获取消息正文和标题 但如果应用程序处于前台模式 运行模式 则可以发送通知 但没有消息和标题 请问该怎么办 代码 Override public void
  • 在 Android 中使用 iText 将图像添加到特定位置

    我想使用 Android 中的 iText 将图像添加到 PDF 文件中的特定位置 这是一个可填写的表单 我添加了作为图像占位符的文本框 我想要做的就是像这样获取该文本框和图像 public class FormFill public st
  • Android 和 Java 中绘制椭圆的区别

    在Java中由于某种原因Ellipse2D Double使用参数 height width x y 当我创建一个RectF在Android中参数是 left top right bottom 所以我对适应差异有点困惑 如果在 Java 中创
  • 当ScrollView滚动到底部时加载更多数据

    我有一个带有动态加载内容的滚动视图 有时可能会有很多内容 所以我想在用户滚动到底部时加载更多内容 我搜索了合适的方法 发现了两种 onScrollChanged and getScrollY 但我不知道如何将它用于我的目的 请给我一些建议

随机推荐