二维码扫描

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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,524评论 5 460
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,869评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,813评论 0 320
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,210评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,085评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,117评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,533评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,219评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,487评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,582评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,362评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,218评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,589评论 3 299
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,899评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,176评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,503评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,707评论 2 335

推荐阅读更多精彩内容

  • 了解二维码这个东西还是从微信中,当时微信推出二维码扫描功能,自己感觉挺新颖的,从一张图片中扫一下竟然能直接加好友,...
    AiPuff阅读 848评论 0 1
  • ¥开启¥ 【iAPP实现进入界面执行逐一显】 〖2017-08-25 15:22:14〗 《//首先开一个线程,因...
    小菜c阅读 6,327评论 0 17
  • 二维码扫描最近两年简直是风靡移动互联网时代,尤其在国内发展神速。围绕条码扫码功能,首先说说通过本文你可以知道啥。一...
    55book阅读 4,117评论 0 1
  • 详情页面 packagecom.example.shoppingcar; importandroid.conten...
    ForCrazyLove阅读 552评论 0 2
  • 本人初学Android,最近做了一个实现安卓简单音乐播放功能的播放器,收获不少,于是便记录下来自己的思路与知识总结...
    落日柳风阅读 19,009评论 2 41