一、打印的效果图,打印照片+二维码+文字

二、蓝牙相关组件介绍

    /*** 代表本地蓝牙适配器(蓝牙无线电)。BluetoothAdapter是所有蓝牙交互的入口。* 使用这个你可以发现其他蓝牙设备,查询已配对的设备列表,* 使用一个已知的MAC地址来实例化一个BluetoothDevice,* 以及创建一个BluetoothServerSocket来为监听与其他设备的通信。*/private BluetoothAdapter mBluetoothAdapter = null;/*** 代表一个远程蓝牙设备,使用这个来请求一个与远程设备的BluetoothSocket连接,* 或者查询关于设备名称、地址、类和连接状态等设备信息。*/private BluetoothDevice mBluetoothDevice = null;/*** 代表一个蓝牙socket的接口(和TCP Socket类似)。这是一个连接点,* 它允许一个应用与其他蓝牙设备通过InputStream和OutputStream交换数据。*/private BluetoothSocket mBluetoothSocket = null;

三、搜索蓝牙设备

    @Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_search_device);mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();//注册设备被发现时的广播IntentFilter filter1=new IntentFilter(BluetoothDevice.ACTION_FOUND);registerReceiver(mReceiver,filter1);//注册一个搜索结束时的广播IntentFilter filter2=new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);registerReceiver(mReceiver,filter2);IntentFilter filter3=new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_STARTED);registerReceiver(mReceiver,filter3);IntentFilter filter4=new IntentFilter(BluetoothAdapter.ACTION_REQUEST_ENABLE);  // 开关变化的广播registerReceiver(mReceiver,filter4);Button btn = (Button)findViewById(R.id.btnV1);   //  设备搜索btn.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {if(mBluetoothAdapter != null){if(!mBluetoothAdapter.isEnabled()){  // 蓝牙开关是否打开mBluetoothAdapter.enable();}else{Log.d(TAG,"mBluetoothAdapter.isEnabled() == true");}mBluetoothAdapter.startDiscovery();  // 开始搜索蓝牙设备,通过监听广播接收搜索到的设备}else{Toast.makeText(SearchBlueToothActivity.this,"该设备没有蓝牙模块",Toast.LENGTH_SHORT).show();}}});}/*** 接收搜索蓝牙设备结果的信息*/private BroadcastReceiver mReceiver = new BroadcastReceiver() {@Overridepublic void onReceive(Context context, Intent intent) {String action = intent.getAction();if(BluetoothDevice.ACTION_FOUND.equals(action)){BluetoothDevice device=intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);Log.d(TAG,"device = " + device);if(device.getBondState()==BluetoothDevice.BOND_BONDED){    //显示已配对设备Log.d(TAG,"已配对设备 : " + device.getName() + ", " + device.getName());}else{Log.d(TAG,"未配对设备 : " + device.getName() + ", " + device.getName());}}else if(BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)){Log.d(TAG,"搜索完成...");}else if(BluetoothAdapter.ACTION_DISCOVERY_STARTED.equals(action)){Log.d(TAG,"搜索开始...");}else if(BluetoothAdapter.ACTION_REQUEST_ENABLE.equals(action)){Log.d(TAG,"蓝牙打开...");}}};

四、蓝牙打印 // 这里写死了,要修改获取蓝牙设备的方式

