Android使用串口打印机打印图片方法

1.将图片放到assets目录中

2.读取assetes中的图片
Bitmap  getBitmap=   getImageFromAssetsFile(context,name);
String data="";
if(getBitmap!=null){
    data=sendPhoto(context,getBitmap);
}

3.组装打印命令

  printMessage=“打印机命令”+data

4.发送命令

com.sendHex(printMessage)

/**
 * 从Assets中读取图片
 */
public static Bitmap getImageFromAssetsFile(Context context,String fileName) {
    Bitmap image = null;
    AssetManager am = context.getResources().getAssets();
    try {
        InputStream is = am.open(fileName);
        image = BitmapFactory.decodeStream(is);
        is.close();
    } catch (IOException e) {
        e.printStackTrace();
    }

    return image;

}

  /**
     * 将图片转换成十六进制字符串
     * @param
     * @return
     */
    public static String sendPhoto(Context context,Bitmap bitmap) {
        int width = bitmap.getWidth();
        int height = bitmap.getHeight(); 

        int widthByte = (width - 1) / 8 + 1;
        int[] pixels = new int[width * height];
        bitmap.getPixels(pixels, 0, width, 0, 0, width, height);
        List<byte[]> dataList = new ArrayList<>();
        ///图片二值化处理
        for (int y = 0; y < height; y++) {
            byte[] rowData = new byte[widthByte];
            byte temp = 0;
            int offset;
            for (int x = 0; x < width; x++) {
                int pixel = pixels[width * y + x];
                int alpha = pixel >> 24 & 0xFF;
                int red = pixel >> 16 & 0xFF;
                int green = pixel >> 8 & 0xFF;
                int blue = pixel & 0xFF;
                int value = alpha == 0 ? 0 : (int) ((float) red * 0.3 + (float) green * 0.59 + (float) blue * 0.11) > 127 ? 0 : 1;
                offset = x % 8;
                if (value == 1) {
                    temp |= (0x80 >> offset);
                }
                if (offset == 7 || x >= width - 1) {
                    rowData[x / 8] = temp;
                    temp = 0;
                }
            }
            
            dataList.add(rowData);
        }
        String photoStr="";  
        StringBuilder dataGet=new StringBuilder();
        for (int i = 0; i < height; i++) {
            dataGet.append(byte2hex(dataList.get(i)));
        }
        photoStr+=photoStr+dataGet.toString();
        return photoStr;


    }

/**
 * 转16进制字符串
 * @param b
 * @return
 */
public static String byte2hex(byte[] b)
{
    StringBuffer sb = new StringBuffer();
    String stmp = "";
    for (int n = 0; n < b.length; n++) {
        stmp = Integer.toHexString(b[n] & 0XFF);
        if (stmp.length() == 1) {
            sb.append("0" + stmp);
        } else {
            sb.append(stmp);
        }


    }
    return sb.toString().toUpperCase();
}
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值