Commit 5068f6a5 authored by pengguangpu's avatar pengguangpu

丰富桑达打印驱动方法

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