Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
M
miya-hardware
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
pengguangpu
miya-hardware
Commits
13d6bbf2
Commit
13d6bbf2
authored
Jan 23, 2019
by
pengguangpu
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
添加商米的打印驱动,测试发现无法正常打印条码和二维码,待调试
parent
eb5a9693
Hide whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
739 additions
and
47 deletions
+739
-47
BasePrinter.java
print/src/main/java/com/miya/print/BasePrinter.java
+1
-1
ChuangjiePrinter.java
print/src/main/java/com/miya/print/ChuangjiePrinter.java
+2
-1
PrinterManager.java
print/src/main/java/com/miya/print/PrinterManager.java
+56
-10
SangdaPrinter.java
print/src/main/java/com/miya/print/SangdaPrinter.java
+3
-1
SunmiK1Printer.java
print/src/main/java/com/miya/print/SunmiK1Printer.java
+635
-0
YingtaiPrinter.java
print/src/main/java/com/miya/print/YingtaiPrinter.java
+1
-0
SunmiWeighing.java
weighing/src/main/java/com/miya/weighing/SunmiWeighing.java
+32
-31
WeighingManager.java
...hing/src/main/java/com/miya/weighing/WeighingManager.java
+9
-3
No files found.
print/src/main/java/com/miya/print/BasePrinter.java
View file @
13d6bbf2
...
...
@@ -6,7 +6,7 @@ import android.graphics.Bitmap;
/**
* 打印基类
*/
public
class
BasePrinter
implements
IPrinter
{
public
abstract
class
BasePrinter
implements
IPrinter
{
/**
* 80mm打印纸允许打印的最大图片的宽度,此为经验值
...
...
print/src/main/java/com/miya/print/ChuangjiePrinter.java
View file @
13d6bbf2
...
...
@@ -43,13 +43,14 @@ public class ChuangjiePrinter extends BasePrinter {
@Override
public
boolean
init
(
Context
context
)
{
D
.
i
(
TAG
,
"开始初始化创捷打印机"
);
this
.
context
=
context
;
if
(
mUsbDriver
==
null
)
{
getUsbDriverService
(
context
);
}
// USB线已经连接
getUsbDriverService
();
return
connet
()
==
0
;
return
connet
()
==
PrinterStatusEnum
.
CODE_SUCCESS
.
status
;
}
@Override
...
...
print/src/main/java/com/miya/print/PrinterManager.java
View file @
13d6bbf2
...
...
@@ -17,7 +17,7 @@ public class PrinterManager {
enum
Type
{
//这里添加扩展的驱动
// TYPE_HISENSE("hisense", ChuangjiePrinter.class.getName()),
TYPE_SUNMI
(
"s
hangmik1"
,
Shang
miK1Printer
.
class
.
getName
()),
TYPE_SUNMI
(
"s
unmiK1"
,
Sun
miK1Printer
.
class
.
getName
()),
TYPE_YINGTAI
(
"yingtai"
,
YingtaiPrinter
.
class
.
getName
()),
TYPE_CHUANGJIE
(
"chuangjie"
,
ChuangjiePrinter
.
class
.
getName
()),
TYPE_SANGDA
(
"sangda"
,
SangdaPrinter
.
class
.
getName
());
...
...
@@ -73,10 +73,10 @@ public class PrinterManager {
*/
public
boolean
init
(
Context
context
)
{
for
(
Type
type
:
Type
.
values
())
{
if
(
printer
!=
null
)
{
printer
.
release
();
}
try
{
if
(
printer
!=
null
)
{
printer
.
release
();
}
Class
printerCls
=
Class
.
forName
(
type
.
clsName
);
printer
=
(
IPrinter
)
printerCls
.
newInstance
();
boolean
result
=
printer
.
init
(
context
);
...
...
@@ -91,7 +91,11 @@ public class PrinterManager {
}
//没有找到对应的打印机
if
(
printer
!=
null
)
{
printer
.
release
();
try
{
printer
.
release
();
}
catch
(
Exception
ex
)
{
ex
.
printStackTrace
();
}
}
D
.
i
(
TAG
,
"没找到对应的打印机器,请检查"
);
return
false
;
...
...
@@ -104,16 +108,58 @@ public class PrinterManager {
*/
public
boolean
release
()
{
if
(
isConnected
())
{
boolean
result
=
printer
.
release
();
if
(
result
==
false
)
{
return
false
;
try
{
boolean
result
=
printer
.
release
();
if
(
result
==
false
)
{
return
false
;
}
printer
=
null
;
return
true
;
}
catch
(
Exception
ex
)
{
ex
.
printStackTrace
();
}
printer
=
null
;
return
true
;
}
return
true
;
}
/**
* 保险起见提供指定硬件的初始化
* ps:谨慎调用
*
* @param context
* @param type 指定硬件类型
* @return
*/
public
boolean
init
(
Context
context
,
Type
type
)
{
if
(
type
==
null
)
{
return
false
;
}
//是否支持指定硬件
boolean
isSupported
=
false
;
for
(
Type
tmpType
:
Type
.
values
())
{
if
(
type
.
typeName
.
equals
(
tmpType
.
typeName
))
{
isSupported
=
true
;
}
}
if
(
isSupported
==
false
)
{
return
false
;
}
//反射新建实例
try
{
Class
printerCls
=
Class
.
forName
(
type
.
clsName
);
printer
=
(
IPrinter
)
printerCls
.
newInstance
();
boolean
result
=
printer
.
init
(
context
);
if
(
result
==
true
)
{
printer
.
setPrinterName
(
type
.
typeName
);
D
.
i
(
TAG
,
"初始化打印机成功,打印机器为:"
+
type
.
typeName
);
}
return
result
;
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
return
false
;
}
/**
* 获取当前打印接口
*
...
...
print/src/main/java/com/miya/print/SangdaPrinter.java
View file @
13d6bbf2
...
...
@@ -30,6 +30,7 @@ public class SangdaPrinter extends BasePrinter {
@Override
public
boolean
init
(
Context
context
)
{
D
.
i
(
TAG
,
"开始初始化桑达打印机"
);
super
.
init
(
context
);
mPrinter
=
PrinterAPI
.
getInstance
();
if
(
isConnect
())
{
...
...
@@ -45,8 +46,9 @@ public class SangdaPrinter extends BasePrinter {
return
false
;
}
}
catch
(
Exception
e
)
{
D
.
e
(
TAG
,
"桑达打印机连接失败"
);
e
.
printStackTrace
(
);
}
D
.
e
(
TAG
,
"桑达打印机连接失败"
);
return
false
;
}
...
...
print/src/main/java/com/miya/print/S
hang
miK1Printer.java
→
print/src/main/java/com/miya/print/S
un
miK1Printer.java
View file @
13d6bbf2
...
...
@@ -9,19 +9,24 @@ import android.os.IBinder;
import
android.os.RemoteException
;
import
com.miya.print.exception.PrinterException
;
import
com.miya.print.utils.BitmapUtils
;
import
com.miya.print.utils.BytesUtil
;
import
com.miya.print.utils.D
;
import
com.miya.print.utils.ESCUtil
;
import
com.sunmi.extprinterservice.ExtPrinterService
;
import
java.io.ByteArrayOutputStream
;
import
java.io.IOException
;
class
S
hang
miK1Printer
extends
BasePrinter
{
class
S
un
miK1Printer
extends
BasePrinter
{
final
static
String
TAG
=
S
hang
miK1Printer
.
class
.
getSimpleName
();
final
static
String
TAG
=
S
un
miK1Printer
.
class
.
getSimpleName
();
private
ServiceConnection
connService
=
new
ServiceConnection
()
{
private
static
final
String
SERVICE
_
PACKAGE
=
"com.sunmi.extprinterservice"
;
private
static
final
String
SERVICE
_
ACTION
=
"com.sunmi.extprinterservice.PrinterService"
;
private
ExtPrinterService
extPrinterService
;
private
ServiceConnection
connService
=
new
ServiceConnection
()
{
@Override
public
void
onServiceDisconnected
(
ComponentName
name
)
{
extPrinterService
=
null
;
...
...
@@ -29,24 +34,288 @@ class ShangmiK1Printer extends BasePrinter {
@Override
public
void
onServiceConnected
(
ComponentName
name
,
IBinder
service
)
{
D
.
i
(
TAG
,
"商米K1服务连接成功"
);
extPrinterService
=
ExtPrinterService
.
Stub
.
asInterface
(
service
);
try
{
int
ret
=
extPrinterService
.
printerInit
();
if
(
ret
==
0
)
{
D
.
i
(
TAG
,
"商米K1服务printerInit成功"
);
}
else
{
D
.
i
(
TAG
,
"商米K1服务printerInit失败"
);
}
}
catch
(
RemoteException
e
)
{
e
.
printStackTrace
();
D
.
i
(
TAG
,
"商米K1服务printerInit失败:"
+
e
.
getMessage
());
}
}
};
@Override
public
boolean
init
(
Context
context
)
{
return
super
.
init
(
context
);
D
.
i
(
TAG
,
"开始初始化商米K1打印机"
);
this
.
context
=
context
;
Intent
intent
=
new
Intent
();
intent
.
setPackage
(
SERVICE
_
PACKAGE
);
intent
.
setAction
(
SERVICE
_
ACTION
);
context
.
getApplicationContext
().
startService
(
intent
);
boolean
result
=
context
.
getApplicationContext
().
bindService
(
intent
,
connService
,
Context
.
BIND_AUTO_CREATE
);
if
(
result
==
false
)
{
D
.
i
(
TAG
,
"商米K1打印机初始化失败"
);
return
false
;
}
D
.
i
(
TAG
,
"商米K1打印机初始化成功"
);
return
true
;
}
@Override
public
boolean
release
()
{
return
super
.
release
();
if
(
this
.
context
!=
null
&&
connService
!=
null
)
{
this
.
context
.
unbindService
(
connService
);
}
context
=
null
;
extPrinterService
=
null
;
return
true
;
}
private
static
final
String
SERVICE
_
PACKAGE
=
"com.sunmi.extprinterservice"
;
private
static
final
String
SERVICE
_
ACTION
=
"com.sunmi.extprinterservice.PrinterService"
;
/**
* 虽然商米机器提供了加粗字体功能,但为了统一接口,暂时抹掉字体加粗选项
* 虽然商米机器提供了下划线字体功能,但为了统一接口,暂时抹掉下划线字体选项
*
* @param align
* @param size
* @param content
* @param isFeed
* @return
*/
@Override
public
int
printText
(
int
align
,
int
size
,
String
content
,
boolean
isFeed
)
{
if
(
extPrinterService
==
null
)
{
throw
new
PrinterException
(
PrinterStatusEnum
.
CODE_FAILED
.
status
,
"服务已断开!"
);
}
try
{
int
ret
=
setAlign
(
align
);
ret
=
extPrinterService
.
printText
(
content
);
if
(
isFeed
)
{
ret
=
feedPaper
();
}
return
changeStatus2Custom
(
ret
);
}
catch
(
RemoteException
e
)
{
throw
new
PrinterException
(
PrinterStatusEnum
.
CODE_FAILED
.
status
,
e
.
getMessage
());
}
}
private
ExtPrinterService
extPrinterService
;
@Override
public
int
setAlign
(
int
align
)
{
if
(
extPrinterService
==
null
)
{
throw
new
PrinterException
(
PrinterStatusEnum
.
CODE_FAILED
.
status
,
"服务已断开!"
);
}
try
{
int
ret
=
PrinterStatusEnum
.
CODE_SUCCESS
.
getStatus
();
if
(
this
.
align
!=
align
&&
(
align
==
0
||
align
==
1
||
align
==
2
))
{
this
.
align
=
align
;
ret
=
extPrinterService
.
setAlignMode
(
align
);
}
return
changeStatus2Custom
(
ret
);
}
catch
(
RemoteException
e
)
{
throw
new
PrinterException
(
PrinterStatusEnum
.
CODE_FAILED
.
status
,
e
.
getMessage
());
}
}
/**
* 打印条形码 symbology条码类型
*
* @param code 打印条形码 symbology条码类型
* @param type 条形码类型 0:UPC-A 1:UPC-E 2:EAN13 3:EAN8 4:CODE39 5:ITF 6:CODABAR 7:CODE93 8:CODE128
* @param width 条形码宽度 2-6 像素 (若条码宽度设置过宽,整个条码超过纸张宽度将不会输出条码内容)
* @param height 条形码⾼度 1-255 像素
* @param hriPos HRI位置 0:不打印 1:条形码上⽅ 2:条形码下⽅ 3:条形码上下⽅
* @desc 条码类型说明:
* code39 最⻓打印 13 个数字
* ⽅法 int setFontZoom(int hori, int veri)
* 说明 由于打印机硬字库限制,字体⼤⼩仅能倍数放⼤,本⽅法可以控制字体在横向和纵向⽅向的放⼤
* 倍数
* 输⼊ hori 、veri 的范围为1-8,表示在横向、纵向上字体的放⼤倍数,若设置在范围外将返回错误参数
* 返回 ⻅接⼝返回值
* ⽅法 int printColumnsText(String[] colsTextArr, int[] colsWidthArr, int[] colsAlign)
* 说明
* 以表格⽅式输出打印内容,每个数组表示在此列上的数据及格式,需要多次调⽤此⽅法才可以达
* 到表格输出的样式效果
* ⚠ 注意:调⽤此⽅法后会将之前设置样式初始化!
* 输⼊
* colsTextArr: 每列要打印的内容,⽀持中⽂及ascii码
* colsWidthArr:每列可容纳的最⼤字符数量,字符数以ascii码个数为单位(⼀个中⽂等于两个ascii
* 码数量),当⽂本内容超出可容纳的最⼤数量时将移动到本列下⼀⾏,当所有列的最⼤字符数量
* 超出⼀⾏所能容纳的字符数时,将不会打印
* colsAlign:每列内容的对⻬⽅式,仅当内容字符数⼩于最⼤字符数量时才有效果
* 返回 ⻅接⼝返回值
* code93 最⻓打印 17 个数字
* code128 code128 分三类:{A,{B,{C;A 类:包⼤写字⺟、数字、标 点等,B 类:⼤⼩写字⺟,数
* 字;C 类:纯数字;默认 B 类编码,若要使本编码需在内容前⾯加“{A”、“{B” 、“{C”声明使⽤的类
* 型,可以混合使⽤,例如: “{A2344A”,”{C123123”,”{A1A{B13Bxc{C12”。
* ean8 要求 8 位数字(最后⼀位校验位),有效⻓度 8 个数字
* ean13 有效⻓度 13 个数字,其中最后⼀位为校验位
* ITF 要求输⼊数字,且有效⼩于 14 位,必须是偶数位
*/
@Override
public
int
printBarcode
(
int
align
,
String
content
,
boolean
isFeed
)
{
if
(
extPrinterService
==
null
)
{
throw
new
PrinterException
(
PrinterStatusEnum
.
CODE_FAILED
.
status
,
"服务已断开!"
);
}
try
{
int
ret
=
setAlign
(
align
);
//打印的样式默认为CODE128码,数字在条码下面
ret
=
extPrinterService
.
printBarCode
(
content
,
8
,
2
,
120
,
2
);
//强制走两行纸才能生效
ret
=
extPrinterService
.
lineWrap
(
2
);
if
(
isFeed
)
{
ret
=
feedPaper
();
}
return
changeStatus2Custom
(
ret
);
}
catch
(
RemoteException
e
)
{
e
.
printStackTrace
();
throw
new
PrinterException
(
PrinterStatusEnum
.
CODE_FAILED
.
status
,
e
.
getMessage
());
}
}
@Override
public
int
printQrcode
(
int
align
,
String
content
,
boolean
isFeed
)
{
if
(
extPrinterService
==
null
)
{
throw
new
PrinterException
(
PrinterStatusEnum
.
CODE_FAILED
.
status
,
"服务已断开!"
);
}
try
{
int
ret
=
setAlign
(
align
);
/**
* code 要打印的⼆维码内容,默认为utf-8字符集
* modeSize ⼆维码块⼤⼩ 1-16 像素点
* errorlevel ⼆维码纠错等级 0-3 四个等级
*/
ret
=
extPrinterService
.
printQrCode
(
content
,
4
,
3
);
//强制走两行纸才能生效
ret
=
extPrinterService
.
lineWrap
(
2
);
if
(
isFeed
)
{
ret
=
feedPaper
();
}
return
changeStatus2Custom
(
ret
);
}
catch
(
RemoteException
ex
)
{
ex
.
printStackTrace
();
throw
new
PrinterException
(
PrinterStatusEnum
.
CODE_FAILED
.
status
,
ex
.
getMessage
());
}
}
@Override
public
int
printImage
(
int
align
,
Bitmap
bm
,
boolean
isFeed
)
{
if
(
extPrinterService
==
null
)
{
throw
new
PrinterException
(
PrinterStatusEnum
.
CODE_FAILED
.
status
,
"服务已断开!"
);
}
try
{
int
ret
=
setAlign
(
align
);
//判断bitmap宽高
Bitmap
actualBm
=
null
;
if
(
bm
.
getWidth
()
>
MAX_IMG_WIDTH_80MM
)
{
actualBm
=
BitmapUtils
.
scaleBitmap
(
bm
,
MAX_IMG_WIDTH_80MM
,
0
);
/**
* 将bitmap图转成光栅位图⽅式打印
* 此⽅法适合打印宽度在打印纸内的图⽚
* 数据过⼤时串⼝流控模式会阻塞,⽆流控时会丢数据
*/
ret
=
extPrinterService
.
printBitmap
(
actualBm
,
0
);
}
else
{
/**
* 将bitmap图转成光栅位图⽅式打印
* 此⽅法适合打印宽度在打印纸内的图⽚
* 数据过⼤时串⼝流控模式会阻塞,⽆流控时会丢数据
*/
ret
=
extPrinterService
.
printBitmap
(
bm
,
0
);
}
if
(
actualBm
!=
null
)
{
actualBm
.
recycle
();
}
if
(
isFeed
)
{
ret
=
feedPaper
();
}
return
changeStatus2Custom
(
ret
);
}
catch
(
RemoteException
e
)
{
throw
new
PrinterException
(
PrinterStatusEnum
.
CODE_FAILED
.
status
,
e
.
getMessage
());
}
}
@Override
public
int
getPrinterStatus
()
{
if
(
extPrinterService
==
null
)
{
throw
new
PrinterException
(
PrinterStatusEnum
.
CODE_FAILED
.
status
,
"服务已断开!"
);
}
try
{
int
ret
=
extPrinterService
.
getPrinterStatus
();
return
changeStatus2Custom
(
ret
);
}
catch
(
RemoteException
e
)
{
e
.
printStackTrace
();
throw
new
PrinterException
(
PrinterStatusEnum
.
CODE_FAILED
.
status
,
e
.
getMessage
());
}
}
@Override
public
int
feedPaper
()
{
if
(
extPrinterService
==
null
)
{
throw
new
PrinterException
(
PrinterStatusEnum
.
CODE_FAILED
.
status
,
"服务已断开!"
);
}
try
{
int
ret
=
extPrinterService
.
lineWrap
(
1
);
return
changeStatus2Custom
(
ret
);
}
catch
(
RemoteException
e
)
{
throw
new
PrinterException
(
PrinterStatusEnum
.
CODE_FAILED
.
status
,
e
.
getMessage
());
}
}
@Override
public
int
cutPaper
()
{
if
(
extPrinterService
==
null
)
{
throw
new
PrinterException
(
PrinterStatusEnum
.
CODE_FAILED
.
status
,
"服务已断开!"
);
}
try
{
int
ret
=
extPrinterService
.
cutPaper
(
0
,
0
);
return
changeStatus2Custom
(
ret
);
}
catch
(
RemoteException
e
)
{
throw
new
PrinterException
(
PrinterStatusEnum
.
CODE_FAILED
.
status
,
e
.
getMessage
());
}
}
/**
* 将商米的状态码翻译成PrinterStatusEnum中的标准状态码
* 注:内部会自动抛出不允许的异常状态
*
* @param status 商米返回的状态码
* @return PrinterStatusEnum中的标准状态码
*/
private
int
changeStatus2Custom
(
int
status
)
{
switch
(
status
)
{
case
0
:
//正常状态
return
PrinterStatusEnum
.
CODE_SUCCESS
.
status
;
case
1
:
//打印机开盖
throw
new
PrinterException
(
PrinterStatusEnum
.
CODE_FAILED
.
status
,
"打印机开盖"
);
case
2
:
//缺纸
throw
new
PrinterException
(
PrinterStatusEnum
.
CODE_NO_PAPER
.
status
,
PrinterStatusEnum
.
CODE_NO_PAPER
.
getDesc
());
case
3
:
return
PrinterStatusEnum
.
CODE_FEW_PAPER
.
status
;
case
4
:
//打印头过热
return
PrinterStatusEnum
.
CODE_PRINT_HEAD_HOT
.
status
;
case
-
1
:
//数据传输错误,请检查连接并重新发送
throw
new
PrinterException
(
PrinterStatusEnum
.
CODE_FAILED
.
status
,
"打印机离线或打印机未准备好"
);
case
-
2
:
throw
new
PrinterException
(
PrinterStatusEnum
.
CODE_FAILED
.
status
,
"缓存已满,不不接收打印数据"
);
case
-
3
:
throw
new
PrinterException
(
PrinterStatusEnum
.
CODE_FAILED
.
status
,
"发送数据异常"
);
case
-
4
:
throw
new
PrinterException
(
PrinterStatusEnum
.
CODE_FAILED
.
status
,
"发送命令或参数错误"
);
default
:
return
PrinterStatusEnum
.
CODE_FAILED
.
status
;
}
}
/**
* 连接服务
...
...
@@ -64,7 +333,7 @@ class ShangmiK1Printer extends BasePrinter {
}
/**
* 断开服务
* 断开服务
q
*
* @param context context
*/
...
...
@@ -86,8 +355,6 @@ class ShangmiK1Printer extends BasePrinter {
if
(
extPrinterService
==
null
)
{
throw
new
PrinterException
(
PrinterStatusEnum
.
CODE_FAILED
.
status
,
"服务已断开!"
);
}
try
{
extPrinterService
.
lineWrap
(
1
);
...
...
@@ -102,14 +369,10 @@ class ShangmiK1Printer extends BasePrinter {
throw
new
PrinterException
(
PrinterStatusEnum
.
CODE_FAILED
.
status
,
"服务已断开!"
);
}
try
{
extPrinterService
.
cutPaper
(
0
,
0
);
}
catch
(
RemoteException
e
)
{
throw
new
PrinterException
(
PrinterStatusEnum
.
CODE_FAILED
.
status
,
e
.
getMessage
());
}
}
...
...
@@ -117,13 +380,9 @@ class ShangmiK1Printer extends BasePrinter {
public
void
setAlignMode
(
int
mode
)
{
if
(
extPrinterService
==
null
)
{
throw
new
PrinterException
(
PrinterStatusEnum
.
CODE_FAILED
.
status
,
"服务已断开!"
);
}
try
{
extPrinterService
.
setAlignMode
(
mode
);
}
catch
(
RemoteException
e
)
{
throw
new
PrinterException
(
PrinterStatusEnum
.
CODE_FAILED
.
status
,
e
.
getMessage
());
}
...
...
@@ -158,7 +417,6 @@ class ShangmiK1Printer extends BasePrinter {
if
(
extPrinterService
==
null
)
{
return
;
}
try
{
extPrinterService
.
printerInit
();
}
catch
(
RemoteException
e
)
{
...
...
@@ -175,8 +433,6 @@ class ShangmiK1Printer extends BasePrinter {
throw
new
PrinterException
(
PrinterStatusEnum
.
CODE_FAILED
.
status
,
"服务已断开!"
);
}
try
{
extPrinterService
.
setAlignMode
(
1
);
extPrinterService
.
printQrCode
(
data
,
modulesize
,
errorlevel
);
...
...
@@ -188,6 +444,38 @@ class ShangmiK1Printer extends BasePrinter {
/**
* 打印条形码 symbology条码类型
*
* @param code 打印条形码 symbology条码类型
* @param type 条形码类型 0:UPC-A 1:UPC-E 2:EAN13 3:EAN8 4:CODE39 5:ITF 6:CODABAR 7:CODE93 8:CODE128
* @param width 条形码宽度 2-6 像素 (若条码宽度设置过宽,整个条码超过纸张宽度将不会输出条码内容)
* @param height 条形码⾼度 1-255 像素
* @param hriPos HRI位置 0:不打印 1:条形码上⽅ 2:条形码下⽅ 3:条形码上下⽅
* @desc 条码类型说明:
* code39 最⻓打印 13 个数字
* ⽅法 int setFontZoom(int hori, int veri)
* 说明 由于打印机硬字库限制,字体⼤⼩仅能倍数放⼤,本⽅法可以控制字体在横向和纵向⽅向的放⼤
* 倍数
* 输⼊ hori 、veri 的范围为1-8,表示在横向、纵向上字体的放⼤倍数,若设置在范围外将返回错误参数
* 返回 ⻅接⼝返回值
* ⽅法 int printColumnsText(String[] colsTextArr, int[] colsWidthArr, int[] colsAlign)
* 说明
* 以表格⽅式输出打印内容,每个数组表示在此列上的数据及格式,需要多次调⽤此⽅法才可以达
* 到表格输出的样式效果
* ⚠ 注意:调⽤此⽅法后会将之前设置样式初始化!
* 输⼊
* colsTextArr: 每列要打印的内容,⽀持中⽂及ascii码
* colsWidthArr:每列可容纳的最⼤字符数量,字符数以ascii码个数为单位(⼀个中⽂等于两个ascii
* 码数量),当⽂本内容超出可容纳的最⼤数量时将移动到本列下⼀⾏,当所有列的最⼤字符数量
* 超出⼀⾏所能容纳的字符数时,将不会打印
* colsAlign:每列内容的对⻬⽅式,仅当内容字符数⼩于最⼤字符数量时才有效果
* 返回 ⻅接⼝返回值
* code93 最⻓打印 17 个数字
* code128 code128 分三类:{A,{B,{C;A 类:包⼤写字⺟、数字、标 点等,B 类:⼤⼩写字⺟,数
* 字;C 类:纯数字;默认 B 类编码,若要使本编码需在内容前⾯加“{A”、“{B” 、“{C”声明使⽤的类
* 型,可以混合使⽤,例如: “{A2344A”,”{C123123”,”{A1A{B13Bxc{C12”。
* ean8 要求 8 位数字(最后⼀位校验位),有效⻓度 8 个数字
* ean13 有效⻓度 13 个数字,其中最后⼀位为校验位
* ITF 要求输⼊数字,且有效⼩于 14 位,必须是偶数位
*/
@Deprecated
public
void
printBarCode
(
String
code
,
int
type
,
int
width
,
int
height
,
int
hriPos
)
{
...
...
@@ -244,7 +532,6 @@ class ShangmiK1Printer extends BasePrinter {
if
(
extPrinterService
==
null
)
{
throw
new
PrinterException
(
PrinterStatusEnum
.
CODE_FAILED
.
status
,
"服务已断开!"
);
}
try
{
if
(
isBold
)
{
extPrinterService
.
sendRawData
(
ESCUtil
.
boldOn
());
...
...
@@ -262,7 +549,6 @@ class ShangmiK1Printer extends BasePrinter {
}
catch
(
RemoteException
e
)
{
throw
new
PrinterException
(
PrinterStatusEnum
.
CODE_FAILED
.
status
,
e
.
getMessage
());
}
}
/*
...
...
@@ -277,6 +563,11 @@ class ShangmiK1Printer extends BasePrinter {
try
{
extPrinterService
.
setAlignMode
(
1
);
/**
* 将bitmap图转成光栅位图⽅式打印
* 此⽅法适合打印宽度在打印纸内的图⽚
* 数据过⼤时串⼝流控模式会阻塞,⽆流控时会丢数据
*/
extPrinterService
.
printBitmap
(
bitmap
,
0
);
extPrinterService
.
lineWrap
(
2
);
}
catch
(
RemoteException
e
)
{
...
...
print/src/main/java/com/miya/print/YingtaiPrinter.java
View file @
13d6bbf2
...
...
@@ -26,6 +26,7 @@ public class YingtaiPrinter extends BasePrinter {
@Override
public
boolean
init
(
Context
context
)
{
D
.
i
(
TAG
,
"开始初始化英泰打印机"
);
this
.
context
=
context
;
mPrinter
=
PrinterAPI
.
getInstance
();
io
=
new
USBAPI
(
context
);
...
...
weighing/src/main/java/com/miya/weighing/SunmiWeighing.java
View file @
13d6bbf2
...
...
@@ -31,6 +31,11 @@ public class SunmiWeighing extends BaseWeighing {
*/
ScaleService
scaleService
;
/**
* 服务连接器
*/
ServiceConnection
serviceConnection
;
/**
* 称重回调监听
*/
...
...
@@ -61,6 +66,33 @@ public class SunmiWeighing extends BaseWeighing {
@Override
public
boolean
init
(
Context
context
)
{
this
.
context
=
context
;
serviceConnection
=
new
ServiceConnection
()
{
@Override
public
void
onServiceConnected
(
ComponentName
name
,
IBinder
service
)
{
scaleService
=
ScaleService
.
Stub
.
asInterface
(
service
);
if
(
scaleService
!=
null
)
{
//设置重量回调
try
{
scaleService
.
getData
(
stub
);
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
D
.
i
(
TAG
,
"称重服务初始化失败!"
);
}
}
else
{
D
.
i
(
TAG
,
"防损称连接失败!"
);
}
}
@Override
public
void
onBindingDied
(
ComponentName
name
)
{
D
.
i
(
TAG
,
"解绑成功 ComponentName==>"
+
name
);
}
@Override
public
void
onServiceDisconnected
(
ComponentName
name
)
{
scaleService
=
null
;
}
};
Intent
intent
=
new
Intent
();
intent
.
setPackage
(
SERVICE_PACKAGE
);
intent
.
setAction
(
SERVICE_ACTION
);
...
...
@@ -83,37 +115,6 @@ public class SunmiWeighing extends BaseWeighing {
return
false
;
}
/**
* 服务连接器
*/
ServiceConnection
serviceConnection
=
new
ServiceConnection
()
{
@Override
public
void
onServiceConnected
(
ComponentName
name
,
IBinder
service
)
{
scaleService
=
ScaleService
.
Stub
.
asInterface
(
service
);
if
(
scaleService
!=
null
)
{
//设置重量回调
try
{
scaleService
.
getData
(
stub
);
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
D
.
i
(
TAG
,
"称重服务初始化失败!"
);
}
}
else
{
D
.
i
(
TAG
,
"防损称连接失败!"
);
}
}
@Override
public
void
onBindingDied
(
ComponentName
name
)
{
D
.
i
(
TAG
,
"解绑成功 ComponentName==>"
+
name
);
}
@Override
public
void
onServiceDisconnected
(
ComponentName
name
)
{
scaleService
=
null
;
}
};
@Override
public
boolean
release
()
{
if
(
context
!=
null
&&
serviceConnection
!=
null
)
{
...
...
weighing/src/main/java/com/miya/weighing/WeighingManager.java
View file @
13d6bbf2
...
...
@@ -4,6 +4,8 @@ import android.content.Context;
import
android.support.annotation.NonNull
;
import
android.util.Log
;
import
com.miya.weighing.utils.D
;
public
class
WeighingManager
{
final
static
String
TAG
=
WeighingManager
.
class
.
getSimpleName
();
...
...
@@ -80,10 +82,10 @@ public class WeighingManager {
//轮询所有的称重服务,注意是同步方法
for
(
Type
type
:
Type
.
values
())
{
//确保之前的称重实例已被销毁
if
(
weighing
!=
null
)
{
weighing
.
release
();
}
try
{
if
(
weighing
!=
null
)
{
weighing
.
release
();
}
//反射新建实例
Class
weighingClass
=
Class
.
forName
(
type
.
clsName
);
weighing
=
(
Weighing
)
weighingClass
.
newInstance
();
...
...
@@ -155,6 +157,10 @@ public class WeighingManager {
Class
weighingClass
=
Class
.
forName
(
type
.
clsName
);
weighing
=
(
Weighing
)
weighingClass
.
newInstance
();
boolean
result
=
weighing
.
init
(
context
);
if
(
result
==
true
)
{
weighing
.
setWeighingName
(
type
.
clsName
);
D
.
i
(
TAG
,
"称重硬件初始化成功==>"
+
weighing
.
getWeighingName
());
}
return
result
;
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment