Java 从内置打印机的平板电脑打印完整的图形布局

Java 从内置打印机的平板电脑打印完整的图形布局,java,android,Java,Android,我有一台安卓平板电脑,内置打印机和扫描仪。 现在我的任务是打印一张通行证。闸门通行证布局包含许多文本字段和编辑文本。我需要我的平板电脑打印机来打印整个页面。我需要java代码 我想知道,如果打印可以直接完成没有任何PDF或蓝牙。因为我只能使用打印按钮打印单个字段,因为我有关于该打印机的所有sdk。现在,我的问题是我想打印整个版面。试试这段代码,希望它能帮助您 openButton.setOnClickListener(new View.OnClickListener() { public voi

我有一台安卓平板电脑,内置打印机和扫描仪。 现在我的任务是打印一张通行证。闸门通行证布局包含许多文本字段和编辑文本。我需要我的平板电脑打印机来打印整个页面。我需要java代码


我想知道,如果打印可以直接完成没有任何PDF或蓝牙。因为我只能使用打印按钮打印单个字段,因为我有关于该打印机的所有sdk。现在,我的问题是我想打印整个版面。

试试这段代码,希望它能帮助您

openButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
    try {
        findBT();
        openBT();
    } catch (IOException ex) {
        ex.printStackTrace();
    }
}});
查找BT方法

// this will find a bluetooth printer device


void findBT() {try {
    mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

    if(mBluetoothAdapter == null) {
        myLabel.setText("No bluetooth adapter available");
    }

    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("RPP300")) {
                mmDevice = device;
                break;
            }
        }
    }

    myLabel.setText("Bluetooth device found.");

}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");
    mmSocket = mmDevice.createRfcommSocketToServiceRecord(uuid);
    mmSocket.connect();
    mmOutputStream = mmSocket.getOutputStream();
    mmInputStream = mmSocket.getInputStream();

    beginListenForData();

    myLabel.setText("Bluetooth Opened");

} catch (Exception e) {
    e.printStackTrace();
}}
我们需要beginListenForData()方法,这样openBT()方法才能工作。

// send data typed by the user to be printed
sendButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
    try {
        sendData();
    } catch (IOException ex) {
        ex.printStackTrace();
    }
}});
// this will send text data to be printed by the bluetooth printer

void sendData() throws IOException {try {

    // the text typed by the user
    String msg = myTextbox.getText().toString();
    msg += "\n";

    mmOutputStream.write(msg.getBytes());

    // tell the user data were sent
    myLabel.setText("Data sent.");

} catch (Exception e) {
    e.printStackTrace();
}}
// close bluetooth connection
closeButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
    try {
        closeBT();
    } catch (IOException ex) {
        ex.printStackTrace();
    }
}});
// close the connection to bluetooth printer.
void closeBT() throws IOException {
try {
    stopWorker = true;
    mmOutputStream.close();
    mmInputStream.close();
    mmSocket.close();
    myLabel.setText("Bluetooth Closed");
} catch (Exception e) {
    e.printStackTrace();
}}
<uses-permission android:name="android.permission.BLUETOOTH" />
打开与蓝牙打印机设备的连接后, 我们必须倾听并检查是否发送了数据进行打印

    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 = mmInputStream.available();

                    if (bytesAvailable > 0) {

                        byte[] packetBytes = new byte[bytesAvailable];
                        mmInputStream.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() {
                                        myLabel.setText(data);
                                    }
                                });

                            } else {
                                readBuffer[readBufferPosition++] = b;
                            }
                        }
                    }

                } catch (IOException ex) {
                    stopWorker = true;
                }

            }
        }
    });

    workerThread.start();

} catch (Exception e) {
    e.printStackTrace();
}}
sendData()方法是“打开”按钮工作所必需的。将其放在beginListenForData()方法代码块下面。

// send data typed by the user to be printed
sendButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
    try {
        sendData();
    } catch (IOException ex) {
        ex.printStackTrace();
    }
}});
// this will send text data to be printed by the bluetooth printer

