Commit 5068f6a5 authored by pengguangpu's avatar pengguangpu

丰富桑达打印驱动方法

parent c792f59f
......@@ -67,6 +67,11 @@ public class PrintActivity extends Activity implements View.OnClickListener {
EditText etAlign;
Button btnSetAlign;
/**
* 走纸
*/
Button btnFeedPaper;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
......@@ -90,6 +95,7 @@ public class PrintActivity extends Activity implements View.OnClickListener {
btnStatus = findViewById(R.id.btnStatus);
btnSetAlign = findViewById(R.id.btnSetAlign);
etAlign = findViewById(R.id.etAlign);
btnFeedPaper = findViewById(R.id.btnFeedPaper);
//set click listener
btnInit.setOnClickListener(this);
......@@ -104,6 +110,7 @@ public class PrintActivity extends Activity implements View.OnClickListener {
btnPrintImg.setOnClickListener(this);
btnStatus.setOnClickListener(this);
btnSetAlign.setOnClickListener(this);
btnFeedPaper.setOnClickListener(this);
}
@Override
......@@ -173,7 +180,8 @@ public class PrintActivity extends Activity implements View.OnClickListener {
break;
case R.id.btnPrintImg:
if (PrinterManager.getInstance().isConnected()) {
Bitmap bm = BitmapUtils.scaleBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.logo_demo), 240, 0);
// Bitmap bm = BitmapUtils.scaleBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.logo_demo), 240, 0);
Bitmap bm = BitmapFactory.decodeResource(getResources(), R.drawable.logo_demo);
try {
PrinterManager.getInstance().getPrinter().printImage(-1, bm, false);
} catch (Exception ex) {
......@@ -212,6 +220,17 @@ public class PrintActivity extends Activity implements View.OnClickListener {
tvResult.setText("未连接打印机");
}
break;
case R.id.btnFeedPaper:
if (PrinterManager.getInstance().isConnected()) {
try {
int ret = PrinterManager.getInstance().getPrinter().feedPaper();
tvResult.setText(PrinterStatusEnum.find(ret).getDesc());
} catch (Exception ex) {
ex.printStackTrace();
tvResult.setText(ex.getMessage());
}
}
break;
case R.id.ivBack:
finish();
break;
......
......@@ -129,6 +129,16 @@
android:text="打印图片" />
</TableRow>
<TableRow>
<Button
android:id="@+id/btnFeedPaper"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="走纸" />
</TableRow>
<TableRow>
<Button
......
......@@ -22,6 +22,12 @@ android {
}
}
sourceSets {
main {
jniLibs.srcDirs = ['libs']
}
}
}
dependencies {
......
......@@ -285,19 +285,28 @@ public class ChuangjiePrinter extends BasePrinter {
return ret;
}
/**
* 打印二维码
* 注:由于机器限制,此方法无法居右打印二维码,当选择居右打印时,仍会居左打印二维码
*
* @param align
* @param content
* @param isFeed
* @return
*/
@Override
public int printQrcode(int align, String content, boolean isFeed) {
int ret = setAlign(align);
//创捷机器打印二维码不能通过setAlign实现居中,永远会居左打印
if (this.align == 0) {
//居左打印
ret = printQrcode(content, 0, 6, 1);
ret = printQrcode(content, 0, 8, 1);
} else if (this.align == 1) {
//居中打印
ret = printQrcode(content, 12, 6, 1);
ret = printQrcode(content, 24, 8, 1);
} else if (this.align == 2) {
//居右
ret = printQrcode(content, 24, 6, 1);
ret = printQrcode(content, 24, 8, 1);
}
if (isFeed) {
ret = feedPaper();
......@@ -308,7 +317,7 @@ public class ChuangjiePrinter extends BasePrinter {
@Override
public int printBarcode(int align, String content, boolean isFeed) {
int ret = setAlign(align);
//创捷机器打印条码只能居中打印,并且内部会强制走纸
//创捷机器打印条码只能居左、居中打印,并且内部会强制走纸
ret = printBarCode(content);
if (isFeed) {
ret = feedPaper();
......
......@@ -19,6 +19,7 @@ public class PrinterManager {
// TYPE_HISENSE("hisense", ChuangjiePrinter.class.getName()),
// TYPE_YINGTAI("yingtai", ChuangjiePrinter.class.getName()),
// TYPE_SUNMI("shangmi", ChuangjiePrinter.class.getName()),
TYPE_SANGDA("sangda", SangdaPrinter.class.getName()),
TYPE_CHUANGJIE("chuangjie", ChuangjiePrinter.class.getName());
/**
......
......@@ -12,6 +12,11 @@ public enum PrinterStatusEnum {
*/
CODE_SUCCESS(0, "操作成功"),
/**
* 未知状态
*/
CODE_UNKOWN(-2, "未知状态"),
/**
* 操作失败
*/
......
package com.miya.print;
import android.content.Context;
import android.graphics.Bitmap;
import com.miya.print.exception.PrinterException;
import com.miya.print.utils.BitmapUtils;
import com.miya.print.utils.D;
import com.miya.print.utils.PrintFormatUtils;
import com.szsicod.print.escpos.PrinterAPI;
import com.szsicod.print.io.InterfaceAPI;
import com.szsicod.print.io.SerialAPI;
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Arrays;
......@@ -22,6 +26,11 @@ public class SangdaPrinter extends BasePrinter {
public PrinterAPI mPrinter = PrinterAPI.getInstance();
InterfaceAPI io = null;
/**
* 80mm打印纸允许打印的最大图片的宽度,此为经验值
*/
final static int MAX_IMG_WIDTH_80MM = 570;
@Override
public boolean init(Context context) {
super.init(context);
......@@ -60,8 +69,9 @@ public class SangdaPrinter extends BasePrinter {
e.printStackTrace();
throw new PrinterException(PrinterStatusEnum.CODE_FAILED.getStatus(), e.getMessage());
}
} else {
throw new PrinterException(PrinterStatusEnum.CODE_FAILED.getStatus(), PrinterStatusEnum.CODE_FAILED.getDesc());
}
throw new PrinterException(PrinterStatusEnum.CODE_FAILED.getStatus(), PrinterStatusEnum.CODE_FAILED.getDesc());
}
@Override
......@@ -69,27 +79,73 @@ public class SangdaPrinter extends BasePrinter {
if (mPrinter != null) {
int ret = setAlign(align);
try {
ret = mPrinter.printBarCode(0, content.length(), content);
if (isFeed) {
ret = feedPaper();
}
//通过printString传入打印指令实现条码的打印
ret = mPrinter.printString(PrintFormatUtils.getBarcodeCmd(content), "GBK", isFeed);
// ret = mPrinter.printBarCode(73, content.length(), content);
return ret;
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
throw new PrinterException(PrinterStatusEnum.CODE_FAILED.getStatus(), e.getMessage());
}
} else {
throw new PrinterException(PrinterStatusEnum.CODE_FAILED.getStatus(), PrinterStatusEnum.CODE_FAILED.getDesc());
}
throw new PrinterException(PrinterStatusEnum.CODE_FAILED.getStatus(), PrinterStatusEnum.CODE_FAILED.getDesc());
}
@Override
public int setAlign(int align) {
public int printQrcode(int align, String content, boolean isFeed) {
if (mPrinter != null) {
int ret = mPrinter.setAlignMode(align);
this.align = align;
int ret = setAlign(align);
ret = mPrinter.printQRCode(content, 4, false);
if (isFeed) {
ret = feedPaper();
}
return ret;
} else {
throw new PrinterException(PrinterStatusEnum.CODE_FAILED.getStatus(), PrinterStatusEnum.CODE_FAILED.getDesc());
}
}
@Override
public int printImage(int align, Bitmap bm, boolean isFeed) {
if (mPrinter != null && bm != null) {
int ret = setAlign(align);
try {
Bitmap actualBm = BitmapUtils.scaleBitmap(bm, MAX_IMG_WIDTH_80MM, 0);
if (bm.getWidth() > MAX_IMG_WIDTH_80MM) {
//如果超过阈值,则缩放至目标大小
ret = mPrinter.printRasterBitmap(actualBm);
} else {
ret = mPrinter.printRasterBitmap(bm);
}
if (actualBm != null) {
actualBm.recycle();
}
} 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());
}
}
@Override
public int setAlign(int align) {
if (mPrinter != null) {
if (this.align != align &&
(align == 0 || align == 1 || align == 2)) {
int ret = mPrinter.setAlignMode(align);
this.align = align;
}
return PrinterStatusEnum.CODE_SUCCESS.getStatus();
} else {
throw new PrinterException(PrinterStatusEnum.CODE_FAILED.getStatus(), PrinterStatusEnum.CODE_FAILED.getDesc());
}
throw new PrinterException(PrinterStatusEnum.CODE_FAILED.getStatus(), PrinterStatusEnum.CODE_FAILED.getDesc());
}
@Override
......@@ -97,8 +153,42 @@ public class SangdaPrinter extends BasePrinter {
if (mPrinter != null) {
int ret = mPrinter.printFeed();
return ret;
} else {
throw new PrinterException(PrinterStatusEnum.CODE_FAILED.getStatus(), PrinterStatusEnum.CODE_FAILED.getDesc());
}
}
@Override
public int cutPaper() {
if (mPrinter != null) {
int ret = mPrinter.cutPaper(66, 0);
return ret;
} else {
throw new PrinterException(PrinterStatusEnum.CODE_FAILED.getStatus(), PrinterStatusEnum.CODE_FAILED.getDesc());
}
}
@Override
public int getPrinterStatus() {
if (mPrinter != null) {
//这个方法参数中的n只能取1或49
int ret = mPrinter.getPrinterStatus(1);
//做一个状态对应转换,转换为自定义标准的状态码类型
switch (ret) {
case 0:
//正常状态
return 0;
case 12:
//缺纸
return PrinterStatusEnum.CODE_NO_PAPER.status;
case 3:
//纸将尽
return PrinterStatusEnum.CODE_FEW_PAPER.status;
}
return PrinterStatusEnum.CODE_UNKOWN.status;
} else {
throw new PrinterException(PrinterStatusEnum.CODE_FAILED.getStatus(), PrinterStatusEnum.CODE_FAILED.getDesc());
}
throw new PrinterException(PrinterStatusEnum.CODE_FAILED.getStatus(), PrinterStatusEnum.CODE_FAILED.getDesc());
}
public PrinterAPI getPrinter() {
......
......@@ -41,10 +41,10 @@ public class BitmapUtils {
}
matrix.preScale(scaleX, scaleY);
Bitmap newBM = Bitmap.createBitmap(origin, 0, 0, widthOrigin, heightOrigin, matrix, false);
if (newBM.equals(origin)) {
return newBM;
}
origin.recycle();
// if (newBM.equals(origin)) {
// return newBM;
// }
// origin.recycle();
return newBM;
}
......
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