Advertisement
androlizer

Untitled

Sep 1st, 2014
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.83 KB | None | 0 0
  1. public static void PrinterReceipt(Context context, String portname, RelativeLayout layout){
  2.  
  3.         String portName = portname; //ip address of your printer
  4.         String portSettings = "";
  5.  
  6.         //have to measure the layout for it to print correctly, otherwise sizes are zero
  7.         layout.measure(View.MeasureSpec.makeMeasureSpec(layout.getLayoutParams().width, View.MeasureSpec.EXACTLY),
  8.                 View.MeasureSpec.makeMeasureSpec(layout.getLayoutParams().height, View.MeasureSpec.EXACTLY));
  9.         layout.layout(0, 0, layout.getMeasuredWidth(), layout.getMeasuredHeight());
  10.  
  11.         Bitmap bitmap = Bitmap.createBitmap(layout.getWidth(),layout.getHeight(), Bitmap.Config.RGB_565);
  12.         Canvas canvas = new Canvas(bitmap);
  13.         canvas.drawColor(Color.WHITE);
  14.         layout.draw(canvas);
  15.  
  16.  
  17.         int maxWidth = 576; //default width of tsp100 receipt
  18.         RasterDocument rasterDoc = new RasterDocument(RasterDocument.RasSpeed.Full, RasterDocument.RasPageEndMode.FeedAndFullCut, RasterDocument.RasPageEndMode.FeedAndFullCut, RasterDocument.RasTopMargin.Standard, 0, 0, 0);
  19.         StarBitmap starbitmap = new StarBitmap(bitmap, false, maxWidth);
  20.  
  21.         StarIOPort port = null;
  22.         try {
  23.             /*
  24.                        using StarIOPort3.1.jar (support USB Port)
  25.                        Android OS Version: upper 2.2
  26.              */
  27.             port = StarIOPort.getPort(portName, portSettings, 10000, context);
  28.             /*
  29.                        using StarIOPort.jar
  30.                        Android OS Version: under 2.1
  31.                        port = StarIOPort.getPort(portName, portSettings, 10000);
  32.              */
  33.  
  34.             try {
  35.                 Thread.sleep(500);
  36.             }
  37.             catch(InterruptedException e) {}
  38.  
  39.             byte[] command = rasterDoc.BeginDocumentCommandData();
  40.             port.writePort(command, 0, command.length);
  41.             command = starbitmap.getImageRasterDataForPrinting(true);
  42.             port.writePort(command, 0, command.length);
  43.             command = rasterDoc.EndDocumentCommandData();
  44.             port.writePort(command, 0, command.length);
  45.  
  46.             try {
  47.                 Thread.sleep(1000);
  48.             }
  49.             catch(InterruptedException e) {
  50.                 e.printStackTrace();
  51.             }
  52.         } catch (StarIOPortException e) {
  53.             ShowAlertMessage(context, "Failure", "Failed to connect to printer. " + e.getMessage());
  54.         }
  55.         finally {
  56.             if(port != null) {
  57.                 try {
  58.                     StarIOPort.releasePort(port);
  59.                 } catch (StarIOPortException e) {
  60.                     e.printStackTrace();
  61.                 }
  62.             }
  63.         }
  64.     }
  65.  
  66.     private static void ShowAlertMessage(final Context context, final String alertTitle, final String message){
  67.         try {
  68.             ((Activity)context).runOnUiThread(new Runnable() {
  69.  
  70.                 @Override
  71.                 public void run() {
  72.                     AlertDialog.Builder dialog = new AlertDialog.Builder(context);
  73.                     dialog.setNegativeButton("Ok", null);
  74.                     AlertDialog alert = dialog.create();
  75.                     alert.setTitle(alertTitle);
  76.                     alert.setMessage(message);
  77.                     alert.show();
  78.                 }});
  79.         } catch (final Exception e) {
  80.             Log.e("PrintReceipt", e.getMessage());
  81.         }
  82.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement