先看效果 扫描内容是下面这张,二维码是用zxing库生成的 由于改了好几个类,还是去年的事都忘得差不多了,所以只能上这个类的代码了,主要就是改了这个CaptureActivity.java 1 package com.zxing.activity; 2 3 imp ...
先看效果
扫描内容是下面这张,二维码是用zxing库生成的
由于改了好几个类,还是去年的事都忘得差不多了,所以只能上这个类的代码了,主要就是改了这个CaptureActivity.java
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.Handler; 14 import android.os.Vibrator; 15 import android.view.SurfaceHolder; 16 import android.view.SurfaceHolder.Callback; 17 import android.view.SurfaceView; 18 import android.widget.Toast; 19 20 import com.ericssonlabs.R; 21 import com.google.zxing.BarcodeFormat; 22 import com.google.zxing.Result; 23 import com.zxing.camera.CameraManager; 24 import com.zxing.decoding.CaptureActivityHandler; 25 import com.zxing.decoding.CaptureActivityHandler.DecodeCallback; 26 import com.zxing.decoding.InactivityTimer; 27 import com.zxing.view.ViewfinderView; 28 /** 29 * Initial the camera 30 * @author Ryan.Tang 31 * @modifier Lemon 32 * @use extends CaptureActivity并且在setContentView方法后调用init方法 33 */ 34 public abstract class CaptureActivity extends Activity implements Callback, DecodeCallback { 35 // private static final String TAG = "CaptureActivity"; 36 37 protected Activity context; 38 protected SurfaceView surfaceView; 39 protected ViewfinderView viewfinderView; 40 /**初始化,必须在setContentView之后 41 * @param context 42 * @param viewfinderView 43 */ 44 protected void init(Activity context, SurfaceView surfaceView, ViewfinderView viewfinderView) { 45 this.context = context; 46 this.surfaceView = surfaceView; 47 this.viewfinderView = viewfinderView; 48 49 CameraManager.init(getApplication()); 50 51 hasSurface = false; 52 inactivityTimer = new InactivityTimer(this); 53 } 54 55 56 private CaptureActivityHandler handler; 57 private boolean hasSurface; 58 private Vector<BarcodeFormat> decodeFormats; 59 private String characterSet; 60 private InactivityTimer inactivityTimer; 61 private MediaPlayer mediaPlayer; 62 private boolean playBeep; 63 private static final float BEEP_VOLUME = 0.10f; 64 private boolean vibrate; 65 66 67 @Override 68 protected void onResume() { 69 super.onResume(); 70 SurfaceHolder surfaceHolder = surfaceView.getHolder(); 71 if (hasSurface) { 72 initCamera(surfaceHolder); 73 } else { 74 surfaceHolder.addCallback(this); 75 surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); 76 } 77 decodeFormats = null; 78 characterSet = null; 79 80 playBeep = true; 81 AudioManager audioService = (AudioManager) getSystemService(AUDIO_SERVICE); 82 if (audioService.getRingerMode() != AudioManager.RINGER_MODE_NORMAL) { 83 playBeep = false; 84 } 85 initBeepSound(); 86 vibrate = true; 87 } 88 89 @Override 90 protected void onPause() { 91 super.onPause(); 92 if (handler != null) { 93 handler.quitSynchronously(); 94 handler = null; 95 } 96 CameraManager.get().closeDriver(); 97 } 98 99 @Override100 protected void onDestroy() {101 inactivityTimer.shutdown();102 super.onDestroy();103 }104 105 106 public static final String RESULT_QRCODE_STRING = "RESULT_QRCODE_STRING";107 /**108 * Handler scan result109 * @param result110 * @param barcode111 */112 public void handleDecode(Result result, Bitmap barcode) {113 inactivityTimer.onActivity();114 playBeepSoundAndVibrate();115 String resultString = result.getText();116 //FIXME117 if (resultString.equals("")) {118 Toast.makeText(CaptureActivity.this, "Scan failed!", Toast.LENGTH_SHORT).show();119 }120 121 setResult(RESULT_OK, new Intent().putExtra(RESULT_QRCODE_STRING, resultString));122 finish();123 }124 125 private void initCamera(SurfaceHolder surfaceHolder) {126 try {127 CameraManager.get().openDriver(surfaceHolder);128 } catch (IOException ioe) {129 return;130 } catch (RuntimeException e) {131 return;132 }133 if (handler == null) {134 handler = new CaptureActivityHandler(this, decodeFormats,135 characterSet, viewfinderView, this);136 }137 }138 139 @Override140 public void drawViewfinder() {141 viewfinderView.drawViewfinder(); 142 }143 144 145 @Override146 public void surfaceChanged(SurfaceHolder holder, int format, int width,147 int height) {148 149 }150 151 @Override152 public void surfaceCreated(SurfaceHolder holder) {153 if (!hasSurface) {154 hasSurface = true;155 initCamera(holder);156 }157 158 }159 160 @Override161 public void surfaceDestroyed(SurfaceHolder holder) {162 hasSurface = false;163 164 }165 166 167 public Handler getHandler() {168 return handler;169 }170 171 172 private void initBeepSound() {173 if (playBeep && mediaPlayer == null) {174 // The volume on STREAM_SYSTEM is not adjustable, and users found it175 // too loud,176 // so we now play on the music stream.177 setVolumeControlStream(AudioManager.STREAM_MUSIC);178 mediaPlayer = new MediaPlayer();179 mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);180 mediaPlayer.setOnCompletionListener(beepListener);181 182 AssetFileDescriptor file = getResources().openRawResourceFd(183 R.raw.beep);184 try {185 mediaPlayer.setDataSource(file.getFileDescriptor(),186 file.getStartOffset(), file.getLength());187 file.close();188 mediaPlayer.setVolume(BEEP_VOLUME, BEEP_VOLUME);189 mediaPlayer.prepare();190 } catch (IOException e) {191 mediaPlayer = null;192 }193 }194 }195 196 private static final long VIBRATE_DURATION = 200L;197 198 private void playBeepSoundAndVibrate() {199 if (playBeep && mediaPlayer != null) {200 mediaPlayer.start();201 }202 if (vibrate) {203 Vibrator vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE);204 vibrator.vibrate(VIBRATE_DURATION);205 }206 }207 208 /**209 * When the beep has finished playing, rewind to queue up another one.210 */211 private final OnCompletionListener beepListener = new OnCompletionListener() {212 public void onCompletion(MediaPlayer mediaPlayer) {213 mediaPlayer.seekTo(0);214 }215 };216 217 }
原标题:自定义ZXing二维码扫描界面并解决取景框拉伸等问题
关键词:
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们:
admin#shaoqun.com
(#换成@)。