android 连接网络打印机
// 定义编码方式
private static String encoding = null;
private Socket sock = null;
// 通过socket流进行读写
private OutputStream socketOut = null;
private OutputStreamWriter writer = null;
/**
* 初始化Pos实例
*
* @param ip
* 打印机IP
* @param port
* 打印机端口号
* @param encoding
* 编码
* @throws IOException
*/
public Pos(String ip, int port, String encoding) throws IOException {
sock = new Socket(ip, port);
socketOut = new DataOutputStream(sock.getOutputStream());
this.encoding = encoding;
writer = new OutputStreamWriter(socketOut, encoding);
}
/**
* 关闭IO流和Socket
*
* @throws IOException
*/
protected void closeIOAndSocket() throws IOException {
writer.close();
socketOut.close();
sock.close();
}
/**
* 打印二维码
*
* @param qrData
* 二维码的内容
* @throws IOException
*/
protected void qrCode(String qrData) throws IOException {
int moduleSize = 8;
int length = qrData.getBytes(encoding).length;
// 打印二维码矩阵
writer.write(0x1D);// init
writer.write("(k");// adjust height of barcode
writer.write(length + 3); // pl
writer.write(0); // ph
writer.write(49); // cn
writer.write(80); // fn
writer.write(48); //
writer.write(qrData);
writer.write(0x1D);
writer.write("(k");
writer.write(3);
writer.write(0);
writer.write(49);
writer.write(69);
writer.write(48);
writer.write(0x1D);
writer.write("(k");
writer.write(3);
writer.write(0);
writer.write(49);
writer.write(67);
writer.write(moduleSize);
writer.write(0x1D);
writer.write("(k");
writer.write(3); // pl
writer.write(0); // ph
writer.write(49); // cn
writer.write(81); // fn
writer.write(48); // m
writer.flush();
}
/**
* 进纸并全部切割
*
* @return
* @throws IOException
*/
protected void feedAndCut() throws IOException {
writer.write(0x1D);
writer.write(86);
writer.write(65);
// writer.write(0);
// 切纸前走纸多少
writer.write(10);
writer.flush();
// 另外一种切纸的方式
// byte[] bytes = {29, 86, 0};
// socketOut.write(bytes);
}
/**
* 打印换行
*
* @return length 需要打印的空行数
* @throws IOException
*/
protected void printLine(int lineNum) throws IOException {
for (int i = 0; i < lineNum; i++) {
writer.write("\n");
}
writer.flush();
}
/**
* 打印换行(只换一行)
*
* @throws IOException
*/
protected void printLine() throws IOException {
writer.write("\n");
writer.flush();
}
/**
* 打印空白(一个Tab的位置,约4个汉字)
*
* @param length
* 需要打印空白的长度,
* @throws IOException
*/
protected void printTabSpace(int length) throws IOException {
for (int i = 0; i < length; i++) {
writer.write("\t");
}
writer.flush();
}
/**
* 打印空白(一个汉字的位置)
*
* @param length
* 需要打印空白的长度,
* @throws IOException
*/
protected void printWordSpace(int length) throws IOException {
for (int i = 0; i < length; i++) {
writer.write(" ");
}
writer.flush();
}
protected void printSpace(int length)throws IOException{
for (int i = 0; i < length; i++) {
writer.write(" ");
}
writer.flush();
}
/**
* 打印位置调整
*
* @param position
* 打印位置 0:居左(默认) 1:居中 2:居右
* @throws IOException
*/
protected void printLocation(int position) throws IOException {
writer.write(0x1B);
writer.write(97);
writer.write(position);
writer.flush();
}
/**
* 绝对打印位置
*
* @throws IOException
*/
protected void printLocation(int light, int weight) throws IOException {
writer.write(0x1B);
writer.write(0x24);
writer.write(light);
writer.write(weight);
writer.flush();
}
/**
* 打印文字
*
* @param text
* @throws IOException
*/
protected void printText(String text) throws IOException {
String s = text;
byte[] content = s.getBytes("gbk");
socketOut.write(content);
socketOut.flush();
}
/**
* 新起一行,打印文字
*
* @param text
* @throws IOException
*/
protected void printTextNewLine(String text) throws IOException {
// 换行
writer.write("\n");
writer.flush();
String s = text;
byte[] content = s.getBytes("gbk");
socketOut.write(content);
socketOut.flush();
}
/**
* 初始化打印机
*
* @throws IOException
*/
protected void initPos() throws IOException {
writer.write(0x1B);
writer.write(0x40);
writer.flush();
}
/**
* 加粗
*
* @param flag
* false为不加粗
* @return
* @throws IOException
*/
protected void bold(boolean flag) throws IOException {
if (flag) {
// 常规粗细
writer.write(0x1B);
writer.write(69);
writer.write(0xF);
writer.flush();
} else {
// 加粗
writer.write(0x1B);
writer.write(69);
writer.write(0);
writer.flush();
}
}
/**
* 设置字体大小
* */
protected void setTextSize(int size)throws IOException{
writer.write(CMD_FontSize(size));
writer.flush();
}
// / <summary>
// / 字体的大小
// / </summary>
// / <param name="nfontsize">0:正常大小 1:两倍高 2:两倍宽 3:两倍大小 4:三倍高 5:三倍宽 6:三倍大小
// 7:四倍高 8:四倍宽 9:四倍大小 10:五倍高 11:五倍宽 12:五倍大小</param>
// / <returns></returns>
public String CMD_FontSize(int nfontsize) {
String _cmdstr = "";
// 设置字体大小
switch (nfontsize) {
case -1:
_cmdstr = new StringBuffer().append((char) 29).append((char) 33)
.append((char) 0).toString();// 29 33
break;
case 0:
_cmdstr = new StringBuffer().append((char) 29).append((char) 33)
.append((char) 0).toString();// 29 33
break;
case 1:
_cmdstr = new StringBuffer().append((char) 29).append((char) 33)
.append((char) 1).toString();
break;
case 2:
_cmdstr = new StringBuffer().append((char) 29).append((char) 33)
.append((char) 16).toString();
break;
case 3:
_cmdstr = new StringBuffer().append((char) 29).append((char) 33)
.append((char) 17).toString();
break;
case 4:
_cmdstr = new StringBuffer().append((char) 29).append((char) 33)
.append((char) 2).toString();
break;
case 5:
_cmdstr = new StringBuffer().append((char) 29).append((char) 33)
.append((char) 32).toString();
break;
case 6:
_cmdstr = new StringBuffer().append((char) 29).append((char) 33)
.append((char) 34).toString();
break;
case 7:
_cmdstr = new StringBuffer().append((char) 29).append((char) 33)
.append((char) 3).toString();
break;
case 8:
_cmdstr = new StringBuffer().append((char) 29).append((char) 33)
.append((char) 48).toString();
break;
case 9:
_cmdstr = new StringBuffer().append((char) 29).append((char) 33)
.append((char) 51).toString();
break;
case 10:
_cmdstr = new StringBuffer().append((char) 29).append((char) 33)
.append((char) 4).toString();
break;
case 11:
_cmdstr = new StringBuffer().append((char) 29).append((char) 33)
.append((char) 64).toString();
break;
case 12:
_cmdstr = new StringBuffer().append((char) 29).append((char) 33)
.append((char) 68).toString();
break;
}
return _cmdstr;
}
public String CMD_FontSize_BTP_M280(int size) {
String _cmdstr = "";
// 只有0和1两种模式
int fontsize = size;
switch (fontsize) {
case 1:
_cmdstr = new StringBuffer().append((char) 28).append((char) 33)
.append((char) 8).toString();
break;
case 2:
_cmdstr = new StringBuffer().append((char) 28).append((char) 33)
.append((char) 4).toString();
break;
case 3:
_cmdstr = new StringBuffer().append((char) 28).append((char) 87)
.append((char) 1).toString();
break;
default:
_cmdstr = new StringBuffer().append((char) 28).append((char) 87)
.append((char) 0).toString();
break;
}
return _cmdstr;
}
// ------------------------字体变大-----------------------------
/**
* 字体变大为标准的n倍
*
* @param num
* @return
*/
public static byte[] fontSizeSetBig(int num) {
byte realSize = 0;
switch (num) {
case 1:
realSize = 0;
break;
case 2:
realSize = 17;
break;
case 3:
realSize = 34;
break;
case 4:
realSize = 51;
break;
case 5:
realSize = 68;
break;
case 6:
realSize = 85;
break;
case 7:
realSize = 102;
break;
case 8:
realSize = 119;
break;
}
byte[] result = new byte[3];
result[0] = 29;
result[1] = 33;
result[2] = realSize;
return result;
}
完整的项目点击这里下载
android 连接网络打印机相关推荐
- Android连接网络打印机进行打印
本文转自:http://www.cnblogs.com/LIANQQ/p/4587637.html 做笔记 首先这是网络打印工具类,通过Socket实现,多说一句,网络打印机端口号一般默认的是9100 ...
- 计算机网络怎么查看连接打印机驱动,涨姿势:手把手教你如何连接网络打印机...
1Win7系统 棒棒哒 [中关村在线办公打印频道原创]在亚历山大的办公生活中,有时仅仅是一个小小的问题,都会让我们瞬间爆发,而处在办公生活中我们又难免会有些磕磕碰碰,你还想让那些微不足道的问题影响你的 ...
- win10pin不可用进不去系统_解决win7系统下连接网络打印机不可用的处理方法
解决win7系统下连接网络打印机不可用的处理方法 作者:电脑帮帮手 win7系统连接网络打印机,相对Xp系统更为智能化,它会自动帮你搜索可用打印机驱动默认给你安装,但是有些网络打印机驱动,自身系统未必 ...
- Android连接逍遥模拟器
Android连接逍遥模拟器 1.找到逍遥模拟器的路径,复制,如图所示 2.打开逍遥模拟器 3.window+R输入cmd 4.输入命令: (1)cd 逍遥模拟器路径(图1的复制) (2)adb.ex ...
- android连接打印机
android连接 网络打印,主要使用socket连接设备,发送指令给设备. 首先要有设备的IP,端口号一般默认的是9100 //打印设备网络IP etIp.setText("192.16 ...
- win10添加网络打印机_win10系统连接网络打印机
(注:以下方法连接适用于打印机是通过网线口连接有设置IP,局域网中某台电脑通过USB连接安装好打印机并共享的方式请查看之前分享的文章) Win10系统如何添加网络打印机: 提示:需要连接网络打印机的电 ...
- android 蓝牙地址连接打印机,android 连接蓝牙打印机 BluetoothAdapter
android 连接蓝牙打印机 BluetoothAdapter 源码下载地址:https://github.com/yylxy/BluetoothText.git public class Prin ...
- Android连接SQLServer详细教程(数据库+服务器+客户端),并在微软Azure云上搭建云服务
Android连接SQLServer详细教程(数据库+服务器+客户端),并在微软Azure云上搭建云服务 参考博客:http://blog.csdn.net/zhyl8157121/article/d ...
- 尼康d3200与智能设备 (android) 连接,,尼康D3200有WIFI功能吗
尼康D3200有WIFI功能吗 尼康D3200内置闪光灯,支持WIFI功能,拥有USB接口,3.5mm立体声麦克风接口和Mini HDMI输出接口.支持JPEG.RAW.JPEG+RAW.DCF 2. ...
最新文章
- Best jQuery Plugins of 2010
- linux字符设备文件的打开操作,Linux字符设备驱动模型之字符设备初始化
- React.Fragment 包裹标签
- ICML 2021文章引发热议:矩阵乘法无需相乘,速度提升100倍
- W10关闭不必要的服务
- 自定义对话框 提示:Unable to add window token null is not for an application
- centos安装virtualbox
- C语言核心技术-C语言概述与开发环境搭建
- asp实训报告摘要_个人网站实训报告---asp.NET 动态网站设计
- linux jmeter 内存,怎么在Linux下改变JMeter内存
- Vue:axios中POST请求传参问题
- 微信小游戏(打飞机1)
- 期货反跟单-镜像零滑点软件真有那么靠谱吗?
- 谷歌浏览器安装失败,解决办法
- 抖音最火的微信推送教程
- windows保护无法启动修复服务器,解决使用sfc命令提示“windows 资源保护无法启动修复服务”的方法...
- python一张纸超过8848_「一张纸的厚度」JavaScript问题:一张纸的厚度是0.0001米,将纸对折,对折多少次厚度超过珠峰高度8848米 - seo实验室...
- 查看修改qcow2文件
- [Python随笔]暴力解解决“崩铁”的引航罗盘解密
- 广播 onReceiver 的 context 可否启动 Activity,显示 Dialog
热门文章
- CAP(Cumulative Accuracy Profile)曲线/AR值释义
- java项目中使用hutool工具箱
- 家校互通平台“我的班”完成Pre-A 轮融资,弘帆资本投资...
- Square类型的移动支付,移动POS机
- 史上最全Eclipse快捷键,学会了开发效率翻倍
- 2021年刷脸支付市场前景怎么样?
- 火车头采集html文档没内容,火车头采集不到数据,源代码显示“访问验证”怎么处理?...
- 2018最新Kotlin基础视频教程终于上线了
- 卧槽,小编在搞事情 。。。
- 【机器学习】数据降维