二维码扫描

很多朋友没有做过二维码扫描方面的可能会觉得比较复杂,先定义就是不好下手,但是自己仔细写一下其实也好,毕竟网上类似二维码扫描的特别多,好人也很多。下面的我就来基于zbar集成的一个二维码扫描。

首先就是扫面的布局的绘制,这里我就简单说明一下,里面就一个扫描的view,然后就加了一个闪光灯的按钮,点击可以打开闪光灯的,然后就是基本按钮了,我的二维码。这里可以点击去自己想去的页面,这里呢我就没有写了。再一个就是返回,现看看xml代码先


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:layout_width="fill_parent"

android:layout_height="fill_parent">

android:id="@+id/viewfinder_view"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:background="#00000000"/>

android:id="@+id/rl_title"

android:layout_width="fill_parent"

android:layout_height="42dp"

android:layout_alignParentTop="true"

android:background="#000000">

android:id="@+id/iv_title_back"

android:layout_width="64dp"

android:layout_height="44dp"

android:layout_centerVertical="true"

android:background="@drawable/top_backup"

android:contentDescription="@null"

/>

android:id="@+id/tv_title_text"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_centerInParent="true"

android:gravity="center"

android:singleLine="true"

android:text="二维码扫描"

android:textColor="#ffffff"

android:textSize="18sp"/>

android:id="@+id/iv_flashlight"

android:layout_width="40dp"

android:layout_height="40dp"

android:layout_alignParentRight="true"

android:layout_marginRight="10dp"

android:layout_centerVertical="true"

android:src="@drawable/icon_light"/>

android:id="@+id/my_qr_code"

android:layout_width="wrap_content"

android:layout_height="54dp"

android:layout_alignParentBottom="true"

android:layout_centerHorizontal="true"

android:layout_marginBottom="10dp">

android:id="@+id/album"

android:layout_width="40dp"

android:layout_height="40dp"

android:layout_centerHorizontal="true"

android:src="@drawable/icon_erweima"/>

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_below="@+id/album"

android:layout_centerHorizontal="true"

android:text="我的二维码"

android:textColor="#fff"

android:textSize="12sp"/>

接下来就是对于那个xml页面的基本操作,比如声音啊,然后recorder的处理,扫描data的分析和处理等,最重要的就是返回个自己的页面的操作。

[java]view plaincopy

packagecom.leo.zbardemo.zbar.client;

importandroid.content.BroadcastReceiver;

importandroid.content.Context;

importandroid.content.Intent;

importandroid.content.IntentFilter;

importandroid.content.res.AssetFileDescriptor;

importandroid.media.AudioManager;

importandroid.media.MediaPlayer;

importandroid.media.MediaPlayer.OnCompletionListener;

importandroid.os.Bundle;

importandroid.os.Handler;

importandroid.os.Message;

importandroid.os.Vibrator;

importandroid.support.v7.app.AppCompatActivity;

importandroid.util.Log;

importandroid.view.SurfaceHolder;

importandroid.view.SurfaceView;

importandroid.view.View;

importandroid.view.Window;

importandroid.view.WindowManager;

importandroid.widget.ImageView;

importandroid.widget.RelativeLayout;

importandroid.widget.Toast;

importcom.leo.zbardemo.R;

importcom.leo.zbardemo.zbar.camera.CameraManager;

importcom.leo.zbardemo.zbar.decode.CaptureActivityHandler;

importcom.leo.zbardemo.zbar.decode.InactivityTimer;

importcom.leo.zbardemo.zbar.view.ViewfinderView;

importjava.io.IOException;

publicclassCaptureActivityextendsAppCompatActivityimplementsSurfaceHolder.Callback, View.OnClickListener {

privateInactivityTimer inactivityTimer;

privateCaptureActivityHandler handler;

privateMediaPlayer mediaPlayer;

privatebooleanplayBeep;

privatebooleanvibrate;

privatebooleanflag =true;

privatebooleanhasSurface =false;

privateViewfinderView viewfinderView;

privateImageView iv_title_back;

privateImageView iv_flashlight;

privateRelativeLayout my_qr_code;

privateScreenOffReceiver mScreenOffReceiver =newScreenOffReceiver();

;

privatestaticfinalfloatBEEP_VOLUME =0.20f;

privatestaticfinallongVIBRATE_DURATION = 200L;

privatefinalOnCompletionListener beepListener =newOnCompletionListener() {

publicvoidonCompletion(MediaPlayer mediaPlayer) {

mediaPlayer.seekTo(0);

}

};

privatevoidinitBeepSound() {

if(playBeep && mediaPlayer ==null) {

setVolumeControlStream(AudioManager.STREAM_MUSIC);

mediaPlayer =newMediaPlayer();

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;

}

}

}

privatevoidinitCamera(SurfaceHolder surfaceHolder) {

try{

CameraManager.get().openDriver(surfaceHolder);

}catch(IOException ioe) {

return;

}catch(RuntimeException e) {

return;

}

if(handler ==null) {

handler =newCaptureActivityHandler(this);

}

}

privatevoidplayBeepSoundAndVibrate() {

if(playBeep && mediaPlayer !=null) {

mediaPlayer.start();

}

if(vibrate) {

Vibrator vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE);

vibrator.vibrate(VIBRATE_DURATION);

}

}

