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
e12bb94c
Commit
e12bb94c
authored
Feb 19, 2019
by
pengguangpu
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
又优化了n个点
海信机器对齐方式采用输入指令的方式进行对齐; printermanager添加自带重试逻辑的检测方法
parent
4e5c9f23
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
139 additions
and
1 deletion
+139
-1
PrintActivity.java
app/src/main/java/com/miya/hardware/PrintActivity.java
+2
-0
HisensePrinter.java
print/src/main/java/com/miya/print/HisensePrinter.java
+18
-1
PrinterManager.java
print/src/main/java/com/miya/print/PrinterManager.java
+14
-0
PrintLogger.java
print/src/main/java/com/miya/print/utils/PrintLogger.java
+105
-0
No files found.
app/src/main/java/com/miya/hardware/PrintActivity.java
View file @
e12bb94c
...
...
@@ -146,7 +146,9 @@ public class PrintActivity extends Activity implements View.OnClickListener {
String
content
=
etText
.
getText
().
toString
();
if
(
PrinterManager
.
getInstance
().
isConnected
())
{
try
{
PrinterManager
.
getInstance
().
getPrinter
().
beginPrint
();
int
ret
=
PrinterManager
.
getInstance
().
getPrinter
().
printText
(-
1
,
textSize
,
content
,
true
);
PrinterManager
.
getInstance
().
getPrinter
().
endPrint
();
}
catch
(
Exception
ex
)
{
ex
.
printStackTrace
();
tvResult
.
setText
(
ex
.
getMessage
());
...
...
print/src/main/java/com/miya/print/HisensePrinter.java
View file @
e12bb94c
...
...
@@ -13,6 +13,7 @@ import com.hisense.hs650service.aidl.HS650Api;
import
com.miya.print.exception.PrinterException
;
import
com.miya.print.utils.AccumulateUtils
;
import
com.miya.print.utils.BitmapUtils
;
import
com.miya.print.utils.ESCUtil
;
import
com.miya.print.utils.PrintLogger
;
public
class
HisensePrinter
extends
BasePrinter
{
...
...
@@ -128,7 +129,7 @@ public class HisensePrinter extends BasePrinter {
try
{
if
(
bm
.
getWidth
()
>
MAX_IMG_WIDTH_80MM
)
{
actualBm
=
BitmapUtils
.
scaleBitmap
(
bm
,
MAX_IMG_WIDTH_80MM
,
0
);
ret
=
hs650Api
.
getPrinter
().
printImage
(
actualBm
,
ALIGN_LEFT
);
ret
=
hs650Api
.
getPrinter
().
printImage
(
actualBm
,
this
.
align
);
}
else
{
ret
=
hs650Api
.
getPrinter
().
printImage
(
bm
,
this
.
align
);
}
...
...
@@ -221,6 +222,22 @@ public class HisensePrinter extends BasePrinter {
if
(
this
.
align
!=
align
&&
(
align
==
0
||
align
==
1
||
align
==
2
))
{
this
.
align
=
align
;
try
{
switch
(
this
.
align
)
{
case
IPrinter
.
ALIGN_LEFT
:
ret
=
hs650Api
.
getPrinter
().
printSend
(
ESCUtil
.
alignLeft
(),
ESCUtil
.
alignLeft
().
length
);
break
;
case
IPrinter
.
ALIGN_CENTER
:
ret
=
hs650Api
.
getPrinter
().
printSend
(
ESCUtil
.
alignCenter
(),
ESCUtil
.
alignLeft
().
length
);
break
;
case
IPrinter
.
ALIGN_RIGHT
:
ret
=
hs650Api
.
getPrinter
().
printSend
(
ESCUtil
.
alignRight
(),
ESCUtil
.
alignLeft
().
length
);
break
;
}
}
catch
(
Exception
ex
)
{
ex
.
printStackTrace
();
throw
new
PrinterException
(
PrinterStatusEnum
.
CODE_FAILED
.
status
,
ex
.
getMessage
());
}
}
return
ret
;
}
...
...
print/src/main/java/com/miya/print/PrinterManager.java
View file @
e12bb94c
...
...
@@ -90,15 +90,21 @@ public class PrinterManager {
}
Class
printerCls
=
Class
.
forName
(
type
.
clsName
);
printer
=
(
IPrinter
)
printerCls
.
newInstance
();
printer
.
setPrinterName
(
type
.
typeName
);
PrintLogger
.
writeLog
(
this
.
ctx
,
"开始初始化打印机:"
+
type
.
typeName
);
boolean
result
=
printer
.
init
(
context
);
if
(
result
==
true
)
{
printer
.
setPrinterName
(
type
.
typeName
);
PrintLogger
.
i
(
TAG
,
"初始化打印机成功,打印机器为:"
+
type
.
typeName
);
PrintLogger
.
writeLog
(
this
.
ctx
,
"初始化打印机成功,打印机器为:"
+
type
.
typeName
);
return
true
;
}
else
{
PrintLogger
.
writeLog
(
this
.
ctx
,
"初始化"
+
type
.
typeName
+
"失败"
);
}
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
printer
=
null
;
PrintLogger
.
writeLog
(
ctx
,
"初始化"
+
type
.
typeName
+
"打印机出现异常:"
+
e
.
getMessage
());
}
}
//没有找到对应的打印机
...
...
@@ -111,6 +117,7 @@ public class PrinterManager {
printer
=
null
;
}
PrintLogger
.
i
(
TAG
,
"没找到对应的打印机器,请检查"
);
PrintLogger
.
writeLog
(
ctx
,
"没找到对应的打印机器,请检查"
);
return
false
;
}
...
...
@@ -162,14 +169,21 @@ public class PrinterManager {
try
{
Class
printerCls
=
Class
.
forName
(
type
.
clsName
);
printer
=
(
IPrinter
)
printerCls
.
newInstance
();
printer
.
setPrinterName
(
type
.
typeName
);
PrintLogger
.
writeLog
(
this
.
ctx
,
"开始初始化打印机:"
+
type
.
typeName
);
boolean
result
=
printer
.
init
(
context
);
if
(
result
==
true
)
{
printer
.
setPrinterName
(
type
.
typeName
);
PrintLogger
.
i
(
TAG
,
"初始化打印机成功,打印机器为:"
+
type
.
typeName
);
}
else
{
printer
=
null
;
PrintLogger
.
writeLog
(
ctx
,
"没找到指定的打印机器:"
+
type
.
typeName
+
",请检查"
);
}
return
result
;
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
printer
=
null
;
PrintLogger
.
writeLog
(
ctx
,
"初始化"
+
type
.
typeName
+
"打印机出现异常:"
+
e
.
getMessage
());
}
return
false
;
}
...
...
print/src/main/java/com/miya/print/utils/PrintLogger.java
View file @
e12bb94c
package
com
.
miya
.
print
.
utils
;
import
android.content.Context
;
import
android.os.Environment
;
import
android.text.TextUtils
;
import
com.miya.print.BuildConfig
;
import
java.io.File
;
import
java.io.FileOutputStream
;
import
java.io.IOException
;
import
java.io.OutputStreamWriter
;
import
java.nio.charset.Charset
;
import
java.text.SimpleDateFormat
;
import
java.util.Date
;
import
java.util.concurrent.Callable
;
import
java.util.concurrent.ExecutionException
;
import
java.util.concurrent.Executors
;
import
java.util.concurrent.Future
;
import
java.util.concurrent.ScheduledExecutorService
;
public
class
PrintLogger
{
private
static
final
String
Tag
=
PrintLogger
.
class
.
getSimpleName
();
...
...
@@ -15,6 +34,74 @@ public class PrintLogger {
*/
static
boolean
isOpenWriteLog
=
true
;
/**
* log文件
*/
private
static
File
logFile
;
/**
* 执行器
*/
private
static
ScheduledExecutorService
sExecutor
;
/**
* 文件夹路径
*/
public
static
final
String
CACHE_FILE_PATH
=
Environment
.
getExternalStorageDirectory
().
getPath
()
+
File
.
separator
+
"miyaterminal"
+
File
.
separator
+
"log"
+
File
.
separator
;
/**
* 写日志
*
* @param input 写入内容
* @return
*/
public
static
boolean
writeLog
(
final
Context
c
,
final
String
input
)
{
if
(
isOpenWriteLog
==
false
)
{
return
false
;
}
if
(
sExecutor
==
null
)
{
sExecutor
=
Executors
.
newScheduledThreadPool
(
5
);
}
Future
<
Boolean
>
submit
=
sExecutor
.
submit
(
new
Callable
<
Boolean
>()
{
@Override
public
Boolean
call
()
{
try
{
File
dir
=
new
File
(
CACHE_FILE_PATH
);
if
(!
dir
.
exists
())
{
dir
.
mkdirs
();
}
String
logPath
=
CACHE_FILE_PATH
+
Tag
+
"_"
+
format
(
new
Date
(),
"yyyyMMdd"
)
+
"_v"
+
BuildConfig
.
VERSION_NAME
+
".txt"
;
logFile
=
new
File
(
logPath
);
FileOutputStream
fos
=
new
FileOutputStream
(
logFile
,
true
);
OutputStreamWriter
writer
=
new
OutputStreamWriter
(
fos
,
Charset
.
forName
(
"utf-8"
));
//写上当前时间点
writer
.
write
(
format
(
new
Date
(),
"yyyy-MM-dd HH:mm:ss"
)
+
" :"
);
writer
.
write
(
input
);
//换行一下
writer
.
write
(
"\n"
);
writer
.
flush
();
writer
.
close
();
return
true
;
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
return
false
;
}
}
});
try
{
return
submit
.
get
();
}
catch
(
InterruptedException
e
)
{
e
.
printStackTrace
();
}
catch
(
ExecutionException
e
)
{
e
.
printStackTrace
();
}
return
false
;
}
public
static
void
log
(
String
text
)
{
if
(
isOpenLogcat
)
{
android
.
util
.
Log
.
v
(
Tag
,
"-----:"
+
text
);
...
...
@@ -87,4 +174,22 @@ public class PrintLogger {
android
.
util
.
Log
.
e
(
tag
,
msg
,
tr
);
}
}
/**
* 将日期转换成字符串
*
* @param date
* @param format
* @return
*/
private
static
String
format
(
Date
date
,
String
format
)
{
if
(
date
==
null
)
{
throw
new
IllegalArgumentException
(
"Param date is null!"
);
}
if
(
TextUtils
.
isEmpty
(
format
))
{
throw
new
IllegalArgumentException
(
"Param format is blank!"
);
}
SimpleDateFormat
sdf
=
new
SimpleDateFormat
(
format
);
return
sdf
.
format
(
date
);
}
}
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