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
eb5a9693
Commit
eb5a9693
authored
Jan 22, 2019
by
pengguangpu
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
测试发现商米机器的打印竟然可以使用桑达的打印驱动
parent
641ec581
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
16 changed files
with
1676 additions
and
21 deletions
+1676
-21
build_file_checksums.ser
.idea/caches/build_file_checksums.ser
+0
-0
PrintActivity.java
app/src/main/java/com/miya/hardware/PrintActivity.java
+10
-12
build.gradle
print/build.gradle
+4
-1
core-3.3.0.jar
print/libs/core-3.3.0.jar
+0
-0
HS650Api.aidl
...src/main/aidl/com/hisense/hs650service/aidl/HS650Api.aidl
+10
-0
LedStripe.aidl
...rc/main/aidl/com/hisense/hs650service/aidl/LedStripe.aidl
+6
-0
Printer.aidl
.../src/main/aidl/com/hisense/hs650service/aidl/Printer.aidl
+18
-0
ExtPrinterService.aidl
...n/aidl/com/sunmi/extprinterservice/ExtPrinterService.aidl
+39
-0
HisensePrinter.java
print/src/main/java/com/miya/print/HisensePrinter.java
+6
-0
PrinterManager.java
print/src/main/java/com/miya/print/PrinterManager.java
+1
-1
ShangmiK1Printer.java
print/src/main/java/com/miya/print/ShangmiK1Printer.java
+344
-0
BytesUtil.java
print/src/main/java/com/miya/print/utils/BytesUtil.java
+714
-0
ESCUtil.java
print/src/main/java/com/miya/print/utils/ESCUtil.java
+431
-0
Page58MmPrintUtils.java
...rc/main/java/com/miya/print/utils/Page58MmPrintUtils.java
+0
-3
Page80MmPrintUtils.java
...rc/main/java/com/miya/print/utils/Page80MmPrintUtils.java
+1
-4
RegexUtils.java
print/src/main/java/com/miya/print/utils/RegexUtils.java
+92
-0
No files found.
.idea/caches/build_file_checksums.ser
View file @
eb5a9693
No preview for this file type
app/src/main/java/com/miya/hardware/PrintActivity.java
View file @
eb5a9693
...
...
@@ -4,7 +4,6 @@ import android.app.Activity;
import
android.graphics.Bitmap
;
import
android.graphics.BitmapFactory
;
import
android.os.Bundle
;
import
android.print.PrintManager
;
import
android.support.annotation.Nullable
;
import
android.view.View
;
import
android.widget.Button
;
...
...
@@ -15,8 +14,7 @@ import android.widget.TextView;
import
com.miya.print.IPrinter
;
import
com.miya.print.PrinterManager
;
import
com.miya.print.PrinterStatusEnum
;
import
com.miya.print.utils.BitmapUtils
;
import
com.miya.print.utils.Page88MmPrintUtils
;
import
com.miya.print.utils.Page80MmPrintUtils
;
public
class
PrintActivity
extends
Activity
implements
View
.
OnClickListener
{
...
...
@@ -254,7 +252,7 @@ public class PrintActivity extends Activity implements View.OnClickListener {
ret
=
printer
.
printText
(-
1
,
0
,
"中百"
,
true
);
ret
=
printer
.
printText
(-
1
,
0
,
"支付宝自助收银"
,
true
);
//分割线
ret
=
printer
.
printText
(-
1
,
0
,
Page8
8
MmPrintUtils
.
printDivideLineString
(),
true
);
ret
=
printer
.
printText
(-
1
,
0
,
Page8
0
MmPrintUtils
.
printDivideLineString
(),
true
);
//居左打印
ret
=
printer
.
setAlign
(
IPrinter
.
ALIGN_LEFT
);
//订单号
...
...
@@ -266,24 +264,24 @@ public class PrintActivity extends Activity implements View.OnClickListener {
//打印时间
ret
=
printer
.
printText
(-
1
,
0
,
"打印时间:2019-01-10 15:16:27"
,
true
);
//分割线
ret
=
printer
.
printText
(-
1
,
0
,
Page8
8
MmPrintUtils
.
printDivideLineString
(),
true
);
ret
=
printer
.
printText
(-
1
,
0
,
Page8
0
MmPrintUtils
.
printDivideLineString
(),
true
);
//四条目打印
ret
=
printer
.
printText
(-
1
,
0
,
Page8
8
MmPrintUtils
.
printFourData
(
"商品名称"
,
"编码"
,
"数量"
,
"金额"
),
true
);
ret
=
printer
.
printText
(-
1
,
0
,
Page8
0
MmPrintUtils
.
printFourData
(
"商品名称"
,
"编码"
,
"数量"
,
"金额"
),
true
);
//商品名称
ret
=
printer
.
printText
(-
1
,
0
,
"中号购物袋520**(300+80)"
,
true
);
//三条目打印
ret
=
printer
.
printText
(-
1
,
0
,
Page8
8
MmPrintUtils
.
printFourData
(
"6924160298876"
,
" "
,
"*1"
,
"0.20元"
),
true
);
ret
=
printer
.
printText
(-
1
,
0
,
Page8
0
MmPrintUtils
.
printFourData
(
"6924160298876"
,
" "
,
"*1"
,
"0.20元"
),
true
);
ret
=
printer
.
feedPaper
();
//分割线
ret
=
printer
.
printText
(-
1
,
0
,
Page8
8
MmPrintUtils
.
printDivideLineString
(),
true
);
ret
=
printer
.
printText
(-
1
,
0
,
Page8
0
MmPrintUtils
.
printDivideLineString
(),
true
);
//总件数
ret
=
printer
.
printText
(-
1
,
0
,
Page8
8
MmPrintUtils
.
printTwoData
(
"总件数:"
,
"1"
),
true
);
ret
=
printer
.
printText
(-
1
,
0
,
Page8
0
MmPrintUtils
.
printTwoData
(
"总件数:"
,
"1"
),
true
);
//商品合计
ret
=
printer
.
printText
(-
1
,
0
,
Page8
8
MmPrintUtils
.
printTwoData
(
"商品合计:"
,
"0.20"
),
true
);
ret
=
printer
.
printText
(-
1
,
0
,
Page8
0
MmPrintUtils
.
printTwoData
(
"商品合计:"
,
"0.20"
),
true
);
//支付宝实付
ret
=
printer
.
printText
(-
1
,
0
,
Page8
8
MmPrintUtils
.
printTwoData
(
"支付宝"
,
"0.01"
),
true
);
ret
=
printer
.
printText
(-
1
,
0
,
Page8
0
MmPrintUtils
.
printTwoData
(
"支付宝"
,
"0.01"
),
true
);
//分割线
ret
=
printer
.
printText
(-
1
,
0
,
Page8
8
MmPrintUtils
.
printDivideLineString
(),
true
);
ret
=
printer
.
printText
(-
1
,
0
,
Page8
0
MmPrintUtils
.
printDivideLineString
(),
true
);
ret
=
printer
.
setAlign
(
IPrinter
.
ALIGN_CENTER
);
//小票尾部
ret
=
printer
.
printText
(-
1
,
0
,
"欢迎下次光临!!!"
,
true
);
...
...
print/build.gradle
View file @
eb5a9693
...
...
@@ -39,7 +39,10 @@ dependencies {
androidTestImplementation
'com.android.support.test.espresso:espresso-core:3.0.2'
//一些工具类集合,从主工程拷贝过来的
implementation
'com.blankj:utilcode:1.16.0'
// implementation 'com.blankj:utilcode:1.16.0'
//zxing
implementation
files
(
'libs/core-3.3.0.jar'
)
/**
* 创捷机器打印jar包
...
...
print/libs/core-3.3.0.jar
0 → 100644
View file @
eb5a9693
File added
print/src/main/aidl/com/hisense/hs650service/aidl/HS650Api.aidl
0 → 100644
View file @
eb5a9693
package
com
.
hisense
.
hs650service
.
aidl
;
import
com
.
hisense
.
hs650service
.
aidl
.
Printer
;
import
com
.
hisense
.
hs650service
.
aidl
.
LedStripe
;
interface
HS650Api
{
Printer
getPrinter
();
LedStripe
getLedStripe
();
}
print/src/main/aidl/com/hisense/hs650service/aidl/LedStripe.aidl
0 → 100644
View file @
eb5a9693
package
com
.
hisense
.
hs650service
.
aidl
;
interface
LedStripe
{
void
lightOn
();
void
lightOff
();
}
print/src/main/aidl/com/hisense/hs650service/aidl/Printer.aidl
0 → 100644
View file @
eb5a9693
package
com
.
hisense
.
hs650service
.
aidl
;
interface
Printer
{
//
printer
int
initPrinter
(
int
type
);
int
printTextStr
(
String
text
,
int
fontSize
,
int
pattern
);
int
printImage
(
in
Bitmap
bitmap
,
int
mode
);
int
feedPaper
(
int
value
,
int
unit
);
int
printerConfig
(
int
baudrate
,
int
uartPort
,
boolean
ctrlOPen
);
int
printBar
(
String
barcode
);
int
printQR
(
String
barcode
,
int
mode
);
void
closePrinter
();
void
cutPaper
();
int
printSend
(
in
byte
[]
data
,
int
len
);
int
printStatus
();
String
getPrnType
(
int
baudrate
,
int
uartPort
,
int
timeout
);
}
\ No newline at end of file
print/src/main/aidl/com/sunmi/extprinterservice/ExtPrinterService.aidl
0 → 100755
View file @
eb5a9693
//
IPrinterService
.
aidl
package
com
.
sunmi
.
extprinterservice
;
//
Declare
any
non
-
default
types
here
with
import
statements
interface
ExtPrinterService
{
int
sendRawData
(
in
byte
[]
cmd
);
int
printerInit
();
int
getPrinterStatus
();
int
lineWrap
(
in
int
n
);
int
pixelWrap
(
in
int
n
);
int
flush
();
int
tab
();
int
printText
(
in
String
text
);
int
printBarCode
(
in
String
code
,
in
int
type
,
in
int
width
,
in
int
height
,
in
int
hriPos
);
int
printQrCode
(
in
String
code
,
in
int
modeSize
,
in
int
errorlevel
);
int
printBitmap
(
in
Bitmap
bitmap
,
in
int
mode
);
int
setHorizontalTab
(
in
int
[]
k
);
int
setAlignMode
(
in
int
type
);
int
cutPaper
(
in
int
m
,
in
int
n
);
int
setFontZoom
(
in
int
hori
,
in
int
veri
);
int
printColumnsText
(
in
String
[]
colsTextArr
,
in
int
[]
colsWidthArr
,
in
int
[]
colsAlign
);
}
print/src/main/java/com/miya/print/HisensePrinter.java
0 → 100644
View file @
eb5a9693
package
com
.
miya
.
print
;
public
class
HisensePrinter
extends
BasePrinter
{
final
static
String
TAG
=
HisensePrinter
.
class
.
getSimpleName
();
}
print/src/main/java/com/miya/print/PrinterManager.java
View file @
eb5a9693
...
...
@@ -17,7 +17,7 @@ public class PrinterManager {
enum
Type
{
//这里添加扩展的驱动
// TYPE_HISENSE("hisense", ChuangjiePrinter.class.getName()),
// TYPE_SUNMI("shangmi", Chuangjie
Printer.class.getName()),
TYPE_SUNMI
(
"shangmik1"
,
ShangmiK1
Printer
.
class
.
getName
()),
TYPE_YINGTAI
(
"yingtai"
,
YingtaiPrinter
.
class
.
getName
()),
TYPE_CHUANGJIE
(
"chuangjie"
,
ChuangjiePrinter
.
class
.
getName
()),
TYPE_SANGDA
(
"sangda"
,
SangdaPrinter
.
class
.
getName
());
...
...
print/src/main/java/com/miya/print/ShangmiK1Printer.java
0 → 100644
View file @
eb5a9693
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
com.miya.print.exception.PrinterException
;
import
com.miya.print.utils.BytesUtil
;
import
com.miya.print.utils.ESCUtil
;
import
com.sunmi.extprinterservice.ExtPrinterService
;
import
java.io.ByteArrayOutputStream
;
import
java.io.IOException
;
class
ShangmiK1Printer
extends
BasePrinter
{
final
static
String
TAG
=
ShangmiK1Printer
.
class
.
getSimpleName
();
private
ServiceConnection
connService
=
new
ServiceConnection
()
{
@Override
public
void
onServiceDisconnected
(
ComponentName
name
)
{
extPrinterService
=
null
;
}
@Override
public
void
onServiceConnected
(
ComponentName
name
,
IBinder
service
)
{
extPrinterService
=
ExtPrinterService
.
Stub
.
asInterface
(
service
);
}
};
@Override
public
boolean
init
(
Context
context
)
{
return
super
.
init
(
context
);
}
@Override
public
boolean
release
()
{
return
super
.
release
();
}
private
static
final
String
SERVICE
_
PACKAGE
=
"com.sunmi.extprinterservice"
;
private
static
final
String
SERVICE
_
ACTION
=
"com.sunmi.extprinterservice.PrinterService"
;
private
ExtPrinterService
extPrinterService
;
/**
* 连接服务
*
* @param context context
*/
@Deprecated
public
void
connectPrinterService
(
Context
context
)
{
this
.
context
=
context
.
getApplicationContext
();
Intent
intent
=
new
Intent
();
intent
.
setPackage
(
SERVICE
_
PACKAGE
);
intent
.
setAction
(
SERVICE
_
ACTION
);
context
.
getApplicationContext
().
startService
(
intent
);
context
.
getApplicationContext
().
bindService
(
intent
,
connService
,
Context
.
BIND_AUTO_CREATE
);
}
/**
* 断开服务
*
* @param context context
*/
@Deprecated
public
void
disconnectPrinterService
(
Context
context
)
{
if
(
extPrinterService
!=
null
)
{
context
.
getApplicationContext
().
unbindService
(
connService
);
extPrinterService
=
null
;
}
}
@Deprecated
public
boolean
isConnect
()
{
return
extPrinterService
!=
null
;
}
@Deprecated
public
void
LF
()
{
if
(
extPrinterService
==
null
)
{
throw
new
PrinterException
(
PrinterStatusEnum
.
CODE_FAILED
.
status
,
"服务已断开!"
);
}
try
{
extPrinterService
.
lineWrap
(
1
);
}
catch
(
RemoteException
e
)
{
throw
new
PrinterException
(
PrinterStatusEnum
.
CODE_FAILED
.
status
,
e
.
getMessage
());
}
}
@Deprecated
public
void
cutPage
()
{
if
(
extPrinterService
==
null
)
{
throw
new
PrinterException
(
PrinterStatusEnum
.
CODE_FAILED
.
status
,
"服务已断开!"
);
}
try
{
extPrinterService
.
cutPaper
(
0
,
0
);
}
catch
(
RemoteException
e
)
{
throw
new
PrinterException
(
PrinterStatusEnum
.
CODE_FAILED
.
status
,
e
.
getMessage
());
}
}
@Deprecated
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
());
}
}
/**
* 设置打印浓度
*/
private
int
[]
darkness
=
new
int
[]{
0x0600
,
0x0500
,
0x0400
,
0x0300
,
0x0200
,
0x0100
,
0
,
0xffff
,
0xfeff
,
0xfdff
,
0xfcff
,
0xfbff
,
0xfaff
};
public
void
setDarkness
(
int
index
)
{
if
(
extPrinterService
==
null
)
{
throw
new
PrinterException
(
PrinterStatusEnum
.
CODE_FAILED
.
status
,
"服务已断开!"
);
}
int
k
=
darkness
[
index
];
try
{
extPrinterService
.
sendRawData
(
ESCUtil
.
setPrinterDarkness
(
k
));
}
catch
(
RemoteException
e
)
{
throw
new
PrinterException
(
PrinterStatusEnum
.
CODE_FAILED
.
status
,
e
.
getMessage
());
}
}
/**
* 初始化打印机
*/
@Deprecated
public
void
initPrinter
()
{
if
(
extPrinterService
==
null
)
{
return
;
}
try
{
extPrinterService
.
printerInit
();
}
catch
(
RemoteException
e
)
{
}
}
/**
* 打印二维码
*/
@Deprecated
public
void
printQr
(
String
data
,
int
modulesize
,
int
errorlevel
)
{
if
(
extPrinterService
==
null
)
{
throw
new
PrinterException
(
PrinterStatusEnum
.
CODE_FAILED
.
status
,
"服务已断开!"
);
}
try
{
extPrinterService
.
setAlignMode
(
1
);
extPrinterService
.
printQrCode
(
data
,
modulesize
,
errorlevel
);
extPrinterService
.
lineWrap
(
2
);
}
catch
(
RemoteException
e
)
{
throw
new
PrinterException
(
PrinterStatusEnum
.
CODE_FAILED
.
status
,
e
.
getMessage
());
}
}
/**
* 打印条形码 symbology条码类型
*/
@Deprecated
public
void
printBarCode
(
String
code
,
int
type
,
int
width
,
int
height
,
int
hriPos
)
{
if
(
extPrinterService
==
null
)
{
throw
new
PrinterException
(
PrinterStatusEnum
.
CODE_FAILED
.
status
,
"服务已断开!"
);
}
try
{
extPrinterService
.
printBarCode
(
code
,
type
,
width
,
height
,
hriPos
);
extPrinterService
.
lineWrap
(
2
);
}
catch
(
RemoteException
e
)
{
throw
new
PrinterException
(
PrinterStatusEnum
.
CODE_FAILED
.
status
,
e
.
getMessage
());
}
}
/**
* 打印一维条形码
* data 条码内容
* height 条码高度
* width 条码宽度
* textposition 条码内容位置
*/
@Deprecated
public
void
printLongBarCode
(
String
data
,
int
height
,
int
width
,
int
textposition
)
throws
RemoteException
,
IOException
{
if
(
width
<
2
||
width
>
6
)
{
width
=
2
;
}
if
(
textposition
<
0
||
textposition
>
3
)
{
textposition
=
0
;
}
if
(
height
<
1
||
height
>
255
)
{
height
=
162
;
}
ByteArrayOutputStream
buffer
=
new
ByteArrayOutputStream
();
buffer
.
write
(
new
byte
[]{
0x1D
,
0x48
,
(
byte
)
textposition
,
0x1D
,
0x77
,
(
byte
)
width
,
0x1D
,
0x68
,
(
byte
)
height
,
0x0A
});
byte
[]
barcode
;
barcode
=
BytesUtil
.
getBytesFromDecString
(
data
);
buffer
.
write
(
new
byte
[]{
0x1D
,
0x6B
,
0x49
,
(
byte
)
(
barcode
.
length
+
2
),
0x7B
,
(
byte
)
(
0x43
)});
buffer
.
write
(
barcode
);
extPrinterService
.
sendRawData
(
buffer
.
toByteArray
());
}
/**
* 打印文字
*/
@Deprecated
public
void
printText
(
String
content
,
float
size
,
boolean
isBold
,
boolean
isUnderLine
)
{
if
(
extPrinterService
==
null
)
{
throw
new
PrinterException
(
PrinterStatusEnum
.
CODE_FAILED
.
status
,
"服务已断开!"
);
}
try
{
if
(
isBold
)
{
extPrinterService
.
sendRawData
(
ESCUtil
.
boldOn
());
}
else
{
extPrinterService
.
sendRawData
(
ESCUtil
.
boldOff
());
}
if
(
isUnderLine
)
{
extPrinterService
.
sendRawData
(
ESCUtil
.
underlineWithOneDotWidthOn
());
}
else
{
extPrinterService
.
sendRawData
(
ESCUtil
.
underlineOff
());
}
extPrinterService
.
printText
(
content
);
}
catch
(
RemoteException
e
)
{
throw
new
PrinterException
(
PrinterStatusEnum
.
CODE_FAILED
.
status
,
e
.
getMessage
());
}
}
/*
*打印图片
*/
@Deprecated
public
void
printBitmap
(
Bitmap
bitmap
)
{
if
(
extPrinterService
==
null
)
{
throw
new
PrinterException
(
PrinterStatusEnum
.
CODE_FAILED
.
status
,
"服务已断开!"
);
}
try
{
extPrinterService
.
setAlignMode
(
1
);
extPrinterService
.
printBitmap
(
bitmap
,
0
);
extPrinterService
.
lineWrap
(
2
);
}
catch
(
RemoteException
e
)
{
throw
new
PrinterException
(
PrinterStatusEnum
.
CODE_FAILED
.
status
,
e
.
getMessage
());
}
}
@Deprecated
public
String
updatePrinterState
()
throws
RemoteException
{
if
(
extPrinterService
==
null
)
{
return
"打印服务已断开!"
;
}
int
state
=
extPrinterService
.
getPrinterStatus
();
if
(
state
==
-
1
)
{
return
"打印机脱机或打印服务还未连接打印机"
;
}
if
(
state
==
0
)
{
return
"正常"
;
}
if
(
state
==
1
)
{
return
"打印机开盖"
;
}
if
(
state
==
2
)
{
return
"打印机缺纸"
;
}
if
(
state
==
3
)
{
return
"打印机即将缺纸"
;
}
if
(
state
==
4
)
{
return
"打印机过热"
;
}
return
"获取状态异常"
;
}
/*
* 空打三行!
*/
@Deprecated
public
void
print3Line
()
{
if
(
extPrinterService
==
null
)
{
throw
new
PrinterException
(
PrinterStatusEnum
.
CODE_FAILED
.
status
,
"服务已断开!"
);
}
try
{
extPrinterService
.
lineWrap
(
3
);
}
catch
(
RemoteException
e
)
{
throw
new
PrinterException
(
PrinterStatusEnum
.
CODE_FAILED
.
status
,
e
.
getMessage
());
}
}
@Deprecated
public
void
sendRawData
(
byte
[]
data
)
{
if
(
extPrinterService
==
null
)
{
throw
new
PrinterException
(
PrinterStatusEnum
.
CODE_FAILED
.
status
,
"服务已断开!"
);
}
try
{
extPrinterService
.
sendRawData
(
data
);
}
catch
(
RemoteException
e
)
{
throw
new
PrinterException
(
PrinterStatusEnum
.
CODE_FAILED
.
status
,
e
.
getMessage
());
}
}
}
print/src/main/java/com/miya/print/utils/BytesUtil.java
0 → 100644
View file @
eb5a9693
This diff is collapsed.
Click to expand it.
print/src/main/java/com/miya/print/utils/ESCUtil.java
0 → 100644
View file @
eb5a9693
This diff is collapsed.
Click to expand it.
print/src/main/java/com/miya/print/utils/Page58MmPrintUtils.java
View file @
eb5a9693
...
...
@@ -2,9 +2,6 @@ package com.miya.print.utils;
import
android.annotation.SuppressLint
;
import
android.text.TextUtils
;
import
com.blankj.utilcode.util.RegexUtils
;
import
java.io.UnsupportedEncodingException
;
import
java.nio.charset.Charset
;
...
...
print/src/main/java/com/miya/print/utils/Page8
8
MmPrintUtils.java
→
print/src/main/java/com/miya/print/utils/Page8
0
MmPrintUtils.java
View file @
eb5a9693
...
...
@@ -2,16 +2,13 @@ package com.miya.print.utils;
import
android.annotation.SuppressLint
;
import
android.text.TextUtils
;
import
com.blankj.utilcode.util.RegexUtils
;
import
java.nio.charset.Charset
;
/**
* Created by fengyu on 2018/2/23.
*/
public
class
Page8
8
MmPrintUtils
{
public
class
Page8
0
MmPrintUtils
{
/**
* 打印纸一行最大的字节
...
...
print/src/main/java/com/miya/print/utils/RegexUtils.java
0 → 100644
View file @
eb5a9693
package
com
.
miya
.
print
.
utils
;
import
java.util.ArrayList
;
import
java.util.Collections
;
import
java.util.List
;
import
java.util.regex.Matcher
;
import
java.util.regex.Pattern
;
public
final
class
RegexUtils
{
private
RegexUtils
()
{
throw
new
UnsupportedOperationException
(
"u can't instantiate me..."
);
}
/**
* Return whether input matches the regex.
*
* @param regex The regex.
* @param input The input.
* @return {@code true}: yes<br>{@code false}: no
*/
public
static
boolean
isMatch
(
final
String
regex
,
final
CharSequence
input
)
{
return
input
!=
null
&&
input
.
length
()
>
0
&&
Pattern
.
matches
(
regex
,
input
);
}
/**
* Return the list of input matches the regex.
*
* @param regex The regex.
* @param input The input.
* @return the list of input matches the regex
*/
public
static
List
<
String
>
getMatches
(
final
String
regex
,
final
CharSequence
input
)
{
if
(
input
==
null
)
return
Collections
.
emptyList
();
List
<
String
>
matches
=
new
ArrayList
<>();
Pattern
pattern
=
Pattern
.
compile
(
regex
);
Matcher
matcher
=
pattern
.
matcher
(
input
);
while
(
matcher
.
find
())
{
matches
.
add
(
matcher
.
group
());
}
return
matches
;
}
/**
* Splits input around matches of the regex.
*
* @param input The input.
* @param regex The regex.
* @return the array of strings computed by splitting input around matches of regex
*/
public
static
String
[]
getSplits
(
final
String
input
,
final
String
regex
)
{
if
(
input
==
null
)
return
new
String
[
0
];
return
input
.
split
(
regex
);
}
/**
* Replace the first subsequence of the input sequence that matches the
* regex with the given replacement string.
*
* @param input The input.
* @param regex The regex.
* @param replacement The replacement string.
* @return the string constructed by replacing the first matching
* subsequence by the replacement string, substituting captured
* subsequences as needed
*/
public
static
String
getReplaceFirst
(
final
String
input
,
final
String
regex
,
final
String
replacement
)
{
if
(
input
==
null
)
return
""
;
return
Pattern
.
compile
(
regex
).
matcher
(
input
).
replaceFirst
(
replacement
);
}
/**
* Replace every subsequence of the input sequence that matches the
* pattern with the given replacement string.
*
* @param input The input.
* @param regex The regex.
* @param replacement The replacement string.
* @return the string constructed by replacing each matching subsequence
* by the replacement string, substituting captured subsequences
* as needed
*/
public
static
String
getReplaceAll
(
final
String
input
,
final
String
regex
,
final
String
replacement
)
{
if
(
input
==
null
)
return
""
;
return
Pattern
.
compile
(
regex
).
matcher
(
input
).
replaceAll
(
replacement
);
}
}
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