|
例: 

apply plugin: 'com.android.application'
android {
compileSdkVersion 26
allprojects {
repositories {
jcenter()
maven { url "https://jitpack.io" }
}
}
defaultConfig {
applicationId "com.example.day_0524_zxing"
minSdkVersion 16
targetSdkVersion 26
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
allprojects {
repositories {
jcenter()
maven { url "https://jitpack.io" }
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation 'com.android.support:appcompat-v7:26.1.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
compile 'com.github.open-android:Zxing:v1.0.3'
}
加个依赖 dependencies {
compile 'com.github.open-android:Zxing:v1.0.3'
}android {
allprojects {
repositories {
jcenter()
maven { url "https://jitpack.io" }
}
}
}
加权限: <uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.FLASHLIGHT" />
MainActivity 主方法类: public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private EditText mContent;
private Button mCreate,mScan;
private ImageView mImage;
private final static int REQ_CODE = 1028;
private TextView mHint;
private TextView mResult;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView() {
mContent = (EditText)findViewById(R.id.edt_content);
mCreate = (Button)findViewById(R.id.btn_create);
mScan = (Button)findViewById(R.id.btn_scan);
mImage = (ImageView)findViewById(R.id.iv_image);
mHint = (TextView)findViewById(R.id.tv_hint);
mResult = (TextView)findViewById(R.id.tv_result);
mCreate.setOnClickListener(this);
mScan.setOnClickListener(this);
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.btn_create :
//生成二维码
String content = mContent.getText().toString().trim();
Bitmap bitmap = null;
try {
bitmap = BitmapUtils.create2DCode(content);
mImage.setVisibility(View.VISIBLE);
mHint.setVisibility(View.GONE);
mImage.setImageBitmap(bitmap);
}catch (Exception e) {
e.printStackTrace();
}
break;
case R.id.btn_scan :
//扫码
Intent intent = new Intent(MainActivity.this, CaptureActivity.class);
startActivityForResult(intent,REQ_CODE);
break;
default:
break;
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQ_CODE) {
mImage.setVisibility(View.VISIBLE);
mContent.setVisibility(View.GONE);
String result = data.getStringExtra(CaptureActivity.SCAN_QRCODE_RESULT);
Bitmap bitmap = data.getParcelableExtra(CaptureActivity.SCAN_QRCODE_BITMAP);
mResult.setText("扫描结果为:" + result);
showToast("扫码结果:"+result);
if (bitmap != null) {
mImage.setImageBitmap(bitmap);
}
}
}
private void showToast(String msg) {
Toast.makeText(MainActivity.this, "" + msg, Toast.LENGTH_SHORT).show();
}
}
activity_main布局: <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<EditText
android:id="@+id/edt_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入内容" />
<Button
android:id="@+id/btn_create"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="生成二维码" />
<Button
android:id="@+id/btn_scan"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="扫码(支持识别相册二维码)" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp">
<ImageView
android:id="@+id/iv_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:visibility="gone" />
<TextView
android:id="@+id/tv_hint"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="扫描返回bitmap"
android:textColor="#f00" />
</RelativeLayout>
<TextView
android:id="@+id/tv_result"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#ffff00"/>
</LinearLayout>
|