Commit 45e85213 authored by pengguangpu's avatar pengguangpu

丰富方法实现,添加打印图片时的逻辑判断及排版处理

parent 3abdadb4
...@@ -13,6 +13,7 @@ import android.widget.TextView; ...@@ -13,6 +13,7 @@ import android.widget.TextView;
import com.miya.print.PrinterManager; import com.miya.print.PrinterManager;
import com.miya.print.PrinterStatusEnum; import com.miya.print.PrinterStatusEnum;
import com.miya.print.utils.BitmapUtils;
public class PrintActivity extends Activity implements View.OnClickListener { public class PrintActivity extends Activity implements View.OnClickListener {
...@@ -172,7 +173,7 @@ public class PrintActivity extends Activity implements View.OnClickListener { ...@@ -172,7 +173,7 @@ 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 = BitmapFactory.decodeResource(getResources(), R.drawable.logo_demo); Bitmap bm = BitmapUtils.scaleBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.logo_demo), 240, 0);
try { try {
PrinterManager.getInstance().getPrinter().printImage(-1, bm, false); PrinterManager.getInstance().getPrinter().printImage(-1, bm, false);
} catch (Exception ex) { } catch (Exception ex) {
......
...@@ -146,7 +146,8 @@ ...@@ -146,7 +146,8 @@
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="设置成58mm打印纸" /> android:text="设置成58mm打印纸"
android:visibility="gone" />
</TableRow> </TableRow>
<TableRow> <TableRow>
...@@ -156,7 +157,8 @@ ...@@ -156,7 +157,8 @@
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="设置成80mm打印纸" /> android:text="设置成80mm打印纸"
android:visibility="gone" />
</TableRow> </TableRow>
<TableRow> <TableRow>
......
...@@ -11,6 +11,7 @@ import android.hardware.usb.UsbManager; ...@@ -11,6 +11,7 @@ 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.utils.BitmapUtils;
import com.miya.print.utils.D; import com.miya.print.utils.D;
import com.miya.print.utils.ImageUtils; import com.miya.print.utils.ImageUtils;
import com.printsdk.cmd.PrintCmd; import com.printsdk.cmd.PrintCmd;
...@@ -49,6 +50,11 @@ public class ChuangjiePrinter extends BasePrinter { ...@@ -49,6 +50,11 @@ public class ChuangjiePrinter extends BasePrinter {
*/ */
int align = 0; int align = 0;
/**
* 80mm打印纸允许打印的最大图片的宽度,此为经验值
*/
final static int MAX_IMG_WIDTH_80MM = 570;
@Override @Override
public boolean init(Context context) { public boolean init(Context context) {
this.context = context; this.context = context;
...@@ -260,8 +266,24 @@ public class ChuangjiePrinter extends BasePrinter { ...@@ -260,8 +266,24 @@ public class ChuangjiePrinter extends BasePrinter {
@Override @Override
public int printImage(int align, Bitmap bm, boolean isFeed) { public int printImage(int align, Bitmap bm, boolean isFeed) {
if (bm == null) {
return PrinterStatusEnum.CODE_FAILED.getStatus();
}
int ret = setAlign(align); int ret = setAlign(align);
ret = printQrcodeBitmap(bm); Bitmap actualBm = null;
if (bm.getWidth() > MAX_IMG_WIDTH_80MM) {
actualBm = BitmapUtils.scaleBitmap(bm, MAX_IMG_WIDTH_80MM, 0);
ret = printQrcodeBitmap(actualBm);
} else {
//如果是居中打印,则拼接白色左边距
if (align == 1) {
actualBm = BitmapUtils.addWhiteLeftMargin(bm, (MAX_IMG_WIDTH_80MM - bm.getWidth()) / 2);
ret = printQrcodeBitmap(actualBm);
}
}
if (actualBm != null) {
actualBm.recycle();
}
if (isFeed) { if (isFeed) {
feedPaper(); feedPaper();
} }
...@@ -271,7 +293,16 @@ public class ChuangjiePrinter extends BasePrinter { ...@@ -271,7 +293,16 @@ 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);
ret = printQrcode(content, 0, 6, 1); if (this.align == 0) {
//居左打印
ret = printQrcode(content, 0, 6, 1);
} else if (this.align == 1) {
//居中打印
// ret = printQrcode()
} else if (this.align == 2) {
//居右
// ret = printQrcode()
}
if (isFeed) { if (isFeed) {
ret = feedPaper(); ret = feedPaper();
} }
......
package com.miya.print.utils;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.Log;
/**
* bitmap处理工具类
*/
public class BitmapUtils {
/**
* 按指定宽高缩放源bitmap
*
* @param origin 源bitmap
* @param width 指定宽度,传0代表按height的比例进行缩放,比例计算公式:scale = origin.getHeight()/height
* @param height 指定高度,传0代表按width的比例进行缩放,比例计算公式:scale = origin.getWidth()/width
* @return width,height都传0代表不缩放,外部调用者注意bitmap回收
*/
public static Bitmap scaleBitmap(Bitmap origin, int width, int height) {
if (origin == null) {
return null;
}
int widthOrigin = origin.getWidth();
int heightOrigin = origin.getHeight();
Matrix matrix = new Matrix();
float scaleX = (float) width / (float) widthOrigin;
float scaleY = (float) height / (float) heightOrigin;
if (width == 0) {
scaleX = scaleY;
}
if (height == 0) {
scaleY = scaleX;
}
if ((width == 0 && height == 0) || width < 0 || height < 0) {
//返回原图片
return origin;
}
matrix.preScale(scaleX, scaleY);
Bitmap newBM = Bitmap.createBitmap(origin, 0, 0, widthOrigin, heightOrigin, matrix, false);
if (newBM.equals(origin)) {
return newBM;
}
origin.recycle();
return newBM;
}
/**
* 给源bitmap添加白色左边距,用于打印对齐
*
* @param origin 源bitmap
* @param width 指定左边距宽度
* @return
*/
public static Bitmap addWhiteLeftMargin(Bitmap origin, int width) {
if (origin == null) {
return null;
}
if (width <= 0) {
return origin;
}
Bitmap newBm = Bitmap.createBitmap(width + origin.getWidth(), origin.getHeight(), origin.getConfig());
Canvas canvas = new Canvas(newBm);
//绘制左边白色边距
canvas.drawColor(0xffffffff);
canvas.drawBitmap(origin, width, 0, null);
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