Blame view

src/com/IDCard/ScanService.java 3.83 KB
c05c7a9b   黄灿宏   标准版本 1.扩展设备8 2....
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
  package com.IDCard;
  
  import android.app.Service;
  import android.content.Intent;
  import android.os.IBinder;
  import android.os.RemoteException;
  
  //import aidl.com.ectrip.scanbarcode_jp.IScan
  import com.ectrip.scanbarcode_jp.IScan;
  import com.hsm.barcode.DecodeResult;
  import com.hsm.barcode.Decoder;
  import com.hsm.barcode.DecoderConfigValues.SymbologyID;
  import com.hsm.barcode.DecoderException;
  import com.hsm.barcode.SymbologyConfig;
  
  public class ScanService extends Service {
  
  
  	@Override
  	public void onCreate() {
  		super.onCreate();
  	}
  
  	@Override
  	public IBinder onBind(Intent arg0) {
  		
  		return mAidlScan;
  	}
  	
  	
  	
  	private AidlScan mAidlScan = new AidlScan();
  	
  	/**
  	 * 实现具体的代码
  	 * @author Administrator
  	 *
  	 */
  	class AidlScan extends IScan.Stub {
  
  		private Decoder mDecoder ;
  		private DecodeResult mDecodeResult;
  		
  		private String barcode = null;
  		private boolean scanning = false ;
  		@Override
  		public void initEngine() throws RemoteException {
  			mDecoder = new Decoder();
  			try {
  				mDecoder.connectDecoderLibrary();
  				initPara();
  			} catch (DecoderException e) {
  				e.printStackTrace();
  			}
  			
  		}
  		
  		private void initPara(){
  			try {
  				mDecoder.enableSymbology(SymbologyID.SYM_QR);
  				mDecoder.enableSymbology(SymbologyID.SYM_PDF417);
  				mDecoder.enableSymbology(SymbologyID.SYM_EAN13);
  				
  //				mDecoder.enableSymbology(SymbologyID.SYM_DATAMATRIX);
  //				mDecoder.enableSymbology(SymbologyID.SYM_UPCA);
  //				mDecoder.enableSymbology(SymbologyID.SYM_CHINAPOST);
  				
  //				mDecoder.enableSymbology(SymbologyID.SYM_CODE39);
  //				mDecoder.enableSymbology(SymbologyID.SYM_CODE128);
  //				mDecoder.enableSymbology(SymbologyID.SYM_EAN8);
  //				mDecoder.enableSymbology(SymbologyID.SYM_CODE32);
  				//打开EAN13码校验
  				SymbologyConfig config = new SymbologyConfig(SymbologyID.SYM_EAN13);
  				config.Flags = 5 ;
  				config.Mask = 1 ;
  				mDecoder.setSymbologyConfig(config);
  				try {
  					mDecoder.startScanning();
  					Thread.sleep(50);
  					mDecoder.stopScanning();
  				} catch (Exception e) {
  					e.printStackTrace();
  				}
  				
  			} catch (DecoderException e) {
  				e.printStackTrace();
  			}
  		}
  
  		private Thread scanThread = null ;
  		@Override
  		public String scan() throws RemoteException {
  			barcode = null;
  			if(!scanning){
  				if(scanThread != null ){
  					//线程不为null强制中断
  					scanThread.interrupt() ;
  					scanThread = null ;
  				}
  				//创建单线程
  				scanThread = new Thread(scanRun);
  				scanThread.start() ;
  			}
  			return getBarcode();
  		}
  		
  		private Runnable scanRun = new Runnable() {
  			
  			@Override
  			public synchronized void run() {
  				if(mDecoder != null){
  					scanning = true ;
  					try {//ɨ��
  						boolean is = mDecoder.callbackKeepGoing();
  						Thread.sleep(50) ;
  						//扫描,超时为5秒
  						mDecoder.waitForDecodeTwo(5000, mDecodeResult);
  						
  						if(mDecoder.getBarcodeData() != null && mDecoder.getBarcodeLength() > 0){
  							barcode = mDecoder.getBarcodeData() ;
  							byte[] barByte = barcode.getBytes() ;
  						}
  						//减慢速度
  						Thread.sleep(50) ;
  						scanning = false ;
  					} catch (DecoderException e) {
  						try {
  							Thread.sleep(100) ;
  						} catch (InterruptedException e1) {
  							e1.printStackTrace();
  						}
  						e.printStackTrace();
  						
  						scanning = false ;
  					} catch (InterruptedException e) {
  						e.printStackTrace();
  					}
  				}
  				
  			}
  		};
  		
  		private String getBarcode(){
  			int count = 50;
  			while(count > 0){
  				count--;
  				if(barcode != null){
  					break;
  				}
  				try {
  					Thread.sleep(100);
  				} catch (InterruptedException e) {
  					e.printStackTrace();
  				}
  			}
  			return barcode;
  		}
  
  		@Override
  		public void close() throws RemoteException {
  			if(mDecoder != null){
  				try {
  					mDecoder.disconnectDecoderLibrary();
  				} catch (DecoderException e) {
  					e.printStackTrace();
  				}
  			}
  			
  		}
  		
  	}
  
  }