1.依赖
implementation 'cn.bingoogolapple:bga-qrcode-zxing:1.3.6'
2.权限
<uses-permission android:name="android.permission.CAMERA" />
3.布局
<cn.bingoogolapple.qrcode.zxing.ZXingView
android:id="@+id/scan_qr_code"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:qrcv_borderColor="#fff"
app:qrcv_borderSize="1dp"
app:qrcv_cornerLength="20dp"
app:qrcv_cornerColor="#ff00"
app:qrcv_cornerSize="3dp"
app:qrcv_maskColor="#33ffffff"
app:qrcv_rectWidth="200dp"
app:qrcv_scanLineColor="@color/colorPrimary"
app:qrcv_scanLineSize="1dp"
app:qrcv_topOffset="90dp"
>
</cn.bingoogolapple.qrcode.zxing.ZXingView>
4.代码
扫描二维码
public class ScanQrCodeActivity extends AppCompatActivity implements QRCodeView.Delegate {
@BindView(R.id.scan_qr_code)
ZXingView scanQrCode;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_scan_qr_code);
ButterKnife.bind(this);
//scanQrCode.setType(BarcodeType.CUSTOM,);
//设置扫描二维码的代理
scanQrCode.setDelegate(this);
//显示扫描框
scanQrCode.showScanRect();
//隐藏扫描框
//scanQrCode.hiddenScanRect();
//打开后置摄像头开始预览,但是并未开始识别
scanQrCode.startCamera();
//关闭摄像头预览,并且隐藏扫描框
//scanQrCode.stopCamera();
//开始识别
scanQrCode.startSpot();
//停止识别
//scanQrCode.stopSpot();
//停止识别,并且隐藏扫描框
//scanQrCode.stopSpotAndHiddenRect();
// 显示扫描框,并开始识别
scanQrCode.startSpotAndShowRect();
//打开闪光灯
//scanQrCode.openFlashlight();
//关闭散网灯
scanQrCode.closeFlashlight();
//解析本地图片二维码。返回二维码图片里的内容 或 null
//解析 Bitmap 二维码。返回二维码图片里的内容 或 null
//scanQrCode.decodeQRCode("");
}
@Override
public void onScanQRCodeSuccess(String result) {
Toast.makeText(ScanQrCodeActivity.this,result,Toast.LENGTH_SHORT).show();
}
@Override
public void onCameraAmbientBrightnessChanged(boolean isDark) {
}
@Override
public void onScanQRCodeOpenCameraError() {
}
}
制作二维码
public class MakeQrCodeActivity extends AppCompatActivity {
@BindView(R.id.make_value)
EditText makeValue;
@BindView(R.id.make_image)
Button makeImage;
@BindView(R.id.image)
ImageView image;
Handler handler=new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
switch (msg.what){
case 0:
break;
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_make_qr_code);
ButterKnife.bind(this);
}
@OnClick({R.id.make_image, R.id.image})
public void onViewClicked(View view) {
switch (view.getId()) {
case R.id.make_image:
String make = makeValue.getText().toString().trim();
makeQrImaae(make);
break;
}
}
private void makeQrImaae(final String make) {
Bitmap bitmap = QRCodeEncoder.syncEncodeQRCode(make, 200);
image.setImageBitmap(bitmap);
}
}