Commit e8363847 authored by pengguangpu's avatar pengguangpu

修复若干bug;创捷机器打印二维码改为打印二维码图片实现

parent 5ba69692
...@@ -27,9 +27,9 @@ dependencies { ...@@ -27,9 +27,9 @@ dependencies {
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2' androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
//打印sdk //打印sdk
// api project(':print') api project(':print')
//称重sdk //称重sdk
api project(':weighing') api project(':weighing')
implementation files('libs/miya-print-1.aar') // implementation files('libs/miya-print-1.aar')
} }
...@@ -169,8 +169,7 @@ public class PrintActivity extends Activity implements View.OnClickListener { ...@@ -169,8 +169,7 @@ public class PrintActivity extends Activity implements View.OnClickListener {
case R.id.btnBarcode: case R.id.btnBarcode:
if (PrinterManager.getInstance().isConnected()) { if (PrinterManager.getInstance().isConnected()) {
try { try {
// PrinterManager.getInstance().getPrinter().printBarcode(-1, etBarcode.getText().toString(), false); PrinterManager.getInstance().getPrinter().printBarcode(-1, etBarcode.getText().toString(), false);
PrinterManager.getInstance().getPrinter().printBarcode(IPrinter.ALIGN_CENTER, "http://60.2717.64.173:18080/Wrapper/jump.do?type=1/jump.do?type=3&orderno=51153001809100005&sjm=1455", false);
} catch (Exception ex) { } catch (Exception ex) {
ex.printStackTrace(); ex.printStackTrace();
tvResult.setText(ex.getMessage()); tvResult.setText(ex.getMessage());
...@@ -180,7 +179,8 @@ public class PrintActivity extends Activity implements View.OnClickListener { ...@@ -180,7 +179,8 @@ 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());
......
...@@ -14,5 +14,6 @@ public class ExampleUnitTest { ...@@ -14,5 +14,6 @@ public class ExampleUnitTest {
public void addition_isCorrect() { public void addition_isCorrect() {
assertEquals(4, 2 + 2); assertEquals(4, 2 + 2);
System.out.println("".getBytes().length); System.out.println("".getBytes().length);
System.out.println("xxxxxx\n".split("\n").length);
} }
} }
\ No newline at end of file
...@@ -12,7 +12,7 @@ android { ...@@ -12,7 +12,7 @@ android {
minSdkVersion 15 minSdkVersion 15
targetSdkVersion 28 targetSdkVersion 28
versionCode 1 versionCode 1
versionName "1" versionName "1.0.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
...@@ -42,10 +42,7 @@ android { ...@@ -42,10 +42,7 @@ android {
dependencies { dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar']) implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:28.0.0'
testImplementation 'junit:junit:4.12' testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
//zxing //zxing
implementation files('libs/core-3.3.0.jar') implementation files('libs/core-3.3.0.jar')
......
...@@ -11,9 +11,12 @@ import android.hardware.usb.UsbManager; ...@@ -11,9 +11,12 @@ import android.hardware.usb.UsbManager;
import android.text.TextUtils; import android.text.TextUtils;
import android.util.Log; import android.util.Log;
import com.miya.print.exception.PrinterException;
import com.miya.print.utils.BitmapUtils; import com.miya.print.utils.BitmapUtils;
import com.miya.print.utils.ESCUtil;
import com.miya.print.utils.PrintLogger; import com.miya.print.utils.PrintLogger;
import com.miya.print.utils.ImageUtils; import com.miya.print.utils.ImageUtils;
import com.miya.print.utils.QRCodeUtil;
import com.printsdk.cmd.PrintCmd; import com.printsdk.cmd.PrintCmd;
import com.printsdk.usbsdk.UsbDriver; import com.printsdk.usbsdk.UsbDriver;
...@@ -273,6 +276,9 @@ public class ChuangjiePrinter extends BasePrinter { ...@@ -273,6 +276,9 @@ public class ChuangjiePrinter extends BasePrinter {
//居右打印 //居右打印
actualBm = BitmapUtils.addWhiteLeftMargin(bm, MAX_IMG_WIDTH_80MM - bm.getWidth()); actualBm = BitmapUtils.addWhiteLeftMargin(bm, MAX_IMG_WIDTH_80MM - bm.getWidth());
ret = printQrcodeBitmap(actualBm); ret = printQrcodeBitmap(actualBm);
} else {
//居左打印
ret = printQrcodeBitmap(bm);
} }
} }
if (actualBm != null) { if (actualBm != null) {
...@@ -296,20 +302,27 @@ public class ChuangjiePrinter extends BasePrinter { ...@@ -296,20 +302,27 @@ public class ChuangjiePrinter extends BasePrinter {
@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实现居中,永远会居左打印 Bitmap qrBm = QRCodeUtil.createQRCodeBitmap(content, 220);
if (this.align == 0) { if (qrBm != null) {
//居左打印 ret = printImage(align, qrBm, true);
ret = printQrcode(content, 0, 6, 1); qrBm.recycle();
} else if (this.align == 1) { } else {
//居中打印 throw new PrinterException(PrinterStatusEnum.CODE_FAILED.status, PrinterStatusEnum.CODE_FAILED.desc);
ret = printQrcode(content, 24, 6, 1); }
} else if (this.align == 2) { // //创捷机器打印二维码不能通过setAlign实现居中,永远会居左打印
//居右 // if (this.align == 0) {
ret = printQrcode(content, 24, 6, 1); // //居左打印
} // ret = printQrcode(content, 0, 6, 1);
if (isFeed) { // } else if (this.align == 1) {
ret = feedPaper(); // //居中打印
} // ret = printQrcode(content, 24, 6, 1);
// } else if (this.align == 2) {
// //居右
// ret = printQrcode(content, 24, 6, 1);
// }
// if (isFeed) {
// ret = feedPaper();
// }
return ret; return ret;
} }
......
...@@ -112,10 +112,10 @@ public class HisensePrinter extends BasePrinter { ...@@ -112,10 +112,10 @@ public class HisensePrinter extends BasePrinter {
@Override @Override
public int printText(int align, int size, String content, boolean isFeed) { public int printText(int align, int size, String content, boolean isFeed) {
int ret = setAlign(align);
//使用累加器进行累加,暂只支持80mm打印纸 //使用累加器进行累加,暂只支持80mm打印纸
AccumulateUtils.accumulateString(AccumulateUtils.PAGE_MODE_80MM, this.align, size, content, isFeed); AccumulateUtils.accumulateString(AccumulateUtils.PAGE_MODE_80MM, this.align, size, content, isFeed);
//再设置对齐方式 return ret;
return setAlign(align);
} }
@Override @Override
...@@ -236,7 +236,7 @@ public class HisensePrinter extends BasePrinter { ...@@ -236,7 +236,7 @@ public class HisensePrinter extends BasePrinter {
checkService(); checkService();
int ret = 0; int ret = 0;
try { try {
ret = hs650Api.getPrinter().printTextStr(accumulateStr, 0, ALIGN_LEFT); ret = hs650Api.getPrinter().printTextStr(accumulateStr, 0, IPrinter.ALIGN_LEFT);
//恢复当前排版格式 //恢复当前排版格式
ret = hs650Api.getPrinter().printTextStr("", 0, this.align); ret = hs650Api.getPrinter().printTextStr("", 0, this.align);
} catch (RemoteException e) { } catch (RemoteException e) {
......
package com.miya.print.utils; package com.miya.print.utils;
import android.support.annotation.Nullable;
import java.nio.charset.Charset; import java.nio.charset.Charset;
/** /**
...@@ -33,7 +31,6 @@ public class AccumulateUtils { ...@@ -33,7 +31,6 @@ public class AccumulateUtils {
/** /**
* 累积字符串 * 累积字符串
*/ */
@Nullable
private static String accumulateStr = ""; private static String accumulateStr = "";
private AccumulateUtils() { private AccumulateUtils() {
...@@ -118,40 +115,17 @@ public class AccumulateUtils { ...@@ -118,40 +115,17 @@ public class AccumulateUtils {
if (str == null) { if (str == null) {
return accumulateStr; return accumulateStr;
} }
//待拼接的字符串 //先用换行符将其分割
String[] strList = str.split("\n");
String targetStr = ""; String targetStr = "";
switch (align) { for (String tmp : strList) {
case 0: targetStr = targetStr + addSpace_80mm(align, tmp);
//居左 if (str.contains("\n")) {
accumulateStr = accumulateStr + str; //如果包含换行符,此处需补偿换行符
break; targetStr = targetStr + "\n";
case 1: }
//居中
if (getBytesLength(str) > MAX_WORDS_80MM) {
accumulateStr = accumulateStr + str;
break;
}
//计算空格数
for (int i = 0; i < (MAX_WORDS_80MM - getBytesLength(str)) / 2; i++) {
targetStr = targetStr + " ";
}
targetStr = targetStr + str;
accumulateStr = accumulateStr + targetStr;
break;
case 2:
//居右
if (getBytesLength(str) > MAX_WORDS_80MM) {
accumulateStr = accumulateStr + str;
break;
}
//计算空格数
for (int i = 0; i < MAX_WORDS_80MM - getBytesLength(str); i++) {
targetStr = targetStr + " ";
}
targetStr = targetStr + str;
accumulateStr = accumulateStr + targetStr;
break;
} }
accumulateStr = accumulateStr + targetStr;
if (isFeed) { if (isFeed) {
accumulateStr += "\n"; accumulateStr += "\n";
} }
...@@ -172,51 +146,92 @@ public class AccumulateUtils { ...@@ -172,51 +146,92 @@ public class AccumulateUtils {
return accumulateStr; return accumulateStr;
} }
//待拼接的字符串 //待拼接的字符串
String[] strList = str.split("\n");
String targetStr = ""; String targetStr = "";
for (String tmp : strList) {
targetStr = targetStr + addSpace_58mm(align, tmp);
if (str.contains("\n")) {
//如果包含换行符,此处需补偿换行符
targetStr = targetStr + "\n";
}
}
accumulateStr = accumulateStr + targetStr;
if (isFeed) {
accumulateStr += "\n";
}
return accumulateStr;
}
/**
* 获取当前已经累积完的字符串
*
* @return 当前已经累积完的字符串
*/
public static String getAccumulatedString() {
return accumulateStr;
}
/**
* 添加空格实现对齐逻辑,传参中的字符不允许有换行符(80mm)
*
* @param align 对齐方式
* @param str 字符串,不允许含换行符
* @return 处理后的字符串
*/
private static String addSpace_80mm(int align, String str) {
String spaceStr = "";
switch (align) { switch (align) {
case 0: case 0:
//居左 //居左
accumulateStr = accumulateStr + str; return str;
break;
case 1: case 1:
//居中
if (getBytesLength(str) > MAX_WORDS_58MM) {
accumulateStr = accumulateStr + str;
break;
}
//计算空格数 //计算空格数
for (int i = 0; i < (MAX_WORDS_58MM - getBytesLength(str)) / 2; i++) { for (int i = 0; i < (MAX_WORDS_80MM - getBytesLength(str)) / 2; i++) {
targetStr = targetStr + " "; spaceStr = spaceStr + " ";
} }
targetStr = targetStr + str; spaceStr = spaceStr + str;
accumulateStr = accumulateStr + targetStr; return spaceStr;
break;
case 2: case 2:
//居右
if (getBytesLength(str) > MAX_WORDS_58MM) {
accumulateStr = accumulateStr + str;
break;
}
//计算空格数 //计算空格数
for (int i = 0; i < MAX_WORDS_58MM - getBytesLength(str); i++) { for (int i = 0; i < MAX_WORDS_80MM - getBytesLength(str); i++) {
targetStr = targetStr + " "; spaceStr = spaceStr + " ";
} }
targetStr = targetStr + str; spaceStr = spaceStr + str;
accumulateStr = accumulateStr + targetStr; return spaceStr;
break; default:
return str;
} }
if (isFeed) {
accumulateStr += "\n";
}
return accumulateStr;
} }
/** /**
* 获取当前已经累积完的字符串 * 添加空格实现对齐逻辑,传参中的字符不允许有换行符(58mm)
* *
* @return 当前已经累积完的字符串 * @param align 对齐方式
* @param str 字符串,不允许含换行符
* @return 处理后的字符串
*/ */
public static String getAccumulatedString() { private static String addSpace_58mm(int align, String str) {
return accumulateStr; String spaceStr = "";
switch (align) {
case 0:
//居左
return str;
case 1:
//计算空格数
for (int i = 0; i < (MAX_WORDS_58MM - getBytesLength(str)) / 2; i++) {
spaceStr = spaceStr + " ";
}
spaceStr = spaceStr + str;
return spaceStr;
case 2:
//计算空格数
for (int i = 0; i < MAX_WORDS_58MM - getBytesLength(str); i++) {
spaceStr = spaceStr + " ";
}
spaceStr = spaceStr + str;
return spaceStr;
default:
return str;
}
} }
} }
package com.miya.print.utils;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.text.TextUtils;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.common.CharacterSetECI;
import com.google.zxing.qrcode.QRCodeWriter;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import java.util.Hashtable;
/**
* @ClassName: QRCodeUtil
* @Description: 二维码工具类
*/
public class QRCodeUtil {
/**
* 创建二维码位图
*
* @param content 字符串内容
* @param size 位图宽&高(单位:px)
* @return
*/
public static Bitmap createQRCodeBitmap(String content, int size) {
return createQRCodeBitmap(content, size, "UTF-8", "M", "1", Color.BLACK, Color.WHITE, null, null, 0F);
}
/**
* 创建二维码位图 (自定义黑、白色块颜色)
*
* @param content 字符串内容
* @param size 位图宽&高(单位:px)
* @param color_black 黑色色块的自定义颜色值
* @param color_white 白色色块的自定义颜色值
* @return
*/
public static Bitmap createQRCodeBitmap(String content, int size, int color_black, int color_white) {
return createQRCodeBitmap(content, size, "UTF-8", "H", "4", color_black, color_white, null, null, 0F);
}
/**
* 创建二维码位图 (带Logo小图片)
*
* @param content 字符串内容
* @param size 位图宽&高(单位:px)
* @param logoBitmap logo图片
* @param logoPercent logo小图片在二维码图片中的占比大小,范围[0F,1F]。超出范围->默认使用0.2F
* @return
*/
public static Bitmap createQRCodeBitmap(String content, int size, Bitmap logoBitmap, float logoPercent) {
return createQRCodeBitmap(content, size, "UTF-8", "H", "4", Color.BLACK, Color.WHITE, null, logoBitmap, logoPercent);
}
/**
* 创建二维码位图 (Bitmap颜色代替黑色) 注意!!!注意!!!注意!!! 选用的Bitmap图片一定不能有白色色块,否则会识别不出来!!!
*
* @param content 字符串内容
* @param size 位图宽&高(单位:px)
* @param targetBitmap 目标图片 (如果targetBitmap != null, 黑色色块将会被该图片像素色值替代)
* @return
*/
public static Bitmap createQRCodeBitmap(String content, int size, Bitmap targetBitmap) {
return createQRCodeBitmap(content, size, "UTF-8", "H", "4", Color.BLACK, Color.WHITE, targetBitmap, null, 0F);
}
/**
* 创建二维码位图 (支持自定义配置和自定义样式)
*
* @param content 字符串内容
* @param size 位图宽&高(单位:px)
* @param character_set 字符集/字符转码格式 (支持格式:{@link CharacterSetECI })。传null时,zxing源码默认使用 "ISO-8859-1"
* @param error_correction 容错级别 (支持级别:{@link ErrorCorrectionLevel })。传null时,zxing源码默认使用 "L"
* @param margin 空白边距 (可修改,要求:整型且>=0), 传null时,zxing源码默认使用"4"。
* @param color_black 黑色色块的自定义颜色值
* @param color_white 白色色块的自定义颜色值
* @param targetBitmap 目标图片 (如果targetBitmap != null, 黑色色块将会被该图片像素色值替代)
* @param logoBitmap logo小图片
* @param logoPercent logo小图片在二维码图片中的占比大小,范围[0F,1F],超出范围->默认使用0.2F。
* @return
*/
public static Bitmap createQRCodeBitmap(String content, int size,
String character_set, String error_correction, String margin,
int color_black, int color_white, Bitmap targetBitmap,
Bitmap logoBitmap, float logoPercent) {
/** 1.参数合法性判断 */
if (TextUtils.isEmpty(content)) { // 字符串内容判空
return null;
}
if (size <= 0) { // 宽&高都需要>0
return null;
}
try {
/** 2.设置二维码相关配置,生成BitMatrix(位矩阵)对象 */
Hashtable<EncodeHintType, String> hints = new Hashtable<>();
if (!TextUtils.isEmpty(character_set)) {
hints.put(EncodeHintType.CHARACTER_SET, character_set); // 字符转码格式设置
}
if (!TextUtils.isEmpty(error_correction)) {
hints.put(EncodeHintType.ERROR_CORRECTION, error_correction); // 容错级别设置
}
if (!TextUtils.isEmpty(margin)) {
hints.put(EncodeHintType.MARGIN, margin); // 空白边距设置
}
BitMatrix bitMatrix = null;
bitMatrix = new QRCodeWriter().encode(content, BarcodeFormat.QR_CODE, size, size, hints);
/** 3.根据BitMatrix(位矩阵)对象为数组元素赋颜色值 */
if (targetBitmap != null) {
targetBitmap = Bitmap.createScaledBitmap(targetBitmap, size, size, false);
}
int[] pixels = new int[size * size];
for (int y = 0; y < size; y++) {
for (int x = 0; x < size; x++) {
if (bitMatrix.get(x, y)) { // 黑色色块像素设置
if (targetBitmap != null) {
pixels[y * size + x] = targetBitmap.getPixel(x, y);
} else {
pixels[y * size + x] = color_black;
}
} else { // 白色色块像素设置
pixels[y * size + x] = color_white;
}
}
}
/** 4.创建Bitmap对象,根据像素数组设置Bitmap每个像素点的颜色值,之后返回Bitmap对象 */
Bitmap bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
bitmap.setPixels(pixels, 0, size, 0, 0, size, size);
/** 5.为二维码添加logo小图标 */
if (logoBitmap != null) {
return addLogo(bitmap, logoBitmap, logoPercent);
}
return bitmap;
} catch (WriterException e) {
e.printStackTrace();
}
return null;
}
/**
* 向一张图片中间添加logo小图片(图片合成)
*
* @param srcBitmap 原图片
* @param logoBitmap logo图片
* @param logoPercent 百分比 (用于调整logo图片在原图片中的显示大小, 取值范围[0,1], 传值不合法时使用0.2F)
* 原图片是二维码时,建议使用0.2F,百分比过大可能导致二维码扫描失败。
* @return
*/
private static Bitmap addLogo(Bitmap srcBitmap, Bitmap logoBitmap, float logoPercent) {
/** 1. 参数合法性判断 */
if (srcBitmap == null) {
return null;
}
if (logoBitmap == null) {
return srcBitmap;
}
if (logoPercent < 0F || logoPercent > 1F) {
logoPercent = 0.2F;
}
/** 2. 获取原图片和Logo图片各自的宽、高值 */
int srcWidth = srcBitmap.getWidth();
int srcHeight = srcBitmap.getHeight();
int logoWidth = logoBitmap.getWidth();
int logoHeight = logoBitmap.getHeight();
/** 3. 计算画布缩放的宽高比 */
float scaleWidth = srcWidth * logoPercent / logoWidth;
float scaleHeight = srcHeight * logoPercent / logoHeight;
/** 4. 使用Canvas绘制,合成图片 */
Bitmap bitmap = Bitmap.createBitmap(srcWidth, srcHeight, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
canvas.drawBitmap(srcBitmap, 0, 0, null);
canvas.scale(scaleWidth, scaleHeight, srcWidth / 2, srcHeight / 2);
canvas.drawBitmap(logoBitmap, srcWidth / 2 - logoWidth / 2, srcHeight / 2 - logoHeight / 2, null);
return bitmap;
}
}
\ No newline at end of file
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