Commit eb5a9693 authored by pengguangpu's avatar pengguangpu

测试发现商米机器的打印竟然可以使用桑达的打印驱动

parent 641ec581
......@@ -4,7 +4,6 @@ import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.print.PrintManager;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.Button;
......@@ -15,8 +14,7 @@ import android.widget.TextView;
import com.miya.print.IPrinter;
import com.miya.print.PrinterManager;
import com.miya.print.PrinterStatusEnum;
import com.miya.print.utils.BitmapUtils;
import com.miya.print.utils.Page88MmPrintUtils;
import com.miya.print.utils.Page80MmPrintUtils;
public class PrintActivity extends Activity implements View.OnClickListener {
......@@ -254,7 +252,7 @@ public class PrintActivity extends Activity implements View.OnClickListener {
ret = printer.printText(-1, 0, "中百", true);
ret = printer.printText(-1, 0, "支付宝自助收银", true);
//分割线
ret = printer.printText(-1, 0, Page88MmPrintUtils.printDivideLineString(), true);
ret = printer.printText(-1, 0, Page80MmPrintUtils.printDivideLineString(), true);
//居左打印
ret = printer.setAlign(IPrinter.ALIGN_LEFT);
//订单号
......@@ -266,24 +264,24 @@ public class PrintActivity extends Activity implements View.OnClickListener {
//打印时间
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, Page80MmPrintUtils.printDivideLineString(), true);
//四条目打印
ret = printer.printText(-1, 0, Page88MmPrintUtils.printFourData("商品名称", "编码", "数量", "金额"), true);
ret = printer.printText(-1, 0, Page80MmPrintUtils.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.printText(-1, 0, Page80MmPrintUtils.printFourData("6924160298876", " ", "*1", "0.20元"), true);
ret = printer.feedPaper();
//分割线
ret = printer.printText(-1, 0, Page88MmPrintUtils.printDivideLineString(), true);
ret = printer.printText(-1, 0, Page80MmPrintUtils.printDivideLineString(), true);
//总件数
ret = printer.printText(-1, 0, Page88MmPrintUtils.printTwoData("总件数:", "1"), true);
ret = printer.printText(-1, 0, Page80MmPrintUtils.printTwoData("总件数:", "1"), true);
//商品合计
ret = printer.printText(-1, 0, Page88MmPrintUtils.printTwoData("商品合计:", "0.20"), true);
ret = printer.printText(-1, 0, Page80MmPrintUtils.printTwoData("商品合计:", "0.20"), true);
//支付宝实付
ret = printer.printText(-1, 0, Page88MmPrintUtils.printTwoData("支付宝", "0.01"), true);
ret = printer.printText(-1, 0, Page80MmPrintUtils.printTwoData("支付宝", "0.01"), true);
//分割线
ret = printer.printText(-1, 0, Page88MmPrintUtils.printDivideLineString(), true);
ret = printer.printText(-1, 0, Page80MmPrintUtils.printDivideLineString(), true);
ret = printer.setAlign(IPrinter.ALIGN_CENTER);
//小票尾部
ret = printer.printText(-1, 0, "欢迎下次光临!!!", true);
......
......@@ -39,7 +39,10 @@ dependencies {
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
//一些工具类集合,从主工程拷贝过来的
implementation 'com.blankj:utilcode:1.16.0'
// implementation 'com.blankj:utilcode:1.16.0'
//zxing
implementation files('libs/core-3.3.0.jar')
/**
* 创捷机器打印jar包
......
package com.hisense.hs650service.aidl;
import com.hisense.hs650service.aidl.Printer;
import com.hisense.hs650service.aidl.LedStripe;
interface HS650Api {
Printer getPrinter();
LedStripe getLedStripe();
}
package com.hisense.hs650service.aidl;
interface LedStripe {
void lightOn();
void lightOff();
}
package com.hisense.hs650service.aidl;
interface Printer{
//printer
int initPrinter(int type);
int printTextStr(String text,int fontSize,int pattern);
int printImage(in Bitmap bitmap, int mode);
int feedPaper(int value,int unit);
int printerConfig(int baudrate,int uartPort,boolean ctrlOPen);
int printBar(String barcode);
int printQR(String barcode,int mode);
void closePrinter();
void cutPaper();
int printSend(in byte[] data,int len);
int printStatus();
String getPrnType(int baudrate,int uartPort,int timeout);
}
\ No newline at end of file
// IPrinterService.aidl
package com.sunmi.extprinterservice;
// Declare any non-default types here with import statements
interface ExtPrinterService {
int sendRawData(in byte[] cmd);
int printerInit();
int getPrinterStatus();
int lineWrap(in int n);
int pixelWrap(in int n);
int flush();
int tab();
int printText(in String text);
int printBarCode(in String code, in int type, in int width, in int height, in int hriPos);
int printQrCode(in String code, in int modeSize, in int errorlevel);
int printBitmap(in Bitmap bitmap, in int mode);
int setHorizontalTab(in int[] k);
int setAlignMode(in int type);
int cutPaper(in int m, in int n);
int setFontZoom(in int hori, in int veri);
int printColumnsText(in String[] colsTextArr, in int[] colsWidthArr, in int[] colsAlign);
}
package com.miya.print;
public class HisensePrinter extends BasePrinter {
final static String TAG = HisensePrinter.class.getSimpleName();
}
......@@ -17,7 +17,7 @@ public class PrinterManager {
enum Type {
//这里添加扩展的驱动
// TYPE_HISENSE("hisense", ChuangjiePrinter.class.getName()),
// TYPE_SUNMI("shangmi", ChuangjiePrinter.class.getName()),
TYPE_SUNMI("shangmik1", ShangmiK1Printer.class.getName()),
TYPE_YINGTAI("yingtai", YingtaiPrinter.class.getName()),
TYPE_CHUANGJIE("chuangjie", ChuangjiePrinter.class.getName()),
TYPE_SANGDA("sangda", SangdaPrinter.class.getName());
......
package com.miya.print;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.graphics.Bitmap;
import android.os.IBinder;
import android.os.RemoteException;
import com.miya.print.exception.PrinterException;
import com.miya.print.utils.BytesUtil;
import com.miya.print.utils.ESCUtil;
import com.sunmi.extprinterservice.ExtPrinterService;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
class ShangmiK1Printer extends BasePrinter {
final static String TAG = ShangmiK1Printer.class.getSimpleName();
private ServiceConnection connService = new ServiceConnection() {
@Override
public void onServiceDisconnected(ComponentName name) {
extPrinterService = null;
}
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
extPrinterService = ExtPrinterService.Stub.asInterface(service);
}
};
@Override
public boolean init(Context context) {
return super.init(context);
}
@Override
public boolean release() {
return super.release();
}
private static final String SERVICE_PACKAGE = "com.sunmi.extprinterservice";
private static final String SERVICE_ACTION = "com.sunmi.extprinterservice.PrinterService";
private ExtPrinterService extPrinterService;
/**
* 连接服务
*
* @param context context
*/
@Deprecated
public void connectPrinterService(Context context) {
this.context = context.getApplicationContext();
Intent intent = new Intent();
intent.setPackage(SERVICE_PACKAGE);
intent.setAction(SERVICE_ACTION);
context.getApplicationContext().startService(intent);
context.getApplicationContext().bindService(intent, connService, Context.BIND_AUTO_CREATE);
}
/**
* 断开服务
*
* @param context context
*/
@Deprecated
public void disconnectPrinterService(Context context) {
if (extPrinterService != null) {
context.getApplicationContext().unbindService(connService);
extPrinterService = null;
}
}
@Deprecated
public boolean isConnect() {
return extPrinterService != null;
}
@Deprecated
public void LF() {
if (extPrinterService == null) {
throw new PrinterException(PrinterStatusEnum.CODE_FAILED.status, "服务已断开!");
}
try {
extPrinterService.lineWrap(1);
} catch (RemoteException e) {
throw new PrinterException(PrinterStatusEnum.CODE_FAILED.status, e.getMessage());
}
}
@Deprecated
public void cutPage() {
if (extPrinterService == null) {
throw new PrinterException(PrinterStatusEnum.CODE_FAILED.status, "服务已断开!");
}
try {
extPrinterService.cutPaper(0, 0);
} catch (RemoteException e) {
throw new PrinterException(PrinterStatusEnum.CODE_FAILED.status, e.getMessage());
}
}
@Deprecated
public void setAlignMode(int mode) {
if (extPrinterService == null) {
throw new PrinterException(PrinterStatusEnum.CODE_FAILED.status, "服务已断开!");
}
try {
extPrinterService.setAlignMode(mode);
} catch (RemoteException e) {
throw new PrinterException(PrinterStatusEnum.CODE_FAILED.status, e.getMessage());
}
}
/**
* 设置打印浓度
*/
private int[] darkness = new int[]{0x0600, 0x0500, 0x0400, 0x0300, 0x0200, 0x0100, 0,
0xffff, 0xfeff, 0xfdff, 0xfcff, 0xfbff, 0xfaff};
public void setDarkness(int index) {
if (extPrinterService == null) {
throw new PrinterException(PrinterStatusEnum.CODE_FAILED.status, "服务已断开!");
}
int k = darkness[index];
try {
extPrinterService.sendRawData(ESCUtil.setPrinterDarkness(k));
} catch (RemoteException e) {
throw new PrinterException(PrinterStatusEnum.CODE_FAILED.status, e.getMessage());
}
}
/**
* 初始化打印机
*/
@Deprecated
public void initPrinter() {
if (extPrinterService == null) {
return;
}
try {
extPrinterService.printerInit();
} catch (RemoteException e) {
}
}
/**
* 打印二维码
*/
@Deprecated
public void printQr(String data, int modulesize, int errorlevel) {
if (extPrinterService == null) {
throw new PrinterException(PrinterStatusEnum.CODE_FAILED.status, "服务已断开!");
}
try {
extPrinterService.setAlignMode(1);
extPrinterService.printQrCode(data, modulesize, errorlevel);
extPrinterService.lineWrap(2);
} catch (RemoteException e) {
throw new PrinterException(PrinterStatusEnum.CODE_FAILED.status, e.getMessage());
}
}
/**
* 打印条形码 symbology条码类型
*/
@Deprecated
public void printBarCode(String code, int type, int width, int height, int hriPos) {
if (extPrinterService == null) {
throw new PrinterException(PrinterStatusEnum.CODE_FAILED.status, "服务已断开!");
}
try {
extPrinterService.printBarCode(code, type, width, height, hriPos);
extPrinterService.lineWrap(2);
} catch (RemoteException e) {
throw new PrinterException(PrinterStatusEnum.CODE_FAILED.status, e.getMessage());
}
}
/**
* 打印一维条形码
* data 条码内容
* height 条码高度
* width 条码宽度
* textposition 条码内容位置
*/
@Deprecated
public void printLongBarCode(String data, int height, int width, int textposition) throws RemoteException, IOException {
if (width < 2 || width > 6) {
width = 2;
}
if (textposition < 0 || textposition > 3) {
textposition = 0;
}
if (height < 1 || height > 255) {
height = 162;
}
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
buffer.write(new byte[]{0x1D, 0x48, (byte) textposition,
0x1D, 0x77, (byte) width, 0x1D, 0x68, (byte) height, 0x0A});
byte[] barcode;
barcode = BytesUtil.getBytesFromDecString(data);
buffer.write(new byte[]{0x1D, 0x6B, 0x49, (byte) (barcode.length + 2), 0x7B, (byte) (0x43)});
buffer.write(barcode);
extPrinterService.sendRawData(buffer.toByteArray());
}
/**
* 打印文字
*/
@Deprecated
public void printText(String content, float size, boolean isBold, boolean isUnderLine) {
if (extPrinterService == null) {
throw new PrinterException(PrinterStatusEnum.CODE_FAILED.status, "服务已断开!");
}
try {
if (isBold) {
extPrinterService.sendRawData(ESCUtil.boldOn());
} else {
extPrinterService.sendRawData(ESCUtil.boldOff());
}
if (isUnderLine) {
extPrinterService.sendRawData(ESCUtil.underlineWithOneDotWidthOn());
} else {
extPrinterService.sendRawData(ESCUtil.underlineOff());
}
extPrinterService.printText(content);
} catch (RemoteException e) {
throw new PrinterException(PrinterStatusEnum.CODE_FAILED.status, e.getMessage());
}
}
/*
*打印图片
*/
@Deprecated
public void printBitmap(Bitmap bitmap) {
if (extPrinterService == null) {
throw new PrinterException(PrinterStatusEnum.CODE_FAILED.status, "服务已断开!");
}
try {
extPrinterService.setAlignMode(1);
extPrinterService.printBitmap(bitmap, 0);
extPrinterService.lineWrap(2);
} catch (RemoteException e) {
throw new PrinterException(PrinterStatusEnum.CODE_FAILED.status, e.getMessage());
}
}
@Deprecated
public String updatePrinterState() throws RemoteException {
if (extPrinterService == null) {
return "打印服务已断开!";
}
int state = extPrinterService.getPrinterStatus();
if (state == -1) {
return "打印机脱机或打印服务还未连接打印机";
}
if (state == 0) {
return "正常";
}
if (state == 1) {
return "打印机开盖";
}
if (state == 2) {
return "打印机缺纸";
}
if (state == 3) {
return "打印机即将缺纸";
}
if (state == 4) {
return "打印机过热";
}
return "获取状态异常";
}
/*
* 空打三行!
*/
@Deprecated
public void print3Line() {
if (extPrinterService == null) {
throw new PrinterException(PrinterStatusEnum.CODE_FAILED.status, "服务已断开!");
}
try {
extPrinterService.lineWrap(3);
} catch (RemoteException e) {
throw new PrinterException(PrinterStatusEnum.CODE_FAILED.status, e.getMessage());
}
}
@Deprecated
public void sendRawData(byte[] data) {
if (extPrinterService == null) {
throw new PrinterException(PrinterStatusEnum.CODE_FAILED.status, "服务已断开!");
}
try {
extPrinterService.sendRawData(data);
} catch (RemoteException e) {
throw new PrinterException(PrinterStatusEnum.CODE_FAILED.status, e.getMessage());
}
}
}
This diff is collapsed.
This diff is collapsed.
......@@ -2,9 +2,6 @@ package com.miya.print.utils;
import android.annotation.SuppressLint;
import android.text.TextUtils;
import com.blankj.utilcode.util.RegexUtils;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
......
......@@ -2,16 +2,13 @@ package com.miya.print.utils;
import android.annotation.SuppressLint;
import android.text.TextUtils;
import com.blankj.utilcode.util.RegexUtils;
import java.nio.charset.Charset;
/**
* Created by fengyu on 2018/2/23.
*/
public class Page88MmPrintUtils {
public class Page80MmPrintUtils {
/**
* 打印纸一行最大的字节
......
package com.miya.print.utils;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public final class RegexUtils {
private RegexUtils() {
throw new UnsupportedOperationException("u can't instantiate me...");
}
/**
* Return whether input matches the regex.
*
* @param regex The regex.
* @param input The input.
* @return {@code true}: yes<br>{@code false}: no
*/
public static boolean isMatch(final String regex, final CharSequence input) {
return input != null && input.length() > 0 && Pattern.matches(regex, input);
}
/**
* Return the list of input matches the regex.
*
* @param regex The regex.
* @param input The input.
* @return the list of input matches the regex
*/
public static List<String> getMatches(final String regex, final CharSequence input) {
if (input == null) return Collections.emptyList();
List<String> matches = new ArrayList<>();
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(input);
while (matcher.find()) {
matches.add(matcher.group());
}
return matches;
}
/**
* Splits input around matches of the regex.
*
* @param input The input.
* @param regex The regex.
* @return the array of strings computed by splitting input around matches of regex
*/
public static String[] getSplits(final String input, final String regex) {
if (input == null) return new String[0];
return input.split(regex);
}
/**
* Replace the first subsequence of the input sequence that matches the
* regex with the given replacement string.
*
* @param input The input.
* @param regex The regex.
* @param replacement The replacement string.
* @return the string constructed by replacing the first matching
* subsequence by the replacement string, substituting captured
* subsequences as needed
*/
public static String getReplaceFirst(final String input,
final String regex,
final String replacement) {
if (input == null) return "";
return Pattern.compile(regex).matcher(input).replaceFirst(replacement);
}
/**
* Replace every subsequence of the input sequence that matches the
* pattern with the given replacement string.
*
* @param input The input.
* @param regex The regex.
* @param replacement The replacement string.
* @return the string constructed by replacing each matching subsequence
* by the replacement string, substituting captured subsequences
* as needed
*/
public static String getReplaceAll(final String input,
final String regex,
final String replacement) {
if (input == null) return "";
return Pattern.compile(regex).matcher(input).replaceAll(replacement);
}
}
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