热敏打印机打印高度大于255的位图

问题描述 投票:0回答:1

我正在尝试打印高度高于255 px的位图,并且将位图转换为位数组的函数尚未准备好该高度。当我尝试更改 if 以获得更高高度时,打印机向我发送了一个空标签。

这是身高的“如果”:

String heightHexString = Integer.toHexString(bmpHeight);
        if (heightHexString.length() > 2) {
            System.out.println("heightHexString.length(): "+heightHexString.length()+" heightHexString: "+heightHexString);
            Log.e("decodeBitmap error", " height is too large");
            return null;
        } else if (heightHexString.length() == 1) {
            heightHexString = "0" + heightHexString;
        }
        heightHexString = heightHexString + "00";

这是我的完整课程:

package pirsch.dev.masaprisaapp.Utilities;

import android.graphics.Bitmap;
import android.util.Log;

import java.io.ByteArrayOutputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by [email protected]
 *
 */

public class Utils {
    // UNICODE 0x23 = #
    public static final byte[] UNICODE_TEXT = new byte[] {0x23, 0x23, 0x23,
            0x23, 0x23, 0x23,0x23, 0x23, 0x23,0x23, 0x23, 0x23,0x23, 0x23, 0x23,
            0x23, 0x23, 0x23,0x23, 0x23, 0x23,0x23, 0x23, 0x23,0x23, 0x23, 0x23,
            0x23, 0x23, 0x23};

    private static String hexStr = "0123456789ABCDEF";
    private static String[] binaryArray = { "0000", "0001", "0010", "0011",
            "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011",
            "1100", "1101", "1110", "1111" };

    public static byte[] decodeBitmap(Bitmap bmp){
        int bmpWidth = bmp.getWidth();
        int bmpHeight = bmp.getHeight();

        List<String> list = new ArrayList<String>(); //binaryString list
        StringBuffer sb;


        int bitLen = bmpWidth / 8;
        int zeroCount = bmpWidth % 8;

        String zeroStr = "";
        if (zeroCount > 0) {
            bitLen = bmpWidth / 8 + 1;
            for (int i = 0; i < (8 - zeroCount); i++) {
                zeroStr = zeroStr + "0";
            }
        }

        for (int i = 0; i < bmpHeight; i++) {
            sb = new StringBuffer();
            for (int j = 0; j < bmpWidth; j++) {
                int color = bmp.getPixel(j, i);

                int r = (color >> 16) & 0xff;
                int g = (color >> 8) & 0xff;
                int b = color & 0xff;

                // if color close to white,bit='0', else bit='1'
                if (r > 160 && g > 160 && b > 160)
                    sb.append("0");
                else
                    sb.append("1");
            }
            if (zeroCount > 0) {
                sb.append(zeroStr);
            }
            list.add(sb.toString());
        }

        List<String> bmpHexList = binaryListToHexStringList(list);
        String commandHexString = "1D763000";
        String widthHexString = Integer
                .toHexString(bmpWidth % 8 == 0 ? bmpWidth / 8
                        : (bmpWidth / 8 + 1));

        if (widthHexString.length() > 2) {
            Log.e("decodeBitmap error", " width is too large");
            return null;
        } else if (widthHexString.length() == 1) {
            widthHexString = "0" + widthHexString;
        }
        widthHexString = widthHexString + "00";

        String heightHexString = Integer.toHexString(bmpHeight);
        if (heightHexString.length() > 2) {
            System.out.println("heightHexString.length(): "+heightHexString.length()+" heightHexString: "+heightHexString);
            Log.e("decodeBitmap error", " height is too large");
            return null;
        } else if (heightHexString.length() == 1) {
            heightHexString = "0" + heightHexString;
        }
        heightHexString = heightHexString + "00";

        List<String> commandList = new ArrayList<String>();
        System.out.println("commandHexString+widthHexString+heightHexString"+ commandHexString+widthHexString+heightHexString);
        commandList.add(commandHexString+widthHexString+heightHexString);
        commandList.addAll(bmpHexList);

        return hexList2Byte(commandList);
    }

    

    public static byte[] BitmapToByteArray(Bitmap bmp){
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        bmp.compress(Bitmap.CompressFormat.JPEG,100,stream);
        byte[] byteArray = stream.toByteArray();

        return byteArray;
    }

    public static List<String> binaryListToHexStringList(List<String> list) {
        List<String> hexList = new ArrayList<String>();
        for (String binaryStr : list) {
            StringBuffer sb = new StringBuffer();
            for (int i = 0; i < binaryStr.length(); i += 8) {
                String str = binaryStr.substring(i, i + 8);

                String hexString = myBinaryStrToHexString(str);
                sb.append(hexString);
            }
            hexList.add(sb.toString());
        }
        return hexList;

    }

    public static String myBinaryStrToHexString(String binaryStr) {
        String hex = "";
        String f4 = binaryStr.substring(0, 4);
        String b4 = binaryStr.substring(4, 8);
        for (int i = 0; i < binaryArray.length; i++) {
            if (f4.equals(binaryArray[i]))
                hex += hexStr.substring(i, i + 1);
        }
        for (int i = 0; i < binaryArray.length; i++) {
            if (b4.equals(binaryArray[i]))
                hex += hexStr.substring(i, i + 1);
        }

        return hex;
    }

    public static byte[] hexList2Byte(List<String> list) {
        List<byte[]> commandList = new ArrayList<byte[]>();

        for (String hexStr : list) {
            commandList.add(hexStringToBytes(hexStr));
        }
        byte[] bytes = sysCopy(commandList);
        return bytes;
    }

