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
836ee784
Commit
836ee784
authored
Jan 03, 2019
by
pengguangpu
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
添加创捷打印驱动代码
parent
3506d520
Expand all
Show whitespace changes
Inline
Side-by-side
Showing
18 changed files
with
2798 additions
and
72 deletions
+2798
-72
build_file_checksums.ser
.idea/caches/build_file_checksums.ser
+0
-0
AndroidManifest.xml
app/src/main/AndroidManifest.xml
+3
-0
PrintActivity.java
app/src/main/java/com/miya/hardware/PrintActivity.java
+95
-1
activity_print.xml
app/src/main/res/layout/activity_print.xml
+93
-3
styles.xml
app/src/main/res/values/styles.xml
+1
-1
build.gradle
print/build.gradle
+3
-0
BasePrinter.java
print/src/main/java/com/miya/print/BasePrinter.java
+68
-0
ChuangjiePrinter.java
print/src/main/java/com/miya/print/ChuangjiePrinter.java
+434
-35
IPrinter.java
print/src/main/java/com/miya/print/IPrinter.java
+79
-32
PrinterManager.java
print/src/main/java/com/miya/print/PrinterManager.java
+115
-0
PrinterStatusEnum.java
print/src/main/java/com/miya/print/PrinterStatusEnum.java
+105
-0
CheckStatusUtils.java
.../src/main/java/com/miya/print/utils/CheckStatusUtils.java
+19
-0
FileUtils.java
print/src/main/java/com/miya/print/utils/FileUtils.java
+75
-0
ImageUtils.java
print/src/main/java/com/miya/print/utils/ImageUtils.java
+204
-0
Page58MmPrintUtils.java
...rc/main/java/com/miya/print/utils/Page58MmPrintUtils.java
+349
-0
Page88MmPrintUtils.java
...rc/main/java/com/miya/print/utils/Page88MmPrintUtils.java
+545
-0
PrintFormatUtils.java
.../src/main/java/com/miya/print/utils/PrintFormatUtils.java
+373
-0
SharePreferenceUtils.java
.../main/java/com/miya/print/utils/SharePreferenceUtils.java
+237
-0
No files found.
.idea/caches/build_file_checksums.ser
View file @
836ee784
No preview for this file type
app/src/main/AndroidManifest.xml
View file @
836ee784
...
@@ -2,6 +2,9 @@
...
@@ -2,6 +2,9 @@
<manifest
xmlns:android=
"http://schemas.android.com/apk/res/android"
<manifest
xmlns:android=
"http://schemas.android.com/apk/res/android"
package=
"com.miya.hardware"
>
package=
"com.miya.hardware"
>
<uses-permission
android:name=
"android.permission.WRITE_EXTERNAL_STORAGE"
/>
<uses-permission
android:name=
"android.permission.READ_EXTERNAL_STORAGE"
/>
<application
<application
android:name=
".app.HardwareApplication"
android:name=
".app.HardwareApplication"
android:allowBackup=
"true"
android:allowBackup=
"true"
...
...
app/src/main/java/com/miya/hardware/PrintActivity.java
View file @
836ee784
...
@@ -3,12 +3,106 @@ package com.miya.hardware;
...
@@ -3,12 +3,106 @@ package com.miya.hardware;
import
android.app.Activity
;
import
android.app.Activity
;
import
android.os.Bundle
;
import
android.os.Bundle
;
import
android.support.annotation.Nullable
;
import
android.support.annotation.Nullable
;
import
android.view.View
;
import
android.widget.Button
;
import
android.widget.EditText
;
import
android.widget.TextView
;
public
class
PrintActivity
extends
Activity
{
import
com.miya.print.PrinterManager
;
import
com.miya.print.utils.CheckStatusUtils
;
public
class
PrintActivity
extends
Activity
implements
View
.
OnClickListener
{
Button
btnInit
;
Button
btnRelease
;
Button
btnPrintText
;
Button
btnSetTextSize
;
Button
btnCut
;
EditText
etText
;
TextView
tvResult
;
/**
* 文字大小或放大倍数
*/
int
textSize
;
@Override
@Override
protected
void
onCreate
(
@Nullable
Bundle
savedInstanceState
)
{
protected
void
onCreate
(
@Nullable
Bundle
savedInstanceState
)
{
super
.
onCreate
(
savedInstanceState
);
super
.
onCreate
(
savedInstanceState
);
setContentView
(
R
.
layout
.
activity_print
);
setContentView
(
R
.
layout
.
activity_print
);
//findView
btnInit
=
findViewById
(
R
.
id
.
btnInit
);
btnRelease
=
findViewById
(
R
.
id
.
btnRelease
);
btnPrintText
=
findViewById
(
R
.
id
.
btnPrintText
);
btnSetTextSize
=
findViewById
(
R
.
id
.
btnSetTextSize
);
etText
=
findViewById
(
R
.
id
.
etText
);
tvResult
=
findViewById
(
R
.
id
.
tvResult
);
btnCut
=
findViewById
(
R
.
id
.
btnCut
);
//set click listener
btnInit
.
setOnClickListener
(
this
);
btnRelease
.
setOnClickListener
(
this
);
btnPrintText
.
setOnClickListener
(
this
);
btnSetTextSize
.
setOnClickListener
(
this
);
btnCut
.
setOnClickListener
(
this
);
findViewById
(
R
.
id
.
ivBack
).
setOnClickListener
(
this
);
}
@Override
public
void
onClick
(
View
v
)
{
switch
(
v
.
getId
())
{
case
R
.
id
.
btnInit
:
//初始化打印硬件
boolean
result
=
PrinterManager
.
getInstance
().
init
(
this
);
if
(
result
)
{
tvResult
.
setText
(
"找到了打印机,机器名称:"
+
PrinterManager
.
getInstance
().
getPrinter
().
getPrinterName
());
}
else
{
tvResult
.
setText
(
"没找到打印机"
);
}
break
;
case
R
.
id
.
btnRelease
:
if
(
PrinterManager
.
getInstance
().
isConnected
())
{
if
(
PrinterManager
.
getInstance
().
getPrinter
().
release
()
==
true
)
{
tvResult
.
setText
(
"销毁成功"
);
}
else
{
tvResult
.
setText
(
"没初始化过"
);
}
}
break
;
case
R
.
id
.
btnPrintText
:
String
content
=
etText
.
getText
().
toString
();
if
(
PrinterManager
.
getInstance
().
isConnected
())
{
try
{
int
ret
=
PrinterManager
.
getInstance
().
getPrinter
().
printText
(
0
,
0
,
content
,
true
);
}
catch
(
Exception
ex
)
{
ex
.
printStackTrace
();
tvResult
.
setText
(
ex
.
getMessage
());
}
}
break
;
case
R
.
id
.
btnSetTextSize
:
break
;
case
R
.
id
.
btnCut
:
if
(
PrinterManager
.
getInstance
().
isConnected
())
{
try
{
PrinterManager
.
getInstance
().
getPrinter
().
cutPaper
();
}
catch
(
Exception
ex
)
{
ex
.
printStackTrace
();
tvResult
.
setText
(
ex
.
getMessage
());
}
}
break
;
case
R
.
id
.
ivBack
:
finish
();
break
;
}
}
}
}
}
app/src/main/res/layout/activity_print.xml
View file @
836ee784
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<
android.support.constraint.Constraint
Layout
xmlns:android=
"http://schemas.android.com/apk/res/android"
<
Linear
Layout
xmlns:android=
"http://schemas.android.com/apk/res/android"
android:layout_width=
"match_parent"
android:layout_width=
"match_parent"
android:layout_height=
"match_parent"
>
android:layout_height=
"match_parent"
android:orientation=
"vertical"
>
</android.support.constraint.ConstraintLayout>
<android.support.v7.widget.ActionMenuView
\ No newline at end of file
android:layout_width=
"match_parent"
android:layout_height=
"88dp"
android:background=
"@color/colorPrimary"
>
<ImageView
android:id=
"@+id/ivBack"
android:layout_width=
"wrap_content"
android:layout_height=
"wrap_content"
android:src=
"@android:drawable/ic_media_previous"
/>
<TextView
android:layout_width=
"wrap_content"
android:layout_height=
"wrap_content"
android:layout_margin=
"16dp"
android:gravity=
"right"
android:text=
"打印开放组件"
android:textColor=
"#ffffff"
android:textSize=
"36sp"
/>
</android.support.v7.widget.ActionMenuView>
<TextView
android:id=
"@+id/tvResult"
android:layout_width=
"wrap_content"
android:layout_height=
"wrap_content"
android:layout_gravity=
"center_horizontal"
android:text=
"显示操作结果"
android:textSize=
"36sp"
/>
<TableLayout
android:id=
"@+id/layoutOp"
android:layout_width=
"match_parent"
android:layout_height=
"wrap_content"
android:clipChildren=
"false"
android:padding=
"16dp"
>
<TableRow>
<Button
android:id=
"@+id/btnInit"
android:layout_width=
"wrap_content"
android:layout_height=
"wrap_content"
android:layout_weight=
"1"
android:text=
"初始化"
/>
<Button
android:id=
"@+id/btnRelease"
android:layout_width=
"wrap_content"
android:layout_height=
"wrap_content"
android:layout_weight=
"1"
android:text=
"销毁资源"
/>
</TableRow>
<TableRow>
<EditText
android:id=
"@+id/etText"
android:layout_width=
"wrap_content"
android:layout_height=
"wrap_content"
android:layout_weight=
"1"
/>
<Button
android:id=
"@+id/btnPrintText"
android:layout_width=
"wrap_content"
android:layout_height=
"wrap_content"
android:text=
"打印默认字符"
/>
</TableRow>
<TableRow>
<Button
android:id=
"@+id/btnSetTextSize"
android:layout_width=
"wrap_content"
android:layout_height=
"wrap_content"
android:layout_weight=
"1"
android:text=
"设置文字大小"
/>
</TableRow>
<TableRow>
<Button
android:id=
"@+id/btnCut"
android:layout_width=
"wrap_content"
android:layout_height=
"wrap_content"
android:layout_weight=
"1"
android:text=
"切纸"
/>
</TableRow>
</TableLayout>
</LinearLayout>
\ No newline at end of file
app/src/main/res/values/styles.xml
View file @
836ee784
<resources>
<resources>
<!-- Base application theme. -->
<!-- Base application theme. -->
<style
name=
"AppTheme"
parent=
"Theme.AppCompat.Light.
Dark
ActionBar"
>
<style
name=
"AppTheme"
parent=
"Theme.AppCompat.Light.
No
ActionBar"
>
<!-- Customize your theme here. -->
<!-- Customize your theme here. -->
<item
name=
"colorPrimary"
>
@color/colorPrimary
</item>
<item
name=
"colorPrimary"
>
@color/colorPrimary
</item>
<item
name=
"colorPrimaryDark"
>
@color/colorPrimaryDark
</item>
<item
name=
"colorPrimaryDark"
>
@color/colorPrimaryDark
</item>
...
...
print/build.gradle
View file @
836ee784
...
@@ -32,6 +32,9 @@ dependencies {
...
@@ -32,6 +32,9 @@ dependencies {
androidTestImplementation
'com.android.support.test:runner:1.0.2'
androidTestImplementation
'com.android.support.test:runner:1.0.2'
androidTestImplementation
'com.android.support.test.espresso:espresso-core:3.0.2'
androidTestImplementation
'com.android.support.test.espresso:espresso-core:3.0.2'
//一些工具类集合,从主工程拷贝过来的
implementation
'com.blankj:utilcode:1.16.0'
/**
/**
* 创捷机器打印jar包
* 创捷机器打印jar包
*/
*/
...
...
print/src/main/java/com/miya/print/BasePrinter.java
View file @
836ee784
package
com
.
miya
.
print
;
package
com
.
miya
.
print
;
import
android.content.Context
;
import
android.content.Context
;
import
android.graphics.Bitmap
;
/**
/**
* 打印基类
* 打印基类
...
@@ -17,8 +18,14 @@ public class BasePrinter implements IPrinter {
...
@@ -17,8 +18,14 @@ public class BasePrinter implements IPrinter {
*/
*/
int
status
;
int
status
;
/**
* 打印机器名称
*/
String
name
;
@Override
@Override
public
boolean
init
(
Context
context
)
{
public
boolean
init
(
Context
context
)
{
this
.
context
=
context
;
return
false
;
return
false
;
}
}
...
@@ -27,8 +34,69 @@ public class BasePrinter implements IPrinter {
...
@@ -27,8 +34,69 @@ public class BasePrinter implements IPrinter {
return
false
;
return
false
;
}
}
@Override
public
int
printText
(
int
align
,
int
size
,
String
content
,
boolean
isFeed
)
{
return
PrinterStatusEnum
.
CODE_SUCCESS
.
status
;
}
@Override
public
int
printBarcode
(
int
align
,
String
content
,
boolean
isFeed
)
{
return
0
;
}
@Override
public
int
printQrcode
(
int
align
,
String
content
,
boolean
isFeed
)
{
return
0
;
}
@Override
public
int
printImage
(
int
align
,
Bitmap
bm
,
boolean
isFeed
)
{
return
0
;
}
@Override
public
int
cutPaper
()
{
return
0
;
}
@Override
public
int
setAlign
(
int
align
)
{
return
0
;
}
@Override
public
int
feedPaper
()
{
return
0
;
}
@Override
public
int
getPrinterStatus
()
{
return
0
;
}
@Override
public
int
excuteCmd
(
String
cmd
)
{
return
0
;
}
@Override
@Override
public
int
beginPrint
()
{
public
int
beginPrint
()
{
return
0
;
return
0
;
}
}
@Override
public
int
endPrint
()
{
return
0
;
}
@Override
public
void
setPrinterName
(
String
name
)
{
this
.
name
=
name
;
}
@Override
public
String
getPrinterName
()
{
return
this
.
name
;
}
}
}
print/src/main/java/com/miya/print/ChuangjiePrinter.java
View file @
836ee784
This diff is collapsed.
Click to expand it.
print/src/main/java/com/miya/print/IPrinter.java
View file @
836ee784
package
com
.
miya
.
print
;
package
com
.
miya
.
print
;
import
android.content.Context
;
import
android.content.Context
;
import
android.graphics.Bitmap
;
/**
/**
* 标准打印接口
* 标准打印接口
...
@@ -10,78 +11,124 @@ import android.content.Context;
...
@@ -10,78 +11,124 @@ import android.content.Context;
*/
*/
public
interface
IPrinter
{
public
interface
IPrinter
{
//================全局状态码定义start===============//
/**
/**
* 操作成功
* 初始化驱动
*
* @param context 调用者上下文索引
* @return true==>初始化成功;false==>初始化失败
*/
*/
int
CODE_SUCCESS
=
0
;
boolean
init
(
Context
context
)
;
/**
/**
* 操作失败
* 销毁资源
*
* @return true==>销毁成功;false==>销毁失败
*/
*/
int
CODE_FAILED
=
-
1
;
boolean
release
()
;
/**
/**
* 打印机未连接或未上电
* 打印文字
*
* @param align 对齐方式
* @param size 文字大小
* @param content 待打印内容
* @param isFeed 是否走纸,true==>走纸
* @return 状态码(参考状态码枚举)
*/
*/
int
CODE_NOT_CONNECTED
=
1
;
int
printText
(
int
align
,
int
size
,
String
content
,
boolean
isFeed
)
;
/**
/**
* 打印机和调用库不匹配
* 打印条码
*
* @param align 对齐方式
* @param content 条码内容
* @param isFeed 是否走纸,true==>走纸
* @return 状态码(参考状态码枚举)
*/
*/
int
CODE_NOT_MATCHED
=
2
;
int
printBarcode
(
int
align
,
String
content
,
boolean
isFeed
)
;
/**
/**
* 打印头打开
* 打印二维码
*
* @param align 对齐方式
* @param content 条码内容
* @param isFeed 是否走纸,true==>走纸
* @return 状态码(参考状态码枚举)
*/
*/
int
CODE_PRINT_HEAD_OPENED
=
3
;
int
printQrcode
(
int
align
,
String
content
,
boolean
isFeed
)
;
/**
/**
* 切刀未复位
* 打印图片
*
* @param align 对齐方式
* @param bm 图片
* @param isFeed 是否走纸,true==>走纸
* @return 状态码(参考状态码枚举)
*/
*/
int
CODE_KNIFE_ERROR
=
4
;
int
printImage
(
int
align
,
Bitmap
bm
,
boolean
isFeed
)
;
/**
/**
* 打印头过热
* 切纸
*
* @return 状态码(参考状态码枚举)
*/
*/
int
CODE_PRINT_HEAD_HOT
=
5
;
int
cutPaper
()
;
/**
/**
* 黑标错误
* 设置对齐方式
*
* @param align 对齐方式,0:巨左;1:居中;2:居右
* @return 状态码(参考状态码枚举)
*/
*/
int
CODE_BLACK_ERROR
=
6
;
int
setAlign
(
int
align
)
;
/**
/**
* 纸尽
* 走纸
*
* @return 状态码(参考状态码枚举)
*/
*/
int
CODE_NO_PAPER
=
7
;
int
feedPaper
()
;
/**
/**
* 纸将尽
* 获取打印机状态
*
* @return 状态码(参考状态码枚举)
*/
*/
int
CODE_FEW_PAPER
=
8
;
int
getPrinterStatus
();
//================全局状态码定义end===============//
/**
/**
*
初始化驱动
*
执行指令
*
*
* @param c
ontext 调用者上下文索引
* @param c
md 指令内容
* @return
true==>初始化成功;false==>初始化失败
* @return
状态码(参考状态码枚举)
*/
*/
boolean
init
(
Context
context
);
int
excuteCmd
(
String
cmd
);
/**
/**
*
销毁资源
*
开始新一轮打印
*
*
* @return
* @return
状态码(参考状态码枚举)
*/
*/
boolean
release
();
int
beginPrint
();
/**
/**
*
开始新一
轮打印
*
结束本
轮打印
*
*
* @return 状态码
* @return 状态码
(参考状态码枚举)
*/
*/
int
beginPrint
();
int
endPrint
();
/**
* 设置打印机器名称
*
* @param name 打印机器名称
*/
void
setPrinterName
(
String
name
);
/**
* 获取打印机器名称
*
* @return 打印机器名称
*/
String
getPrinterName
();
}
}
\ No newline at end of file
print/src/main/java/com/miya/print/PrinterManager.java
0 → 100644
View file @
836ee784
package
com
.
miya
.
print
;
import
android.content.Context
;
import
com.miya.print.utils.D
;
/**
* 打印机管理者
*/
public
class
PrinterManager
{
final
static
String
TAG
=
PrinterManager
.
class
.
getSimpleName
();
/**
* 设备类型枚举,用于轮询过程中的反射
*/
enum
Type
{
//这里添加扩展的驱动
// TYPE_HISENSE("hisense", ChuangjiePrinter.class.getName()),
// TYPE_YINGTAI("yingtai", ChuangjiePrinter.class.getName()),
// TYPE_SUNMI("shangmi", ChuangjiePrinter.class.getName()),
TYPE_CHUANGJIE
(
"chuangjie"
,
ChuangjiePrinter
.
class
.
getName
());
/**
* 设备类型名称
*/
String
typeName
;
/**
* 设备驱动类全类名
*/
String
clsName
;
Type
(
String
typeName
,
String
clsName
)
{
this
.
typeName
=
typeName
;
this
.
clsName
=
clsName
;
}
@Override
public
String
toString
()
{
return
"Type{"
+
"typeName='"
+
typeName
+
'\''
+
", clsName='"
+
clsName
+
'\''
+
'}'
;
}
}
public
static
PrinterManager
instance
;
private
PrinterManager
()
{
}
public
static
PrinterManager
getInstance
()
{
synchronized
(
PrinterManager
.
class
)
{
if
(
instance
==
null
)
{
instance
=
new
PrinterManager
();
}
return
instance
;
}
}
/**
* 打印机器
*/
IPrinter
printer
;
/**
* 轮询初始化
*
* @return 状态码
*/
public
boolean
init
(
Context
context
)
{
for
(
Type
type
:
Type
.
values
())
{
if
(
printer
!=
null
)
{
printer
.
release
();
}
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
true
;
}
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
}
//没有找到对应的打印机
if
(
printer
!=
null
)
{
printer
.
release
();
}
D
.
i
(
TAG
,
"没找到对应的打印机器,请检查"
);
return
false
;
}
/**
* 获取当前打印接口
*
* @return
*/
public
IPrinter
getPrinter
()
{
return
printer
;
}
/**
* 判断当前打印机是否已连接
*
* @return
*/
public
boolean
isConnected
()
{
return
printer
!=
null
;
}
}
print/src/main/java/com/miya/print/PrinterStatusEnum.java
0 → 100644
View file @
836ee784
package
com
.
miya
.
print
;
/**
* 打印枚举类
*
* @author pupu
*/
public
enum
PrinterStatusEnum
{
/**
* 操作成功
*/
CODE_SUCCESS
(
0
,
"操作成功"
),
/**
* 操作失败
*/
CODE_FAILED
(-
1
,
"操作失败"
),
/**
* 打印机未连接或未上电
*/
CODE_NOT_CONNECTED
(
1
,
"打印机未连接或未上电"
),
/**
* 打印机和调用库不匹配
*/
CODE_NOT_MATCHED
(
2
,
"打印机和调用库不匹配"
),
/**
* 打印头打开
*/
CODE_PRINT_HEAD_OPENED
(
3
,
"打印头打开"
),
/**
* 切刀未复位
*/
CODE_KNIFE_ERROR
(
4
,
"切刀未复位"
),
/**
* 打印头过热
*/
CODE_PRINT_HEAD_HOT
(
5
,
"打印头过热"
),
/**
* 黑标错误
*/
CODE_BLACK_ERROR
(
6
,
"黑标错误"
),
/**
* 纸尽
*/
CODE_NO_PAPER
(
7
,
"纸尽"
),
/**
* 纸将尽
*/
CODE_FEW_PAPER
(
8
,
"纸将尽"
);
/**
* 状态码
*/
int
status
;
/**
* 描述
*/
String
desc
;
public
int
getStatus
()
{
return
status
;
}
public
void
setStatus
(
int
status
)
{
this
.
status
=
status
;
}
public
String
getDesc
()
{
return
desc
;
}
public
void
setDesc
(
String
desc
)
{
this
.
desc
=
desc
;
}
PrinterStatusEnum
(
int
status
,
String
desc
)
{
this
.
status
=
status
;
this
.
desc
=
desc
;
}
/**
* 根据状态码查找对应的枚举
*
* @param status 状态码
* @return 找到了就返回对应的枚举,没找到就返回null
*/
public
static
PrinterStatusEnum
find
(
int
status
)
{
for
(
PrinterStatusEnum
statusEnum
:
PrinterStatusEnum
.
values
())
{
if
(
statusEnum
.
status
==
status
)
{
return
statusEnum
;
}
}
return
null
;
}
}
print/src/main/java/com/miya/print/utils/CheckStatusUtils.java
0 → 100644
View file @
836ee784
package
com
.
miya
.
print
.
utils
;
import
com.miya.print.PrinterStatusEnum
;
/**
* 检查操作结果工具类
*/
public
class
CheckStatusUtils
{
public
static
void
checkState
(
int
ret
)
{
if
(
ret
!=
0
)
{
for
(
PrinterStatusEnum
statusEnum
:
PrinterStatusEnum
.
values
())
{
if
(
ret
==
statusEnum
.
getStatus
())
{
throw
new
RuntimeException
(
statusEnum
.
getStatus
()
+
":"
+
statusEnum
.
getDesc
());
}
}
}
}
}
print/src/main/java/com/miya/print/utils/FileUtils.java
0 → 100644
View file @
836ee784
package
com
.
miya
.
print
.
utils
;
import
android.content.Context
;
import
android.os.Environment
;
import
java.io.File
;
import
java.io.FileInputStream
;
import
java.io.FileOutputStream
;
import
static
android
.
content
.
Context
.
MODE_PRIVATE
;
/**
* Created by chen on 2016/8/23.
*/
public
class
FileUtils
{
//文件存储根目录
public
String
getFileRoot
(
Context
context
)
{
if
(
Environment
.
getExternalStorageState
().
equals
(
Environment
.
MEDIA_MOUNTED
))
{
File
external
=
context
.
getExternalFilesDir
(
null
);
if
(
external
!=
null
)
{
return
external
.
getAbsolutePath
();
}
}
return
context
.
getFilesDir
().
getAbsolutePath
();
}
//向指定的文件中写入指定的数据
public
void
writeFileData
(
Context
context
,
String
filename
,
String
content
){
try
{
FileOutputStream
fos
=
context
.
openFileOutput
(
filename
,
MODE_PRIVATE
);
//获得FileOutputStream
//将要写入的字符串转换为byte数组
byte
[]
bytes
=
content
.
getBytes
();
fos
.
write
(
bytes
);
//将byte数组写入文件
fos
.
close
();
//关闭文件输出流
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
}
//打开指定文件,读取其数据,返回字符串对象
public
String
readFileData
(
Context
context
,
String
fileName
){
String
result
=
""
;
try
{
FileInputStream
fis
=
context
.
openFileInput
(
fileName
);
//获取文件长度
int
lenght
=
fis
.
available
();
byte
[]
buffer
=
new
byte
[
lenght
];
fis
.
read
(
buffer
);
//将byte数组转换成指定格式的字符串
result
=
new
String
(
buffer
,
"UTF-8"
);
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
return
result
;
}
}
print/src/main/java/com/miya/print/utils/ImageUtils.java
0 → 100644
View file @
836ee784
package
com
.
miya
.
print
.
utils
;
import
android.graphics.Bitmap
;
import
android.graphics.Canvas
;
import
android.graphics.Color
;
import
android.graphics.Rect
;
/**
* ImageUtils
* 图片处理功能,主要功能是进行黑白化
*
* @author cq
* @create by chen in 2016年8月19日11:43:59
* @modify,
* @modifytime
*/
public
class
ImageUtils
{
/**
* 对图片进行压缩(去除透明度)
*
* @param
*/
private
static
int
errorcode
;
public
static
Bitmap
compressPic
(
Bitmap
bitmap
,
int
newWidth
,
int
newHeight
)
{
// 获取这个图片的宽和高
int
width
=
bitmap
.
getWidth
();
int
height
=
bitmap
.
getHeight
();
// 指定调整后的宽度和高度
// int newWidth = 240;
// int newHeight = 240;
if
(
newWidth
<=
0
)
{
newWidth
=
240
;
//setStatus(PrintErrorCode.IMAGE_WIDTH_ERROR);
}
if
(
newHeight
<=
0
)
{
newHeight
=
240
;
//setStatus(PrintErrorCode.IMAGE_HEIGHT_ERROR);
}
Bitmap
targetBmp
=
Bitmap
.
createBitmap
(
newWidth
,
newHeight
,
Bitmap
.
Config
.
ARGB_8888
);
Canvas
targetCanvas
=
new
Canvas
(
targetBmp
);
targetCanvas
.
drawColor
(
0xffffffff
);
targetCanvas
.
drawBitmap
(
bitmap
,
new
Rect
(
0
,
0
,
width
,
height
),
new
Rect
(
0
,
0
,
newWidth
,
newHeight
),
null
);
return
targetBmp
;
}
/**
* 灰度图片黑白化,黑色是1,白色是0
*
* @param x 横坐标
* @param y 纵坐标
* @param bit 位图
* @return
*/
public
static
byte
px2Byte
(
int
x
,
int
y
,
Bitmap
bit
)
{
if
(
x
<
bit
.
getWidth
()
&&
y
<
bit
.
getHeight
())
{
byte
b
;
int
pixel
=
bit
.
getPixel
(
x
,
y
);
int
red
=
(
pixel
&
0x00ff0000
)
>>
16
;
// 取高两位
int
green
=
(
pixel
&
0x0000ff00
)
>>
8
;
// 取中两位
int
blue
=
pixel
&
0x000000ff
;
// 取低两位
int
gray
=
RGB2Gray
(
red
,
green
,
blue
);
if
(
gray
<
128
)
{
b
=
1
;
}
else
{
b
=
0
;
}
return
b
;
}
return
0
;
}
/**
* 图片灰度的转化
*/
private
static
int
RGB2Gray
(
int
r
,
int
g
,
int
b
)
{
int
gray
=
(
int
)
(
0.29900
*
r
+
0.58700
*
g
+
0.11400
*
b
);
//灰度转化公式
return
gray
;
}
/* *************************************************************************
* 假设一个240*240的图片,分辨率设为24, 共分10行打印
* 每一行,是一个 240*24 的点阵, 每一列有24个点,存储在3个byte里面。
* 每个byte存储8个像素点信息。因为只有黑白两色,所以对应为1的位是黑色,对应为0的位是白色
**************************************************************************/
/**
* 把一张Bitmap图片转化为打印机可以打印的字节流
*
* @param bmp
* @return
*/
public
static
byte
[]
draw2PxPoint
(
Bitmap
bmp
)
{
//用来存储转换后的 bitmap 数据。为什么要再加1000,这是为了应对当图片高度无法
//整除24时的情况。比如bitmap 分辨率为 240 * 250,占用 7500 byte,5:5455,3,5447,4,5427
//但是实际上要存储11行数据,每一行需要 24 * 240 / 8 =720byte 的空间。再加上一些指令存储的开销,
//所以多申请 1000byte 的空间是稳妥的,不然运行时会抛出数组访问越界的异常。
int
size
=
bmp
.
getWidth
()
*
bmp
.
getHeight
()
/
8
+
1000
;
byte
[]
data
=
new
byte
[
size
];
int
k
=
0
;
//设置行距为0的指令
data
[
k
++]
=
0x1B
;
data
[
k
++]
=
0x33
;
data
[
k
++]
=
0x00
;
// 逐行打印
for
(
int
j
=
0
;
j
<
bmp
.
getHeight
()
/
24
f
;
j
++)
{
//打印图片的指令
data
[
k
++]
=
0x1B
;
data
[
k
++]
=
0x2A
;
data
[
k
++]
=
33
;
data
[
k
++]
=
(
byte
)
(
bmp
.
getWidth
()
%
256
);
//nL
data
[
k
++]
=
(
byte
)
(
bmp
.
getWidth
()
/
256
);
//nH
//对于每一行,逐列打印
for
(
int
i
=
0
;
i
<
bmp
.
getWidth
();
i
++)
{
//每一列24个像素点,分为3个字节存储
for
(
int
m
=
0
;
m
<
3
;
m
++)
{
//每个字节表示8个像素点,0表示白色,1表示黑色
for
(
int
n
=
0
;
n
<
8
;
n
++)
{
byte
b
=
px2Byte
(
i
,
j
*
24
+
m
*
8
+
n
,
bmp
);
data
[
k
]
+=
data
[
k
]
+
b
;
}
k
++;
}
}
data
[
k
++]
=
10
;
//换行
}
// long a=System.currentTimeMillis();
byte
[]
data1
=
new
byte
[
k
];
System
.
arraycopy
(
data
,
0
,
data1
,
0
,
k
);
// long b=System.currentTimeMillis();
// System.out.println("结束字节:"+k+"---"+data.length+"耗时:"+(b-a));
return
data1
;
}
/**
* jpg png bmp 彩色图片转换Bitmap数据为int[]数组
* @param bm
* @return int[]
*/
public
static
int
[]
getPixelsByBitmap
(
Bitmap
bm
)
{
int
width
,
heigh
;
width
=
bm
.
getWidth
();
heigh
=
bm
.
getHeight
();
int
iDataLen
=
width
*
heigh
;
int
[]
pixels
=
new
int
[
iDataLen
];
bm
.
getPixels
(
pixels
,
0
,
width
,
0
,
0
,
width
,
heigh
);
return
pixels
;
}
/**
* 获取单色位图
* @param inputBMP
* @return Bitmap
*/
public
static
Bitmap
getSinglePic
(
Bitmap
inputBMP
)
{
int
[]
pix
=
new
int
[
inputBMP
.
getWidth
()
*
inputBMP
.
getHeight
()];
int
[]
colorTemp
=
new
int
[
inputBMP
.
getWidth
()
*
inputBMP
.
getHeight
()];
inputBMP
.
getPixels
(
pix
,
0
,
inputBMP
.
getWidth
(),
0
,
0
,
inputBMP
.
getWidth
(),
inputBMP
.
getHeight
());
Bitmap
returnBMP
=
Bitmap
.
createBitmap
(
inputBMP
.
getWidth
(),
inputBMP
.
getHeight
(),
Bitmap
.
Config
.
RGB_565
);
int
lightNumber
=
127
;
// 曝光度,這個顔色是中間值,如果大於中間值,那就是黑色,否則白色,数值越小,曝光度越高
// for (int j = 0; j < colorTemp.length; j++) {
// // 将颜色数组中的RGB值取反,255减去当前颜色值就获得当前颜色的反色
// // 網上的,但是我要進行曝光處理,使他變成單色圖
// colorTemp[j] = Color.rgb(Color.red(pix[j]) > lightNumber ? 255 : 0,
// Color.green(pix[j]) > lightNumber ? 255 : 0,
// Color.blue(pix[j]) > lightNumber ? 255 : 0);
// }
for
(
int
j
=
0
;
j
<
colorTemp
.
length
;
j
++)
{
colorTemp
[
j
]
=
Color
.
rgb
(
Color
.
red
(
pix
[
j
]),
Color
.
green
(
pix
[
j
]),
Color
.
blue
(
pix
[
j
]));
}
for
(
int
i
=
0
;
i
<
colorTemp
.
length
;
i
++)
{
// 這裏需要思考一下,上一步有可能得到:純紅,純黃,純藍,黑色,白色這樣5種顔色,前三種是應該變成白色還是黑色呢?
// 發現這是一個很複雜的問題,涉及到不同區域閒顔色的對比,如果是黑色包圍紅色,那紅色就應該是白色,反之變成黑色。。。
// 似乎衹能具體問題具體分析,這裏就先把黃色設成白色,藍色=白色,紅色=黑色
int
r
=
Color
.
red
(
pix
[
i
]);
int
g
=
Color
.
green
(
pix
[
i
]);
int
b
=
Color
.
blue
(
pix
[
i
]);
// 有兩種顔色以上的混合,那就是變成黑色但目前这种方法,对于黑白的曝光效果更出色,
// 原理是设置一个曝光值,然后三种颜色相加大于3倍的曝光值,才是黑色,否则白色
if
(
r
+
g
+
b
>
3
*
lightNumber
)
{
colorTemp
[
i
]
=
Color
.
rgb
(
255
,
255
,
255
);
}
else
{
colorTemp
[
i
]
=
Color
.
rgb
(
0
,
0
,
0
);
}
}
returnBMP
.
setPixels
(
colorTemp
,
0
,
inputBMP
.
getWidth
(),
0
,
0
,
inputBMP
.
getWidth
(),
inputBMP
.
getHeight
());
return
returnBMP
;
}
public
int
getStatus
()
{
return
errorcode
;
}
private
void
setStatus
(
int
errorcode
)
{
this
.
errorcode
=
errorcode
;
}
}
print/src/main/java/com/miya/print/utils/Page58MmPrintUtils.java
0 → 100644
View file @
836ee784
This diff is collapsed.
Click to expand it.
print/src/main/java/com/miya/print/utils/Page88MmPrintUtils.java
0 → 100644
View file @
836ee784
This diff is collapsed.
Click to expand it.
print/src/main/java/com/miya/print/utils/PrintFormatUtils.java
0 → 100644
View file @
836ee784
This diff is collapsed.
Click to expand it.
print/src/main/java/com/miya/print/utils/SharePreferenceUtils.java
0 → 100644
View file @
836ee784
package
com
.
miya
.
print
.
utils
;
import
android.annotation.SuppressLint
;
import
android.content.Context
;
import
android.content.SharedPreferences
;
import
android.content.SharedPreferences.Editor
;
import
android.util.Base64
;
import
android.util.Log
;
import
java.io.ByteArrayInputStream
;
import
java.io.ByteArrayOutputStream
;
import
java.io.IOException
;
import
java.io.ObjectInputStream
;
import
java.io.ObjectOutputStream
;
import
java.util.Map
;
/**
* @describe sharepreference 数据存储,包括对象
* @author cq
*
*/
public
class
SharePreferenceUtils
{
private
SharedPreferences
mKV
;
private
Editor
mEditor
;
private
static
SharePreferenceUtils
instance
;
private
Context
context
;
public
String
ERROR_INFO
=
null
;
private
static
final
String
TAG
=
"SharePreferenceUtils"
;
/**
* 构造方法。
*
* @param context
* @param kvName
* 键值表名称。
* @param * 打开的模式。值为Context.MODE_APPEND, Context.MODE_PRIVATE,
* Context.WORLD_READABLE, Context.WORLD_WRITEABLE.
*/
public
SharePreferenceUtils
(
Context
context
,
String
kvName
)
{
this
.
context
=
context
;
mKV
=
context
.
getSharedPreferences
(
kvName
,
Context
.
MODE_PRIVATE
);
mEditor
=
mKV
.
edit
();
}
/**
* 获取保存着的boolean对象。
*
* @param key
* 键名
* @param defValue
* 当不存在时返回的默认值。
* @return 返回获取到的值,当不存在时返回默认值。
*/
public
boolean
getBoolean
(
String
key
,
boolean
defValue
)
{
return
mKV
.
getBoolean
(
key
,
defValue
);
}
/**
* 获取保存着的int对象。
*
* @param key
* 键名
* @param defValue
* 当不存在时返回的默认值。
* @return 返回获取到的值,当不存在时返回默认值。
*/
public
int
getInt
(
String
key
,
int
defValue
)
{
return
mKV
.
getInt
(
key
,
defValue
);
}
/**
* 获取保存着的long对象。
*
* @param key
* 键名
* @param defValue
* 当不存在时返回的默认值。
* @return 返回获取到的值,当不存在时返回默认值。
*/
public
long
getLong
(
String
key
,
long
defValue
)
{
return
mKV
.
getLong
(
key
,
defValue
);
}
/**
* 获取保存着的float对象。
*
* @param key
* 键名
* @param defValue
* 当不存在时返回的默认值。
* @return 返回获取到的值,当不存在时返回默认值。
*/
public
float
getFloat
(
String
key
,
float
defValue
)
{
return
mKV
.
getFloat
(
key
,
defValue
);
}
/**
* 获取保存着的String对象。
*
* @param key
* 键名
* @param defValue
* 当不存在时返回的默认值。
* @return 返回获取到的值,当不存在时返回默认值。
*/
public
String
getString
(
String
key
,
String
defValue
)
{
return
mKV
.
getString
(
key
,
defValue
);
}
/**
* 获取所有键值对。
*
* @return 获取到的所胡键值对。
*/
public
Map
<
String
,
?>
getAll
()
{
return
mKV
.
getAll
();
}
/**
* 设置一个键值对,它将在{@linkplain #commit()}被调用时保存。<br/>
* 注意:当保存的value不是boolean, byte(会被转换成int保存),int, long, float,
* String等类型时将调用它的toString()方法进行值的保存。
*
* @param key
* 键名称。
* @param value
* 值。
* @return 引用的KV对象。
*/
public
SharePreferenceUtils
put
(
String
key
,
Object
value
)
{
if
(
value
instanceof
Boolean
)
{
mEditor
.
putBoolean
(
key
,
(
Boolean
)
value
);
}
else
if
(
value
instanceof
Integer
||
value
instanceof
Byte
)
{
mEditor
.
putInt
(
key
,
(
Integer
)
value
);
}
else
if
(
value
instanceof
Long
)
{
mEditor
.
putLong
(
key
,
(
Long
)
value
);
}
else
if
(
value
instanceof
Float
)
{
mEditor
.
putFloat
(
key
,
(
Float
)
value
);
}
else
if
(
value
instanceof
String
)
{
mEditor
.
putString
(
key
,
(
String
)
value
);
}
else
{
mEditor
.
putString
(
key
,
value
.
toString
());
}
return
this
;
}
/**
* SharedPreferences保存对象-----
*
* @param
* @param key
* @param object
*/
@SuppressLint
(
"NewApi"
)
public
void
setObjectValue
(
String
key
,
Object
object
)
{
String
objectBase64
=
""
;
try
{
ByteArrayOutputStream
baos
=
new
ByteArrayOutputStream
();
ObjectOutputStream
oos
=
new
ObjectOutputStream
(
baos
);
oos
.
writeObject
(
object
);
objectBase64
=
Base64
.
encodeToString
(
baos
.
toByteArray
(),
Base64
.
DEFAULT
);
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
mEditor
.
putString
(
key
,
objectBase64
);
mEditor
.
commit
();
}
/**
* SharedPreferences取得对象
*
* @param
* @param key
* @return
*/
@SuppressLint
(
"NewApi"
)
public
Object
getObjectValue
(
String
key
)
{
Object
object
=
null
;
try
{
String
objectBase64
=
mKV
.
getString
(
key
,
""
);
byte
[]
base64Bytes
=
Base64
.
decode
(
objectBase64
.
getBytes
(),
Base64
.
DEFAULT
);
ByteArrayInputStream
bais
=
new
ByteArrayInputStream
(
base64Bytes
);
ObjectInputStream
ois
=
new
ObjectInputStream
(
bais
);
object
=
ois
.
readObject
();
}
catch
(
Exception
e
)
{
// 发生异常情况下清空对应缓存
ERROR_INFO
=
e
.
toString
();
Log
.
e
(
TAG
,
e
.
toString
());
}
return
object
;
}
/**
* 移除键值对。
*
* @param key
* 要移除的键名称。
* @return 引用的KV对象。
*/
public
SharePreferenceUtils
remove
(
String
key
)
{
mEditor
.
remove
(
key
);
return
this
;
}
/**
* 清除所有键值对。
*
* @return 引用的KV对象。
*/
public
SharePreferenceUtils
clear
()
{
mEditor
.
clear
();
return
this
;
}
/**
* 是否包含某个键。
*
* @param key
* 查询的键名称。
* @return 当且仅当包含该键时返回true, 否则返回false.
*/
public
boolean
contains
(
String
key
)
{
return
mKV
.
contains
(
key
);
}
/**
* 返回是否提交成功。
*
* @return 当且仅当提交成功时返回true, 否则返回false.
*/
public
boolean
commit
()
{
return
mEditor
.
commit
();
}
}
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