Commit ed9f6ba9 authored by pengguangpu's avatar pengguangpu

桑达打印驱动尘埃落定

parent 5068f6a5
...@@ -3,6 +3,9 @@ ...@@ -3,6 +3,9 @@
<component name="GradleSettings"> <component name="GradleSettings">
<option name="linkedExternalProjectsSettings"> <option name="linkedExternalProjectsSettings">
<GradleProjectSettings> <GradleProjectSettings>
<compositeConfiguration>
<compositeBuild compositeDefinitionSource="SCRIPT" />
</compositeConfiguration>
<option name="distributionType" value="DEFAULT_WRAPPED" /> <option name="distributionType" value="DEFAULT_WRAPPED" />
<option name="externalProjectPath" value="$PROJECT_DIR$" /> <option name="externalProjectPath" value="$PROJECT_DIR$" />
<option name="modules"> <option name="modules">
......
...@@ -5,7 +5,7 @@ ...@@ -5,7 +5,7 @@
<option name="myDefaultNotNull" value="android.support.annotation.NonNull" /> <option name="myDefaultNotNull" value="android.support.annotation.NonNull" />
<option name="myNullables"> <option name="myNullables">
<value> <value>
<list size="7"> <list size="10">
<item index="0" class="java.lang.String" itemvalue="org.jetbrains.annotations.Nullable" /> <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="1" class="java.lang.String" itemvalue="javax.annotation.Nullable" />
<item index="2" class="java.lang.String" itemvalue="javax.annotation.CheckForNull" /> <item index="2" class="java.lang.String" itemvalue="javax.annotation.CheckForNull" />
...@@ -13,18 +13,24 @@ ...@@ -13,18 +13,24 @@
<item index="4" class="java.lang.String" itemvalue="android.support.annotation.Nullable" /> <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="5" class="java.lang.String" itemvalue="androidx.annotation.Nullable" />
<item index="6" class="java.lang.String" itemvalue="androidx.annotation.RecentlyNullable" /> <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> </list>
</value> </value>
</option> </option>
<option name="myNotNulls"> <option name="myNotNulls">
<value> <value>
<list size="6"> <list size="9">
<item index="0" class="java.lang.String" itemvalue="org.jetbrains.annotations.NotNull" /> <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="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="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="3" class="java.lang.String" itemvalue="android.support.annotation.NonNull" />
<item index="4" class="java.lang.String" itemvalue="androidx.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="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> </list>
</value> </value>
</option> </option>
......
...@@ -4,6 +4,7 @@ import android.app.Activity; ...@@ -4,6 +4,7 @@ import android.app.Activity;
import android.graphics.Bitmap; import android.graphics.Bitmap;
import android.graphics.BitmapFactory; import android.graphics.BitmapFactory;
import android.os.Bundle; import android.os.Bundle;
import android.print.PrintManager;
import android.support.annotation.Nullable; import android.support.annotation.Nullable;
import android.view.View; import android.view.View;
import android.widget.Button; import android.widget.Button;
...@@ -11,9 +12,11 @@ import android.widget.EditText; ...@@ -11,9 +12,11 @@ import android.widget.EditText;
import android.widget.ImageView; import android.widget.ImageView;
import android.widget.TextView; import android.widget.TextView;
import com.miya.print.IPrinter;
import com.miya.print.PrinterManager; import com.miya.print.PrinterManager;
import com.miya.print.PrinterStatusEnum; import com.miya.print.PrinterStatusEnum;
import com.miya.print.utils.BitmapUtils; import com.miya.print.utils.BitmapUtils;
import com.miya.print.utils.Page88MmPrintUtils;
public class PrintActivity extends Activity implements View.OnClickListener { public class PrintActivity extends Activity implements View.OnClickListener {
...@@ -72,6 +75,11 @@ public class PrintActivity extends Activity implements View.OnClickListener { ...@@ -72,6 +75,11 @@ public class PrintActivity extends Activity implements View.OnClickListener {
*/ */
Button btnFeedPaper; Button btnFeedPaper;
/**
* 打印测试订单
*/
Button btnPrintTest;
@Override @Override
protected void onCreate(@Nullable Bundle savedInstanceState) { protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
...@@ -96,6 +104,7 @@ public class PrintActivity extends Activity implements View.OnClickListener { ...@@ -96,6 +104,7 @@ public class PrintActivity extends Activity implements View.OnClickListener {
btnSetAlign = findViewById(R.id.btnSetAlign); btnSetAlign = findViewById(R.id.btnSetAlign);
etAlign = findViewById(R.id.etAlign); etAlign = findViewById(R.id.etAlign);
btnFeedPaper = findViewById(R.id.btnFeedPaper); btnFeedPaper = findViewById(R.id.btnFeedPaper);
btnPrintTest = findViewById(R.id.btnPrintTest);
//set click listener //set click listener
btnInit.setOnClickListener(this); btnInit.setOnClickListener(this);
...@@ -111,6 +120,7 @@ public class PrintActivity extends Activity implements View.OnClickListener { ...@@ -111,6 +120,7 @@ public class PrintActivity extends Activity implements View.OnClickListener {
btnStatus.setOnClickListener(this); btnStatus.setOnClickListener(this);
btnSetAlign.setOnClickListener(this); btnSetAlign.setOnClickListener(this);
btnFeedPaper.setOnClickListener(this); btnFeedPaper.setOnClickListener(this);
btnPrintTest.setOnClickListener(this);
} }
@Override @Override
...@@ -231,6 +241,69 @@ public class PrintActivity extends Activity implements View.OnClickListener { ...@@ -231,6 +241,69 @@ public class PrintActivity extends Activity implements View.OnClickListener {
} }
} }
break; 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: case R.id.ivBack:
finish(); finish();
break; break;
......
...@@ -212,5 +212,15 @@ ...@@ -212,5 +212,15 @@
android:layout_weight="1" android:layout_weight="1"
android:text="获取当前打印机状态" /> android:text="获取当前打印机状态" />
</TableRow> </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> </TableLayout>
</LinearLayout> </LinearLayout>
\ No newline at end of file
...@@ -11,6 +11,21 @@ import android.graphics.Bitmap; ...@@ -11,6 +11,21 @@ import android.graphics.Bitmap;
*/ */
public interface IPrinter { public interface IPrinter {
/**
* 居左对齐方式
*/
int ALIGN_LEFT = 0;
/**
* 居中对齐方式
*/
int ALIGN_CENTER = 1;
/**
* 居右对齐方式
*/
int ALIGN_RIGHT = 2;
/** /**
* 初始化驱动 * 初始化驱动
* *
......
...@@ -76,18 +76,10 @@ public enum PrinterStatusEnum { ...@@ -76,18 +76,10 @@ public enum PrinterStatusEnum {
return status; return status;
} }
public void setStatus(int status) {
this.status = status;
}
public String getDesc() { public String getDesc() {
return desc; return desc;
} }
public void setDesc(String desc) {
this.desc = desc;
}
PrinterStatusEnum(int status, String desc) { PrinterStatusEnum(int status, String desc) {
this.status = status; this.status = status;
this.desc = desc; this.desc = desc;
......
...@@ -2,6 +2,7 @@ package com.miya.print; ...@@ -2,6 +2,7 @@ package com.miya.print;
import android.content.Context; import android.content.Context;
import android.graphics.Bitmap; import android.graphics.Bitmap;
import android.util.Printer;
import com.miya.print.exception.PrinterException; import com.miya.print.exception.PrinterException;
import com.miya.print.utils.BitmapUtils; import com.miya.print.utils.BitmapUtils;
...@@ -15,6 +16,7 @@ import java.io.File; ...@@ -15,6 +16,7 @@ import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.io.UnsupportedEncodingException; import java.io.UnsupportedEncodingException;
import java.util.Arrays; import java.util.Arrays;
import java.util.List;
/** /**
* 桑达机器打印驱动 * 桑达机器打印驱动
...@@ -96,11 +98,11 @@ public class SangdaPrinter extends BasePrinter { ...@@ -96,11 +98,11 @@ public class SangdaPrinter extends BasePrinter {
public int printQrcode(int align, String content, boolean isFeed) { public int printQrcode(int align, String content, boolean isFeed) {
if (mPrinter != null) { if (mPrinter != null) {
int ret = setAlign(align); int ret = setAlign(align);
ret = mPrinter.printQRCode(content, 4, false); ret = mPrinter.printQRCode(content, 6, false);
if (isFeed) { if (isFeed) {
ret = feedPaper(); ret = feedPaper();
} }
return ret; return changeStatus2Custom(ret);
} else { } else {
throw new PrinterException(PrinterStatusEnum.CODE_FAILED.getStatus(), PrinterStatusEnum.CODE_FAILED.getDesc()); throw new PrinterException(PrinterStatusEnum.CODE_FAILED.getStatus(), PrinterStatusEnum.CODE_FAILED.getDesc());
} }
...@@ -121,13 +123,12 @@ public class SangdaPrinter extends BasePrinter { ...@@ -121,13 +123,12 @@ public class SangdaPrinter extends BasePrinter {
if (actualBm != null) { if (actualBm != null) {
actualBm.recycle(); actualBm.recycle();
} }
changeStatus2Custom(ret);
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
throw new PrinterException(PrinterStatusEnum.CODE_FAILED.getStatus(), e.getMessage()); throw new PrinterException(PrinterStatusEnum.CODE_FAILED.getStatus(), e.getMessage());
} }
if (isFeed) { //桑达打印图片时,本身就会强制走纸一行,这里不再判断是否再走纸
ret = feedPaper();
}
return ret; return ret;
} else { } else {
throw new PrinterException(PrinterStatusEnum.CODE_FAILED.getStatus(), PrinterStatusEnum.CODE_FAILED.getDesc()); throw new PrinterException(PrinterStatusEnum.CODE_FAILED.getStatus(), PrinterStatusEnum.CODE_FAILED.getDesc());
...@@ -140,6 +141,7 @@ public class SangdaPrinter extends BasePrinter { ...@@ -140,6 +141,7 @@ public class SangdaPrinter extends BasePrinter {
if (this.align != align && if (this.align != align &&
(align == 0 || align == 1 || align == 2)) { (align == 0 || align == 1 || align == 2)) {
int ret = mPrinter.setAlignMode(align); int ret = mPrinter.setAlignMode(align);
changeStatus2Custom(ret);
this.align = align; this.align = align;
} }
return PrinterStatusEnum.CODE_SUCCESS.getStatus(); return PrinterStatusEnum.CODE_SUCCESS.getStatus();
...@@ -152,7 +154,7 @@ public class SangdaPrinter extends BasePrinter { ...@@ -152,7 +154,7 @@ public class SangdaPrinter extends BasePrinter {
public int feedPaper() { public int feedPaper() {
if (mPrinter != null) { if (mPrinter != null) {
int ret = mPrinter.printFeed(); int ret = mPrinter.printFeed();
return ret; return changeStatus2Custom(ret);
} else { } else {
throw new PrinterException(PrinterStatusEnum.CODE_FAILED.getStatus(), PrinterStatusEnum.CODE_FAILED.getDesc()); throw new PrinterException(PrinterStatusEnum.CODE_FAILED.getStatus(), PrinterStatusEnum.CODE_FAILED.getDesc());
} }
...@@ -177,7 +179,7 @@ public class SangdaPrinter extends BasePrinter { ...@@ -177,7 +179,7 @@ public class SangdaPrinter extends BasePrinter {
switch (ret) { switch (ret) {
case 0: case 0:
//正常状态 //正常状态
return 0; return PrinterStatusEnum.CODE_SUCCESS.status;
case 12: case 12:
//缺纸 //缺纸
return PrinterStatusEnum.CODE_NO_PAPER.status; return PrinterStatusEnum.CODE_NO_PAPER.status;
...@@ -211,6 +213,52 @@ public class SangdaPrinter extends BasePrinter { ...@@ -211,6 +213,52 @@ public class SangdaPrinter extends BasePrinter {
return false; 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() { public String getStatusDescribe() {
int status = mPrinter.getStatus(); int status = mPrinter.getStatus();
Arrays.fill(statusList, 0); 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