[心缘地方]同学录
首页 | 功能说明 | 站长通知 | 最近更新 | 编码查看转换 | 代码下载 | 常见问题及讨论 | 《深入解析ASP核心技术》 | 王小鸭自动发工资条VBA版
登录系统:用户名: 密码: 如果要讨论问题,请先注册。

[备忘]ESC指令打印图片

上一篇:[备忘]DailyRollingFileAppender可能丢日志
下一篇:[备忘]postgresql报错:heap_update_redo: invalid lp

添加日期:2022/10/14 10:15:32 快速返回   返回列表 阅读359次


/**
     * 横向打印图片.
     * @param printer 打印机
     * @param col 列位置
     * @param image 图片
     * @return 指令List
     */
    private static List<EscCmd> convertImageToCmdH(Printer printer, String col, Bitmap image) {
        List<EscCmd> cmdList = new ArrayList<>();

        //宽度,高度
        int width = image.getWidth();
        int height = image.getHeight();
        System.out.println(image.getWidth());
        System.out.println(image.getHeight());

        //一个字节表示8个点,所以字节数=图片宽度/8
        int byteWidth = (width - 1) / 8 + 1;

        //获取图片像素
        int[] pixels = new int[width * height];
        image.getPixels(pixels, 0, width, 0, 0, width, height);

        // "PRINTBMP_H": "27,64,29,118,48,0,#:0{x%256}x,#:0{x\256}x,#:1{x%256}x,#:1{x\256}x,#m",
        //参数是:宽度,高度,图片数据
        EscCmd cmd = convertCmd(printer, "PRINTBMP_H", "image", String.valueOf(byteWidth), String.valueOf(height)).get(0);
        EscCmdImage imageCmd = new EscCmdImage(cmd);

        //按行转换
        List<byte[]> dataList = new ArrayList<byte[]>();
        for (int y = 0; y < height; y++) {
            byte[] rowData = new byte[byteWidth];
            for (int x = 0; x < width; x++) {

                // 黑白位图,不是00(黑色),就是FF(白色)
                int color = pixels[width * y + x];
                int A = Color.alpha(color);
                int R = Color.red(color);
                int G = Color.green(color);
                int B = Color.blue(color);
                //RGB图,(R+G+B)/3<阈值,认为是黑色。
                //条形码,ARGB,黑色FF000000,白色FFFFFFFF,这是RGBA?
                if (R == 0) {
                    rowData[x / 8] += (byte) (128 >> (x % 8)); // 8个像素的值合并到一个字节里
                }
            }
            dataList.add(rowData);
        }

        // 保存字节流
        imageCmd.setImageDataList(dataList);
        cmdList.add(imageCmd);
        return cmdList;
    }

    /**
     * 竖向打印图片.
     * @param printer 打印机
     * @param col 列位置
     * @param image 图片
     * @return 指令List
     */
    private static List<EscCmd> convertImageToCmdV(Printer printer,String col, Bitmap image) {
        List<EscCmd> cmdList = new ArrayList<>();

        //宽度,高度
        int width = image.getWidth();
        int height = image.getHeight();
        System.out.println(image.getWidth());
        System.out.println(image.getHeight());

        // 24点双密度位图
        // "PRINTBMP_V": "27,42,33,#:0{x%256}x,#:0{x\\256}x,#m",
        EscCmd cmd = convertCmd(printer, "PRINTBMP_V", "image", String.valueOf(width)).get(0);

        //8点还是24点
        int bitCount = 8;
        if (cmd.getCmdPrev().endsWith("32,") || cmd.getCmdPrev().endsWith("33,")) {
            bitCount = 24;
        }

        // 循环图片像素打印图片
        // 循环高
        for (int i = 0; i < (height / bitCount + 1); i++) {

            // 图片不是第一列,每块图片都需要跳定位
            // i==0时,外面跳定位了,所以无需
            // 没有图文混排,图片独占一行
            if (!col.equals("1") && i > 0) {
                // 设置定位
                String paramCol = String.valueOf(Integer.valueOf(col) - 1);
                EscCmd colCmd = convertCmd(printer, "SETCOL", paramCol).get(0);
                cmdList.add(colCmd);

                // 跳定位
                cmdList.addAll(convertSingleCmd(printer, "HT"));
            }

            EscCmdImage imageCmd = new EscCmdImage(cmd);

            // 循环宽
            List<byte[]> dataList = new ArrayList<byte[]>();
            for (int j = 0; j < width; j++) {
                // 竖着3个字节,24个像素
                byte[] data = new byte[bitCount / 8];
                for (int k = 0; k < bitCount; k++) {
                    if (((i * bitCount) + k) < height) // if within the BMP size
                    {
                        // 黑白位图,不是00(黑色),就是FF(白色)
                        int color = image.getPixel(j, (i * bitCount) + k);
                        int A = Color.alpha(color);
                        int R = Color.red(color);
                        int G = Color.green(color);
                        int B = Color.blue(color);
                        //RGB图,(R+G+B)/3<阈值,认为是黑色。
                        //条形码,ARGB,黑色FF000000,白色FFFFFFFF,这是RGBA?
                        if (R==0) {
                            data[k / 8] += (byte) (128 >> (k % 8)); // 8个像素的值合并到一个字节里
                        }
                    }
                }
                // 一次写入一个data,24个像素
                dataList.add(data);
            }

            // 不更改行间距,使用自定义换行,指定高度为0
            byte[] data2 = {0x1B, 0x4A, 0x0};
            dataList.add(data2);

            // 保存字节流
            imageCmd.setImageDataList(dataList);
            cmdList.add(imageCmd);
        }
        return cmdList;
    }

 

评论 COMMENTS
没有评论 No Comments.

添加评论 Add new comment.
昵称 Name:
评论内容 Comment:
验证码(不区分大小写)
Validation Code:
(not case sensitive)
看不清?点这里换一张!(Change it here!)
 
评论由管理员查看后才能显示。the comment will be showed after it is checked by admin.
CopyRight © 心缘地方 2005-2999. All Rights Reserved