Commit ed9f6ba9 authored by pengguangpu's avatar pengguangpu

桑达打印驱动尘埃落定

parent 5068f6a5
......@@ -3,6 +3,9 @@
<component name="GradleSettings">
<option name="linkedExternalProjectsSettings">
<GradleProjectSettings>
<compositeConfiguration>
<compositeBuild compositeDefinitionSource="SCRIPT" />
</compositeConfiguration>
<option name="distributionType" value="DEFAULT_WRAPPED" />
<option name="externalProjectPath" value="$PROJECT_DIR$" />
<option name="modules">
......
......@@ -5,7 +5,7 @@
<option name="myDefaultNotNull" value="android.support.annotation.NonNull" />
<option name="myNullables">
<value>
<list size="7">
<list size="10">
<item index="0" class="java.lang.String" itemvalue="org.jetbrains.annotations.Nullable" />
<item index="1" class="java.lang.String" itemvalue="javax.annotation.Nullable" />
<item index="2" class="java.lang.String" itemvalue="javax.annotation.CheckForNull" />
......@@ -13,18 +13,24 @@
<item index="4" class="java.lang.String" itemvalue="android.support.annotation.Nullable" />
<item index="5" class="java.lang.String" itemvalue="androidx.annotation.Nullable" />
<item index="6" class="java.lang.String" itemvalue="androidx.annotation.RecentlyNullable" />
<item index="7" class="java.lang.String" itemvalue="org.checkerframework.checker.nullness.qual.Nullable" />
<item index="8" class="java.lang.String" itemvalue="org.checkerframework.checker.nullness.compatqual.NullableDecl" />
<item index="9" class="java.lang.String" itemvalue="org.checkerframework.checker.nullness.compatqual.NullableType" />
</list>
</value>
</option>
<option name="myNotNulls">
<value>
<list size="6">
<list size="9">
<item index="0" class="java.lang.String" itemvalue="org.jetbrains.annotations.NotNull" />
<item index="1" class="java.lang.String" itemvalue="javax.annotation.Nonnull" />
<item index="2" class="java.lang.String" itemvalue="edu.umd.cs.findbugs.annotations.NonNull" />
<item index="3" class="java.lang.String" itemvalue="android.support.annotation.NonNull" />
<item index="4" class="java.lang.String" itemvalue="androidx.annotation.NonNull" />
<item index="5" class="java.lang.String" itemvalue="androidx.annotation.RecentlyNonNull" />
<item index="6" class="java.lang.String" itemvalue="org.checkerframework.checker.nullness.qual.NonNull" />
<item index="7" class="java.lang.String" itemvalue="org.checkerframework.checker.nullness.compatqual.NonNullDecl" />
<item index="8" class="java.lang.String" itemvalue="org.checkerframework.checker.nullness.compatqual.NonNullType" />
</list>
</value>
</option>
......
......@@ -4,6 +4,7 @@ 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;
......@@ -11,9 +12,11 @@ import android.widget.EditText;
import android.widget.ImageView;
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;
public class PrintActivity extends Activity implements View.OnClickListener {
......@@ -72,6 +75,11 @@ public class PrintActivity extends Activity implements View.OnClickListener {
*/
Button btnFeedPaper;
/**
* 打印测试订单
*/
Button btnPrintTest;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
......@@ -96,6 +104,7 @@ public class PrintActivity extends Activity implements View.OnClickListener {
btnSetAlign = findViewById(R.id.btnSetAlign);
etAlign = findViewById(R.id.etAlign);
btnFeedPaper = findViewById(R.id.btnFeedPaper);
btnPrintTest = findViewById(R.id.btnPrintTest);
//set click listener
btnInit.setOnClickListener(this);
......@@ -111,6 +120,7 @@ public class PrintActivity extends Activity implements View.OnClickListener {
btnStatus.setOnClickListener(this);
btnSetAlign.setOnClickListener(this);
btnFeedPaper.setOnClickListener(this);
btnPrintTest.setOnClickListener(this);
}
@Override
......@@ -231,6 +241,69 @@ public class PrintActivity extends Activity implements View.OnClickListener {
}
}
break;
case R.id.btnPrintTest:
if (PrinterManager.getInstance().isConnected()) {
Bitmap bm = BitmapFactory.decodeResource(getResources(), R.drawable.logo_demo);
try {
//居中打印商户名称
IPrinter printer = PrinterManager.getInstance().getPrinter();
int ret = printer.beginPrint();
ret = printer.setAlign(IPrinter.ALIGN_CENTER);
//打印logo
ret = printer.printImage(-1, bm, true);
ret = printer.printText(-1, 0, "中百", true);
ret = printer.printText(-1, 0, "支付宝自助收银", true);
//分割线
ret = printer.printText(-1, 0, Page88MmPrintUtils.printDivideLineString(), true);
//居左打印
ret = printer.setAlign(IPrinter.ALIGN_LEFT);
//订单号
ret = printer.printText(-1, 0, "订单号:0759201123908129487982739878354", true);
//流水号
ret = printer.printText(-1, 0, "流水号:2019011002200012312377857948982", true);
//小票号
ret = printer.printText(-1, 0, "小票号:1128-2876-532055", true);
//打印时间
ret = printer.printText(-1, 0, "打印时间:2019-01-10 15:16:27", true);
//分割线
ret = printer.printText(-1, 0, Page88MmPrintUtils.printDivideLineString(), true);
//四条目打印
ret = printer.printText(-1, 0, Page88MmPrintUtils.printFourData("商品名称", "编码", "数量", "金额"), true);
//商品名称
ret = printer.printText(-1, 0, "中号购物袋520**(300+80)", true);
//三条目打印
ret = printer.printText(-1, 0, Page88MmPrintUtils.printFourData("6924160298876", " ", "*1", "0.20元"), true);
ret = printer.feedPaper();
//分割线
ret = printer.printText(-1, 0, Page88MmPrintUtils.printDivideLineString(), true);
//总件数
ret = printer.printText(-1, 0, Page88MmPrintUtils.printTwoData("总件数:", "1"), true);
//商品合计
ret = printer.printText(-1, 0, Page88MmPrintUtils.printTwoData("商品合计:", "0.20"), true);
//支付宝实付
ret = printer.printText(-1, 0, Page88MmPrintUtils.printTwoData("支付宝", "0.01"), true);
//分割线
ret = printer.printText(-1, 0, Page88MmPrintUtils.printDivideLineString(), true);
ret = printer.setAlign(IPrinter.ALIGN_CENTER);
//小票尾部
ret = printer.printText(-1, 0, "欢迎下次光临!!!", true);
//小票尾部条码
ret = printer.printBarcode(-1, "0759201123908129487982739878354", true);
//小票尾部二维码
ret = printer.printQrcode(-1, "2019011002200012312377857948982", false);
ret = printer.endPrint();
//剪纸
ret = printer.cutPaper();
} catch (Exception ex) {
ex.printStackTrace();
tvResult.setText(ex.getMessage());
} finally {
if (bm != null) {
bm.recycle();
}
}
}
break;
case R.id.ivBack:
finish();
break;
......
......@@ -212,5 +212,15 @@
android:layout_weight="1"
android:text="获取当前打印机状态" />
</TableRow>
<TableRow>
<Button
android:id="@+id/btnPrintTest"
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,6 +11,21 @@ import android.graphics.Bitmap;
*/
public interface IPrinter {
/**
* 居左对齐方式
*/
int ALIGN_LEFT = 0;
/**
* 居中对齐方式
*/
int ALIGN_CENTER = 1;
/**
* 居右对齐方式
*/
int ALIGN_RIGHT = 2;
/**
* 初始化驱动
*
......
......@@ -76,18 +76,10 @@ public enum PrinterStatusEnum {
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;
......
......@@ -2,6 +2,7 @@ package com.miya.print;
import android.content.Context;
import android.graphics.Bitmap;
import android.util.Printer;
import com.miya.print.exception.PrinterException;
import com.miya.print.utils.BitmapUtils;
......@@ -15,6 +16,7 @@ import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Arrays;
import java.util.List;
/**
* 桑达机器打印驱动
......@@ -96,11 +98,11 @@ public class SangdaPrinter extends BasePrinter {
public int printQrcode(int align, String content, boolean isFeed) {
if (mPrinter != null) {
int ret = setAlign(align);
ret = mPrinter.printQRCode(content, 4, false);
ret = mPrinter.printQRCode(content, 6, false);
if (isFeed) {
ret = feedPaper();
}
return ret;
return changeStatus2Custom(ret);
} else {
throw new PrinterException(PrinterStatusEnum.CODE_FAILED.getStatus(), PrinterStatusEnum.CODE_FAILED.getDesc());
}
......@@ -121,13 +123,12 @@ public class SangdaPrinter extends BasePrinter {
if (actualBm != null) {
actualBm.recycle();
}
changeStatus2Custom(ret);
} catch (IOException e) {
e.printStackTrace();
throw new PrinterException(PrinterStatusEnum.CODE_FAILED.getStatus(), e.getMessage());
}
if (isFeed) {
ret = feedPaper();
}
//桑达打印图片时,本身就会强制走纸一行,这里不再判断是否再走纸
return ret;
} else {
throw new PrinterException(PrinterStatusEnum.CODE_FAILED.getStatus(), PrinterStatusEnum.CODE_FAILED.getDesc());
......@@ -140,6 +141,7 @@ public class SangdaPrinter extends BasePrinter {
if (this.align != align &&
(align == 0 || align == 1 || align == 2)) {
int ret = mPrinter.setAlignMode(align);
changeStatus2Custom(ret);
this.align = align;
}
return PrinterStatusEnum.CODE_SUCCESS.getStatus();
......@@ -152,7 +154,7 @@ public class SangdaPrinter extends BasePrinter {
public int feedPaper() {
if (mPrinter != null) {
int ret = mPrinter.printFeed();
return ret;
return changeStatus2Custom(ret);
} else {
throw new PrinterException(PrinterStatusEnum.CODE_FAILED.getStatus(), PrinterStatusEnum.CODE_FAILED.getDesc());
}
......@@ -177,7 +179,7 @@ public class SangdaPrinter extends BasePrinter {
switch (ret) {
case 0:
//正常状态
return 0;
return PrinterStatusEnum.CODE_SUCCESS.status;
case 12:
//缺纸
return PrinterStatusEnum.CODE_NO_PAPER.status;
......@@ -211,6 +213,52 @@ public class SangdaPrinter extends BasePrinter {
return false;
}
/**
* 将桑达的状态码翻译成PrinterStatusEnum中的标准状态码
* 注:内部会自动抛出不允许的异常状态
*
* @param status 桑达返回的状态码
* @return PrinterStatusEnum中的标准状态码
*/
private int changeStatus2Custom(int status) {
if (status == 0) {
//正常状态
return PrinterStatusEnum.CODE_SUCCESS.status;
}
if (status == -1) {
//数据传输错误,请检查连接并重新发送
throw new PrinterException(PrinterStatusEnum.CODE_FAILED.status, "数据传输错误,请检查连接并重新发送");
}
if ((status & 0x200) > 0) {
return PrinterStatusEnum.CODE_FEW_PAPER.status;
}
if ((status & 0x400) > 0 || (status & 0x08) > 0) {
//缺纸
throw new PrinterException(PrinterStatusEnum.CODE_NO_PAPER.status, PrinterStatusEnum.CODE_NO_PAPER.desc);
}
if ((status & 0x4) > 0) {
//脱机状态
throw new PrinterException(PrinterStatusEnum.CODE_NOT_CONNECTED.status, "发生错误");
}
if ((status & 0x20) > 0) {
//盖板打开
throw new PrinterException(PrinterStatusEnum.CODE_NOT_CONNECTED.getStatus(), "盖板打开");
}
if ((status & 0x1) > 0) {
//脱机
throw new PrinterException(PrinterStatusEnum.CODE_NOT_CONNECTED.status, "脱机");
}
if ((status & 0x100) > 0) {
//机械错误
throw new PrinterException(PrinterStatusEnum.CODE_FAILED.status, "机械错误");
}
if ((status & 0x80) > 0) {
//不可恢复错误
throw new PrinterException(PrinterStatusEnum.CODE_FAILED.status, "不可恢复错误");
}
return PrinterStatusEnum.CODE_SUCCESS.status;
}
public String getStatusDescribe() {
int status = mPrinter.getStatus();
Arrays.fill(statusList, 0);
......
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