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());
......
This diff is collapsed.
...@@ -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