    public static byte[] hexStringToBytes(String hexString) {
        if (hexString == null || hexString.equals("")) {
            return null;
        }
        hexString = hexString.toUpperCase();
        int length = hexString.length() / 2;
        char[] hexChars = hexString.toCharArray();
        byte[] d = new byte[length];
        for (int i = 0; i < length; i++) {
            int pos = i * 2;
            d[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1]));
        }
        return d;
    }

    public static byte[] sysCopy(List<byte[]> srcArrays) {
        int len = 0;
        for (byte[] srcArray : srcArrays) {
            len += srcArray.length;
        }
        byte[] destArray = new byte[len];
        int destLen = 0;
        for (byte[] srcArray : srcArrays) {
            System.arraycopy(srcArray, 0, destArray, destLen, srcArray.length);
            destLen += srcArray.length;
        }
        return destArray;
    }

    private static byte charToByte(char c) {
        return (byte) "0123456789ABCDEF".indexOf(c);
    }
}

这是发送打印的行:

    public void printPhotoLabel(Bitmap bitmap,byte[] arrrayImage) {
        System.out.println("Poller width: "+bitmap.getWidth()+" height: "+bitmap.getHeight());
        Bitmap rescaled = Bitmap.createScaledBitmap(bitmap,125,205,true);
        try {
            if(rescaled!=null){

                byte[] command = Utils.decodeBitmapBig(rescaled);
                outputStream.write(PrinterCommands.ESC_ALIGN_LEFT);
                outputStream.write(command);
            }else{
                System.out.println("Print Photo error, the file isn't exists");
            }
        } catch (Exception e) {
            System.out.println("image resource: "+e.getMessage());
        }
    }

我想打印缩放位图,使其比下图大一倍:

打印机非常瘦,因为我想打印旋转图像:

    我尝试通过
  1. outputStream热敏打印机中将位图图像打印为字节数组
  2. 我期望
  3. 打印一个字节数组具有良好标签高度的图像。
  4. 当高度大于**大于255**时,该功能无法像我希望的那样工作
java android-studio bluetooth thermal-printer
1个回答
0
投票
我解决了这个问题,在位图图像中进行了分割,然后为每个分割添加单独的打印命令

这是分割图像:

public static ArrayList<Bitmap> splitBitmap(Bitmap image){ try{ ArrayList<Bitmap> listImages = new ArrayList<Bitmap>(); double splits = (double) image.getHeight() / (double) 255; int splitInt = (int) Math.ceil((double) image.getHeight() / (double) 255); for(int i = 0; i < splitInt; i++){ if(i == (splitInt - 1)){ Bitmap bm1 = Bitmap.createBitmap(image, 0, (255 * i), image.getWidth(), (image.getHeight() - (255 * i))); listImages.add(bm1); } else{ Bitmap bm1 = Bitmap.createBitmap(image, 0, (255 * i), image.getWidth(), 255); listImages.add(bm1); } } return listImages; } catch (Exception ex){ System.out.println("split: "+ex.getMessage()); } return null; }
这是在列表中创建每个打印命令的方法:

public static byte[] decodeBitmapBig(ArrayList<Bitmap> bmp){ List<String> commandList = new ArrayList<String>(); for (int z = 0 ; z < bmp.size() ; z++){ int bmpWidth = bmp.get(z).getWidth(); int bmpHeight = bmp.get(z).getHeight(); List<String> list = new ArrayList<String>(); //binaryString list StringBuffer sb; int bitLen = bmpWidth / 8; int zeroCount = bmpWidth % 8; String zeroStr = ""; if (zeroCount > 0) { bitLen = bmpWidth / 8 + 1; for (int i = 0; i < (8 - zeroCount); i++) { zeroStr = zeroStr + "0"; } } for (int i = 0; i < bmpHeight; i++) { sb = new StringBuffer(); for (int j = 0; j < bmpWidth; j++) { int color = bmp.get(z).getPixel(j, i); int r = (color >> 16) & 0xff; int g = (color >> 8) & 0xff; int b = color & 0xff; // if color close to white,bit='0', else bit='1' if (r > 160 && g > 160 && b > 160) sb.append("0"); else sb.append("1"); } if (zeroCount > 0) { sb.append(zeroStr); } list.add(sb.toString()); } List<String> bmpHexList = binaryListToHexStringList(list); String commandHexString = "1D763000"; String widthHexString = Integer .toHexString(bmpWidth % 8 == 0 ? bmpWidth / 8 : (bmpWidth / 8 + 1)); if (widthHexString.length() > 2) { Log.e("decodeBitmap error", " width is too large"); return null; } else if (widthHexString.length() == 1) { widthHexString = "0" + widthHexString; } widthHexString = widthHexString + "00"; String heightHexString = Integer.toHexString(bmpHeight); if (heightHexString.length() > 2) { System.out.println("heightHexString.length(): "+heightHexString.length()+" heightHexString: "+heightHexString); Log.e("decodeBitmap error", " height is too large"); return null; } else if (heightHexString.length() == 1) { heightHexString = "0" + heightHexString; } heightHexString = heightHexString + "00"; System.out.println("commandHexString+widthHexString+heightHexString"+ commandHexString+widthHexString+heightHexString); commandList.add(commandHexString+widthHexString+heightHexString); commandList.addAll(bmpHexList); } return hexList2Byte(commandList); }
    
© www.soinside.com 2019 - 2024. All rights reserved.