2013-03-21 55 views
5

Tôi đang phát triển một ứng dụng trong đó tôi phải in một biên nhận, biên nhận có một hình ảnh (tĩnh) hình ảnh xem, làm thế nào tôi có thể in này vào máy in bluetooth? và tôi cũng đã có chữ ký bằng cách sử dụng GestureOverlayView, bây giờ tôi phải in cử chỉ đó cùng với logo và một số dữ liệu liên quan đến biên lai. enter image description hereCách in hình ảnh và một số dữ liệu từ thiết bị Android, sử dụng máy in (in qua bluetooth)?

và tôi cũng cần in một chuỗi arabic. được hiển thị trong TEXT VIEW. để hiển thị chữ ký tôi đang sử dụng chế độ xem hình ảnh trong bố cục của tôi. Vui lòng kiểm tra hình ảnh, tôi đang đính kèm hình ảnh mà tôi phải in, xin vui lòng cho tôi một số ý tưởng về in nó.

tôi có thể thay đổi định dạng trong in, nghĩa là tôi không phải in dữ liệu trong hình chữ nhật, nhưng căn chỉnh hình ảnh là vấn đề chính, làm cách nào tôi biết về căn chỉnh?

Trả lời

9

Hãy thử sử dụng này một ....

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

từ liên kết này Bluetooth Printer issue in android

+0

tôi đã sử dụng mã này nó làm việc tốt ... nhưng nó sẽ không in ar bic văn bản vì vậy nếu bạn biết sau đó bạn có thể giúp tôi – CoronaPintu

+0

hi pintu. tôi chưa giải quyết được vấn đề này .. – UdiT

3

tôi cố gắng hết sức để cung cấp cho các câu trả lời trước khi bạn có thể có được giải pháp từ đã hỏi questions

bạn có 3 lựa chọn để in từ ứng dụng android

1>SDK/Libraries: (như starmicronics, nó giới hạn ở vài thiết bị)

2>Google play Apps: (trực tiếp kêu gọi sự chú ý và thirparty apps)

3>Google cloud print: (được khuyến nghị. Thật dễ dàng để sử dụng và tích hợp vào một ứng dụng) Bằng cách này, chúng tôi kết nối mọi máy in như máy in cổ điển, máy in Cloud Print.

đã sử dụng Google in như người dùng góc độ người dùng nên kích hoạt dịch vụ in google để tài khoản Gmail, Google cloud print used in many places!

Thiết lập dịch vụ in ấn google:

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

Tích hợp máy in Cloud App:

Trong Android không có lựa chọn cho Airprint như các nền tảng khác, nhưng Google đã tùy chọn in ấn đám mây tuyệt vời cho rằng đó bất kỳ máy in nào cũng có thể sử dụng tùy chọn in từ thiết bị di động.

mã mẫu:

funcode

Google cloud print code

Các vấn đề liên quan