依赖
implementation ‘cn.bingoogolapple:bga-qrcode-zxing:1.3.4’
布局
<? version=\"1.0\" encoding=\"utf-8\"?>
<LinearLayout ns:android=\"http://schemas.android.com/apk/res/android\"
ns:app=\"http://schemas.android.com/apk/res-auto\"
ns:tools=\"http://schemas.android.com/tools\"
android:layout_width=\"match_parent\"
android:layout_height=\"match_parent\"
android:orientation=\"vertical\"
tools:context=\".QrCodeActivity\">
<EditText
android:layout_margin=\"20dp\"
android:id=\"@+id/qr\"
android:layout_width=\"match_parent\"
android:layout_height=\"wrap_content\" />
<Button
android:id=\"@+id/click\"
android:layout_margin=\"20dp\"
android:layout_width=\"match_parent\"
android:layout_height=\"wrap_content\"
android:text=\"生成带logo二维码\"/>
<Button
android:id=\"@+id/no_logo\"
android:layout_margin=\"20dp\"
android:layout_width=\"match_parent\"
android:layout_height=\"wrap_content\"
android:text=\"生成不带logo二维码\"/>
<Button
android:id=\"@+id/sm\"
android:layout_margin=\"20dp\"
android:layout_width=\"match_parent\"
android:layout_height=\"wrap_content\"
android:text=\"扫描二维码\"/>
<ImageView
android:id=\"@+id/make_qr\"
android:layout_gravity=\"center\"
android:layout_width=\"100dp\"
android:layout_height=\"100dp\"
android:layout_margin=\"20dp\"/>
<cn.bingoogolapple.qrcode.zxing.ZXingView
android:id=\"@+id/zxing\"
android:layout_width=\"wrap_content\"
android:layout_height=\"wrap_content\"/>
</LinearLayout>
Activity
package com.example.day20rk;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;
import cn.bingoogolapple.qrcode.core.BGAQRCodeUtil;
import cn.bingoogolapple.qrcode.core.QRCodeView;
import cn.bingoogolapple.qrcode.zxing.QRCodeEncoder;
import cn.bingoogolapple.qrcode.zxing.ZXingView;
// TODO: 2018/12/20
public class QrCodeActivity extends AppCompatActivity implements QRCodeView.Delegate {
private EditText qr;
private Button click;
private ImageView make_qr;
private Button sm;
private ZXingView zxing;
private Button no_logo;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_qr_code);
qr = findViewById(R.id.qr);
click = findViewById(R.id.click);
make_qr = findViewById(R.id.make_qr);
sm = findViewById(R.id.sm);
zxing = findViewById(R.id.zxing);
no_logo = findViewById(R.id.no_logo);
sm.set Listener(new View. Listener() {
@Override
public void (View v) {
initZingView();
}
});
click.set Listener(new View. Listener() {
@Override
public void (View v) {
onCreateQrCode();
}
});
no_logo.set Listener(new View. Listener() {
@Override
public void (View v) {
new AsyncTask<String, Integer, Bitmap>() {
@Override
protected Bitmap doInBackground(String... strings) {
String QrContent = qr.getText().toString().trim();
return QRCodeEncoder.syncEncodeQRCode(QrContent,150);
}
@Override
protected void onPostExecute(Bitmap bitmap) {
super.onPostExecute(bitmap);
make_qr.setImageBitmap(bitmap);
}
}.execute();
}
});
}
private void onCreateQrCode() {
new AsyncTask<String, Integer, Bitmap>() {
@Override
protected Bitmap doInBackground(String... strings) {
String QrContent = qr.getText().toString().trim();
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher);
return QRCodeEncoder.syncEncodeQRCode(QrContent,BGAQRCodeUtil.dp2px(QrCodeActivity.this,150),Color.BLACK,bitmap);
}
@Override
protected void onPostExecute(Bitmap bitmap) {
super.onPostExecute(bitmap);
if(bitmap!=null){
make_qr.setImageBitmap(bitmap);
}else {
Toast.makeText(QrCodeActivity.this, \"生成二维码失败\", Toast.LENGTH_SHORT).show();
}
}
}.execute();
}
@Override
protected void () {
super. ();
}
private void initZingView() {
//设置扫描二维码的代理
zxing.setDelegate(this);
//显示扫描框
zxing.showScanRect();
//打开摄像头
zxing.startCamera();
//延迟delay毫秒后开始识别
zxing.startSpotDelay(100);
}
@Override
protected void () {
super. ();
zxing.stopCamera();
}
@Override
protected void onDestroy() {
super.onDestroy();
zxing.onDestroy();
}
@Override
public void onScanQRCodeSuccess(String result) {
}
@Override
public void onCameraAmbientBrightnessChanged(boolean isDark) {
}
@Override
public void onScanQRCodeOpenCameraError() {
}
}
继续阅读与本文标签相同的文章
上一篇 :
镭速云传企业网盘为外贸行业提供解决方案
下一篇 :
教你如何关闭微信朋友圈广告
-
2019年9月份 阿里云域名优惠口令汇总
2026-05-18栏目: 教程
-
《安全说道》第三期 | 你家“大门”关好了吗?猪猪侠有话说
2026-05-18栏目: 教程
-
为什么支付宝有这么多“原生”技术牛人?
2026-05-18栏目: 教程
-
阿里巴巴小程序繁星计划 9月27日有话要说
2026-05-18栏目: 教程
-
为什么我会这么多技术,还这么难找工作
2026-05-18栏目: 教程
