2016-02-26 16 views
-7

我有一个内置打印机和扫描仪的Android平板电脑。 现在我的任务是打印一个通行证。门票布局包含许多文本字段和编辑文本。我需要我的平板电脑打印机来打印整个页面。我需要java中的代码。从具有内置打印机的平板电脑打印完整的图形布局android

我想知道是否可以在没有任何PDF或蓝牙的情况下直接打印。因为我可以使用打印按钮只打印一个字段,因为我拥有关于该打印机的所有sdk。现在,我的问题是我想打印整个布局。

+1

'。我需要在java.'中的代码:s – Dhina

+0

您的打印机有蓝牙? –

+0

你可以尝试一些逻辑,比如当你填充数据然后点击打印,你可以尝试多种方式,比如先把你的页面保存为pdf然后打印出来 –

回答

0

试试这个代码,希望它帮助你

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(); 
}} 

打开BT方法

// 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()方法可以工作。

打开蓝牙打印机设备连接 后,我们必须聆听并检查是否发送了数据以便打印。

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(); 
}} 

我们将为“发送”按钮设置一个onClickListener。将下面的代码放在onCreate()方法内的“Open”按钮的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(); 
    } 
}}); 

的SendData()方法,使“打开”按钮将工作。把它放在beginListenForData()方法的代码块下面。

// 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(); 
}} 

我们将编写一个onClickListener的“关闭”按钮,所以我们可以关闭蓝牙打印机的连接和节省电池。将下面的代码放在onCreate()方法内的“Send”按钮的onClickListener后面。

// close bluetooth connection 
closeButton.setOnClickListener(new View.OnClickListener() { 
public void onClick(View v) { 
    try { 
     closeBT(); 
    } catch (IOException ex) { 
     ex.printStackTrace(); 
    } 
}}); 

closeBT()在步骤12中方法将无法使用以下代码工作。把它放在sendData()方法代码块下面。

// 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(); 
}} 

确保蓝牙权限被添加到您的清单文件。它位于manifest/AndroidManifest.xml中,里面的代码应该如下所示。

<uses-permission android:name="android.permission.BLUETOOTH" /> 
+0

感谢代码,但我们不想使用蓝牙,而是我们必须使用打印机内置在平板电脑中。 设备信息:内置热敏2英寸打印机和指纹扫描仪的Android平板电脑。 – Mounika

+0

检查此链接http://stackoverflow.com/questions/20717189/how-to-print-pdf-using-android-4-4-printing-framework/20719729#20719729 –

+0

非常感谢!我会看看 – Mounika