zxing-android-embedded:3.5自定义布局

针对zxing-android-embedded,网上很多自定义布局的例子,但是代码都不全,不方便直接用,于是自己写了一个相对较全的例子,并加入了闪光灯功能。

zxing-android-embedded:3.5自定义布局_第1张图片  自定义布局效果图

1)布局文件activity_capture.xml,代码如下:


  xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto">
            android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/zxing_barcode_scanner"
        app:zxing_preview_scaling_strategy="centerCrop"
        app:zxing_use_texture_view="false"/>

            android:layout_width="fill_parent"
        android:layout_height="48.0dip"
        android:layout_alignParentTop="true"
        android:background="#99000000"
        android:gravity="center"
        android:orientation="vertical"
        android:visibility="visible">

                    android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="扫一扫"
            android:gravity="center"
            android:textColor="#ffffffff"
            android:textSize="20.0sp" />
   

   
       
       
   

2)自定义扫描类,代码如下:

import android.app.Activity;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.journeyapps.barcodescanner.CaptureActivity;
import com.journeyapps.barcodescanner.CaptureManager;
import com.journeyapps.barcodescanner.DecoratedBarcodeView;

import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import cn.com.dqg.ydps.R;

public class ScanActivity extends Activity {

    @ViewInject(R.id.btn_text)
    TextView light_text;
    @ViewInject(R.id.btn_switch)
    ImageView swichLight;
    @ViewInject(R.id.zxing_barcode_scanner)
    DecoratedBarcodeView mDBV;
    private CaptureManager captureManager;
    private boolean isLightOn = false;
    private CaptureActivity  cameraManger;
    @Override
    protected void onPause() {
        super.onPause();
        captureManager.onPause();
    }

    @Override
    protected void onResume() {
        super.onResume();
        captureManager.onResume();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        captureManager.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 mDBV.onKeyDown(keyCode, event) || super.onKeyDown(keyCode, event);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_capture);
        x.view().inject(this);

        // 如果没有闪光灯功能,就去掉相关按钮
        if(!hasFlash()) {
            swichLight.setVisibility(View.GONE);
        }

        //重要代码,初始化捕获
        captureManager = new CaptureManager(this,mDBV);
        captureManager.initializeFromIntent(getIntent(),savedInstanceState);
        captureManager.decode();
    }


    // 判断是否有闪光灯功能
    private boolean hasFlash() {
        return getApplicationContext().getPackageManager()
                .hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);
    }

    // 点击切换闪光灯
    @Event({R.id.btn_switch})
    private void onClick(View v) {
        if(isLightOn){
            mDBV.setTorchOff();
            swichLight.setImageResource(R.drawable.lightoff);
            light_text.setText("打开闪光灯");
            light_text.setTextColor(Color.WHITE);
        }else{
            mDBV.setTorchOn();
            swichLight.setImageResource(R.drawable.lighton);
            light_text.setText("关闭闪光灯");
            light_text.setTextColor(Color.YELLOW);
        }
        isLightOn  = !isLightOn;
    }
}

 

3)调用代码示例:

IntentIntegrator integrator = new IntentIntegrator(ScanQrcodeActivity.this);
integrator.setPrompt(""); //底部的提示文字,设为""可以置空
integrator.setCameraId(0); //前置或者后置摄像头
integrator.setBeepEnabled(false); //扫描成功的「哔哔」声,默认开启

integrator.setDesiredBarcodeFormats(IntentIntegrator.QR_CODE_TYPES); //设置只扫描二维码,提高效率
integrator.setCaptureActivity(ScanActivity.class);//设置自定义类

integrator.initiateScan();

你可能感兴趣的:(zxing-android-embedded:3.5自定义布局)