demo下载链接:https://download.csdn.net/download/meixi_android/10779714
zxing依赖:
compile 'cn.yipianfengye.android:zxing-library:2.2'
扫描类:
/*** 作者:created by meixi* 邮箱:13164716840@163.com* 日期:2018/9/17 08*/
public class TwoActivity extends AppCompatActivity {private CaptureFragment captureFragment;@Overrideprotected void onCreate(@Nullable Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_second);captureFragment = new CaptureFragment();// 为二维码扫描界面设置定制化界面CodeUtils.setFragmentArgs(captureFragment, R.layout.my_camera);captureFragment.setAnalyzeCallback(analyzeCallback);getSupportFragmentManager().beginTransaction().replace(R.id.fl_my_container, captureFragment).commit();}/*** 二维码解析回调函数*/CodeUtils.AnalyzeCallback analyzeCallback = new CodeUtils.AnalyzeCallback() {@Overridepublic void onAnalyzeSuccess(Bitmap mBitmap, String result) {Intent resultIntent = new Intent();Bundle bundle = new Bundle();bundle.putInt(CodeUtils.RESULT_TYPE, CodeUtils.RESULT_SUCCESS);bundle.putString(CodeUtils.RESULT_STRING, result);resultIntent.putExtras(bundle);TwoActivity.this.setResult(RESULT_OK, resultIntent);TwoActivity.this.finish();}@Overridepublic void onAnalyzeFailed() {Intent resultIntent = new Intent();Bundle bundle = new Bundle();bundle.putInt(CodeUtils.RESULT_TYPE, CodeUtils.RESULT_FAILED);bundle.putString(CodeUtils.RESULT_STRING, "");resultIntent.putExtras(bundle);TwoActivity.this.setResult(RESULT_OK, resultIntent);TwoActivity.this.finish();}};
}
mainactivity跳转到扫描类以及扫描结果回调、
public static final int REQUEST_CODE = 111;
默认扫描类
Intent intent = new Intent(getApplication(), CaptureActivity.class);
startActivityForResult(intent, REQUEST_CODE);
自定义扫描类
Intent intent = new Intent(MainActivity.this, TwoActivity.class);
startActivityForResult(intent, REQUEST_CODE);
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {/*** 处理二维码扫描结果*/if (requestCode == REQUEST_CODE) {//处理扫描结果(在界面上显示)if (null != data) {Bundle bundle = data.getExtras();if (bundle == null) {return;}if (bundle.getInt(CodeUtils.RESULT_TYPE) == CodeUtils.RESULT_SUCCESS) {String result = bundle.getString(CodeUtils.RESULT_STRING);Toast.makeText(this, "解析结果:" + result, Toast.LENGTH_LONG).show();} else if (bundle.getInt(CodeUtils.RESULT_TYPE) == CodeUtils.RESULT_FAILED) {Toast.makeText(MainActivity.this, "解析二维码失败", Toast.LENGTH_LONG).show();}}}}
1、文件activity_second.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"android:id="@+id/activity_second"android:layout_width="match_parent"android:layout_height="match_parent"><FrameLayoutandroid:id="@+id/fl_my_container"android:layout_width="match_parent"android:layout_height="match_parent"android:visibility="visible"></FrameLayout></FrameLayout>
2、文件my_camera.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"android:layout_width="fill_parent"android:layout_height="fill_parent" ><SurfaceViewandroid:id="@+id/preview_view"android:layout_width="wrap_content"android:layout_height="wrap_content"/><com.uuzuche.lib_zxing.view.ViewfinderViewandroid:id="@+id/viewfinder_view"android:layout_width="wrap_content"android:layout_height="wrap_content"app:inner_width="200dp"app:inner_height="200dp"app:inner_margintop="150dp"app:inner_corner_color="@color/scan_corner_color"app:inner_corner_length="30dp"app:inner_corner_width="5dp"app:inner_scan_bitmap="@drawable/scan_image"app:inner_scan_speed="10"app:inner_scan_iscircle="false"/></FrameLayout>