导包
implementation project(':zxinglibrary')
权限
<uses-permission android:name="android.permission.CAMERA" /> <uses-permission android:name="android.permission.FLASHLIGHT" /> <uses-permission android:name="android.permission.HARDWARE_TEST" tools:ignore="ProtectedPermissions"></uses-permission>
布局
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.example.day4_m.MainActivity"> <Button android:id="@+id/btStart" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="开启二维码扫描"/> <Button android:id="@+id/btStart2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="开启自己二维码扫描"/> <Button android:id="@+id/btStart3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="生成二维码"/> <ImageView android:id="@+id/iv" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </LinearLayout>
item
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <FrameLayout android:id="@+id/fl" android:layout_width="match_parent" android:layout_height="match_parent"></FrameLayout> <Button android:id="@+id/bt" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_marginBottom="50dp" android:text="打开闪光灯"/> </RelativeLayout>
java
Myapp
package com.example.day4_m; import android.app.Application; import android.util.Log; import com.dash.zxinglibrary.activity.ZXingLibrary; /** * Created by asus on 2018/4/13. */ public class Myapp extends Application { @Override public void onCreate() { super.onCreate(); Log.e("MyApp","MyApp 执行了"); //初始化类库 ZXingLibrary.initDisplayOpinion(this); } }
MyCaptureActivity
package com.example.day4_m; import android.content.Intent; import android.graphics.Bitmap; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.Button; import android.widget.FrameLayout; import com.dash.zxinglibrary.activity.CaptureFragment; import com.dash.zxinglibrary.activity.CodeUtils; public class MyCaptureActivity extends AppCompatActivity implements View.OnClickListener { private FrameLayout mFl; /** * 打开闪光灯 */ private Button mBt; private boolean isLightOpen; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.item); initView(); CaptureFragment captureFragment = new CaptureFragment(); captureFragment.setAnalyzeCallback(analyzeCallback); getSupportFragmentManager().beginTransaction().replace(R.id.fl, captureFragment).commit(); } private void initView() { mFl = (FrameLayout) findViewById(R.id.fl); mBt = (Button) findViewById(R.id.bt); mBt.setOnClickListener(this); } /** * 二维码解析回调函数 */ CodeUtils.AnalyzeCallback analyzeCallback = new CodeUtils.AnalyzeCallback() { //解析成功的回调 @Override public 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); MyCaptureActivity.this.setResult(RESULT_OK, resultIntent); MyCaptureActivity.this.finish(); } //解析失败的回调 @Override public 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); MyCaptureActivity.this.setResult(RESULT_OK, resultIntent); MyCaptureActivity.this.finish(); } }; @Override public void onClick(View v) { switch (v.getId()) { default: break; case R.id.bt: //开启或者关闭闪光灯 isLightOpen = !isLightOpen; CodeUtils.isLightEnable(isLightOpen); break; } } }
MainActivity
package com.example.day4_m; import android.content.Intent; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.Button; import android.widget.ImageView; import android.widget.Toast; import com.dash.zxinglibrary.activity.CaptureActivity; import com.dash.zxinglibrary.activity.CodeUtils; public class MainActivity extends AppCompatActivity implements View.OnClickListener { /** * 开启二维码扫描 */ private Button mBtStart; /** * 开启自己二维码扫描 */ private Button mBtStart2; /** * 生成二维码 */ private Button mBtStart3; private ImageView mIv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); } private void initView() { mBtStart = (Button) findViewById(R.id.btStart); mBtStart.setOnClickListener(this); mBtStart2 = (Button) findViewById(R.id.btStart2); mBtStart2.setOnClickListener(this); mBtStart3 = (Button) findViewById(R.id.btStart3); mBtStart3.setOnClickListener(this); mIv = (ImageView) findViewById(R.id.iv); } @Override public void onClick(View v) { switch (v.getId()) { default: break; case R.id.btStart: Intent intent = new Intent(this, CaptureActivity.class); startActivityForResult(intent,1); break; case R.id.btStart2: Intent intent1 = new Intent(this, MyCaptureActivity.class); startActivityForResult(intent1,1); break; case R.id.btStart3: //生成二维码 Bitmap logo = BitmapFactory.decodeResource(getResources(), R.drawable.a); Bitmap bitmap = CodeUtils.createImage("https://www.vmall.com/", 200, 200, logo); mIv.setImageBitmap(bitmap); break; } } @Override protected void onActivityResult(int requestCode, int resultCode, Intent intent) { //取Bundle if (requestCode == 1&& intent !=null) { Bundle bundle = intent.getExtras(); if (bundle == null) { return; } if (bundle.getInt(CodeUtils.RESULT_TYPE) == CodeUtils.RESULT_SUCCESS) { String string = bundle.getString(CodeUtils.RESULT_STRING); Toast.makeText(MainActivity.this, "扫描内容是:" + string, Toast.LENGTH_SHORT).show(); } else if (bundle.getInt(CodeUtils.RESULT_TYPE) == CodeUtils.RESULT_FAILED) { Toast.makeText(MainActivity.this, "扫描失败", Toast.LENGTH_SHORT).show(); } } } }