編輯:關於Android編程
先給大家展示下效果圖:
掃描內容是下面這張,二維碼是用zxing庫生成的
由於改了好幾個類,還是去年的事都忘得差不多了,所以只能上這個類的代碼了,主要就是改了這個CaptureActivity.java
package com.zxing.activity; import java.io.IOException; import java.util.Vector; import android.app.Activity; import android.content.Intent; import android.content.res.AssetFileDescriptor; import android.graphics.Bitmap; import android.media.AudioManager; import android.media.MediaPlayer; import android.media.MediaPlayer.OnCompletionListener; import android.os.Handler; import android.os.Vibrator; import android.view.SurfaceHolder; import android.view.SurfaceHolder.Callback; import android.view.SurfaceView; import android.widget.Toast; import com.ericssonlabs.R; import com.google.zxing.BarcodeFormat; import com.google.zxing.Result; import com.zxing.camera.CameraManager; import com.zxing.decoding.CaptureActivityHandler; import com.zxing.decoding.CaptureActivityHandler.DecodeCallback; import com.zxing.decoding.InactivityTimer; import com.zxing.view.ViewfinderView; /** * Initial the camera * @author Ryan.Tang * @modifier Lemon * @use extends CaptureActivity並且在setContentView方法後調用init方法 */ public abstract class CaptureActivity extends Activity implements Callback, DecodeCallback { // private static final String TAG = "CaptureActivity"; protected Activity context; protected SurfaceView surfaceView; protected ViewfinderView viewfinderView; /**初始化,必須在setContentView之後 * @param context * @param viewfinderView */ protected void init(Activity context, SurfaceView surfaceView, ViewfinderView viewfinderView) { this.context = context; this.surfaceView = surfaceView; this.viewfinderView = viewfinderView; CameraManager.init(getApplication()); hasSurface = false; inactivityTimer = new InactivityTimer(this); } private CaptureActivityHandler handler; private boolean hasSurface; private Vector<BarcodeFormat> decodeFormats; private String characterSet; private InactivityTimer inactivityTimer; private MediaPlayer mediaPlayer; private boolean playBeep; private static final float BEEP_VOLUME = .f; private boolean vibrate; @Override protected void onResume() { super.onResume(); SurfaceHolder surfaceHolder = surfaceView.getHolder(); if (hasSurface) { initCamera(surfaceHolder); } else { surfaceHolder.addCallback(this); surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); } decodeFormats = null; characterSet = null; playBeep = true; AudioManager audioService = (AudioManager) getSystemService(AUDIO_SERVICE); if (audioService.getRingerMode() != AudioManager.RINGER_MODE_NORMAL) { playBeep = false; } initBeepSound(); vibrate = true; } @Override protected void onPause() { super.onPause(); if (handler != null) { handler.quitSynchronously(); handler = null; } CameraManager.get().closeDriver(); } @Override protected void onDestroy() { inactivityTimer.shutdown(); super.onDestroy(); } public static final String RESULT_QRCODE_STRING = "RESULT_QRCODE_STRING"; /** * Handler scan result * @param result * @param barcode */ public void handleDecode(Result result, Bitmap barcode) { inactivityTimer.onActivity(); playBeepSoundAndVibrate(); String resultString = result.getText(); //FIXME if (resultString.equals("")) { Toast.makeText(CaptureActivity.this, "Scan failed!", Toast.LENGTH_SHORT).show(); } setResult(RESULT_OK, new Intent().putExtra(RESULT_QRCODE_STRING, resultString)); finish(); } private void initCamera(SurfaceHolder surfaceHolder) { try { CameraManager.get().openDriver(surfaceHolder); } catch (IOException ioe) { return; } catch (RuntimeException e) { return; } if (handler == null) { handler = new CaptureActivityHandler(this, decodeFormats, characterSet, viewfinderView, this); } } @Override public void drawViewfinder() { viewfinderView.drawViewfinder(); } @Override public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { } @Override public void surfaceCreated(SurfaceHolder holder) { if (!hasSurface) { hasSurface = true; initCamera(holder); } } @Override public void surfaceDestroyed(SurfaceHolder holder) { hasSurface = false; } public Handler getHandler() { return handler; } private void initBeepSound() { if (playBeep && mediaPlayer == null) { // The volume on STREAM_SYSTEM is not adjustable, and users found it // too loud, // so we now play on the music stream. setVolumeControlStream(AudioManager.STREAM_MUSIC); mediaPlayer = new MediaPlayer(); mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC); mediaPlayer.setOnCompletionListener(beepListener); AssetFileDescriptor file = getResources().openRawResourceFd( R.raw.beep); try { mediaPlayer.setDataSource(file.getFileDescriptor(), file.getStartOffset(), file.getLength()); file.close(); mediaPlayer.setVolume(BEEP_VOLUME, BEEP_VOLUME); mediaPlayer.prepare(); } catch (IOException e) { mediaPlayer = null; } } } private static final long VIBRATE_DURATION = L; private void playBeepSoundAndVibrate() { if (playBeep && mediaPlayer != null) { mediaPlayer.start(); } if (vibrate) { Vibrator vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE); vibrator.vibrate(VIBRATE_DURATION); } } /** * When the beep has finished playing, rewind to queue up another one. */ private final OnCompletionListener beepListener = new OnCompletionListener() { public void onCompletion(MediaPlayer mediaPlayer) { mediaPlayer.seekTo(); } }; }
使用方法: 新建一個Activity繼承CaptureActivity並且在setContentView方法後調用init方法即可。
示例:
CameraScanActivity.java
package zuo.biao.activity; import zuo.biao.R; import zuo.biao.library.interfaces.OnBottomDragListener; import zuo.biao.util.ActivityUtil; import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.view.SurfaceHolder.Callback; import android.view.SurfaceView; import android.view.View; import android.view.View.OnClickListener; import com.zxing.activity.CaptureActivity; import com.zxing.camera.CameraManager; import com.zxing.view.ViewfinderView; /**掃描二維碼Activity * @author Lemon * @use 參考zuo.biao.library.ModelActivity */ public class CameraScanActivity extends CaptureActivity implements Callback, OnClickListener, OnBottomDragListener { public static final String TAG = "CameraScanActivity"; //啟動方法<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< /**啟動這個Activity的Intent * @param context * @param title * @return */ public static Intent createIntent(Context context) { return new Intent(context, CameraScanActivity.class); } //啟動方法>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.camera_scan_activity); init(this, (SurfaceView) findViewById(R.id.svCameraScan), (ViewfinderView) findViewById(R.id.vfvCameraScan)); //功能歸類分區方法,必須調用<<<<<<<<<< initView(); initData(); initListener(); //功能歸類分區方法,必須調用>>>>>>>>>> } //UI顯示區(操作UI,但不存在數據獲取或處理代碼,也不存在事件監聽代碼)<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< public void initView() {//必須調用 } //UI顯示區(操作UI,但不存在數據獲取或處理代碼,也不存在事件監聽代碼)>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> //data數據區(存在數據獲取或處理代碼,但不存在事件監聽代碼)<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< public void initData() {//必須調用 } //data數據區(存在數據獲取或處理代碼,但不存在事件監聽代碼)>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> //listener事件監聽區(只要存在事件監聽代碼就是)<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< public void initListener() {//必須調用 findViewById(R.id.tvCameraScanReturn).setOnClickListener(this); findViewById(R.id.ivCameraScanReturn).setOnClickListener(this); findViewById(R.id.ivCameraScanLight).setOnClickListener(this); findViewById(R.id.ivCameraScanMyQRCode).setOnClickListener(this); } //系統自帶監聽方法<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< @Override public void onDragBottom(boolean rightToLeft) { if (rightToLeft) { return; } finish(); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.tvCameraScanReturn: case R.id.ivCameraScanReturn: onDragBottom(false); break; case R.id.ivCameraScanLight: switchLight(! isOpen); break; case R.id.ivCameraScanMyQRCode: // break; default: break; } } private boolean isOpen = false; /**打開或關閉閃關燈 * @param open */ private void switchLight(boolean open) { if (open == isOpen) { return; } isOpen = CameraManager.get().switchLight(open); } //類相關監聽<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< //類相關監聽>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> //系統自帶監聽方法>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> //listener事件監聽區(只要存在事件監聽代碼就是)>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> //內部類,盡量少用<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< //內部類,盡量少用>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> }
CameraScanActivity布局文件camera_scan_activity.xml
<?xml version="." encoding="utf-"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" > <SurfaceView android:id="@+id/svCameraScan" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" /> <!-- 必須在最底層,且不能指定寬高,否則掃描讀取很難實現 --> <com.zxing.view.ViewfinderView android:id="@+id/vfvCameraScan" android:layout_width="match_parent" android:layout_height="match_parent" /> <LinearLayout android:baselineAligned="false" > <RelativeLayout android:background="@color/white_alpha" > <TextView android:id="@+id/tvCameraScanReturn" android:text=" " /> <TextView android:layout_centerHorizontal="true" android:text="掃一掃" /> </RelativeLayout> <LinearLayout android:layout_gravity="center_horizontal" android:layout_weight="" > <TextView android:layout_margin="dp" android:text="@string/camera_scan_remind" /> </LinearLayout> <LinearLayout android:layout_gravity="bottom" android:background="@color/white_alpha" android:gravity="center" > <LinearLayout android:layout_margin="dp" android:paddingBottom="dp" android:paddingLeft="dp" android:paddingRight="dp" android:paddingTop="dp" > <ImageView android:id="@+id/ivCameraScanReturn" android:background="@drawable/cilcle_gray_to_white" android:padding="dp" android:src="@drawable/back_black_light" /> <TextView android:layout_marginTop="dp" android:text="返回" /> </LinearLayout> <LinearLayout android:layout_margin="dp" android:paddingBottom="dp" android:paddingLeft="dp" android:paddingRight="dp" android:paddingTop="dp" > <ImageView android:id="@+id/ivCameraScanLight" android:background="@drawable/cilcle_gray_to_white" android:padding="dp" android:src="@drawable/flash_light" /> <TextView android:layout_marginTop="dp" android:text="開燈/關燈" /> </LinearLayout> <LinearLayout android:layout_margin="dp" android:paddingBottom="dp" android:paddingLeft="dp" android:paddingRight="dp" android:paddingTop="dp" > <ImageView android:id="@+id/ivCameraScanMyQRCode" android:background="@drawable/cilcle_gray_to_white" android:padding="dp" android:src="@drawable/qrcode" /> <TextView android:layout_marginTop="dp" android:text="我的名片" /> </LinearLayout> </LinearLayout> </LinearLayout> </FrameLayout>
布局文件因為使用了ZBLibrary中的一些style,color等,只有這個layout的話會出現一些錯誤。自己新建一個layout文件並把ViewfinderView放到最外層布局文件內就行了。當然下載好下面附上的源碼就沒這問題了。
對了,記得在AndroidManifest.xml中加上這些權限:
<uses-permission android:name="android.permission.CAMERA" /> <uses-feature android:name="android.hardware.camera" /> <uses-feature android:name="android.hardware.camera.autofocus" /> <uses-permission android:name="android.permission.FLASHLIGHT" />
附源碼(含使用方法)
GitHub源碼 https://github.com/TommyLemon/Android-ZBLibrary
開源中國源碼 http://git.oschina.net/Lemon19950301/Android-ZBLibrary
關於Android開發框架之自定義ZXing二維碼掃描界面並解決取景框拉伸問題就給大家介紹到這裡,以上內容有點長,希望大家能耐心看完,有任何問題歡迎給我留言,小編會及時回復大家的,在此也非常感謝大家對本站網站的支持!
1.效果這個效果在很多App裡都可以用到,基本上就已經泛濫了.這裡就記錄一下如何實現這一種效果.(截圖沒注意大小,丟幀也嚴重,所以看上去有點卡頓)2.實現步驟2.1布局文
一、字符串關鍵字變色在界面顯示的時候,偶爾需要將某些字符串中特定的字符串重點標出如下圖所示:便有了下面的方法。這個方法針對於比較 固定的字符串 ,並且需要自己 計算 需要
Android系統原本就為手機設計,所以,在android系統中的任何App中,只要願意,撥打指定電話非常方便。 核心就是使用Intent跳轉,指定請求Action為In
一、前言自從去年中微信添加搶紅包的功能,微信的電商之旅算是正式開始正式火爆起來。但是作為Android開發者來說,我們在搶紅包的同時意識到了很多問題,就是手動去搶紅包的速