您好我是Android新手我花了很多时间试图在我的应用中打印收据。我使用 Zicox XT4131 热敏打印机通过蓝牙连接到运行 Android 4.4操作系统的平板电脑。
到目前为止,我已经成功地只能在收据上打印文本,现在我的下一个障碍是尝试在收据上打印条形码,qrcodes和徽标,我花了很多时间在互联网上试图找到解决方案但似乎没什么用。
然而,我学到的是要打印位图图像,我首先需要将其转换为字节数组(我做过)并将其写入输出流(我也做过),所有我得到的是打印机上的长空格或一些奇怪的字符。
现在我拒绝相信从平板电脑到打印机的2016年打印没有简单的解决方案,所以我只假设我这样做的方式不对。
我的应用程序的工作方式很简单,在我的活动中,我有一个购买预览,有一个按钮,点击后发送命令到蓝牙打印机打印。这就是全部,没有转换为pdf,什么不是,必须选择打印机。
所以我想知道的是,从我的平板电脑到我的蓝牙打印机正确打印收据的方式究竟是什么,以及我如何使用这种方法打印条形码,qrcode或徽标以及文本在收据上?
我不希望将其作为主题投票,因为这可能要求我放弃这个项目(所有这些都是因为打印)我只想要一个可行的高效解决方案来帮助我做我想做的事。
这是我现在打印的代码:
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.content.Intent;
import android.os.Handler;
import android.view.View;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.nio.ByteBuffer;
import java.util.EnumMap;
import java.util.GregorianCalendar;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
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;
这些是它的功能:
// this will find a bluetooth printer device
void findBT() {
try {
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if(mBluetoothAdapter == null) {
Toast.makeText(PurchaseSummaryActivity.this, "No bluetooth adapter available", Toast.LENGTH_SHORT).show();
}
if(!mBluetoothAdapter.isEnabled()) {
Intent enableBluetooth = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBluetooth, 0);
}
Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
if(pairedDevices.size() > 0) {
for (BluetoothDevice device : pairedDevices) {
// RPP300 is the name of the bluetooth printer device
// we got this name from the list of paired devices
if (device.getName().equals("XT4131A")) {
mDevice = device;
break;
}
}
}
Toast.makeText(PurchaseSummaryActivity.this, "Bluetooth device found.", Toast.LENGTH_SHORT).show();
}catch(Exception e){
e.printStackTrace();
}
}
// tries to open a connection to the bluetooth printer device
void openBT() throws IOException {
try {
// Standard SerialPortService ID
UUID uuid = UUID.fromString("00001101-0000-1000-8000-00805f9b34fb");
mSocket = mDevice.createRfcommSocketToServiceRecord(uuid);
mSocket.connect();
mOutputStream = mSocket.getOutputStream();
mInputStream = mSocket.getInputStream();
beginListenForData();
Toast.makeText(PurchaseSummaryActivity.this, "Bluetooth Connection Opened", Toast.LENGTH_SHORT).show();
} catch (Exception e) {
e.printStackTrace();
}
}
/*
* after opening a connection to bluetooth printer device,
* we have to listen and check if a data were sent to be printed.
*/
void beginListenForData() {
try {
final Handler handler = new Handler();
// this is the ASCII code for a newline character
final byte delimiter = 10;
stopWorker = false;
readBufferPosition = 0;
readBuffer = new byte[1024];
workerThread = new Thread(new Runnable() {
public void run() {
while (!Thread.currentThread().isInterrupted() && !stopWorker) {
try {
int bytesAvailable = mInputStream.available();
if (bytesAvailable > 0) {
byte[] packetBytes = new byte[bytesAvailable];
mInputStream.read(packetBytes);
for (int i = 0; i < bytesAvailable; i++) {
byte b = packetBytes[i];
if (b == delimiter) {
byte[] encodedBytes = new byte[readBufferPosition];
System.arraycopy(
readBuffer, 0,
encodedBytes, 0,
encodedBytes.length
);
// specify US-ASCII encoding
final String data = new String(encodedBytes, "US-ASCII");
readBufferPosition = 0;
// tell the user data were sent to bluetooth printer device
handler.post(new Runnable() {
public void run() {
Toast.makeText(PurchaseSummaryActivity.this, data, Toast.LENGTH_SHORT).show();
}
});
} else {
readBuffer[readBufferPosition++] = b;
}
}
}
} catch (IOException ex) {
stopWorker = true;
}
}
}
});
workerThread.start();
} catch (Exception e) {
e.printStackTrace();
}
}
// this will send text data to be printed by the bluetooth printer
void printTickets(Event eo, Cart.Ticket to) throws IOException {
try {
byte[] arrayOfByte1 = { 27, 33, 0 };
byte[] format = { 27, 33, 0 };
// Bold
format[2] = ((byte)(0x8 | arrayOfByte1[2]));
// Height
format[2] = ((byte)(0x10 | arrayOfByte1[2]));
// Width
format[2] = ((byte) (0x20 | arrayOfByte1[2]));
byte[] center = new byte[]{ 0x1b, 0x61, 0x01 };
// the text typed by the user
StringBuilder msg = new StringBuilder();
msg.append("\n");
msg.append("\n");
msg.append("\n myTicketGH");
mOutputStream.write(center);
mOutputStream.write(format);
mOutputStream.write(msg.toString().getBytes());
// Bold
format[2] = ((byte)(0x8 | arrayOfByte1[2]));
msg = new StringBuilder();
msg.append("\n Address: No 10, Otublohum street");
msg.append("\n North Industrial Area");
msg.append("\n Tel.: 0266 8888 64");
msg.append("\n");
msg.append("\n");
msg.append("\n");
mOutputStream.write(center);
mOutputStream.write(format);
mOutputStream.write(msg.toString().getBytes());
// Bold
format[2] = ((byte)(0x8 | arrayOfByte1[2]));
byte[] left = new byte[]{ 0x1b, 0x61, 0x00 };
msg = new StringBuilder();
msg.append("\n Event");
mOutputStream.write(format);
mOutputStream.write(left);
mOutputStream.write(msg.toString().getBytes());
mOutputStream.write(arrayOfByte1);
left = new byte[]{ 0x1b, 0x61, 0x00 };
msg = new StringBuilder();
msg.append("\n Name : " + eo.getName());
msg.append("\n Date : " + eo.getDate());
msg.append("\n Time : " + eo.getTime());
msg.append("\n Venue : " + eo.getVenue());
msg.append("\n");
msg.append("\n");
mOutputStream.write(arrayOfByte1);
mOutputStream.write(left);
mOutputStream.write(msg.toString().getBytes());
// Bold
format[2] = ((byte)(0x8 | arrayOfByte1[2]));
msg = new StringBuilder();
msg.append("\n Ticket");
mOutputStream.write(format);
mOutputStream.write(left);
mOutputStream.write(msg.toString().getBytes());
mOutputStream.write(arrayOfByte1);
left = new byte[]{ 0x1b, 0x61, 0x00 };
msg = new StringBuilder();
msg.append("\n Code : " + to.getCode());
msg.append("\n Class : " + to.getTclass() );
msg.append("\n Fee : " + to.getPrice());
msg.append("\n Seat : " + "N / A");
msg.append("\n Date of Purchase : " + Calendar.getInstance().getTime().toString());
msg.append("\n");
msg.append("\n");
msg.append("\n");
msg.append("\n");
msg.append("\n");
mOutputStream.write(arrayOfByte1);
mOutputStream.write(left);
mOutputStream.write(msg.toString().getBytes());
// barcode data
String barcode_data = "123456";
// barcode image
byte[] barCodeBytes;
String barCodeHex = "";
try {
barCode = encodeAsBitmap(barcode_data, BarcodeFormat.CODE_128, 300, 40);
barCode = bitmapToGrayscale(barCode);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
barCodeBytes = stream.toByteArray();
mOutputStream.write(barCodeBytes);
} catch (WriterException e) {
e.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
}
}
// close the connection to bluetooth printer.
void closeBT() throws IOException {
try {
stopWorker = true;
mOutputStream.close();
mInputStream.close();
mSocket.close();
// myLabel.setText("Bluetooth Closed");
} catch (Exception e) {
e.printStackTrace();
}
}
这是我的onClickEvent处理程序:
this.buttonNextView.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
try {
findBT();
openBT();
for (int i = 0; i < ta.length(); i++)
{
try {
printTickets(eo, new Cart(). new Ticket(
ta.getJSONObject(i).getInt("id"),
ta.getJSONObject(i).getString("code"),
ta.getJSONObject(i).getString("class"),
ta.getJSONObject(i).getString("description"),
Float.parseFloat(ta.getJSONObject(i).getString("price")),
ta.getJSONObject(i).getInt("quantity")
));
} catch (JSONException e) {
e.printStackTrace();
}
}
closeBT();
} catch (IOException ex) {
ex.printStackTrace();
}
}
});
是的,我使用的打印机能够打印条形码和二维码。