先来看效果图怎样,可以的话就留着用吧,开发中要的就是速度快,效果好,简单易操作
1:在这里写个button按钮用于启动二维码
2:这是打开后的界面 左下角可以设置开启灯光,右下角可以选择相册中的二维码
一:喜欢的话往下看教你一步步实现,如果想把图中的蓝条换成雷达扫描类的只需要在布局中添加SurfaceView然后在代码中简单设置就可以了,我就不在这里写那个了原理一样
1:添加jar包
compile 'com.github.XuDaojie:QRCode-Android:v0.4.2'
2:一定把仓库地址加上,很重要
3:权限清单文件
4;部分代码
下面是代码部分
MAinActivity部分
package com.lt.scantest2;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
private static final int REQUEST_QR_CODE = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button qrCodeBtn = (Button) findViewById(R.id.qrcode_btn);
qrCodeBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(MainActivity.this, SimpleCaptureActivity.class);
MainActivity.this.startActivityForResult(i, REQUEST_QR_CODE);
}
});
}
// @Override
// protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// super.onActivityResult(requestCode, resultCode, data);
// if (resultCode == RESULT_OK
// && requestCode == REQUEST_QR_CODE
// && data != null) {
// String result = data.getStringExtra("result");
// Toast.makeText(MainActivity.this, result, Toast.LENGTH_SHORT).show();
// }
// }
}
SimpleCaptureActivity
package com.lt.scantest2;
import android.app.Activity;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.text.TextUtils;
import android.widget.Toast;
import io.github.xudaojie.qrcodelib.CaptureActivity;
/**
* Created by xdj on 16/9/17.
*/
public class SimpleCaptureActivity extends CaptureActivity {
protected Activity mActivity = this;
private AlertDialog mDialog;
@Override
public void onCreate(Bundle savedInstanceState) {
mActivity = this;
super.onCreate(savedInstanceState);
}
@Override
protected void handleResult(String resultString) {
if (TextUtils.isEmpty(resultString)) {
Toast.makeText(mActivity, io.github.xudaojie.qrcodelib.R.string.scan_failed, Toast.LENGTH_SHORT).show();
restartPreview();
} else {
if (mDialog == null) {
mDialog = new AlertDialog.Builder(mActivity)
.setMessage(resultString)
.setPositiveButton("确定", null)
.create();
mDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
@Override
public void onDismiss(DialogInterface dialog) {
restartPreview();
}
});
}
if (!mDialog.isShowing()) {
mDialog.setMessage(resultString);
mDialog.show();
}
}
}
}
布局文件我就不贴了如果有需要的我给大家我的源码地址查看全部
https://git.oschina.net/null_838_9031/ScanTest2