butterknife依赖和工具类
implementation 'com.jakewharton:butterknife:10.2.1'
annotationProcessor 'com.jakewharton:butterknife-compiler:10.2.1'
implementation 'com.github.tongchexinfeitao:QRCodeLibrary:1.0'
app build.gradle最上面
apply plugin: 'com.jakewharton.butterknife'
defaultConfig里
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
根目录
repositories里
mavenCentral()
maven { url 'https://jitpack.io' }
dependencies 里
classpath 'com.jakewharton:butterknife-gradle-plugin:10.2.1'
package com.bawei.dinghongxiangforwardnetsample;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import com.uuzuche.lib_zxing.activity.CodeUtils;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
public class MainActivity extends AppCompatActivity {
@BindView(R.id.e1)//输入框
EditText e1;
@BindView(R.id.b1)//生成二维码
Button b1;
@BindView(R.id.iv)//生成的二维码占位图
ImageView iv;
@BindView(R.id.bb)//打开相机扫一扫
Button bb;
@BindView(R.id.b2)//从相册扫取二维码
Button b2;
Unbinder unbinder;
private Bitmap image;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
unbinder=ButterKnife.bind(this);//初始化黄油刀
CodeUtils.init(this);
initData();
}
private void initData() {
//生成二维码
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//判空处理
if (TextUtils.isEmpty(e1.getText().toString())){
Toast.makeText(MainActivity.this, "关键字不能为空", Toast.LENGTH_SHORT).show();
return;
}
//给要生成的图片设置数据,大小,以及有无logo
image = CodeUtils.createImage(e1.getText().toString(), 400, 400, null);
//设置图片
iv.setImageBitmap(image);
}
});
//打开相机扫一扫
bb.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
CodeUtils.analyzeByCamera(MainActivity.this);
}
});
//从相册扫取二维码
b2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
CodeUtils.analyzeByPhotos(MainActivity.this);
}
});
//长按二维码图片进行识别
iv.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View view) {
CodeUtils.analyzeByImageView(iv, new CodeUtils.AnalyzeCallback() {
//识别成功
@Override
public void onAnalyzeSuccess(Bitmap mBitmap, String result) {
Toast.makeText(MainActivity.this, ""+result, Toast.LENGTH_SHORT).show();
}
//识别失败
@Override
public void onAnalyzeFailed() {
Toast.makeText(MainActivity.this, "获取失败", Toast.LENGTH_SHORT).show();
}
});
return true;
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
CodeUtils.onActivityResult(this, requestCode, resultCode, data, new CodeUtils.AnalyzeCallback() {
//扫描成功
@Override
public void onAnalyzeSuccess(Bitmap mBitmap, String result) {
Toast.makeText(MainActivity.this, ""+result, Toast.LENGTH_SHORT).show();
}
//扫描失败
@Override
public void onAnalyzeFailed() {
Toast.makeText(MainActivity.this, "扫描失败", Toast.LENGTH_SHORT).show();
}
});
}
@Override
protected void onDestroy() {
super.onDestroy();
if (unbinder!=null){
unbinder.unbind();//释放资源
}
}
}