Android zxing扫码,在预览页面持续扫描

修改zxing里的CaptureManager,使扫描后不结束activity。

扫描activity的代码如下:

package com.byxc.scanning;

import android.app.Activity;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.PersistableBundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.widget.TextView;

import com.journeyapps.barcodescanner.DecoratedBarcodeView;

/**
 * 扫描二维码
 * Created by ypp on 2018/5/16.
 */

public class ScanningActivity extends Activity {
    private final String TAG="ScanningActivity";
    private CustomerCaptureManager captureManager;//扫描
    private DecoratedBarcodeView dbv_custom;
    /**
     * 当前闪光灯是否打开了
     */
    private boolean isTorchOn=false;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_scanning);
        dbv_custom=(DecoratedBarcodeView)findViewById(R.id.dbv_custom);

        //重要代码,初始化捕获
        initCapture();
//        isScanHandler.sendEmptyMessageDelayed(0,30000);//30秒未扫描到,提示

    }

    /**
     * 初始化捕捉
     */
    private void initCapture(){
        if(captureManager!=null){
            captureManager.onDestroy();
            captureManager=null;
        }
        captureManager = new CustomerCaptureManager(this,dbv_custom,scanningListener);
        captureManager.initializeFromIntent(getIntent(),null);
        captureManager.decode();
    }
    /**
     * 扫描结果监听
     */
    private CustomerCaptureManager.ScanningListener scanningListener=new CustomerCaptureManager.ScanningListener(){

        @Override
        public void scanResult(String result) {
            HLog.toast(ScanningActivity.this,result);
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    initCapture();
                }
            },500);
        }
    };

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void onSaveInstanceState(Bundle outState, PersistableBundle outPersistentState) {
        super.onSaveInstanceState(outState, outPersistentState);
        captureManager.onSaveInstanceState(outState);
    }
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        return dbv_custom.onKeyDown(keyCode, event) || super.onKeyDown(keyCode, event);
    }
    public void onResume()
    {
        super.onResume();
        captureManager.onResume();
    }

    public void onPause()
    {
        super.onPause();
        captureManager.onPause();
    }

    /**
     * 切换闪光灯
     * @param view
     */
    public void openOrCloseTorch(View view) {
        if(isTorchOn){
            if(hasTorch()){
                TextView flash_btn=(TextView)findViewById(R.id.flash_btn);
                flash_btn.setBackgroundResource(R.drawable.close_flash_light_bg);
                flash_btn.setText(getResources().getString(R.string.open_flash_light));
                dbv_custom.setTorchOff();
            }

        }else{
            if(hasTorch()){
                TextView flash_btn=(TextView)findViewById(R.id.flash_btn);
                flash_btn.setBackgroundResource(R.drawable.open_flash_light_bg);
                flash_btn.setText(getResources().getString(R.string.close_flash_light));
                dbv_custom.setTorchOn();
            }

        }
        isTorchOn=!isTorchOn;
    }
    /**
     * 判断是否有闪光灯
     * @return
     */
    public boolean hasTorch(){
        return getApplication().getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);
    }
}

CustomerCaptureManager代码:


                
  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值