Android手持系统PDA打印小票
生活随笔
收集整理的這篇文章主要介紹了
Android手持系统PDA打印小票
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
?這幾天有個朋友要在安卓系統的PDA上進行打印小票,PDA是智谷聯的3505,自帶打印小票功能,經過幾天的研究,把打印代碼分享出來,PDA打印的底層是調用第三方的DLL,是用C++寫的,安卓要調用C++的代碼進行打印。
?PDA上打印是使用串口進行打印的,首先要打開串口號,使用完要關閉端口。
?打印效果如下圖
打印代碼如下
?打印包括如下步驟,初始化端口,打開端口 ,打印小票,打印二維碼,關閉端口
package com.example.cangku;import java.text.SimpleDateFormat; import java.util.Hashtable;import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.util.EntityUtils;import com.example.application.MyApplication; import com.google.zxing.BarcodeFormat; import com.google.zxing.EncodeHintType; import com.google.zxing.WriterException; import com.google.zxing.common.BitMatrix; import com.google.zxing.qrcode.QRCodeWriter;import com.smartdevicesdk.printer.PrintService; import com.smartdevicesdk.printer.PrinterClassSerialPort3505; import com.smartdevicesdk.printer.PrinterCommand;import android.app.Activity; import android.content.Intent; import android.content.SharedPreferences; import android.graphics.Bitmap; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.view.View; import android.view.Window; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import android.widget.Toast;public class PrintBaoZhuangActivity extends Activity {private Button btnpringka;private String device = "/dev/ttyMT0";private int baudrate = 115200;PrinterClassSerialPort3505 printerClass = null;private String barCode,baozhuangcode;private String kehu,orderno,zhiling,xingti,pinming,guige,yanse,num,daiwei;private TextView guanbi,tvpkehu,tvporder,tvpzhiling,tvpxingti,tvppinming,tvpguige,tvpyanse,tvpnum,tvpdaiwei;private static final int RESULT_FROM_SERVER=1;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);requestWindowFeature(Window.FEATURE_NO_TITLE);setContentView(R.layout.printbaozhuang);baozhuangcode=getIntent().getStringExtra("baozhuangcode");barCode=getIntent().getStringExtra("code");kehu=getIntent().getStringExtra("kehu");orderno=getIntent().getStringExtra("orderno");zhiling=getIntent().getStringExtra("zhiling");xingti=getIntent().getStringExtra("xingti");pinming=getIntent().getStringExtra("pinming");guige=getIntent().getStringExtra("guige");yanse=getIntent().getStringExtra("yanse");num=getIntent().getStringExtra("num");daiwei=getIntent().getStringExtra("danwei");tvpkehu=(TextView) findViewById(R.id.tvpkehu);tvporder=(TextView) findViewById(R.id.tvporder);tvpzhiling=(TextView) findViewById(R.id.tvpzhiling);tvpxingti=(TextView) findViewById(R.id.tvpxingti);tvppinming=(TextView) findViewById(R.id.tvppinming);tvpguige=(TextView) findViewById(R.id.tvpguige);tvpyanse=(TextView) findViewById(R.id.tvpyanse);tvpnum=(TextView) findViewById(R.id.tvpnum);tvpdaiwei=(TextView) findViewById(R.id.tvpdaiwei);tvpkehu.setText(kehu);tvporder.setText(orderno);tvpzhiling.setText(zhiling);tvpxingti.setText(xingti);tvpyanse.setText(yanse);tvppinming.setText(pinming);tvpnum.setText(num);tvpguige.setText(guige);tvpdaiwei.setText(daiwei);guanbi=(TextView) findViewById(R.id.guanbi);guanbi.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {finish();}});//打印btnpringka=(Button) findViewById(R.id.btnpringka);printerClass = new PrinterClassSerialPort3505(device, baudrate, mhandler);btnpringka.setOnClickListener(new OnClickListener(){@Overridepublic void onClick(View arg0){// TODO Auto-generated method stubopenAndcloseDevice();printerClass.printText(GetPrintStr());createQRImage(baozhuangcode);System.gc();//printerClass.printText("******************************\n");if (printerClass.mSerialPort.isOpen){printerClass.close();}setResult(Activity.RESULT_OK); finish();} }); }//打開端口private void openAndcloseDevice() {if (printerClass.mSerialPort.isOpen) {printerClass.close();printerClass = new PrinterClassSerialPort3505(device, baudrate, mhandler);printerClass.device = device;printerClass.baudrate = baudrate;printerClass.open();//printerClass.write(new byte[] { 0x1b, 0x76 });printerClass.write(new byte[] { 0x1b, 0x4d, 0x01 });//設置字體printerClass.write(new byte[] { 0x1d, 0x21, 0x11 });//設置字體大小printerClass.write(new byte[] { 0x1b, 0x33, 0x10 });//深圳行間距} else {printerClass.device = device;printerClass.baudrate = baudrate;printerClass.open();//printerClass.write(new byte[] { 0x1b, 0x76 });printerClass.write(new byte[] { 0x1b, 0x4d, 0x01 });//設置字體printerClass.write(new byte[] { 0x1d, 0x21, 0x11 });//設置字體大小printerClass.write(new byte[] { 0x1b, 0x33, 0x10 });//深圳行間距}}//打印小票public String GetPrintStr() {/*kehu="愛都";pinming="56條網絡";zhiling="Z001";xingti="X001";guige="55英寸";num="20";orderno="PO-16-001";*/StringBuilder sb = new StringBuilder();sb.append("\n");//sb.append("*********************************\n");sb.append("客 戶:" +kehu+ "\n");sb.append("品 名:" + pinming + "\n");sb.append("指 令:" + zhiling + "\n");sb.append("型體號:" + xingti + "\n");sb.append("規 格:" + guige + "\n");sb.append("數 量:" + num +daiwei+"\n");sb.append("訂單號:" + orderno + "\n");sb.append("顏 色:" + yanse + "\n");sb.append(" "+baozhuangcode+"\n");//sb.append("------------------------------\r\n");return sb.toString();}//打印二維碼public void createQRImage(String url){try{int QR_WIDTH=250;int QR_HEIGHT=250;//判斷URL合法性if (url == null || "".equals(url) || url.length() < 1){return;}Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>();hints.put(EncodeHintType.CHARACTER_SET, "utf-8");//圖像數據轉換,使用了矩陣轉換BitMatrix bitMatrix = new QRCodeWriter().encode(url, BarcodeFormat.QR_CODE, QR_WIDTH, QR_HEIGHT, hints);int[] pixels = new int[QR_WIDTH * QR_HEIGHT];//下面這里按照二維碼的算法,逐個生成二維碼的圖片,//兩個for循環是圖片橫列掃描的結果for (int y = 0; y < QR_HEIGHT; y++){for (int x = 0; x < QR_WIDTH; x++){if (bitMatrix.get(x, y)){pixels[y * QR_WIDTH + x] = 0xff000000;}else{pixels[y * QR_WIDTH + x] = 0xffffffff;}}}//生成二維碼圖片的格式,使用ARGB_8888Bitmap bitmap = Bitmap.createBitmap(QR_WIDTH, QR_HEIGHT, Bitmap.Config.ARGB_8888);bitmap.setPixels(pixels, 0, QR_WIDTH, 0, 0, QR_WIDTH, QR_HEIGHT);//顯示到一個ImageView上面// imgQr.setImageBitmap(bitmap);printerClass.printImage(bitmap);//bitmap.recycle();}catch (WriterException e){e.printStackTrace();}}Handler mhandler = new Handler() {public void handleMessage(Message msg) {switch (msg.what) {case PrinterCommand.MESSAGE_READ:byte[] readBuf = (byte[]) msg.obj;//Log.i(TAG, "readBuf:" + readBuf[0]);if (readBuf[0] == 0x13) {//checkBoxAuto.setChecked(false);PrintService.isFUll = true;/*textViewState.setText(getResources().getString(R.string.str_printer_state)+ ":"+ getResources().getString(R.string.str_printer_bufferfull));*/} else if (readBuf[0] == 0x11) {PrintService.isFUll = false;/* textViewState.setText(getResources().getString(R.string.str_printer_state)+ ":"+ getResources().getString(R.string.str_printer_buffernull));*/} else if (readBuf[0] == 0x08) {/*checkBoxAuto.setChecked(false);textViewState.setText(getResources().getString(R.string.str_printer_state)+ ":"+ getResources().getString(R.string.str_printer_nopaper));*/} else if (readBuf[0] == 0x01) {/*textViewState.setText(getResources().getString(R.string.str_printer_state)+ ":"+ getResources().getString(R.string.str_printer_printing));*/} else if (readBuf[0] == 0x04) {/*checkBoxAuto.setChecked(false);textViewState.setText(getResources().getString(R.string.str_printer_state)+ ":"+ getResources().getString(R.string.str_printer_hightemperature));*/} else if (readBuf[0] == 0x02) {/*checkBoxAuto.setChecked(false);textViewState.setText(getResources().getString(R.string.str_printer_state)+ ":"+ getResources().getString(R.string.str_printer_lowpower));*/} else if (readBuf[0] == 0x00) {} else {String readMessage = new String(readBuf, 0, msg.arg1);if (readMessage.contains("800"))// 80mm paper{PrintService.imageWidth = 72;Toast.makeText(getApplicationContext(), "80mm",Toast.LENGTH_SHORT).show();} else if (readMessage.contains("580"))// 58mm paper{PrintService.imageWidth = 48;Toast.makeText(getApplicationContext(), "58mm",Toast.LENGTH_SHORT).show();}}break;case PrinterCommand.MESSAGE_STATE_CHANGE:switch (msg.arg1) {case PrinterCommand.STATE_CONNECTED:break;case PrinterCommand.STATE_CONNECTING:Toast.makeText(getApplicationContext(),"STATE_CONNECTING", Toast.LENGTH_SHORT).show();break;case PrinterCommand.STATE_LISTEN:case PrinterCommand.STATE_NONE:break;case PrinterCommand.SUCCESS_CONNECT:printerClass.write(new byte[] { 0x1b, 0x2b });Toast.makeText(getApplicationContext(),"SUCCESS_CONNECT", Toast.LENGTH_SHORT).show();break;case PrinterCommand.FAILED_CONNECT:Toast.makeText(getApplicationContext(),"FAILED_CONNECT", Toast.LENGTH_SHORT).show();break;case PrinterCommand.LOSE_CONNECT:Toast.makeText(getApplicationContext(), "LOSE_CONNECT",Toast.LENGTH_SHORT).show();}break;case PrinterCommand.MESSAGE_WRITE:break;case PrinterCommand.PERMIT_PRINTER:String result = (String) msg.obj;//Toast.makeText(getApplicationContext(),"允許打印", Toast.LENGTH_SHORT).show();break;case PrinterCommand.FORBID_PRINTER:String forbid_print = (String) msg.obj;Toast.makeText(getApplicationContext(),"禁止打印", Toast.LENGTH_SHORT).show();break;case PrinterCommand.TIMEOUT_PRINTER:String print_timeout = (String) msg.obj;Toast.makeText(getApplicationContext(),"打印超時", Toast.LENGTH_SHORT).show();break;}super.handleMessage(msg);}}; }總結
以上是生活随笔為你收集整理的Android手持系统PDA打印小票的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 某宝x-sign签名分析-店铺全量商品接
- 下一篇: 利用hilink 插座远程开电脑