void sendData() throws IOException {try {

    // the text typed by the user
    String msg = myTextbox.getText().toString();
    msg += "\n";

    mmOutputStream.write(msg.getBytes());

    // tell the user data were sent
    myLabel.setText("Data sent.");

} catch (Exception e) {
    e.printStackTrace();
}}
// close bluetooth connection
closeButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
    try {
        closeBT();
    } catch (IOException ex) {
        ex.printStackTrace();
    }
}});
// close the connection to bluetooth printer.
void closeBT() throws IOException {
try {
    stopWorker = true;
    mmOutputStream.close();
    mmInputStream.close();
    mmSocket.close();
    myLabel.setText("Bluetooth Closed");
} catch (Exception e) {
    e.printStackTrace();
}}
<uses-permission android:name="android.permission.BLUETOOTH" />
我们将为“关闭”按钮编写一个onClickListener代码,这样我们就可以关闭与蓝牙打印机的连接并节省电池。将以下代码放在onCreate()方法中“发送”按钮的onClickListener之后。

// send data typed by the user to be printed
sendButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
    try {
        sendData();
    } catch (IOException ex) {
        ex.printStackTrace();
    }
}});
// this will send text data to be printed by the bluetooth printer

void sendData() throws IOException {try {

    // the text typed by the user
    String msg = myTextbox.getText().toString();
    msg += "\n";

    mmOutputStream.write(msg.getBytes());

    // tell the user data were sent
    myLabel.setText("Data sent.");

} catch (Exception e) {
    e.printStackTrace();
}}
// close bluetooth connection
closeButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
    try {
        closeBT();
    } catch (IOException ex) {
        ex.printStackTrace();
    }
}});
// close the connection to bluetooth printer.
void closeBT() throws IOException {
try {
    stopWorker = true;
    mmOutputStream.close();
    mmInputStream.close();
    mmSocket.close();
    myLabel.setText("Bluetooth Closed");
} catch (Exception e) {
    e.printStackTrace();
}}
<uses-permission android:name="android.permission.BLUETOOTH" />
如果没有以下代码,步骤12中的closeBT()方法将无法工作。将其放在sendData()方法代码块下面。

// send data typed by the user to be printed
sendButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
    try {
        sendData();
    } catch (IOException ex) {
        ex.printStackTrace();
    }
}});
// this will send text data to be printed by the bluetooth printer

void sendData() throws IOException {try {

    // the text typed by the user
    String msg = myTextbox.getText().toString();
    msg += "\n";

    mmOutputStream.write(msg.getBytes());

    // tell the user data were sent
    myLabel.setText("Data sent.");

} catch (Exception e) {
    e.printStackTrace();
}}
// close bluetooth connection
closeButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
    try {
        closeBT();
    } catch (IOException ex) {
        ex.printStackTrace();
    }
}});
// close the connection to bluetooth printer.
void closeBT() throws IOException {
try {
    stopWorker = true;
    mmOutputStream.close();
    mmInputStream.close();
    mmSocket.close();
    myLabel.setText("Bluetooth Closed");
} catch (Exception e) {
    e.printStackTrace();
}}
<uses-permission android:name="android.permission.BLUETOOTH" />
确保蓝牙权限已添加到清单文件中。它位于manifests/AndroidManifest.xml中,其中的代码如下所示。

// send data typed by the user to be printed
sendButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
    try {
        sendData();
    } catch (IOException ex) {
        ex.printStackTrace();
    }
}});
// this will send text data to be printed by the bluetooth printer

void sendData() throws IOException {try {

    // the text typed by the user
    String msg = myTextbox.getText().toString();
    msg += "\n";

    mmOutputStream.write(msg.getBytes());

    // tell the user data were sent
    myLabel.setText("Data sent.");

} catch (Exception e) {
    e.printStackTrace();
}}
// close bluetooth connection
closeButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
    try {
        closeBT();
    } catch (IOException ex) {
        ex.printStackTrace();
    }
}});
// close the connection to bluetooth printer.
void closeBT() throws IOException {
try {
    stopWorker = true;
    mmOutputStream.close();
    mmInputStream.close();
    mmSocket.close();
    myLabel.setText("Bluetooth Closed");
} catch (Exception e) {
    e.printStackTrace();
}}
<uses-permission android:name="android.permission.BLUETOOTH" />


。我需要java语言的代码。
:您的打印机是否有蓝牙?您可以尝试一些逻辑,例如在填充数据时单击打印,您可以尝试多种方法,例如首先将页面保存为pdf格式,然后打印。这意味着在“打印”按钮上,您可以打开包含所需图形数据的片段,然后打印我的打印机没有蓝牙。谢谢你的代码,但我们不想使用蓝牙,我们必须使用内置在平板电脑中的打印机。设备信息:安卓平板电脑,内置2英寸热敏打印机和指纹扫描仪。请查看此链接