1+ package com .zxing .activity ;
2+
3+ import java .io .IOException ;
4+ import java .util .Vector ;
5+
6+ import android .app .Activity ;
7+ import android .content .Intent ;
8+ import android .content .res .AssetFileDescriptor ;
9+ import android .graphics .Bitmap ;
10+ import android .media .AudioManager ;
11+ import android .media .MediaPlayer ;
12+ import android .media .MediaPlayer .OnCompletionListener ;
13+ import android .os .Bundle ;
14+ import android .os .Handler ;
15+ import android .os .Vibrator ;
16+ import android .view .SurfaceHolder ;
17+ import android .view .SurfaceHolder .Callback ;
18+ import android .view .SurfaceView ;
19+ import android .view .Window ;
20+ import android .widget .Toast ;
21+
22+ import com .ericssonlabs .R ;
23+ import com .google .zxing .BarcodeFormat ;
24+ import com .google .zxing .Result ;
25+ import com .zxing .camera .CameraManager ;
26+ import com .zxing .decoding .CaptureActivityHandler ;
27+ import com .zxing .decoding .CaptureActivityHandler .DecodeCallback ;
28+ import com .zxing .decoding .InactivityTimer ;
29+ import com .zxing .view .ViewfinderView ;
30+ /**
31+ * Initial the camera
32+ * @author Ryan.Tang
33+ * @modifier Lemon
34+ * @use extends CaptureActivity并且在setContentView方法后调用init方法
35+ */
36+ public abstract class CaptureActivity extends Activity implements Callback , DecodeCallback {
37+ // private static final String TAG = "CaptureActivity";
38+
39+ protected Activity context ;
40+ protected SurfaceView surfaceView ;
41+ protected ViewfinderView viewfinderView ;
42+ /**初始化,必须在setContentView之后
43+ * @param context
44+ * @param viewfinderView
45+ */
46+ protected void init (Activity context , SurfaceView surfaceView , ViewfinderView viewfinderView ) {
47+ this .context = context ;
48+ this .surfaceView = surfaceView ;
49+ this .viewfinderView = viewfinderView ;
50+
51+ CameraManager .init (getApplication ());
52+
53+ hasSurface = false ;
54+ inactivityTimer = new InactivityTimer (this );
55+ }
56+
57+ private boolean isOn = false ;
58+ protected final boolean isOn () {
59+ return isOn ;
60+ }
61+ /**打开或关闭闪关灯
62+ * @param open
63+ */
64+ protected void switchLight (boolean on ) {
65+ if (on == isOn ()) {
66+ return ;
67+ }
68+ isOn = CameraManager .get ().switchLight (on );
69+ }
70+
71+
72+
73+ @ Override
74+ protected void onCreate (Bundle savedInstanceState ) {
75+ super .onCreate (savedInstanceState );
76+ requestWindowFeature (Window .FEATURE_NO_TITLE );
77+ }
78+
79+
80+ private CaptureActivityHandler handler ;
81+ private boolean hasSurface ;
82+ private Vector <BarcodeFormat > decodeFormats ;
83+ private String characterSet ;
84+ private InactivityTimer inactivityTimer ;
85+ private MediaPlayer mediaPlayer ;
86+ private boolean playBeep ;
87+ private static final float BEEP_VOLUME = 0.10f ;
88+ private boolean vibrate ;
89+
90+
91+ @ Override
92+ protected void onResume () {
93+ super .onResume ();
94+ SurfaceHolder surfaceHolder = surfaceView .getHolder ();
95+ if (hasSurface ) {
96+ initCamera (surfaceHolder );
97+ } else {
98+ surfaceHolder .addCallback (this );
99+ surfaceHolder .setType (SurfaceHolder .SURFACE_TYPE_PUSH_BUFFERS );
100+ }
101+ decodeFormats = null ;
102+ characterSet = null ;
103+
104+ playBeep = true ;
105+ AudioManager audioService = (AudioManager ) getSystemService (AUDIO_SERVICE );
106+ if (audioService .getRingerMode () != AudioManager .RINGER_MODE_NORMAL ) {
107+ playBeep = false ;
108+ }
109+ initBeepSound ();
110+ vibrate = true ;
111+ }
112+
113+ @ Override
114+ protected void onPause () {
115+ super .onPause ();
116+ if (handler != null ) {
117+ handler .quitSynchronously ();
118+ handler = null ;
119+ }
120+ isOn = false ;
121+ CameraManager .get ().closeDriver ();
122+ }
123+
124+
125+ @ Override
126+ protected void onDestroy () {
127+ inactivityTimer .shutdown ();
128+ super .onDestroy ();
129+ }
130+
131+
132+ public static final String RESULT_QRCODE_STRING = "RESULT_QRCODE_STRING" ;
133+ /**
134+ * Handler scan result
135+ * @param result
136+ * @param barcode
137+ */
138+ public void handleDecode (Result result , Bitmap barcode ) {
139+ inactivityTimer .onActivity ();
140+ playBeepSoundAndVibrate ();
141+ String resultString = result .getText ();
142+ //FIXME
143+ if (resultString .equals ("" )) {
144+ Toast .makeText (CaptureActivity .this , "Scan failed!" , Toast .LENGTH_SHORT ).show ();
145+ }
146+
147+ setResult (RESULT_OK , new Intent ().putExtra (RESULT_QRCODE_STRING , resultString ));
148+ finish ();
149+ }
150+
151+ private void initCamera (SurfaceHolder surfaceHolder ) {
152+ try {
153+ CameraManager .get ().openDriver (surfaceHolder );
154+ } catch (IOException ioe ) {
155+ return ;
156+ } catch (RuntimeException e ) {
157+ return ;
158+ }
159+ if (handler == null ) {
160+ handler = new CaptureActivityHandler (this , decodeFormats ,
161+ characterSet , viewfinderView , this );
162+ }
163+ }
164+
165+ @ Override
166+ public void drawViewfinder () {
167+ viewfinderView .drawViewfinder ();
168+ }
169+
170+
171+ @ Override
172+ public void surfaceChanged (SurfaceHolder holder , int format , int width ,
173+ int height ) {
174+
175+ }
176+
177+ @ Override
178+ public void surfaceCreated (SurfaceHolder holder ) {
179+ if (!hasSurface ) {
180+ hasSurface = true ;
181+ initCamera (holder );
182+ }
183+
184+ }
185+
186+ @ Override
187+ public void surfaceDestroyed (SurfaceHolder holder ) {
188+ hasSurface = false ;
189+
190+ }
191+
192+
193+ public Handler getHandler () {
194+ return handler ;
195+ }
196+
197+
198+ private void initBeepSound () {
199+ if (playBeep && mediaPlayer == null ) {
200+ // The volume on STREAM_SYSTEM is not adjustable, and users found it
201+ // too loud,
202+ // so we now play on the music stream.
203+ setVolumeControlStream (AudioManager .STREAM_MUSIC );
204+ mediaPlayer = new MediaPlayer ();
205+ mediaPlayer .setAudioStreamType (AudioManager .STREAM_MUSIC );
206+ mediaPlayer .setOnCompletionListener (beepListener );
207+
208+ AssetFileDescriptor file = getResources ().openRawResourceFd (
209+ R .raw .beep );
210+ try {
211+ mediaPlayer .setDataSource (file .getFileDescriptor (),
212+ file .getStartOffset (), file .getLength ());
213+ file .close ();
214+ mediaPlayer .setVolume (BEEP_VOLUME , BEEP_VOLUME );
215+ mediaPlayer .prepare ();
216+ } catch (IOException e ) {
217+ mediaPlayer = null ;
218+ }
219+ }
220+ }
221+
222+ private static final long VIBRATE_DURATION = 200L ;
223+
224+ private void playBeepSoundAndVibrate () {
225+ if (playBeep && mediaPlayer != null ) {
226+ mediaPlayer .start ();
227+ }
228+ if (vibrate ) {
229+ Vibrator vibrator = (Vibrator ) getSystemService (VIBRATOR_SERVICE );
230+ vibrator .vibrate (VIBRATE_DURATION );
231+ }
232+ }
233+
234+ /**
235+ * When the beep has finished playing, rewind to queue up another one.
236+ */
237+ private final OnCompletionListener beepListener = new OnCompletionListener () {
238+ public void onCompletion (MediaPlayer mediaPlayer ) {
239+ mediaPlayer .seekTo (0 );
240+ }
241+ };
242+
243+ }
0 commit comments