public class BtPrint {private static BtPrint instance;public static String TAG = BtPrint.class.getSimpleName();private List<String> mpairedDeviceList = new ArrayList<String>();private BluetoothAdapter mBluetoothAdapter = null;private BluetoothDevice mBluetoothDevice = null;private BluetoothSocket mBluetoothSocket = null;private static final UUID SPP_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");private OutputStream mOutputStream = null;Set<BluetoothDevice> pairedDevices = null;/*** @return* @throws* @Title: getInstance*/public static BtPrint getInstance() {if (instance == null) {synchronized (BtPrint.class) {if (instance == null) {instance = new BtPrint();}}}return instance;}public void releasePrint(){mBluetoothAdapter = null;if(mBluetoothSocket != null){try {mBluetoothSocket.close();mBluetoothSocket = null;} catch (IOException e) {e.printStackTrace();Log.d(TAG,"releasePrint(),IOException :",e);mBluetoothSocket = null;}}}/*** 连接打印机设备*/public void connectPrinter(){mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();if(mBluetoothAdapter == null){   // 蓝牙功能没有打开,UI界面弹出打开蓝牙功能Log.d(TAG,"connectPrinter(),mBluetoothAdapter = null");}else if(mBluetoothAdapter.isEnabled()){  // 蓝牙已经打开// 列出已经配对过的设备String getName = mBluetoothAdapter.getName();Log.d(TAG, "getName = " + getName);pairedDevices = mBluetoothAdapter.getBondedDevices(); // 已经配对了的设备?while (mpairedDeviceList.size() > 1) {mpairedDeviceList.remove(1);}if (pairedDevices.size() == 0) {Log.d(TAG,"connectPrinter(),pairedDevices.size == 0");return;  // 没有配对过的设备}for (BluetoothDevice device : pairedDevices) {getName = device.getAddress();mpairedDeviceList.add(getName);//蓝牙名}String temString = mpairedDeviceList.get(0);Log.d(TAG,"temString = " + temString + ", mBluetoothDevice = " + mBluetoothDevice);mBluetoothDevice = mBluetoothAdapter.getRemoteDevice(temString);try {mBluetoothSocket = mBluetoothDevice.createRfcommSocketToServiceRecord(SPP_UUID);mBluetoothSocket.connect();Log.d(TAG,"mBluetoothDevice = " + mBluetoothDevice +", mBluetoothSocket = " + mBluetoothSocket);} catch (IOException e) {Log.d(TAG,"connectPrinter(),IOException :",e);}}else {  // 蓝牙未打开Log.d(TAG,"connectPrinter(),bluetooth isnot open");}}private  static int printTimes = 0;  // 出错时重试打印的次数/*** 打印小条*/public void printVerifyData(String tmpName,String tmpIdNo,String curName,String idcardNo,String headerPic) {if(mBluetoothSocket == null){connectPrinter();}else{if(!mBluetoothSocket.isConnected()){try {mBluetoothSocket.connect();} catch (IOException e) {e.printStackTrace();}}else{Log.d(TAG,"printVerifyData(),mBluetoothSocket.isConnected.");}}// 打印图片// 打印标题String title = Const.VALUE.TITLE;String line = ".......................................";try {mOutputStream = mBluetoothSocket.getOutputStream();PrintUtils PrintUtils = new PrintUtils();PrintUtils.setOutputStream(mOutputStream);PrintUtils.selectCommand(PrintUtils.RESET);PrintUtils.selectCommand(PrintUtils.LINE_SPACING_DEFAULT);PrintUtils.selectCommand(PrintUtils.LINE_SPACING);PrintUtils.selectCommand(PrintUtils.ALIGN_CENTER);PrintUtils.selectCommand(PrintUtils.BOLD);PrintUtils.selectCommand(PrintUtils.DOUBLE_HEIGHT_WIDTH);  //宽高加倍PrintUtils.printText(title + "\n");PrintUtils.selectCommand(PrintUtils.BOLD_CANCEL);PrintUtils.selectCommand(PrintUtils.NORMAL);PrintUtils.printText(line + "\n");// 打印照片Bitmap image = ImageUtils.getBitmap(headerPic);image = convertGreyImgByFloyd(image);byte[]imageData = PrinterUtils.decodeBitmap(image);PrintUtils.selectCommand(imageData);PrintUtils.printText(line + "\n");PrintUtils.selectCommand(PrintUtils.ALIGN_CENTER);PrintUtils.selectCommand(PrintUtils.BOLD);Log.d(TAG,"BtPrint # tmpName = " + tmpName + ",tmpIdNo = " + tmpIdNo);PrintUtils.printText(PrintUtils.printTwoData("姓名:", tmpName +"\n"));PrintUtils.printText(PrintUtils.printTwoData("身份证号码:", tmpIdNo+"\n"));PrintUtils.printText(PrintUtils.printTwoData("凭证有效期:", TimeUtils.date2String(new Date(),new SimpleDateFormat("yyyy年MM月dd日"))+"\n"));PrintUtils.selectCommand(PrintUtils.BOLD_CANCEL);PrintUtils.printText(line + "\n");String oriString = curName+"|" +idcardNo;Log.d(TAG,"BtPrint # oriString = " + oriString);String qrString = Base64.encodeToString(oriString.getBytes("UTF-8"),Base64.NO_WRAP);Log.d(TAG,"qrString = " + qrString);Bitmap qrCodeImage = QRCodeUtil.createQRCodeBitmap(qrString, 300, "0");byte[] qrData = PrinterUtils.decodeBitmap(qrCodeImage);PrintUtils.selectCommand(qrData);PrintUtils.printText(line + "\n");PrintUtils.selectCommand(PrintUtils.NORMAL);PrintUtils.selectCommand(PrintUtils.ALIGN_LEFT);PrintUtils.selectCommand(PrintUtils.BOLD);PrintUtils.selectCommand(PrintUtils.COLUMN_SPACING);String tips1 = " lajdflajdfl,"+ "\n";String tips2 = " lasdjflkajsdlfjalksdjflajsdflja"+"\n";PrintUtils.printText(tips1);PrintUtils.printText(tips2);PrintUtils.selectCommand(PrintUtils.BOLD_CANCEL);PrintUtils.selectCommand(PrintUtils.COLUMN_SPACING_CANCEL);PrintUtils.printText("\n\n");// 切纸mOutputStream = mBluetoothSocket.getOutputStream();mOutputStream.write(new byte[]{0x0a, 0x0a, 0x1d, 0x56, 0x01});mOutputStream.flush();BusEvent busEvent = new BusEvent();busEvent.action = "printer_completed";busEvent.data = "ok";EventBus.getDefault().post(busEvent);} catch (Exception e) {Log.d(TAG, "print error printTimes = " + printTimes);if(printTimes < 5){printTimes ++;printVerifyData(tmpName,tmpIdNo,curName,idcardNo,headerPic);}else{Log.d(TAG, "print error :", e);LogUtils.file(e.getCause() + "--" + e.getMessage());sendPrintErr();}}}/*** 发送打印错误的信息*/private void sendPrintErr(){BusEvent busEvent = new BusEvent();busEvent.action = "printer_completed";busEvent.data = "error";EventBus.getDefault().post(busEvent);BtPrint.getInstance().releasePrint();}public Bitmap convertGreyImgByFloyd(Bitmap img) {int width = img.getWidth();//获取位图的宽int height = img.getHeight();//获取位图的高 \int[] pixels = new int[width * height];//通过位图的大小创建像素点数组img.getPixels(pixels, 0, width, 0, 0, width, height);int[] gray = new int[height * width];for (int i = 0; i < height; i++) {for (int j = 0; j < width; j++) {int grey = pixels[width * i + j];int red = ((grey & 0x00FF0000) >> 16);gray[width * i + j] = red;}}int e = 0;for (int i = 0; i < height; i++) {for (int j = 0; j < width; j++) {int g = gray[width * i + j];if (g >= 128) {pixels[width * i + j] = 0xffffffff;e = g - 255;} else {pixels[width * i + j] = 0xff000000;e = g - 0;}if (j < width - 1 && i < height - 1) {//右边像素处理gray[width * i + j + 1] += 3 * e / 8;//下gray[width * (i + 1) + j] += 3 * e / 8;//右下gray[width * (i + 1) + j + 1] += e / 4;} else if (j == width - 1 && i < height - 1) {//靠右或靠下边的像素的情况//下方像素处理gray[width * (i + 1) + j] += 3 * e / 8;} else if (j < width - 1 && i == height - 1) {//右边像素处理gray[width * (i) + j + 1] += e / 4;}}}Bitmap mBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);mBitmap.setPixels(pixels, 0, width, 0, 0, width, height);return mBitmap;}
}

