2013-03-21 158 views
5

我正在開發一個應用程序,我必須打印一張收據,收據有一個徽標(靜態)圖像視圖,如何將此打印到藍牙打印機?並且我通過使用GestureOverlayView拍了簽名,現在我必須打印該手勢以及徽標和有關收據的一些數據。 enter image description here如何使用打印機(通過藍牙打印)從Android設備打印圖像和一些數據?

,我需要打印一個阿拉伯語的字符串爲好。這在文本視圖中顯示。 顯示簽名我在我的佈局中使用圖像視圖。請檢查圖像, 我附上我必須打印的圖像,請給我一些打印它的想法。

我可以改變的格式打印,意味着我沒有在矩形打印數據,但圖像定位是主要的問題,我怎麼會去了解比對?

回答

9

嘗試使用這個....

public class BluetoothPrinterActivity extends Activity { 

BluetoothAdapter mBTAdapter; 
BluetoothSocket mBTSocket = null; 
Dialog dialogProgress; 
String BILL, TRANS_ID; 
String PRINTER_MAC_ID = "00:1F:B7:02:8F:44"; 
final String ERROR_MESSAGE = "There has been an error in printing the bill."; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    try { 

     BILL = "\nSale Slip No: 12345678" + " " + "04-08-2011\n"; 
     BILL = BILL + "----------------------------------------"; 
     BILL = BILL + "\n\n"; 
     BILL = BILL + "Total Qty:" + " " + "2.0\n"; 
     BILL = BILL + "Total Value:" + " " + "17625.0\n"; 
     BILL = BILL + "-----------------------------------------"; 

     mBTAdapter = BluetoothAdapter.getDefaultAdapter(); 

     if (mBTAdapter == null) { 
      Toast.makeText(this, "Device has no bluetooth capability",Toast.LENGTH_LONG).show(); 
      finish(); 
     } else { 
      if (!mBTAdapter.isEnabled()) { 
       Intent i = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); 
       startActivityForResult(i, 0); 
      } 

      // Register the BroadcastReceiver 
      IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND); 
      registerReceiver(mReceiver, filter); // Don't forget to unregister during onDestroy 

      dialogProgress = new Dialog(BluetoothPrinterActivity.this); 
      dialogProgress.setTitle("Finding printer..."); 
      dialogProgress.setOnDismissListener(new DialogInterface.OnDismissListener() { 
         public void onDismiss(DialogInterface dialog) { 
          dialog.dismiss(); 
          setResult(RESULT_CANCELED); 
          finish(); 
         } 
        }); 
      dialogProgress.show(); 

     } 

     if (mBTAdapter.isDiscovering()) 
      mBTAdapter.cancelDiscovery(); 
     else 
      mBTAdapter.startDiscovery(); 

     System.out.println("BT Searching status :" + mBTAdapter.isDiscovering()); 

    } catch (Exception e) { 
     Log.e("Class ", "My Exe ", e); 
    } 
} 

private final BroadcastReceiver mReceiver = new BroadcastReceiver() { 
    public void onReceive(Context context, Intent intent) { 
     try { 
      String action = intent.getAction(); 
      // When discovery finds a device 
      if (BluetoothDevice.ACTION_FOUND.equals(action)) { 
       // Get the BluetoothDevice object from the Intent 
       BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); 

       System.out.println("***" + device.getName() + " : "+ device.getAddress()); 

       if (device.getAddress().equalsIgnoreCase(PRINTER_MAC_ID)) { 
        mBTAdapter.cancelDiscovery(); 
        dialogProgress.dismiss(); 
        Toast.makeText(BluetoothPrinterActivity.this,device.getName() + " Printing data",Toast.LENGTH_LONG).show(); 
        printBillToDevice(PRINTER_MAC_ID); 
        Toast.makeText(BluetoothPrinterActivity.this,device.getName() + " found", Toast.LENGTH_LONG).show(); 
       } 
      } 
     } catch (Exception e) { 
      Log.e("Class ", "My Exe ", e); 
     } 
    } 
}; 


@Override 
protected void onDestroy() { 
    super.onDestroy(); 
    try { 
     if (dialogProgress != null) 
      dialogProgress.dismiss(); 
     if (mBTAdapter != null) 
      mBTAdapter.cancelDiscovery(); 
     this.unregisterReceiver(mReceiver); 
    } catch (Exception e) { 
     Log.e("Class ", "My Exe ", e); 
    } 
} 


@Override 
public void onBackPressed() { 
    try { 
     if (mBTAdapter != null) 
      mBTAdapter.cancelDiscovery(); 
      this.unregisterReceiver(mReceiver); 
    } catch (Exception e) { 
     Log.e("Class ", "My Exe ", e); 
    } 
    setResult(RESULT_CANCELED); 
    finish(); 
} 


public void printBillToDevice(final String address) { 
    new Thread(new Runnable() { 
     public void run() { 
      runOnUiThread(new Runnable() { 
       public void run() { 
        dialogProgress.setTitle("Connecting..."); 
        dialogProgress.show(); 
       } 

      }); 

      mBTAdapter.cancelDiscovery(); 

      try { 
       System.out.println("**************************#****connecting"); 
       BluetoothDevice mdevice = mBTAdapter.getRemoteDevice(address); 
       Method m = mdevice.getClass().getMethod("createRfcommSocket", new Class[] { int.class }); 
       mBTSocket = (BluetoothSocket) m.invoke(mdevice, 1); 

       mBTSocket.connect(); 
       OutputStream os = mBTSocket.getOutputStream(); 
       os.flush(); 

       os.write(BILL.getBytes()); 
       System.out.println(BILL); 

       setResult(RESULT_OK); 
       finish(); 
      } catch (Exception e) { 
       Log.e("Class ", "My Exe ", e); 
       e.printStackTrace(); 
       setResult(RESULT_CANCELED); 
       finish(); 

      } 

      runOnUiThread(new Runnable() { 
       public void run() { 
        try { 
         dialogProgress.dismiss(); 
        } catch (Exception e) { 
         Log.e("Class ", "My Exe ", e); 
        } 
       } 

      }); 

     } 

    }).start(); 
    } 
} 

從這個鏈接Bluetooth Printer issue in android

+0

我已經使用這個代碼,它的工作罰款...但它不會打印ar bic文本,所以如果你知道,那麼你能幫我 – CoronaPintu 2013-07-02 04:21:48

+0

嗨pintu。我havnt解決這個問題呢.. – UdiT 2013-08-08 07:23:50

3

我盡我所能來給出答案之前,你可以得到的解決方案,從已經問questions

你有3個選項,從Android應用程序打印

1>的SDK /庫:(如starmicronics,它僅限於少數設備)

2>谷歌Play應用:(直接調用的意圖thirparty apps

3>Google雲打印:(推薦。它很容易使用並集成到應用程序中) 通過此連接,我們可以連接任何打印機,如經典打印機,雲打印機。

使用谷歌的打印作爲用戶的角度用戶應該啓用谷歌打印服務Gmail帳戶,Google cloud print used in many places!

設置谷歌的打印服務

Blog

https://stackoverflow.com/questions/11323805/how-to-setup-network-printer-to-google-cloud-print/14911180#14911180

Google cloud print set up1

Google cloud print set up2

Printing via gchrome

Google cloud printers

整合雲打印機應用:

在Android中有沒有選項Airprint像其他平臺,但谷歌作出真棒雲打印選項,這樣任何打印機都可以使用移動設備的打印選項。

樣品代碼:

funcode

Google cloud print code