Commit 52a50a41 authored by pengguangpu's avatar pengguangpu

商米T1打印条码中增加位数判断,大于12位且位奇数的条码使用打印图片的方法实现

parent e8363847
...@@ -179,8 +179,7 @@ public class PrintActivity extends Activity implements View.OnClickListener { ...@@ -179,8 +179,7 @@ public class PrintActivity extends Activity implements View.OnClickListener {
case R.id.btnQrcode: case R.id.btnQrcode:
if (PrinterManager.getInstance().isConnected()) { if (PrinterManager.getInstance().isConnected()) {
try { try {
// PrinterManager.getInstance().getPrinter().printQrcode(-1, etQrcode.getText().toString(), false); PrinterManager.getInstance().getPrinter().printQrcode(-1, etQrcode.getText().toString(), false);
PrinterManager.getInstance().getPrinter().printQrcode(-1, "http://60.217.64.173:18080/Wrapper/jump.do?type=1/jump.do?type=3&orderno=511530018091900005&sjm=1455", false);
} catch (Exception ex) { } catch (Exception ex) {
ex.printStackTrace(); ex.printStackTrace();
tvResult.setText(ex.getMessage()); tvResult.setText(ex.getMessage());
......
...@@ -4,223 +4,235 @@ ...@@ -4,223 +4,235 @@
android:layout_height="match_parent" android:layout_height="match_parent"
android:orientation="vertical"> android:orientation="vertical">
<android.support.v7.widget.ActionMenuView <ScrollView
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="88dp" android:layout_height="match_parent">
android:background="@color/colorPrimary">
<LinearLayout
<ImageView android:layout_width="match_parent"
android:id="@+id/ivBack" android:layout_height="match_parent"
android:layout_width="wrap_content" android:orientation="vertical">
android:layout_height="wrap_content"
android:src="@android:drawable/ic_media_previous" /> <android.support.v7.widget.ActionMenuView
android:layout_width="match_parent"
<TextView android:layout_height="88dp"
android:layout_width="wrap_content" android:background="@color/colorPrimary">
android:layout_height="wrap_content"
android:layout_margin="16dp" <ImageView
android:gravity="right" android:id="@+id/ivBack"
android:text="打印开放组件" android:layout_width="wrap_content"
android:textColor="#ffffff" android:layout_height="wrap_content"
android:textSize="36sp" /> android:src="@android:drawable/ic_media_previous" />
</android.support.v7.widget.ActionMenuView>
<TextView
android:layout_width="wrap_content"
<TextView android:layout_height="wrap_content"
android:id="@+id/tvResult" android:layout_margin="16dp"
android:layout_width="wrap_content" android:gravity="right"
android:layout_height="wrap_content" android:text="打印开放组件"
android:layout_gravity="center_horizontal" android:textColor="#ffffff"
android:text="显示操作结果" android:textSize="36sp" />
android:textSize="36sp" /> </android.support.v7.widget.ActionMenuView>
<TableLayout
android:id="@+id/layoutOp" <TextView
android:layout_width="match_parent" android:id="@+id/tvResult"
android:layout_height="wrap_content" android:layout_width="wrap_content"
android:clipChildren="false" android:layout_height="wrap_content"
android:padding="16dp"> android:layout_gravity="center_horizontal"
android:text="显示操作结果"
<TableRow> android:textSize="36sp" />
<Button <TableLayout
android:id="@+id/btnInit" android:id="@+id/layoutOp"
android:layout_width="wrap_content" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_weight="1" android:clipChildren="false"
android:text="初始化" /> android:padding="16dp">
<Button <TableRow>
android:id="@+id/btnRelease"
android:layout_width="wrap_content" <Button
android:layout_height="wrap_content" android:id="@+id/btnInit"
android:layout_weight="1" android:layout_width="wrap_content"
android:text="销毁资源" /> android:layout_height="wrap_content"
</TableRow> android:layout_weight="1"
android:text="初始化" />
<TableRow>
<Button
<EditText android:id="@+id/btnRelease"
android:id="@+id/etText" android:layout_width="wrap_content"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_height="wrap_content" android:layout_weight="1"
android:layout_weight="1" /> android:text="销毁资源" />
</TableRow>
<Button
android:id="@+id/btnPrintText" <TableRow>
android:layout_width="wrap_content"
android:layout_height="wrap_content" <EditText
android:text="打印默认字符" /> android:id="@+id/etText"
</TableRow> android:layout_width="wrap_content"
android:layout_height="wrap_content"
<TableRow android:visibility="gone"> android:layout_weight="1" />
<EditText <Button
android:id="@+id/etTextSize" android:id="@+id/btnPrintText"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_weight="1" android:text="打印默认字符" />
android:hint="只能输入数字" </TableRow>
android:inputType="number" />
<TableRow android:visibility="gone">
<Button
android:id="@+id/btnSetTextSize" <EditText
android:layout_width="wrap_content" android:id="@+id/etTextSize"
android:layout_height="wrap_content" android:layout_width="wrap_content"
android:layout_weight="1" android:layout_height="wrap_content"
android:text="设置文字大小" /> android:layout_weight="1"
</TableRow> android:hint="只能输入数字"
android:inputType="number" />
<TableRow>
<Button
<EditText android:id="@+id/btnSetTextSize"
android:id="@+id/etAlign" android:layout_width="wrap_content"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_height="wrap_content" android:layout_weight="1"
android:layout_weight="1" android:text="设置文字大小" />
android:hint="只能输入数字0、1、2" </TableRow>
android:inputType="number" />
<TableRow>
<Button
android:id="@+id/btnSetAlign" <EditText
android:layout_width="wrap_content" android:id="@+id/etAlign"
android:layout_height="wrap_content" android:layout_width="wrap_content"
android:layout_weight="1" android:layout_height="wrap_content"
android:text="设置对齐方式" /> android:layout_weight="1"
</TableRow> android:hint="只能输入数字0、1、2"
android:inputType="number" />
<TableRow>
<Button
<ImageView android:id="@+id/btnSetAlign"
android:id="@+id/ivImg" android:layout_width="wrap_content"
android:layout_width="100dp" android:layout_height="wrap_content"
android:layout_height="100dp" android:layout_weight="1"
android:layout_weight="1" android:text="设置对齐方式" />
android:src="@drawable/logo_demo" /> </TableRow>
</TableRow>
<TableRow>
<TableRow>
<ImageView
<Button android:id="@+id/ivImg"
android:id="@+id/btnPrintImg" android:layout_width="100dp"
android:layout_width="wrap_content" android:layout_height="100dp"
android:layout_height="wrap_content" android:layout_weight="1"
android:layout_weight="1" android:src="@drawable/logo_demo" />
android:text="打印图片" /> </TableRow>
</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
android:id="@+id/btnFeedPaper"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="走纸" />
</TableRow>
<TableRow>
<Button
android:id="@+id/btnCut"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="切纸" />
</TableRow>
<TableRow android:visibility="gone">
<Button
android:id="@+id/btnSet58"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="设置成58mm打印纸" />
</TableRow>
<TableRow android:visibility="gone">
<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>
<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>
</ScrollView>
<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
android:id="@+id/btnCut"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="切纸" />
</TableRow>
<TableRow android:visibility="gone">
<Button
android:id="@+id/btnSet58"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="设置成58mm打印纸" />
</TableRow>
<TableRow android:visibility="gone">
<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>
<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> </LinearLayout>
\ No newline at end of file
...@@ -86,18 +86,24 @@ public class ChuangjiePrinter extends BasePrinter { ...@@ -86,18 +86,24 @@ public class ChuangjiePrinter extends BasePrinter {
@Override @Override
public boolean release() { public boolean release() {
if (mUsbDriver != null && mUsbDev != null) { try {
if (mUsbDriver.isConnected()) { if (mUsbDriver != null && mUsbDev != null) {
if (null != mUsbDev) { if (mUsbDriver.isConnected()) {
mUsbDriver.disconnet(mUsbDev); if (null != mUsbDev) {
mUsbDriver.disconnet(mUsbDev);
}
} }
mUsbDev = null;
mUsbDriver = null;
context = null;
} }
mUsbDev = null; PrintLogger.i(TAG, "创捷打印资源已关闭");
mUsbDriver = null; return true;
context = null; } catch (Exception ex) {
ex.printStackTrace();
PrintLogger.e(TAG, ex.getMessage());
} }
PrintLogger.i(TAG, "创捷打印资源已关闭"); return false;
return true;
} }
@Override @Override
......
...@@ -76,7 +76,11 @@ public class PrinterManager { ...@@ -76,7 +76,11 @@ public class PrinterManager {
for (Type type : Type.values()) { for (Type type : Type.values()) {
try { try {
if (printer != null) { if (printer != null) {
printer.release(); try {
printer.release();
} catch (Exception ex) {
//出现异常也继续进行轮询
}
} }
Class printerCls = Class.forName(type.clsName); Class printerCls = Class.forName(type.clsName);
printer = (IPrinter) printerCls.newInstance(); printer = (IPrinter) printerCls.newInstance();
......
...@@ -70,12 +70,19 @@ class SunmiK1Printer extends BasePrinter { ...@@ -70,12 +70,19 @@ class SunmiK1Printer extends BasePrinter {
@Override @Override
public boolean release() { public boolean release() {
if (this.context != null && connService != null) { try {
this.context.unbindService(connService); if (this.context != null && connService != null) {
this.context.unbindService(connService);
}
context = null;
extPrinterService = null;
PrintLogger.i(TAG, "商米K1销毁成功");
return true;
} catch (Exception ex) {
ex.printStackTrace();
PrintLogger.e(TAG, ex.getMessage());
} }
context = null; return false;
extPrinterService = null;
return true;
} }
/** /**
......
...@@ -69,6 +69,9 @@ public class SunmiT1Printer extends BasePrinter { ...@@ -69,6 +69,9 @@ public class SunmiT1Printer extends BasePrinter {
woyouService.sendRAWData(ESCUtil.boldOff(), null); woyouService.sendRAWData(ESCUtil.boldOff(), null);
woyouService.sendRAWData(ESCUtil.underlineOff(), null); woyouService.sendRAWData(ESCUtil.underlineOff(), null);
woyouService.printTextWithFont(content, null, size, null); woyouService.printTextWithFont(content, null, size, null);
if (isFeed) {
woyouService.lineWrap(1, null);
}
return ret; return ret;
} catch (Exception ex) { } catch (Exception ex) {
ex.printStackTrace(); ex.printStackTrace();
...@@ -123,12 +126,33 @@ public class SunmiT1Printer extends BasePrinter { ...@@ -123,12 +126,33 @@ public class SunmiT1Printer extends BasePrinter {
} }
try { try {
int ret = setAlign(align); int ret = setAlign(align);
woyouService.printBarCode(content, 8, 162, 2, 2, null); if (content != null && content.trim().length() <= 12) {
//该方法最多只能打印12位以内的二维码
woyouService.printBarCode(content, 8, 148, 2, 2, null);
} else if (content != null && content.trim().length() > 12 && content.trim().length() % 2 == 0) {
//该方法只能打印12位以上位数位偶数的条码
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
buffer.write(new byte[]{0x1D, 0x48, (byte) 2,
0x1D, 0x77, (byte) 2, 0x1D, 0x68, (byte) 148, 0x0A});
byte[] barcode;
barcode = BytesUtil.getBytesFromDecString(content);
buffer.write(new byte[]{0x1D, 0x6B, 0x49, (byte) (barcode.length + 2), 0x7B, (byte) (0x43)});
buffer.write(barcode);
woyouService.sendRAWData(buffer.toByteArray(), null);
} else if (content != null) {
//超过12位且长度位奇数的情况,采用打印一维条码图片的形式
Bitmap barcodeBm = BitmapUtils.createBarcodeBitmap(content, 8, 460, 148);
if (barcodeBm != null) {
ret = printImage(align, barcodeBm, isFeed);
barcodeBm.recycle();
}
}
if (isFeed) { if (isFeed) {
woyouService.lineWrap(1, null); woyouService.lineWrap(1, null);
} }
return ret; return ret;
} catch (RemoteException e) { } catch (Exception e) {
throw new PrinterException(PrinterStatusEnum.CODE_FAILED.status, e.getMessage()); throw new PrinterException(PrinterStatusEnum.CODE_FAILED.status, e.getMessage());
} }
} }
...@@ -211,6 +235,8 @@ public class SunmiT1Printer extends BasePrinter { ...@@ -211,6 +235,8 @@ public class SunmiT1Printer extends BasePrinter {
*/ */
private int changeStatus2Custom(int status) { private int changeStatus2Custom(int status) {
switch (status) { switch (status) {
case 0:
return PrinterStatusEnum.CODE_SUCCESS.status;
case 1: case 1:
//正常状态 //正常状态
return PrinterStatusEnum.CODE_SUCCESS.status; return PrinterStatusEnum.CODE_SUCCESS.status;
...@@ -226,8 +252,10 @@ public class SunmiT1Printer extends BasePrinter { ...@@ -226,8 +252,10 @@ public class SunmiT1Printer extends BasePrinter {
case 6: case 6:
//开盖 //开盖
throw new PrinterException(PrinterStatusEnum.CODE_FAILED.status, "打印机开盖"); throw new PrinterException(PrinterStatusEnum.CODE_FAILED.status, "打印机开盖");
case 7:
throw new PrinterException(PrinterStatusEnum.CODE_KNIFE_ERROR.status, "切刀异常");
default: default:
throw new PrinterException(PrinterStatusEnum.CODE_NO_PAPER.status, "获取状态异常"); throw new PrinterException(PrinterStatusEnum.CODE_FAILED.status, "获取状态异常");
} }
} }
......
...@@ -7,6 +7,16 @@ import android.graphics.Paint; ...@@ -7,6 +7,16 @@ import android.graphics.Paint;
import android.graphics.Rect; import android.graphics.Rect;
import android.util.Log; import android.util.Log;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import java.util.HashMap;
import java.util.Map;
/** /**
* bitmap处理工具类 * bitmap处理工具类
*/ */
...@@ -69,4 +79,87 @@ public class BitmapUtils { ...@@ -69,4 +79,87 @@ public class BitmapUtils {
canvas.drawBitmap(origin, width, 0, null); canvas.drawBitmap(origin, width, 0, null);
return newBm; return newBm;
} }
/**
* 生成条码bitmap
*
* @param content 条码内容
* @param format 条码类型
* * 0 -- UPC-A,
* * 1 -- UPC-E,
* * 2 -- JAN13(EAN13),
* * 3 -- JAN8(EAN8),
* * 4 -- CODE39,
* * 5 -- ITF,
* * 6 -- CODABAR,
* * 7 -- CODE93,
* * 8 -- CODE128
* @param width 条码宽度
* @param height 条码高度
* @return 条码bitmap(可为null)
*/
public static Bitmap createBarcodeBitmap(String content, int format, int width, int height) {
if (content == null || content.equals(""))
return null;
BarcodeFormat barcodeFormat;
switch (format) {
case 0:
barcodeFormat = BarcodeFormat.UPC_A;
break;
case 1:
barcodeFormat = BarcodeFormat.UPC_E;
break;
case 2:
barcodeFormat = BarcodeFormat.EAN_13;
break;
case 3:
barcodeFormat = BarcodeFormat.EAN_8;
break;
case 4:
barcodeFormat = BarcodeFormat.CODE_39;
break;
case 5:
barcodeFormat = BarcodeFormat.ITF;
break;
case 6:
barcodeFormat = BarcodeFormat.CODABAR;
break;
case 7:
barcodeFormat = BarcodeFormat.CODE_93;
break;
case 8:
barcodeFormat = BarcodeFormat.CODE_128;
break;
case 9:
barcodeFormat = BarcodeFormat.QR_CODE;
break;
default:
barcodeFormat = BarcodeFormat.QR_CODE;
height = width;
break;
}
MultiFormatWriter qrCodeWriter = new MultiFormatWriter();
Map<EncodeHintType, Object> hints = new HashMap<>();
hints.put(EncodeHintType.CHARACTER_SET, "GBK");
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
try {
BitMatrix encode = qrCodeWriter.encode(content, barcodeFormat, width, height, hints);
int[] pixels = new int[width * height];
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
if (encode.get(j, i)) {
pixels[i * width + j] = 0x00000000;
} else {
pixels[i * width + j] = 0xffffffff;
}
}
}
return Bitmap.createBitmap(pixels, 0, width, width, height, Bitmap.Config.RGB_565);
} catch (WriterException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
}
return null;
}
} }
package com.miya.print.utils; package com.miya.print.utils;
import com.miya.print.BuildConfig;
public class PrintLogger { public class PrintLogger {
private static final String Tag = PrintLogger.class.getSimpleName(); private static final String Tag = PrintLogger.class.getSimpleName();
......
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