PrintUtils


import android.annotation.SuppressLint;import java.io.IOException;
import java.io.OutputStream;
import java.nio.charset.Charset;public class PrintUtils {private static final int LINE_BYTE_SIZE = 32;private static OutputStream outputStream = null;public static void setOutputStream(OutputStream outputStream) {PrintUtils.outputStream = outputStream;}/*** 打印文字** @param text 要打印的文字*/public static void printText(String text) {try {byte[] data = text.getBytes("gbk");outputStream.write(data, 0, data.length);outputStream.flush();} catch (IOException e) {e.printStackTrace();}}/*** 设置打印格式** @param command 格式指令*/public static void selectCommand(byte[] command) {try {outputStream.write(command);outputStream.flush();} catch (IOException e) {//Toast.makeText(this.context, "发送失败!", Toast.LENGTH_SHORT).show();e.printStackTrace();}}/*** 复位打印机*/public static final byte[] RESET = {0x1b, 0x40};/*** 左对齐*/public static final byte[] ALIGN_LEFT = {0x1b, 0x61, 0x00};/*** 中间对齐*/public static final byte[] ALIGN_CENTER = {0x1b, 0x61, 0x01};/*** 选择加粗模式*/public static final byte[] BOLD = {0x1b, 0x45, 0x01};/*** 取消加粗模式*/public static final byte[] BOLD_CANCEL = {0x1b, 0x45, 0x00};/*** 宽高加倍*/public static final byte[] DOUBLE_HEIGHT_WIDTH = {0x1d, 0x21, 0x11};/*** 字体不放大*/public static final byte[] NORMAL = {0x1d, 0x21, 0x00};/*** 设置默认行间距*/public static final byte[] LINE_SPACING_DEFAULT = {0x1b, 0x32};//    /**
//     * 设置行间距
//     */public static final byte[] LINE_SPACING = {0x1b, 0x33, 0x50};  // 20的行间距(0,255)/*** 设置字符间距*/public static final byte[] COLUMN_SPACING = {0x1b, 0x20, 0x25};/*** 取消设置字符间距*/public static final byte[] COLUMN_SPACING_CANCEL = {0x1b, 0x20, 0x00};/*** 打印两列** @param leftText  左侧文字* @param rightText 右侧文字* @return*/@SuppressLint("NewApi")public static String printTwoData(String leftText, String rightText) {StringBuilder sb = new StringBuilder();int leftTextLength = getBytesLength(leftText);int rightTextLength = getBytesLength(rightText);sb.append(leftText);// 计算两侧文字中间的空格int marginBetweenMiddleAndRight = LINE_BYTE_SIZE - leftTextLength - rightTextLength;for (int i = 0; i < marginBetweenMiddleAndRight; i++) {sb.append(" ");}sb.append(rightText);return sb.toString();}/*** 获取数据长度** @param msg* @return*/@SuppressLint("NewApi")private static int getBytesLength(String msg) {return msg.getBytes(Charset.forName("GB2312")).length;}
}

Android蓝牙打印机打印图片文字相关推荐

  1. android蓝牙打印机打印图片,如何使用打印机(通过蓝牙打印)从Android设备打印图像和一些数据?...

    尝试使用这个-. public class BluetoothPrinterActivity extends Activity { BluetoothAdapter mBTAdapter; Bluet ...

  2. 微信小程序连接蓝牙打印机打印图片示例

    微信小程序连接蓝牙打印机示例 完整的代码示例请点击看github 小程序连接蓝牙打印机打印文本与二维码等示例在 github 上都能找到一些,唯独打印图片这个案例几乎没有.希望能帮助到有打印图片需求的 ...

  3. 打印机打印图片文字有重影

    打印机打印图片出来的文字有重影 解决办法: 打印机属性里面"纸张/质量"选项里"颜色"选项选择黑白,不过打印有彩色有黑字的照片还是个问题

  4. android 蓝牙打印机(ESC/POS 热敏打印机),打印菜单小票和图片,对蓝牙配对和连接打印功能进行了封装,让你超快实现蓝牙打印功能

    BluetoothPrint 项目地址:liuGuiRong18/BluetoothPrint  简介:android 蓝牙打印机(ESC/POS 热敏打印机),打印菜单小票和图片,对蓝牙配对和连接打 ...

  5. uni-app H5+ 连接蓝牙打印机打印文字及二维码

    基于Native.js 实现的连接蓝牙打印机 打印效果图 核心代码 测试代码 运行设备及环境 PS: PPS: Demo 打印效果图 核心代码 /*** @Description: 蓝牙打印类 基于h ...

  6. Android 实现系统打印机打印图片,文本,以及二维码生成与解析

    打码机:扫码,生成打印自定义标签   一.Android 打印机要支持,网络.WiFi :手机与打印机在同一网络下才行,本文用WiFi连接 打开系统打印服务: 设置--更多设置--打印--选择默认打印 ...

  7. mui android连接蓝牙打印机打印

    android设备连蓝牙打印机打印,代码如下:  mui.plusReady(function(){             main = plus.android.runtimeMainActivi ...

  8. 蓝牙打印 设置打印样式_GitHub - shen173869710/PrintUtils: Android蓝牙打印机,带你真正了解各种打印格式。...

    PrintUtils Android蓝牙打印机,带你真正了解各种打印格式. 效果图如下: 具体用法是: (1)手机通过蓝牙连接打印机 (2)从BluetoothSocket中getOutputStre ...

  9. 小程序蓝牙打印机CPCL图片打印问题梳理

    小程序蓝牙打印机CPCL指令图片打印问题梳理 本文以汉印HM-T3便携式打印机示例讲解 手册中要求需要用ASCII码十六进制字符下发命令,源引自 有赞零售小票打印跨平台解决方案 图片处理 由于 JS ...

  10. delphi android 打印机,用delphi控制小票打印机打印图片

    用delphi控制小票打印机打印图片 unit PosPrintBmp; {************************************************************** ...

最新文章

  1. golang 编译后文件过大问题处理
  2. oracle:SAVEPOINT(保存点)
  3. python文件读取 ,json文件的存储
  4. java高级数据类型_最新精品JAVA高级备课完美版——Java基本数据类型.pdf
  5. Apache Flink 零基础入门(六)Flink核心概念
  6. mvc登录设计的详细设计_产品设计:APP指纹密码登录设计
  7. “疫”外爆发:没那么简单的视频会议
  8. Centos7静态ip设置(亲测有效)
  9. 建议收藏!数据中台行业发展概况及展望
  10. 已知信码序列为1011_信息论与编码理论习题答案解析.doc
  11. kasp技术原理_KASP基因型测定技术在各个学科的应用实践(内附官方重要资料)...
  12. 对象序列化与反序列化(二进制 byte[])
  13. 无痕 PS、读得懂文字,OpenAI 的二代 DALL·E 惊艳亮相
  14. FastDFS原理及维护
  15. 一文读懂 delete和delete[ ]
  16. 老司机带你飞——DIY桌面小电视
  17. CCIE理论-第十二篇-IPV6-NDP协议
  18. vue动态面包屑导航的使用
  19. 【GD32F310开发板试用】PWM+TMC5160驱动步进电机
  20. PyTorch中 nn.Conv2d与nn.ConvTranspose2d函数的用法

热门文章

  1. Excel排序、筛选
  2. Zemax操作38--POP(物理光学传播)的用法
  3. band math函数_波段运算(bandmath)工具中常用的函数
  4. x射线微型计算机断层扫描技术,X射线断层扫描技术,分析缺陷对性能影响的利器...
  5. DP83848IVV硬件电路设计
  6. java JNI调用流程
  7. 最新海康摄像机、NVR、流媒体服务器、回放取流RTSP地址规则说明
  8. android apk反编译(Doapk工具和dex2jar工具介绍)
  9. RS-485半双工延时问题
  10. 非线性动力学中的同步,同步有哪几类?