gpt4 book ai didi

java - 在 POS/ESC 热敏打印机 Android 上打印图像

转载 作者:太空宇宙 更新时间:2023-11-04 12:03:08 24 4
gpt4 key购买 nike

我正在尝试在蓝牙 pos/esc 热敏打印机上打印 Logo ,打印的图像是坏字符或垃圾字符。如果您对以下问题有答案,或者哪里做错了,请提供帮助。我尝试了此代码“ stack overflow-How can I print an image on a Bluetooth printer in Android? ”仍然无法正常工作当前使用的图像是:

576px width X 95px height

我当前的代码是。

public void print_image() {

try {
mOutputStream = mSocket.getOutputStream();

Bitmap bmp = BitmapFactory.decodeResource(getResources(),R.drawable.logo1);

convertBitmap(bmp);
mOutputStream.write(PrinterCommands.SET_LINE_SPACING_24);

int offset = 0;
while (offset < bmp.getHeight()) {
mService.write(PrinterCommands.SELECT_BIT_IMAGE_MODE);
for (int x = 0; x < bmp.getWidth(); ++x) {

for (int k = 0; k < 3; ++k) {

byte slice = 0;
for (int b = 0; b < 8; ++b) {
int y = (((offset / 8) + k) * 8) + b;
int i = (y * bmp.getWidth()) + x;
boolean v = false;
if (i < dots.length()) {
v = dots.get(i);
}
slice |= (byte) ((v ? 1 : 0) << (7 - b));
}
mService.write(slice);
}
}
offset += 24;
mOutputStream.write(PrinterCommands.FEED_LINE);
mOutputStream.write(PrinterCommands.FEED_LINE);
mOutputStream.write(PrinterCommands.FEED_LINE);
mOutputStream.write(PrinterCommands.FEED_LINE);
mOutputStream.write(PrinterCommands.FEED_LINE);
mOutputStream.write(PrinterCommands.FEED_LINE);
}
mOutputStream.write(PrinterCommands.SET_LINE_SPACING_30);



}catch (Exception e){
Log.e("errorme",e.getMessage());
}
}
public String convertBitmap(Bitmap inputBitmap) {

mWidth = inputBitmap.getWidth();
mHeight = inputBitmap.getHeight();

convertArgbToGrayscale(inputBitmap, mWidth, mHeight);
mStatus = "ok";
return mStatus;
}

private void convertArgbToGrayscale(Bitmap bmpOriginal, int width,
int height) {
int pixel;
int k = 0;
int B = 0, G = 0, R = 0;
dots = new BitSet();
try {

for (int x = 0; x < height; x++) {
for (int y = 0; y < width; y++) {
// get one pixel color
pixel = bmpOriginal.getPixel(y, x);

// retrieve color of all channels
R = Color.red(pixel);
G = Color.green(pixel);
B = Color.blue(pixel);
// take conversion up to one single value by calculating
// pixel intensity.
R = G = B = (int) (0.299 * R + 0.587 * G + 0.114 * B);
// set bit into bitset, by calculating the pixel's luma
if (R < 55) {
dots.set(k);//this is the bitset that i'm printing
}
k++;
}
}

} catch (Exception e) {
// TODO: handle exception
Log.e(TAG, e.toString());
}

}

然后我有一个名为打印机命令的类,如下。

public class PrinterCommands {
public static final byte[] INIT = {27, 64};
public static byte[] FEED_LINE = {10};

public static byte[] SELECT_FONT_A = {27, 33, 0};

public static byte[] SET_BAR_CODE_HEIGHT = {29, 104, 100};
public static byte[] PRINT_BAR_CODE_1 = {29, 107, 2};
public static byte[] SEND_NULL_BYTE = {0x00};

public static byte[] SELECT_PRINT_SHEET = {0x1B, 0x63, 0x30, 0x02};
public static byte[] FEED_PAPER_AND_CUT = {0x1D, 0x56, 66, 0x00};

public static byte[] SELECT_CYRILLIC_CHARACTER_CODE_TABLE = {0x1B, 0x74, 0x11};

public static byte[] SELECT_BIT_IMAGE_MODE = {0x1B, 0x2A, 33, (byte)255, 3};
public static byte[] SET_LINE_SPACING_24 = {0x1B, 0x33, 24};
public static byte[] SET_LINE_SPACING_30 = {0x1B, 0x33, 30};

public static byte[] TRANSMIT_DLE_PRINTER_STATUS = {0x10, 0x04, 0x01};
public static byte[] TRANSMIT_DLE_OFFLINE_PRINTER_STATUS = {0x10, 0x04, 0x02};
public static byte[] TRANSMIT_DLE_ERROR_STATUS = {0x10, 0x04, 0x03};
public static byte[] TRANSMIT_DLE_ROLL_PAPER_SENSOR_STATUS = {0x10, 0x04, 0x04};
}

我发现打印时重要的地方 public static byte[] SELECT_BIT_IMAGE_MODE = {0x1B, 0x2A, 33, (byte)255, 3}; 。我尝试使用不同宽度像素大小的图像来更改它。 mOutputStream = mSocket.getOutputStream();意味着我正在从蓝牙套接字(即蓝牙套接字 mSocket)获取输出流。

最佳答案

使用下面的代码使用蓝牙 pos/esc 热敏打印机打印图像

创建以下方法。

public void printPhoto(int img) {
try {
Bitmap bmp = BitmapFactory.decodeResource(getResources(),
img);
if(bmp!=null){
byte[] command = Utils.decodeBitmap(bmp);
outputStream.write(PrinterCommands.ESC_ALIGN_CENTER);
printText(command);
}else{
Log.e("Print Photo error", "the file isn't exists");
}
} catch (Exception e) {
e.printStackTrace();
Log.e("PrintTools", "the file isn't exists");
}
}

使用这样的方法。

printPhoto(R.drawable.demoimage);

printText() 方法如下所示

private void printText(byte[] msg) {
try {
// Print normal text
outputStream.write(msg);
printNewLine();
} catch (IOException e) {
e.printStackTrace();
}
}

printNewLine() 如下所示

private void printNewLine() {
try {
outputStream.write(PrinterCommands.FEED_LINE);
} catch (IOException e) {
e.printStackTrace();
}

}

关于java - 在 POS/ESC 热敏打印机 Android 上打印图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40693036/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com