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
721d3247
Commit
721d3247
authored
Jan 28, 2019
by
pengguangpu
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
添加海信驱动并测试
parent
13d6bbf2
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
802 additions
and
17 deletions
+802
-17
gradle_models.ser
.idea/caches/gradle_models.ser
+0
-0
ExampleUnitTest.java
app/src/test/java/com/miya/hardware/ExampleUnitTest.java
+1
-0
HisensePrinter.java
print/src/main/java/com/miya/print/HisensePrinter.java
+529
-0
PrinterManager.java
print/src/main/java/com/miya/print/PrinterManager.java
+6
-2
SunmiK1Printer.java
print/src/main/java/com/miya/print/SunmiK1Printer.java
+21
-15
SunmiT1Printer.java
print/src/main/java/com/miya/print/SunmiT1Printer.java
+23
-0
AccumulateUtils.java
...t/src/main/java/com/miya/print/utils/AccumulateUtils.java
+222
-0
No files found.
.idea/caches/gradle_models.ser
0 → 100644
View file @
721d3247
File added
app/src/test/java/com/miya/hardware/ExampleUnitTest.java
View file @
721d3247
...
@@ -13,5 +13,6 @@ public class ExampleUnitTest {
...
@@ -13,5 +13,6 @@ public class ExampleUnitTest {
@Test
@Test
public
void
addition_isCorrect
()
{
public
void
addition_isCorrect
()
{
assertEquals
(
4
,
2
+
2
);
assertEquals
(
4
,
2
+
2
);
System
.
out
.
println
(
""
.
getBytes
().
length
);
}
}
}
}
\ No newline at end of file
print/src/main/java/com/miya/print/HisensePrinter.java
View file @
721d3247
package
com
.
miya
.
print
;
package
com
.
miya
.
print
;
import
android.content.ComponentName
;
import
android.content.Context
;
import
android.content.Intent
;
import
android.content.ServiceConnection
;
import
android.graphics.Bitmap
;
import
android.os.IBinder
;
import
android.os.RemoteException
;
import
android.text.TextUtils
;
import
com.hisense.hs650service.aidl.HS650Api
;
import
com.hisense.hs650service.aidl.Printer
;
import
com.miya.print.exception.PrinterException
;
import
com.miya.print.utils.AccumulateUtils
;
import
com.miya.print.utils.BitmapUtils
;
import
com.miya.print.utils.D
;
public
class
HisensePrinter
extends
BasePrinter
{
public
class
HisensePrinter
extends
BasePrinter
{
final
static
String
TAG
=
HisensePrinter
.
class
.
getSimpleName
();
final
static
String
TAG
=
HisensePrinter
.
class
.
getSimpleName
();
final
static
String
SERVICE_PACKAGE
=
"com.hisense.hs650api"
;
final
static
String
SERVICE_ACTION
=
"com.hisense.hs650api"
;
/**
* 波特率
*/
final
static
int
BAUD_RATE
=
115200
;
/**
* 串口号
*/
final
static
int
COM
=
0
;
/**
* 58mm打印纸机器
*/
final
static
int
PRINTER_TYPE_58
=
0
;
/**
* 80mm打印纸机器
*/
final
static
int
PRINTER_TYPE_80
=
1
;
/**
* 打印机类型
*/
final
static
int
PRINTER_TYPE
=
PRINTER_TYPE_80
;
HS650Api
hs650Api
;
Context
context
;
ServiceConnection
serviceConnection
=
new
ServiceConnection
()
{
@Override
public
void
onServiceConnected
(
ComponentName
name
,
IBinder
service
)
{
hs650Api
=
HS650Api
.
Stub
.
asInterface
(
service
);
if
(
hs650Api
==
null
)
{
//连接打印进程服务失败
D
.
e
(
TAG
,
"连接海信打印进程服务失败"
);
}
else
{
D
.
i
(
TAG
,
"连接海信打印进程服务成功"
);
}
try
{
initPrinter
();
D
.
i
(
TAG
,
"海信打印机初始化成功"
);
}
catch
(
Exception
ex
)
{
ex
.
printStackTrace
();
D
.
e
(
TAG
,
"海信打印机初始化失败"
);
}
}
@Override
public
void
onServiceDisconnected
(
ComponentName
name
)
{
hs650Api
=
null
;
}
};
@Override
public
boolean
init
(
Context
context
)
{
D
.
i
(
TAG
,
"开始初始化海信打印机"
);
this
.
context
=
context
.
getApplicationContext
();
Intent
intent
=
new
Intent
();
intent
.
setPackage
(
SERVICE_PACKAGE
);
// intent.setAction(SERVICE_ACTION);
// context.getApplicationContext().startService(intent);
boolean
result
=
this
.
context
.
bindService
(
intent
,
serviceConnection
,
Context
.
BIND_AUTO_CREATE
);
if
(
result
==
false
)
{
D
.
e
(
TAG
,
"海信打印机服务连接失败"
);
}
else
{
D
.
i
(
TAG
,
"海信打印机服务连接成功"
);
}
return
result
;
}
@Override
public
boolean
release
()
{
checkService
();
try
{
hs650Api
.
getPrinter
().
closePrinter
();
}
catch
(
RemoteException
e
)
{
e
.
printStackTrace
();
throw
new
PrinterException
(
PrinterStatusEnum
.
CODE_FAILED
.
status
,
e
.
getMessage
());
}
return
true
;
}
@Override
public
int
beginPrint
()
{
AccumulateUtils
.
clearAccmulateString
();
return
PrinterStatusEnum
.
CODE_SUCCESS
.
status
;
}
@Override
public
int
endPrint
()
{
printAccumulateString
();
AccumulateUtils
.
clearAccmulateString
();
return
PrinterStatusEnum
.
CODE_SUCCESS
.
status
;
}
@Override
public
int
printText
(
int
align
,
int
size
,
String
content
,
boolean
isFeed
)
{
//使用累加器进行累加,暂只支持80mm打印纸
AccumulateUtils
.
accumulateString
(
AccumulateUtils
.
PAGE_MODE_80MM
,
this
.
align
,
size
,
content
,
isFeed
);
//再设置对齐方式
return
setAlign
(
align
);
}
@Override
public
int
printImage
(
int
align
,
Bitmap
bm
,
boolean
isFeed
)
{
//先打印之前累积的字符串
int
ret
=
printAccumulateString
();
//接着打印图片
ret
=
setAlign
(
align
);
Bitmap
actualBm
=
null
;
try
{
if
(
bm
.
getWidth
()
>
MAX_IMG_WIDTH_80MM
)
{
actualBm
=
BitmapUtils
.
scaleBitmap
(
bm
,
MAX_IMG_WIDTH_80MM
,
0
);
ret
=
hs650Api
.
getPrinter
().
printImage
(
actualBm
,
ALIGN_LEFT
);
}
else
{
ret
=
hs650Api
.
getPrinter
().
printImage
(
bm
,
this
.
align
);
}
}
catch
(
Exception
ex
)
{
ex
.
printStackTrace
();
throw
new
PrinterException
(
PrinterStatusEnum
.
CODE_FAILED
.
status
,
ex
.
getMessage
());
}
if
(
actualBm
!=
null
)
{
actualBm
.
recycle
();
}
if
(
ret
<
0
)
{
throw
new
PrinterException
(
PrinterStatusEnum
.
CODE_FAILED
.
status
,
PrinterStatusEnum
.
CODE_FAILED
.
desc
);
}
return
PrinterStatusEnum
.
CODE_SUCCESS
.
status
;
}
@Override
public
int
printBarcode
(
int
align
,
String
content
,
boolean
isFeed
)
{
//先打印之前累积的字符串
int
ret
=
printAccumulateString
();
//接着打印一维条码
ret
=
setAlign
(
align
);
try
{
ret
=
hs650Api
.
getPrinter
().
printBar
(
content
);
}
catch
(
RemoteException
e
)
{
e
.
printStackTrace
();
throw
new
PrinterException
(
PrinterStatusEnum
.
CODE_FAILED
.
status
,
e
.
getMessage
());
}
return
PrinterStatusEnum
.
CODE_SUCCESS
.
status
;
}
@Override
public
int
printQrcode
(
int
align
,
String
content
,
boolean
isFeed
)
{
//先打印之前累积的字符串
int
ret
=
printAccumulateString
();
//接着打印二维条码
ret
=
setAlign
(
align
);
try
{
//printQR方法第二个参数为放大倍数,取值3~6
ret
=
hs650Api
.
getPrinter
().
printQR
(
content
,
6
);
}
catch
(
RemoteException
e
)
{
e
.
printStackTrace
();
throw
new
PrinterException
(
PrinterStatusEnum
.
CODE_FAILED
.
status
,
e
.
getMessage
());
}
if
(
ret
<
0
)
{
throw
new
PrinterException
(
PrinterStatusEnum
.
CODE_FAILED
.
status
,
PrinterStatusEnum
.
CODE_FAILED
.
desc
);
}
return
PrinterStatusEnum
.
CODE_SUCCESS
.
status
;
}
@Override
public
int
getPrinterStatus
()
{
return
super
.
getPrinterStatus
();
}
@Override
public
int
cutPaper
()
{
checkService
();
//先打印之前累积的字符
printAccumulateString
();
try
{
hs650Api
.
getPrinter
().
cutPaper
();
}
catch
(
RemoteException
e
)
{
e
.
printStackTrace
();
throw
new
PrinterException
(
PrinterStatusEnum
.
CODE_FAILED
.
status
,
e
.
getMessage
());
}
return
PrinterStatusEnum
.
CODE_SUCCESS
.
status
;
}
@Override
public
int
feedPaper
()
{
checkService
();
int
ret
=
0
;
try
{
ret
=
hs650Api
.
getPrinter
().
feedPaper
(
1
,
0
);
}
catch
(
RemoteException
e
)
{
e
.
printStackTrace
();
throw
new
PrinterException
(
PrinterStatusEnum
.
CODE_FAILED
.
status
,
e
.
getMessage
());
}
if
(
ret
<
0
)
{
throw
new
PrinterException
(
PrinterStatusEnum
.
CODE_FAILED
.
status
,
PrinterStatusEnum
.
CODE_FAILED
.
desc
);
}
return
PrinterStatusEnum
.
CODE_SUCCESS
.
status
;
}
@Override
public
int
setAlign
(
int
align
)
{
checkService
();
int
ret
=
PrinterStatusEnum
.
CODE_SUCCESS
.
getStatus
();
if
(
this
.
align
!=
align
&&
(
align
==
0
||
align
==
1
||
align
==
2
))
{
this
.
align
=
align
;
}
return
ret
;
}
/**
* 打印累积的字符串
*
* @return 状态码或抛出异常
*/
private
int
printAccumulateString
()
{
String
accumulateStr
=
AccumulateUtils
.
getAccumulatedString
();
if
(!
TextUtils
.
isEmpty
(
accumulateStr
))
{
checkService
();
int
ret
=
0
;
try
{
ret
=
hs650Api
.
getPrinter
().
printTextStr
(
accumulateStr
,
0
,
ALIGN_LEFT
);
//恢复当前排版格式
ret
=
hs650Api
.
getPrinter
().
printTextStr
(
""
,
0
,
this
.
align
);
}
catch
(
RemoteException
e
)
{
e
.
printStackTrace
();
throw
new
PrinterException
(
PrinterStatusEnum
.
CODE_FAILED
.
status
,
e
.
getMessage
());
}
if
(
ret
<
0
)
{
throw
new
PrinterException
(
PrinterStatusEnum
.
CODE_FAILED
.
status
,
PrinterStatusEnum
.
CODE_FAILED
.
desc
);
}
}
//清空累积值
AccumulateUtils
.
clearAccmulateString
();
return
PrinterStatusEnum
.
CODE_SUCCESS
.
status
;
}
/**
* 初始化连接打印机,app起来的时候调用
*/
@Deprecated
public
void
connectPrinterService
(
Context
context
)
{
this
.
context
=
context
.
getApplicationContext
();
Intent
intent
=
new
Intent
();
intent
.
setPackage
(
SERVICE_PACKAGE
);
// intent.setAction(SERVICE_ACTION);
// this.context.startService(intent);
this
.
context
.
bindService
(
intent
,
serviceConnection
,
Context
.
BIND_AUTO_CREATE
);
}
/**
* 是否已经连接打印机
*
* @return
*/
@Deprecated
public
boolean
isConnected
()
{
return
hs650Api
!=
null
;
}
/**
* 断开服务
*
* @param context context
*/
public
void
disconnectPrinterService
(
Context
context
)
{
if
(
hs650Api
!=
null
)
{
context
.
getApplicationContext
().
unbindService
(
serviceConnection
);
hs650Api
=
null
;
}
}
/**
* 配置打印者,打印的第一步
*
* @param baudrate 波特率
* @param port 端口
* @param ctrlOPen RTS/CTS true打开 false关闭
*/
public
void
configPrinter
(
int
baudrate
,
int
port
,
boolean
ctrlOPen
)
{
checkService
();
try
{
int
ret
=
hs650Api
.
getPrinter
().
printerConfig
(
baudrate
,
port
,
ctrlOPen
);
if
(
ret
!=
0
)
{
new
PrinterException
(
PrinterStatusEnum
.
CODE_FAILED
.
status
,
"配置打印机失败!"
);
}
}
catch
(
RemoteException
e
)
{
e
.
printStackTrace
();
throw
new
PrinterException
(
PrinterStatusEnum
.
CODE_FAILED
.
status
,
e
.
getMessage
());
}
}
/**
* 检查服务是否正常,正常就通过,不正常就抛异常
*/
public
void
checkService
()
{
if
(
hs650Api
==
null
)
{
throw
new
PrinterException
(
PrinterStatusEnum
.
CODE_FAILED
.
status
,
"服务已断开!"
);
}
}
public
void
getPrinterType
()
{
}
/**
* 检查是否缺纸
*
* @return true==>有纸;false==>缺纸
*/
public
void
checkPaper
()
{
checkService
();
try
{
int
ret
=
hs650Api
.
getPrinter
().
printStatus
();
if
(
ret
!=
0
)
{
throw
new
PrinterException
(
PrinterStatusEnum
.
CODE_FAILED
.
status
,
"缺纸"
);
}
}
catch
(
RemoteException
e
)
{
e
.
printStackTrace
();
throw
new
PrinterException
(
PrinterStatusEnum
.
CODE_FAILED
.
status
,
e
.
getMessage
());
}
}
/**
* 打印初始化,包括获取打印机型号getPrnType->配置打印机printerConfig->初始化打印机initPrinter
* 注:本地先写死波特率为115200,串口为2
*/
public
void
initPrinter
()
{
checkService
();
try
{
long
tmp1
=
System
.
currentTimeMillis
();
String
printerType
=
hs650Api
.
getPrinter
().
getPrnType
(
BAUD_RATE
,
COM
,
500
);
long
tmp2
=
System
.
currentTimeMillis
();
int
ret
=
hs650Api
.
getPrinter
().
printerConfig
(
BAUD_RATE
,
COM
,
false
);
if
(
ret
!=
0
)
{
throw
new
PrinterException
(
PrinterStatusEnum
.
CODE_FAILED
.
status
,
"打印机配置失败!"
);
}
ret
=
hs650Api
.
getPrinter
().
initPrinter
(
PRINTER_TYPE
);
if
(
ret
!=
0
)
{
throw
new
PrinterException
(
PrinterStatusEnum
.
CODE_FAILED
.
status
,
"打印机初始化失败!"
);
}
}
catch
(
RemoteException
e
)
{
e
.
printStackTrace
();
throw
new
PrinterException
(
PrinterStatusEnum
.
CODE_FAILED
.
status
,
"打印机异常!"
);
}
}
/**
* 打印字符(带换行)
*
* @param content 字符内容
* @param size 字符大小 0==正常大小;1==2倍大小
* @param align 字符对齐方式 0==居左;1==居中;2==居右
*/
public
void
printTextWithLF
(
String
content
,
int
size
,
int
align
)
{
checkService
();
checkPaper
();
printText
(
content
,
size
,
align
);
LF
();
}
/**
* 打印字符
*
* @param content 字符内容
* @param size 字符大小 0==正常大小;1==2倍大小
* @param align 字符对齐方式 0==居左;1==居中;2==居右
*/
@Deprecated
public
void
printText
(
String
content
,
int
size
,
int
align
)
{
checkService
();
checkPaper
();
try
{
int
ret
=
hs650Api
.
getPrinter
().
printTextStr
(
content
,
size
,
align
);
if
(
ret
!=
0
)
{
throw
new
PrinterException
(
PrinterStatusEnum
.
CODE_FAILED
.
status
,
"打印失败"
);
}
}
catch
(
RemoteException
e
)
{
e
.
printStackTrace
();
throw
new
PrinterException
(
PrinterStatusEnum
.
CODE_FAILED
.
status
,
"打印失败"
);
}
}
/**
* 默认居中 正常大小
*
* @param content
*/
public
void
printText
(
String
content
)
{
printText
(
content
,
0
,
1
);
}
/**
* 强制打印机走纸一行
*/
public
void
LF
()
{
checkService
();
checkPaper
();
try
{
int
ret
=
hs650Api
.
getPrinter
().
feedPaper
(
1
,
0
);
if
(
ret
!=
0
)
{
throw
new
PrinterException
(
PrinterStatusEnum
.
CODE_FAILED
.
status
,
"换行失败"
);
}
}
catch
(
RemoteException
e
)
{
e
.
printStackTrace
();
throw
new
PrinterException
(
PrinterStatusEnum
.
CODE_FAILED
.
status
,
"换行失败"
);
}
}
/**
* 强制切纸
*/
@Deprecated
public
void
cutPaperOld
()
{
checkService
();
try
{
hs650Api
.
getPrinter
().
cutPaper
();
}
catch
(
RemoteException
e
)
{
e
.
printStackTrace
();
throw
new
PrinterException
(
PrinterStatusEnum
.
CODE_FAILED
.
status
,
"切纸失败"
);
}
}
/**
* 打印二维码
*
* @param code 二维码内容
* @param mode 放大倍数(3~6)
*/
public
void
printQrCode
(
String
code
,
int
mode
)
{
checkService
();
checkPaper
();
try
{
int
ret
=
hs650Api
.
getPrinter
().
printQR
(
code
,
mode
);
if
(
ret
!=
0
)
{
throw
new
PrinterException
(
PrinterStatusEnum
.
CODE_FAILED
.
status
,
"打印二维码失败!"
);
}
}
catch
(
RemoteException
e
)
{
e
.
printStackTrace
();
throw
new
PrinterException
(
PrinterStatusEnum
.
CODE_FAILED
.
status
,
e
.
getMessage
());
}
}
/**
* 打印图片
*
* @param bitmap 指定图片
* @param mode mode:0:左对齐1:居中2:右对齐
*/
public
void
printBitmap
(
Bitmap
bitmap
,
int
mode
)
{
checkService
();
checkPaper
();
try
{
int
ret
=
hs650Api
.
getPrinter
().
printImage
(
bitmap
,
mode
);
if
(
ret
!=
0
)
{
throw
new
PrinterException
(
PrinterStatusEnum
.
CODE_FAILED
.
status
,
"打印图片失败!"
);
}
}
catch
(
RemoteException
e
)
{
e
.
printStackTrace
();
throw
new
PrinterException
(
PrinterStatusEnum
.
CODE_FAILED
.
status
,
e
.
getMessage
());
}
}
/**
* 打印一维码
*
* @param code 一维码内容
*/
public
void
printBarCode
(
String
code
)
{
checkService
();
checkPaper
();
try
{
int
ret
=
hs650Api
.
getPrinter
().
printBar
(
code
);
if
(
ret
!=
0
)
{
throw
new
PrinterException
(
PrinterStatusEnum
.
CODE_FAILED
.
status
,
"打印一维条码失败"
);
}
}
catch
(
RemoteException
e
)
{
e
.
printStackTrace
();
throw
new
PrinterException
(
PrinterStatusEnum
.
CODE_FAILED
.
status
,
e
.
getMessage
());
}
}
/**
* 打印图片
*
* @param bitmap
* @param mode 0==居左;1==居中;2==居右
*/
public
void
printImage
(
Bitmap
bitmap
,
int
mode
)
{
checkService
();
checkPaper
();
try
{
int
ret
=
hs650Api
.
getPrinter
().
printImage
(
bitmap
,
mode
);
if
(
ret
!=
0
)
{
throw
new
PrinterException
(
PrinterStatusEnum
.
CODE_FAILED
.
status
,
"打印图片失败!"
);
}
}
catch
(
RemoteException
e
)
{
e
.
printStackTrace
();
throw
new
PrinterException
(
PrinterStatusEnum
.
CODE_FAILED
.
status
,
e
.
getMessage
());
}
}
/**
* 关闭打印机
*/
public
void
closePrinter
()
{
checkService
();
try
{
hs650Api
.
getPrinter
().
closePrinter
();
}
catch
(
RemoteException
e
)
{
e
.
printStackTrace
();
throw
new
PrinterException
(
PrinterStatusEnum
.
CODE_FAILED
.
status
,
e
.
getMessage
());
}
}
}
}
print/src/main/java/com/miya/print/PrinterManager.java
View file @
721d3247
...
@@ -2,6 +2,7 @@ package com.miya.print;
...
@@ -2,6 +2,7 @@ package com.miya.print;
import
android.content.Context
;
import
android.content.Context
;
import
com.miya.print.exception.PrinterException
;
import
com.miya.print.utils.D
;
import
com.miya.print.utils.D
;
/**
/**
...
@@ -16,8 +17,9 @@ public class PrinterManager {
...
@@ -16,8 +17,9 @@ public class PrinterManager {
*/
*/
enum
Type
{
enum
Type
{
//这里添加扩展的驱动
//这里添加扩展的驱动
// TYPE_HISENSE("hisense", ChuangjiePrinter.class.getName()),
TYPE_HISENSE
(
"hisense"
,
HisensePrinter
.
class
.
getName
()),
TYPE_SUNMI
(
"sunmiK1"
,
SunmiK1Printer
.
class
.
getName
()),
TYPE_SUNMI_TI
(
"sunmiT1"
,
SunmiT1Printer
.
class
.
getName
()),
TYPE_SUNMI_K1
(
"sunmiK1"
,
SunmiK1Printer
.
class
.
getName
()),
TYPE_YINGTAI
(
"yingtai"
,
YingtaiPrinter
.
class
.
getName
()),
TYPE_YINGTAI
(
"yingtai"
,
YingtaiPrinter
.
class
.
getName
()),
TYPE_CHUANGJIE
(
"chuangjie"
,
ChuangjiePrinter
.
class
.
getName
()),
TYPE_CHUANGJIE
(
"chuangjie"
,
ChuangjiePrinter
.
class
.
getName
()),
TYPE_SANGDA
(
"sangda"
,
SangdaPrinter
.
class
.
getName
());
TYPE_SANGDA
(
"sangda"
,
SangdaPrinter
.
class
.
getName
());
...
@@ -87,6 +89,7 @@ public class PrinterManager {
...
@@ -87,6 +89,7 @@ public class PrinterManager {
}
}
}
catch
(
Exception
e
)
{
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
e
.
printStackTrace
();
printer
=
null
;
}
}
}
}
//没有找到对应的打印机
//没有找到对应的打印机
...
@@ -96,6 +99,7 @@ public class PrinterManager {
...
@@ -96,6 +99,7 @@ public class PrinterManager {
}
catch
(
Exception
ex
)
{
}
catch
(
Exception
ex
)
{
ex
.
printStackTrace
();
ex
.
printStackTrace
();
}
}
printer
=
null
;
}
}
D
.
i
(
TAG
,
"没找到对应的打印机器,请检查"
);
D
.
i
(
TAG
,
"没找到对应的打印机器,请检查"
);
return
false
;
return
false
;
...
...
print/src/main/java/com/miya/print/SunmiK1Printer.java
View file @
721d3247
...
@@ -49,6 +49,7 @@ class SunmiK1Printer extends BasePrinter {
...
@@ -49,6 +49,7 @@ class SunmiK1Printer extends BasePrinter {
}
}
}
}
};
};
;
@Override
@Override
public
boolean
init
(
Context
context
)
{
public
boolean
init
(
Context
context
)
{
...
@@ -163,16 +164,24 @@ class SunmiK1Printer extends BasePrinter {
...
@@ -163,16 +164,24 @@ class SunmiK1Printer extends BasePrinter {
throw
new
PrinterException
(
PrinterStatusEnum
.
CODE_FAILED
.
status
,
"服务已断开!"
);
throw
new
PrinterException
(
PrinterStatusEnum
.
CODE_FAILED
.
status
,
"服务已断开!"
);
}
}
try
{
try
{
int
ret
=
setAlign
(
align
);
/**
//打印的样式默认为CODE128码,数字在条码下面
* 打印一维条形码
ret
=
extPrinterService
.
printBarCode
(
content
,
8
,
2
,
120
,
2
);
* data 条码内容
//强制走两行纸才能生效
* height 条码高度
ret
=
extPrinterService
.
lineWrap
(
2
);
* width 条码宽度
if
(
isFeed
)
{
* textposition 条码内容位置
ret
=
feedPaper
();
*/
}
//这里的高度固定传100,宽度传2,条码内容文字传2
ByteArrayOutputStream
buffer
=
new
ByteArrayOutputStream
();
buffer
.
write
(
new
byte
[]{
0x1D
,
0x48
,
(
byte
)
2
,
0x1D
,
0x77
,
(
byte
)
2
,
0x1D
,
0x68
,
(
byte
)
100
,
0x0A
});
byte
[]
barcode
;
barcode
=
BytesUtil
.
getBytesFromDecString
(
content
);
buffer
.
write
(
new
byte
[]{
0x1D
,
0x6B
,
0x49
,
(
byte
)
(
barcode
.
length
+
2
),
0x7B
,
(
byte
)
(
0x43
)});
buffer
.
write
(
barcode
);
int
ret
=
extPrinterService
.
sendRawData
(
buffer
.
toByteArray
());
return
changeStatus2Custom
(
ret
);
return
changeStatus2Custom
(
ret
);
}
catch
(
Remote
Exception
e
)
{
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
e
.
printStackTrace
();
throw
new
PrinterException
(
PrinterStatusEnum
.
CODE_FAILED
.
status
,
e
.
getMessage
());
throw
new
PrinterException
(
PrinterStatusEnum
.
CODE_FAILED
.
status
,
e
.
getMessage
());
}
}
...
@@ -190,9 +199,9 @@ class SunmiK1Printer extends BasePrinter {
...
@@ -190,9 +199,9 @@ class SunmiK1Printer extends BasePrinter {
* modeSize ⼆维码块⼤⼩ 1-16 像素点
* modeSize ⼆维码块⼤⼩ 1-16 像素点
* errorlevel ⼆维码纠错等级 0-3 四个等级
* errorlevel ⼆维码纠错等级 0-3 四个等级
*/
*/
ret
=
extPrinterService
.
printQrCode
(
content
,
4
,
3
);
ret
=
extPrinterService
.
printQrCode
(
content
,
6
,
3
);
//强制走两行纸才能生效
//强制走两行纸才能生效
ret
=
extPrinterService
.
lineWrap
(
2
);
ret
=
extPrinterService
.
lineWrap
(
3
);
if
(
isFeed
)
{
if
(
isFeed
)
{
ret
=
feedPaper
();
ret
=
feedPaper
();
}
}
...
@@ -231,9 +240,7 @@ class SunmiK1Printer extends BasePrinter {
...
@@ -231,9 +240,7 @@ class SunmiK1Printer extends BasePrinter {
if
(
actualBm
!=
null
)
{
if
(
actualBm
!=
null
)
{
actualBm
.
recycle
();
actualBm
.
recycle
();
}
}
if
(
isFeed
)
{
//打印图片会默认强制走纸一行
ret
=
feedPaper
();
}
return
changeStatus2Custom
(
ret
);
return
changeStatus2Custom
(
ret
);
}
catch
(
RemoteException
e
)
{
}
catch
(
RemoteException
e
)
{
throw
new
PrinterException
(
PrinterStatusEnum
.
CODE_FAILED
.
status
,
e
.
getMessage
());
throw
new
PrinterException
(
PrinterStatusEnum
.
CODE_FAILED
.
status
,
e
.
getMessage
());
...
@@ -431,7 +438,6 @@ class SunmiK1Printer extends BasePrinter {
...
@@ -431,7 +438,6 @@ class SunmiK1Printer extends BasePrinter {
public
void
printQr
(
String
data
,
int
modulesize
,
int
errorlevel
)
{
public
void
printQr
(
String
data
,
int
modulesize
,
int
errorlevel
)
{
if
(
extPrinterService
==
null
)
{
if
(
extPrinterService
==
null
)
{
throw
new
PrinterException
(
PrinterStatusEnum
.
CODE_FAILED
.
status
,
"服务已断开!"
);
throw
new
PrinterException
(
PrinterStatusEnum
.
CODE_FAILED
.
status
,
"服务已断开!"
);
}
}
try
{
try
{
extPrinterService
.
setAlignMode
(
1
);
extPrinterService
.
setAlignMode
(
1
);
...
...
print/src/main/java/com/miya/print/SunmiT1Printer.java
0 → 100644
View file @
721d3247
package
com
.
miya
.
print
;
import
android.content.Context
;
public
class
SunmiT1Printer
extends
BasePrinter
{
final
static
String
TAG
=
SunmiT1Printer
.
class
.
getSimpleName
();
@Override
public
boolean
init
(
Context
context
)
{
return
super
.
init
(
context
);
}
@Override
public
boolean
release
()
{
return
super
.
release
();
}
@Override
public
int
printText
(
int
align
,
int
size
,
String
content
,
boolean
isFeed
)
{
return
super
.
printText
(
align
,
size
,
content
,
isFeed
);
}
}
print/src/main/java/com/miya/print/utils/AccumulateUtils.java
0 → 100644
View file @
721d3247
package
com
.
miya
.
print
.
utils
;
import
android.support.annotation.Nullable
;
import
java.nio.charset.Charset
;
/**
* 累积工具类
*/
public
class
AccumulateUtils
{
/**
* 80mm打印纸一行允许的最大打印字符数(默认字符大小)
*/
final
static
int
MAX_WORDS_80MM
=
46
;
/**
* 58mm打印纸一行允许的最大打印字符数(默认字符大小)
*/
final
static
int
MAX_WORDS_58MM
=
32
;
/**
* 80mm打印纸类型
*/
public
final
static
int
PAGE_MODE_80MM
=
0
;
/**
* 58mm打印纸类型
*/
public
final
static
int
PAGE_MODE_58MM
=
1
;
/**
* 累积字符串
*/
@Nullable
private
static
String
accumulateStr
=
""
;
private
AccumulateUtils
()
{
throw
new
UnsupportedOperationException
();
}
/**
* 累积字符串,自带换行
*
* @param pageMode 打印纸类型
* @param size 字符大小
* @param align 对齐方式
* @param str 待累积的新字符串
* @return 当前累积的字符串
*/
public
static
String
accumulateStringWithLF
(
int
pageMode
,
int
align
,
int
size
,
String
str
)
{
if
(
str
==
null
)
{
return
accumulateStr
;
}
switch
(
pageMode
)
{
case
PAGE_MODE_80MM:
return
accumulateString_80mm
(
align
,
size
,
str
,
true
);
case
PAGE_MODE_58MM:
return
accumulateString_58mm
(
align
,
size
,
str
,
true
);
}
return
accumulateStr
;
}
/**
* 累积字符串
*
* @param pageMode 打印纸类型
* @param size 字符大小
* @param align 对齐方式
* @param str 待累积的新字符串
* @param isFeed 是否走纸
* @return 当前累积的字符串
*/
public
static
String
accumulateString
(
int
pageMode
,
int
align
,
int
size
,
String
str
,
boolean
isFeed
)
{
if
(
str
==
null
)
{
return
accumulateStr
;
}
switch
(
pageMode
)
{
case
PAGE_MODE_80MM:
return
accumulateString_80mm
(
align
,
size
,
str
,
isFeed
);
case
PAGE_MODE_58MM:
return
accumulateString_58mm
(
align
,
size
,
str
,
isFeed
);
}
return
accumulateStr
;
}
/**
* 立即清除已经累积的字符串
*/
public
static
void
clearAccmulateString
()
{
accumulateStr
=
""
;
}
/**
* 获取字符串的byte长度
*
* @param str
* @return
*/
private
static
int
getBytesLength
(
String
str
)
{
if
(
str
!=
null
)
{
return
str
.
getBytes
(
Charset
.
forName
(
"utf-8"
)).
length
;
}
return
0
;
}
/**
* 80mm打印纸累积字符串,自带换行
*
* @param align 对齐方式
* @param size 字符大小
* @param str 字符串内容
* @param isFeed 是否走纸
* @return 当前累积的字符串
*/
public
static
String
accumulateString_80mm
(
int
align
,
int
size
,
String
str
,
boolean
isFeed
)
{
if
(
str
==
null
)
{
return
accumulateStr
;
}
//待拼接的字符串
String
targetStr
=
""
;
switch
(
align
)
{
case
0
:
//居左
accumulateStr
=
accumulateStr
+
str
;
break
;
case
1
:
//居中
if
(
getBytesLength
(
str
)
>
MAX_WORDS_80MM
)
{
accumulateStr
=
accumulateStr
+
str
;
break
;
}
//计算空格数
for
(
int
i
=
0
;
i
<
(
MAX_WORDS_80MM
-
getBytesLength
(
str
))
/
2
;
i
++)
{
targetStr
=
targetStr
+
" "
;
}
targetStr
=
targetStr
+
str
;
accumulateStr
=
accumulateStr
+
targetStr
;
break
;
case
2
:
//居右
if
(
getBytesLength
(
str
)
>
MAX_WORDS_80MM
)
{
accumulateStr
=
accumulateStr
+
str
;
break
;
}
//计算空格数
for
(
int
i
=
0
;
i
<
MAX_WORDS_80MM
-
getBytesLength
(
str
);
i
++)
{
targetStr
=
targetStr
+
" "
;
}
targetStr
=
targetStr
+
str
;
accumulateStr
=
accumulateStr
+
targetStr
;
break
;
}
if
(
isFeed
)
{
accumulateStr
+=
"\n"
;
}
return
accumulateStr
;
}
/**
* 58mm打印纸累积字符串,自带换行
*
* @param align 对齐方式
* @param size 字符大小
* @param str 字符串内容
* @param isFeed 是否走纸
* @return 当前累积的字符串
*/
public
static
String
accumulateString_58mm
(
int
align
,
int
size
,
String
str
,
boolean
isFeed
)
{
if
(
str
==
null
)
{
return
accumulateStr
;
}
//待拼接的字符串
String
targetStr
=
""
;
switch
(
align
)
{
case
0
:
//居左
accumulateStr
=
accumulateStr
+
str
;
break
;
case
1
:
//居中
if
(
getBytesLength
(
str
)
>
MAX_WORDS_58MM
)
{
accumulateStr
=
accumulateStr
+
str
;
break
;
}
//计算空格数
for
(
int
i
=
0
;
i
<
(
MAX_WORDS_58MM
-
getBytesLength
(
str
))
/
2
;
i
++)
{
targetStr
=
targetStr
+
" "
;
}
targetStr
=
targetStr
+
str
;
accumulateStr
=
accumulateStr
+
targetStr
;
break
;
case
2
:
//居右
if
(
getBytesLength
(
str
)
>
MAX_WORDS_58MM
)
{
accumulateStr
=
accumulateStr
+
str
;
break
;
}
//计算空格数
for
(
int
i
=
0
;
i
<
MAX_WORDS_58MM
-
getBytesLength
(
str
);
i
++)
{
targetStr
=
targetStr
+
" "
;
}
targetStr
=
targetStr
+
str
;
accumulateStr
=
accumulateStr
+
targetStr
;
break
;
}
if
(
isFeed
)
{
accumulateStr
+=
"\n"
;
}
return
accumulateStr
;
}
/**
* 获取当前已经累积完的字符串
*
* @return 当前已经累积完的字符串
*/
public
static
String
getAccumulatedString
()
{
return
accumulateStr
;
}
}
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