publicViewfinderView getViewfinderView() {

returnviewfinderView;

}

publicHandler getHandler() {

returnhandler;

}

publicvoiddrawViewfinder() {

viewfinderView.drawViewfinder();

}

publicvoidhandleDecode(String result) {

inactivityTimer.onActivity();

playBeepSoundAndVibrate();

Intent intent =newIntent();

intent.putExtra("result", result);

Message message = Message.obtain(getHandler(), R.id.return_scan_result, intent);

message.sendToTarget();

}

publicvoidlight() {

if(this.flag) {

CameraManager.get().openLight();

this.flag =false;

this.iv_flashlight.setImageDrawable(getResources().getDrawable(R.drawable.icon_light_hover));

return;

}

CameraManager.get().offLight();

this.flag =true;

this.iv_flashlight.setImageDrawable(getResources().getDrawable(R.drawable.icon_light));

}

@Override

publicvoidonCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

Window window = getWindow();

window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

setContentView(R.layout.capture);

CameraManager.init(getApplication());

this.viewfinderView = (ViewfinderView) findViewById(R.id.viewfinder_view);

this.iv_flashlight = (ImageView) findViewById(R.id.iv_flashlight);

this.my_qr_code = (RelativeLayout) findViewById(R.id.my_qr_code);

this.iv_flashlight.setOnClickListener(this);

this.iv_title_back = (ImageView) findViewById(R.id.iv_title_back);

this.iv_title_back.setOnClickListener(this);

this.my_qr_code.setOnClickListener(this);

this.hasSurface =false;

this.inactivityTimer =newInactivityTimer(this);

IntentFilter intentFilter =newIntentFilter();

intentFilter.addAction(Intent.ACTION_SCREEN_OFF);

registerReceiver(this.mScreenOffReceiver, intentFilter);

}

@Override

protectedvoidonPause() {

if(handler !=null) {

handler.quitSynchronously();

handler =null;

}

CameraManager.get().offLight();

inactivityTimer.onPause();

CameraManager.get().closeDriver();

if(!hasSurface) {

SurfaceView surfaceView = (SurfaceView) findViewById(R.id.preview_view);

SurfaceHolder surfaceHolder = surfaceView.getHolder();

surfaceHolder.removeCallback(this);

}

super.onPause();

}

@SuppressWarnings("deprecation")

@Override

protectedvoidonResume() {

super.onResume();

handler =null;

SurfaceView surfaceView = (SurfaceView) findViewById(R.id.preview_view);

SurfaceHolder surfaceHolder = surfaceView.getHolder();

if(hasSurface) {

initCamera(surfaceHolder);

}else{

surfaceHolder.addCallback(this);

surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

}

playBeep =true;

AudioManager audioService = (AudioManager) getSystemService(AUDIO_SERVICE);

if(audioService.getRingerMode() != AudioManager.RINGER_MODE_NORMAL) {

playBeep =false;

}

initBeepSound();

inactivityTimer.onResume();

vibrate =true;

}

@Override

protectedvoidonDestroy() {

inactivityTimer.shutdown();

unregisterReceiver(this.mScreenOffReceiver);

super.onDestroy();

}

@Override

publicvoidsurfaceChanged(SurfaceHolder holder,intformat,intwidth,intheight) {

}

@Override

publicvoidsurfaceCreated(SurfaceHolder holder) {

if(!hasSurface) {

hasSurface =true;

initCamera(holder);

}

}

@Override

publicvoidsurfaceDestroyed(SurfaceHolder holder) {

hasSurface =false;

}

@Override

publicvoidonClick(View v) {

switch(v.getId()) {

caseR.id.iv_flashlight:

light();

break;

caseR.id.iv_title_back:

this.finish();

break;

caseR.id.my_qr_code:

Toast.makeText(this,"我的二维码", Toast.LENGTH_SHORT).show();

break;

}

}

privateclassScreenOffReceiverextendsBroadcastReceiver {

@Override

publicvoidonReceive(Context arg0, Intent arg1) {

Log.d("CaptureActivity","CaptureActivity receive screen off command ++");

CaptureActivity.this.finish();

}

}

}

我采用的是StartActivityForResult的跳转,然后在OnActivityResult里面的获取数据和分析数据,也就是扫描得到的东西。

@Override

publicvoidonClick(View view) {

switch(view.getId()){

caseR.id.tv_click:

Intent intent =newIntent(MainActivity.this, CaptureActivity.class);

startActivityForResult(intent,1234);

break;

}

}

@Override

publicvoidonActivityResult(intrequestCode,intresultCode, Intent data) {

super.onActivityResult(requestCode, resultCode, data);

if(requestCode ==1234&& resultCode == RESULT_OK) {

Bundle bundle = data.getExtras();

//显示扫描到的内容

String content = bundle.getString("result");

Toast.makeText(this, content, Toast.LENGTH_SHORT).show();

}

}

剩下的也就是jar包和so库了,so库建议放在armeabi-v7a这个文件夹下面。

然后附在demo一个:

http://download.csdn.NET/detail/greatdaocaoren/9903754

欢迎下载。


csdn项目地址:http://blog.csdn.net/greatdaocaoren/article/details/75370540

你可能感兴趣的:(二维码扫描)