Commit 3abdadb4 authored by pengguangpu's avatar pengguangpu

丰富方法实现

parent a90e3acc
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="" vcs="Git" />
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>
\ No newline at end of file
package com.miya.hardware;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import com.miya.print.PrinterManager;
import com.miya.print.utils.CheckStatusUtils;
import com.miya.print.PrinterStatusEnum;
public class PrintActivity extends Activity implements View.OnClickListener {
......@@ -26,12 +29,42 @@ public class PrintActivity extends Activity implements View.OnClickListener {
EditText etText;
/**
* 文字大小或放大倍数,取值0~8
*/
int textSize;
EditText etTextSize;
TextView tvResult;
/**
* 文字大小或放大倍数
* 一维条码打印
*/
int textSize;
EditText etBarcode;
Button btnBarcode;
/**
* 二维码打印
*/
EditText etQrcode;
Button btnQrcode;
/**
* 图片打印
*/
ImageView ivImg;
Button btnPrintImg;
/**
* 获取打印机状态
*/
Button btnStatus;
/**
* 设置对齐方式
*/
EditText etAlign;
Button btnSetAlign;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
......@@ -43,9 +76,19 @@ public class PrintActivity extends Activity implements View.OnClickListener {
btnRelease = findViewById(R.id.btnRelease);
btnPrintText = findViewById(R.id.btnPrintText);
btnSetTextSize = findViewById(R.id.btnSetTextSize);
etTextSize = findViewById(R.id.etTextSize);
etText = findViewById(R.id.etText);
tvResult = findViewById(R.id.tvResult);
btnCut = findViewById(R.id.btnCut);
etBarcode = findViewById(R.id.etBarcode);
btnBarcode = findViewById(R.id.btnBarcode);
etQrcode = findViewById(R.id.etQrcode);
btnQrcode = findViewById(R.id.btnQrcode);
ivImg = findViewById(R.id.ivImg);
btnPrintImg = findViewById(R.id.btnPrintImg);
btnStatus = findViewById(R.id.btnStatus);
btnSetAlign = findViewById(R.id.btnSetAlign);
etAlign = findViewById(R.id.etAlign);
//set click listener
btnInit.setOnClickListener(this);
......@@ -54,6 +97,12 @@ public class PrintActivity extends Activity implements View.OnClickListener {
btnSetTextSize.setOnClickListener(this);
btnCut.setOnClickListener(this);
findViewById(R.id.ivBack).setOnClickListener(this);
btnBarcode.setOnClickListener(this);
btnQrcode.setOnClickListener(this);
btnPrintImg.setOnClickListener(this);
btnStatus.setOnClickListener(this);
btnSetAlign.setOnClickListener(this);
}
@Override
......@@ -81,7 +130,7 @@ public class PrintActivity extends Activity implements View.OnClickListener {
String content = etText.getText().toString();
if (PrinterManager.getInstance().isConnected()) {
try {
int ret = PrinterManager.getInstance().getPrinter().printText(0, 0, content, true);
int ret = PrinterManager.getInstance().getPrinter().printText(-1, textSize, content, true);
} catch (Exception ex) {
ex.printStackTrace();
tvResult.setText(ex.getMessage());
......@@ -89,6 +138,7 @@ public class PrintActivity extends Activity implements View.OnClickListener {
}
break;
case R.id.btnSetTextSize:
textSize = Integer.parseInt(etTextSize.getText().toString());
break;
case R.id.btnCut:
if (PrinterManager.getInstance().isConnected()) {
......@@ -100,6 +150,67 @@ public class PrintActivity extends Activity implements View.OnClickListener {
}
}
break;
case R.id.btnBarcode:
if (PrinterManager.getInstance().isConnected()) {
try {
PrinterManager.getInstance().getPrinter().printBarcode(-1, etBarcode.getText().toString(), false);
} catch (Exception ex) {
ex.printStackTrace();
tvResult.setText(ex.getMessage());
}
}
break;
case R.id.btnQrcode:
if (PrinterManager.getInstance().isConnected()) {
try {
PrinterManager.getInstance().getPrinter().printQrcode(-1, etQrcode.getText().toString(), false);
} catch (Exception ex) {
ex.printStackTrace();
tvResult.setText(ex.getMessage());
}
}
break;
case R.id.btnPrintImg:
if (PrinterManager.getInstance().isConnected()) {
Bitmap bm = BitmapFactory.decodeResource(getResources(), R.drawable.logo_demo);
try {
PrinterManager.getInstance().getPrinter().printImage(-1, bm, false);
} catch (Exception ex) {
ex.printStackTrace();
tvResult.setText(ex.getMessage());
} finally {
if (bm != null) {
bm.recycle();
}
}
}
break;
case R.id.btnStatus:
if (PrinterManager.getInstance().isConnected()) {
try {
int ret = PrinterManager.getInstance().getPrinter().getPrinterStatus();
tvResult.setText(PrinterStatusEnum.find(ret).getDesc());
} catch (Exception ex) {
ex.printStackTrace();
tvResult.setText(ex.getMessage());
}
} else {
tvResult.setText("未连接打印机");
}
break;
case R.id.btnSetAlign:
if (PrinterManager.getInstance().isConnected()) {
try {
int ret = PrinterManager.getInstance().getPrinter().setAlign(Integer.parseInt(etAlign.getText().toString().trim()));
tvResult.setText(PrinterStatusEnum.find(ret).getDesc());
} catch (Exception ex) {
ex.printStackTrace();
tvResult.setText(ex.getMessage());
}
} else {
tvResult.setText("未连接打印机");
}
break;
case R.id.ivBack:
finish();
break;
......
......@@ -75,6 +75,14 @@
<TableRow>
<EditText
android:id="@+id/etTextSize"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="只能输入数字"
android:inputType="number" />
<Button
android:id="@+id/btnSetTextSize"
android:layout_width="wrap_content"
......@@ -83,6 +91,44 @@
android:text="设置文字大小" />
</TableRow>
<TableRow>
<EditText
android:id="@+id/etAlign"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="只能输入数字0、1、2"
android:inputType="number" />
<Button
android:id="@+id/btnSetAlign"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="设置对齐方式" />
</TableRow>
<TableRow>
<ImageView
android:id="@+id/ivImg"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_weight="1"
android:src="@drawable/logo_demo" />
</TableRow>
<TableRow>
<Button
android:id="@+id/btnPrintImg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="打印图片" />
</TableRow>
<TableRow>
<Button
......@@ -92,5 +138,69 @@
android:layout_weight="1"
android:text="切纸" />
</TableRow>
<TableRow>
<Button
android:id="@+id/btnSet58"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="设置成58mm打印纸" />
</TableRow>
<TableRow>
<Button
android:id="@+id/btnSet80"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="设置成80mm打印纸" />
</TableRow>
<TableRow>
<EditText
android:id="@+id/etBarcode"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="输入条码内容" />
<Button
android:id="@+id/btnBarcode"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="打印一维条码" />
</TableRow>
<TableRow>
<EditText
android:id="@+id/etQrcode"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="输入条码内容" />
<Button
android:id="@+id/btnQrcode"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="打印二维码" />
</TableRow>
<TableRow>
<Button
android:id="@+id/btnStatus"
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
......@@ -11,7 +11,6 @@ import android.hardware.usb.UsbManager;
import android.text.TextUtils;
import android.util.Log;
import com.miya.print.utils.CheckStatusUtils;
import com.miya.print.utils.D;
import com.miya.print.utils.ImageUtils;
import com.printsdk.cmd.PrintCmd;
......@@ -41,6 +40,15 @@ public class ChuangjiePrinter extends BasePrinter {
*/
int textSize = -1;
/**
* 对齐方式
* 0:居左对齐
* 1:居中对齐
* 2:居右对齐
* 其它值:走当前配置对齐方式
*/
int align = 0;
@Override
public boolean init(Context context) {
this.context = context;
......@@ -61,9 +69,10 @@ public class ChuangjiePrinter extends BasePrinter {
public int printText(int align, int size, String content, boolean isFeed) {
int ret;
if (textSize != size) {
textSize = size;
ret = setSizeText(size, size);
}
ret = setAlignMode(align);
ret = setAlign(align);
ret = printString(content);
if (isFeed) {
ret = LF();
......@@ -92,6 +101,14 @@ public class ChuangjiePrinter extends BasePrinter {
return true;
}
@Override
public int getPrinterStatus() {
if (mUsbDev != null) {
status = getPrinterStatus(mUsbDev);
}
return status;
}
/**
* 0 打印机正常
* 1 打印机未连接或未上电
......@@ -241,6 +258,51 @@ public class ChuangjiePrinter extends BasePrinter {
}
}
@Override
public int printImage(int align, Bitmap bm, boolean isFeed) {
int ret = setAlign(align);
ret = printQrcodeBitmap(bm);
if (isFeed) {
feedPaper();
}
return ret;
}
@Override
public int printQrcode(int align, String content, boolean isFeed) {
int ret = setAlign(align);
ret = printQrcode(content, 0, 6, 1);
if (isFeed) {
ret = feedPaper();
}
return ret;
}
@Override
public int printBarcode(int align, String content, boolean isFeed) {
int ret = setAlign(align);
ret = printBarCode(content);
if (isFeed) {
ret = feedPaper();
}
return ret;
}
@Override
public int feedPaper() {
return LF();
}
@Override
public int setAlign(int align) {
int ret = PrinterStatusEnum.CODE_SUCCESS.getStatus();
if (this.align != align &&
(align == 0 || align == 1 || align == 2)) {
ret = setAlignMode(align);
this.align = align;
}
return ret;
}
public int printBarCode(String barcode) {
int printState = -1;
......@@ -259,7 +321,15 @@ public class ChuangjiePrinter extends BasePrinter {
return printState;
}
/**
* 打印二维码
*
* @param StrData 内容
* @param iLmargin 左边距,取值0-27 单位mm
* @param iMside 单位长度,即QR码大小,取值1-8,(有些打印机型只支持1-4)
* @param iRound 环绕模式,0环绕(混排,有些机型不支持)、1立即打印(不混排)
* @return
*/
public int printQrcode(String StrData, int iLmargin, int iMside, int iRound) {
int printState = -1;
if (null != mUsbDriver && null != mUsbDev) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment