android掃碼源碼
Ⅰ Android 二維碼掃描怎樣實現第二次重復掃描
掃描出現問題事彈出dialog,當用戶點擊確定按鈕後重新開始掃描。 private void showErrorDialog() { closeCamera(); viewfinderView.setVisibility(View.GONE); AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle(getString(R.string.app_name)); builder.setMessage("掃描確認出錯,請重新掃描櫃子上的二維碼!"); builder.setPositiveButton(android.R.string.ok, new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { restartCamera(); } }); builder.setOnCancelListener(new OnCancelListener() { @Override public void onCancel(DialogInterface dialog) { CaptureCodeActivity.this.finish(); } }); builder.show(); }
Ⅱ androidapp二維碼掃碼下載,途牛網站的效果,怎麼實現
Android 二維碼掃描是很常用的工具,是不是很Cool,到底如何實現的呢,下面我們就來探討一下Zxing的實現方法(底部附上下載鏈接):
首先
工程結構:
如何引用:(內容來自雪炭網SnowCoal.com)
一般來說,你就可以改改就用了,但如果你只想把它當成一個小小的子集加入項目,你需要將三個包Copy至你的項目中:(camera、decoding、view),然後引入相對應的資源進去,不要訪記還有一個Jar包哦!(Zxing.jar)
關於布局:
com.example.qr_codescan包裡面有一個MipcaActivityCapture,也是直接引入,這個Activity主要處理掃描界面的類,比如,掃描成功有聲音和振動等等,主要關注裡面的handleDecode(Result
result, Bitmap barcode)方法,掃描完成之後將掃描到的結果和二維碼的bitmap當初參數傳遞到handleDecode(Result
result, Bitmap barcode)裡面,我們只需要在裡面寫出相對應的處理代碼即可,其他的地方都不用改得,這里處理掃描結果和掃描拍的照片.
* 處理掃描結果
* @param result
* @param barcode
*/
public void handleDecode(Result result, Bitmap barcode) {
inactivityTimer.onActivity();
playBeepSoundAndVibrate();
String resultString = result.getText();
if (resultString.equals( "" )) {
Toast.makeText(MipcaActivityCapture. this , "Scan failed!" ,
Toast.LENGTH_SHORT).show();
} else {
Intent resultIntent = new Intent();
Bundle bundle = new Bundle();
bundle.putString( "result" , resultString);
bundle.putParcelable( "bitmap" , barcode);
resultIntent.putExtras(bundle);
this .setResult(RESULT_OK, resultIntent);
}
MipcaActivityCapture. this .finish();
}
對MipcaActivityCapture界面的布局做了自己的改動,先看下效果圖,主要是用到FrameLayout,裡面嵌套RelativeLayout。
//xml
android:layout_width= "fill_parent"
android:layout_height= "fill_parent" >
android:layout_width= "fill_parent"
android:layout_height= "fill_parent" >
android:id= "@+id/preview_view"
android:layout_width= "fill_parent"
android:layout_height= "fill_parent"
android:layout_gravity= "center" />
android:id= "@+id/viewfinder_view"
android:layout_width= "wrap_content"
android:layout_height= "wrap_content" />
< include
android:id= "@+id/include1"
android:layout_width= "fill_parent"
android:layout_height= "wrap_content"
android:layout_alignParentTop= "true"
layout= "@layout/activity_title" />
實現:
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
public class MainActivity extends Activity {
private final static int SCANNIN_GREQUEST_CODE = 1 ;
/**
* 顯示掃描結果
*/
private TextView mTextView ;
/**
* 顯示掃描拍的圖片
*/
private ImageView mImageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super .onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTextView = (TextView) findViewById(R.id.result);
mImageView = (ImageView) findViewById(R.id.qrcode_bitmap);
//點擊按鈕跳轉到二維碼掃描界面,這里用的是startActivityForResult跳轉
//掃描完了之後調到該界面
Button mButton = (Button) findViewById(R.id.button1);
mButton.setOnClickListener( new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setClass(MainActivity. this , MipcaActivityCapture. class );
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivityForResult(intent, SCANNIN_GREQUEST_CODE);
}
});
}
@Override
protected void onActivityResult( int requestCode, int resultCode, Intent
data) {
super .onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case SCANNIN_GREQUEST_CODE:
if (resultCode == RESULT_OK){
Bundle bundle = data.getExtras();
//顯示掃描到的內容
mTextView.setText(bundle.getString( "result" ));
//顯示
mImageView.setImageBitmap((Bitmap) data.getParcelableExtra( "bitmap"
));
}
break ;
}
}
}
Ⅲ Android 我用zxing開發二維碼掃描 為什麼只能掃描一次
二維碼裡面存儲的都是文本,只不過有些二維碼在生成的時候加了點兒格式而已,你可以在網上隨便找兩個在線生成二維碼的,然後生成二維碼,用你自己的項目掃描,你就可以看出這些格式了,然後在你的掃描結果上進行判斷,
Ⅳ 掃碼登錄的原理
原理如下:
每次用戶打開PC端登錄請求,系統返回一個唯一的uid,並將uid的信息繪製成二維碼返回給用喊櫻帶戶。這里的uid一定是唯一的,否則就會造成你登錄了其他用戶的賬號或者其他用戶登錄你的賬號。
當用戶使用登錄後的微信掃描該二維碼的時候,會將這個uid和手機鄭蘆上的微信賬號及密碼產生的token進行綁定,並上傳到伺服器。
二維碼又稱二維條碼,常見的二維碼為QR Code,QR全稱Quick Response,是一個近幾年來移動設備上超流行的一種編碼方式,它比傳統的Bar Code條形碼能存更多的信息,也能表示更多的數據類型。
二維條碼/二維碼(2-dimensional bar code)是用某種特定的幾何圖形按一定規律在平面(二維方向上)分布的、黑白相間的、記錄數據符號信息的圖形。
在代碼編制上巧妙地利用構成計算機內部邏輯基礎的「0」、「1」比特流的概念,使用若干個與二進制相對應的幾何形體來表示文字數值信息,通過圖象輸入設備或光電掃描設備自動識讀以實現信息自動處理:它具有條碼技術的一些共性:每種碼制有頌談其特定的字元集;每個字元佔有一定的寬度;具有一定的校驗功能等。同時還具有對不同行的信息自動識別功能、及處理圖形旋轉變化點。
Ⅳ Android 二維碼掃描怎樣實現第二次掃描
首先看一下掃描界面
CaptureCodeActivity.java代碼:
packagecom.haier.qr.code;
importjava.io.IOException;
importjava.io.Serializable;
importjava.util.Map;
importjava.util.Vector;
importandroid.app.Activity;
importandroid.app.AlertDialog;
importandroid.content.DialogInterface;
importandroid.content.DialogInterface.OnCancelListener;
importandroid.content.DialogInterface.OnClickListener;
importandroid.content.Intent;
importandroid.graphics.Bitmap;
importandroid.os.Bundle;
importandroid.os.Handler;
importandroid.text.TextUtils;
importandroid.util.Log;
importandroid.view.SurfaceHolder;
importandroid.view.SurfaceHolder.Callback;
importandroid.view.SurfaceView;
importandroid.view.View;
importandroid.view.Window;
importandroid.view.WindowManager;
importandroid.widget.Toast;
importcom.google.zxing.BarcodeFormat;
importcom.google.zxing.DecodeHintType;
importcom.google.zxing.Result;
importcom.haier.cabinet.customer.R;
importcom.haier.cabinet.customer.activity.DeliveryBoxListActivity;
importcom.haier.common.util.AppToast;
importcom.haier.qr.code.zxing.camera.CameraManager;
importcom.haier.qr.code.zxing.decoding.CaptureActivityHandler;
importcom.haier.qr.code.zxing.decoding.FinishListener;
importcom.haier.qr.code.zxing.decoding.InactivityTimer;
importcom.haier.qr.code.zxing.view.ViewfinderView;
/**
*Initialthecamera
*
*@authorjdsjlzx
*/
,View.OnClickListener{
privatestaticfinalStringTAG=CaptureCodeActivity.class
.getSimpleName();
;
;
;
privatebooleanhasSurface;
privateMap<DecodeHintType,?>decodeHints;
privateVector<BarcodeFormat>decodeFormats;
privateStringcharacterSet;
;
;
/**
*聲音震動管理器。如果掃描成功後可以播放一段音頻,也可以震動提醒,可以通過配置來決定掃描成功後的行為。
*/
privateBeepManagerbeepManager;
/**
*閃光燈調節器。自動檢測環境光線強弱並決定是否開啟閃光燈
*/
;
(){
returncameraManager;
}
/**.*/
@Override
publicvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
Windowwindow=getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
setContentView(R.layout.activity_capture);
hasSurface=false;
inactivityTimer=newInactivityTimer(this);
beepManager=newBeepManager(this);
ambientLightManager=newAmbientLightManager(this);
//按鈕監聽事件
findViewById(R.id.capture_flashlight).setOnClickListener(this);
}
@Override
protectedvoidonResume(){
super.onResume();
Log.d(TAG,"onResume");
cameraManager=newCameraManager(getApplication());
viewfinderView=(ViewfinderView)findViewById(R.id.viewfinder_view);
viewfinderView.setCameraManager(cameraManager);
viewfinderView.setVisibility(View.VISIBLE);
SurfaceViewsurfaceView=(SurfaceView)findViewById(R.id.preview_view);
SurfaceHoldersurfaceHolder=surfaceView.getHolder();
if(hasSurface){
initCamera(surfaceHolder);
}else{
//防止sdk8的設備初始化預覽異常
surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
//()toinitthe
//camera.
surfaceHolder.addCallback(this);
}
decodeFormats=null;
characterSet=null;
//載入聲音配置,其實在BeemManager的構造器中也會調用該方法,即在onCreate的時候會調用一次
beepManager.updatePrefs();
//啟動閃光燈調節器
ambientLightManager.start(cameraManager);
//恢復活動監控器
inactivityTimer.onResume();
}
@Override
protectedvoidonPause(){
Log.d(TAG,"onPause");
if(handler!=null){
handler.quitSynchronously();
handler=null;
}
inactivityTimer.onPause();
ambientLightManager.stop();
beepManager.close();
//關閉攝像頭
cameraManager.closeDriver();
if(!hasSurface){
SurfaceViewsurfaceView=(SurfaceView)findViewById(R.id.preview_view);
SurfaceHoldersurfaceHolder=surfaceView.getHolder();
surfaceHolder.removeCallback(this);
}
super.onPause();
}
@Override
protectedvoidonDestroy(){
inactivityTimer.shutdown();
super.onDestroy();
}
/**
*Avalidbarcodehasbeenfound,
*theresults.
*
*@paramrawResult
*Thecontentsofthebarcode.
*@paramscaleFactor
*
*@parambarcode
*.
*/
publicvoidhandleDecode(ResultrawResult,Bitmapbarcode,floatscaleFactor){
Log.d(TAG,"handleDecode");
inactivityTimer.onActivity();
beepManager.playBeepSoundAndVibrate();
StringresultString=rawResult.getText();
if(TextUtils.isEmpty(resultString)){
Toast.makeText(CaptureCodeActivity.this,"Scanfailed!",
Toast.LENGTH_SHORT).show();
}else{
//掃碼完成,處理結果(跳轉到箱子列表)
/*IntentresultIntent=newIntent();
Bundlebundle=newBundle();
bundle.putString("result",resultString);
bundle.putParcelable("bitmap",barcode);
resultIntent.putExtras(bundle);
this.setResult(RESULT_OK,resultIntent);*/
StringterminalNo=getIntent().getStringExtra("terminal_no");
if(resultString.equals(terminalNo)){
AppToast.makeToast(this,"確認成功!");
Intentintent=newIntent(this,DeliveryBoxListActivity.class);
intent.putExtra("terminal_no",terminalNo);
intent.putExtra("box_list",(Serializable)getIntent().getSerializableExtra("box_list"));
startActivity(intent);
CaptureCodeActivity.this.finish();
}else{
/*AppToast.makeToast(this,"確認失敗,請重新掃描確認");*/
showErrorDialog();
}
}
}
privatevoidinitCamera(SurfaceHoldersurfaceHolder){
if(surfaceHolder==null){
thrownewIllegalStateException("NoSurfaceHolderprovided");
}
if(cameraManager.isOpen()){
Log.w(TAG,
"initCamera()whilealreadyopen--lateSurfaceViewcallback?");
return;
}
try{
cameraManager.openDriver(surfaceHolder);
//,whichcanalsothrowa
//RuntimeException.
if(handler==null){
handler=newCaptureActivityHandler(this,decodeFormats,
decodeHints,characterSet,cameraManager);
}
}catch(IOExceptionioe){
Log.w(TAG,ioe);
();
}catch(RuntimeExceptione){
//:
//java.?lang.?RuntimeException:Failtoconnecttocameraservice
Log.w(TAG,"",e);
();
}
}
privatevoid(){
AlertDialog.Builderbuilder=newAlertDialog.Builder(this);
builder.setTitle(getString(R.string.app_name));
builder.setMessage(getString(R.string.msg_camera_framework_bug));
builder.setPositiveButton(android.R.string.yes,newFinishListener(this));
builder.setOnCancelListener(newFinishListener(this));
builder.show();
}
@Override
publicvoidsurfaceChanged(SurfaceHolderholder,intformat,intwidth,
intheight){
}
@Override
publicvoidsurfaceCreated(SurfaceHolderholder){
if(holder==null){
Log.e(TAG,
"***WARNING***surfaceCreated()gaveusanullsurface!");
}
if(!hasSurface){
hasSurface=true;
initCamera(holder);
}
}
@Override
publicvoidsurfaceDestroyed(SurfaceHolderholder){
hasSurface=false;
}
(){
returnviewfinderView;
}
publicHandlergetHandler(){
returnhandler;
}
publicvoiddrawViewfinder(){
viewfinderView.drawViewfinder();
}
@Override
publicvoidonClick(Viewv){
switch(v.getId()){
caseR.id.capture_flashlight:
if(isFlashlightOpen){
cameraManager.setTorch(false);//關閉閃光燈
isFlashlightOpen=false;
}
else{
cameraManager.setTorch(true);//打開閃光燈
isFlashlightOpen=true;
}
break;
default:
break;
}
}
privatevoidshowErrorDialog(){
closeCamera();
viewfinderView.setVisibility(View.GONE);
AlertDialog.Builderbuilder=newAlertDialog.Builder(this);
builder.setTitle(getString(R.string.app_name));
builder.setMessage("掃描確認出錯,請重新掃描二維碼!");
builder.setPositiveButton(android.R.string.ok,newOnClickListener(){
@Override
publicvoidonClick(DialogInterfacedialog,intwhich){
restartCamera();
}
});
builder.setOnCancelListener(newOnCancelListener(){
@Override
publicvoidonCancel(DialogInterfacedialog){
CaptureCodeActivity.this.finish();
}
});
builder.show();
}
voidrestartCamera(){
Log.d(TAG,"hasSurface"+hasSurface);
viewfinderView.setVisibility(View.VISIBLE);
SurfaceViewsurfaceView=(SurfaceView)findViewById(R.id.preview_view);
SurfaceHoldersurfaceHolder=surfaceView.getHolder();
initCamera(surfaceHolder);
//恢復活動監控器
inactivityTimer.onResume();
}
voidcloseCamera(){
if(handler!=null){
handler.quitSynchronously();
handler=null;
}
inactivityTimer.onPause();
//關閉攝像頭
cameraManager.closeDriver();
}
}
在掃描出現問題事彈出dialog,當用戶點擊確定按鈕後重新開始掃描。
privatevoidshowErrorDialog(){
closeCamera();
viewfinderView.setVisibility(View.GONE);
AlertDialog.Builderbuilder=newAlertDialog.Builder(this);
builder.setTitle(getString(R.string.app_name));
builder.setMessage("掃描確認出錯,請重新掃描櫃子上的二維碼!");
builder.setPositiveButton(android.R.string.ok,newOnClickListener(){
@Override
publicvoidonClick(DialogInterfacedialog,intwhich){
restartCamera();
}
});
builder.setOnCancelListener(newOnCancelListener(){
@Override
publicvoidonCancel(DialogInterfacedialog){
CaptureCodeActivity.this.finish();
}
});
builder.show();
}