implementation 'cn.bingoogolapple:bga-qrcode-zxing:1.3.4'
activity
public class AnimatorOrCodeActivity extends AppCompatActivity implements View.OnClickListener {
private ImageView animatorIcon;
private ImageView iconqrcode;
private Button btn_qrcode_sweep, btn_QQLogin;
private Button btn_qrcode;
private EditText textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_animator_or_code);
initView();
}
private void initView() {
animatorIcon = findViewById(R.id.animator_icon);
iconqrcode = findViewById(R.id.icon_qrcode);
btn_qrcode_sweep = findViewById(R.id.btn_qrcode_sweep);
btn_qrcode = findViewById(R.id.btn_qrcode);
textView = findViewById(R.id.text_qrcode);
btn_QQLogin = findViewById(R.id.btn_QQLogin);
animatorIcon.setOnClickListener(this);
btn_qrcode.setOnClickListener(this);
btn_qrcode_sweep.setOnClickListener(this);
btn_QQLogin.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.animator_icon:
break;
case R.id.btn_qrcode:
creatQRCode();
break;
case R.id.btn_qrcode_sweep:
checkedPermission();
break;
case R.id.btn_QQLogin:
UMShareAPI umShareAPI = UMShareAPI.get(AnimatorOrCodeActivity.this);
umShareAPI.getPlatformInfo(AnimatorOrCodeActivity.this, SHARE_MEDIA.QQ, new UMAuthListener() {
@Override
public void onStart(SHARE_MEDIA share_media) {
}
@Override
public void onComplete(SHARE_MEDIA share_media, int i, Map<String, String> map) {
String name = map.get("screen_name");
String img = map.get("profile_image_url");
Log.i("sl", "name is "+name);
Log.i("sl",img);
Intent intent = new Intent(AnimatorOrCodeActivity.this, SuccessActivity.class);
intent.putExtra("name", name);
intent.putExtra("img", img);
startActivity(intent);
}
@Override
public void onError(SHARE_MEDIA share_media, int i, Throwable throwable) {
}
@Override
public void onCancel(SHARE_MEDIA share_media, int i) {
}
});
break;
default:
break;
}
}
private void checkedPermission() {
startActivity(new Intent(AnimatorOrCodeActivity.this, SweepActivity.class));
}
private void creatQRCode() {
QRTask qrTask = new QRTask(this, iconqrcode, textView);
qrTask.execute(textView.getText().toString());
}
static class QRTask extends AsyncTask<String, Void, Bitmap> {
private WeakReference<Context> mContext;
private WeakReference<ImageView> imageView;
public QRTask(Context context, ImageView mimageView, EditText editText) {
mContext = new WeakReference<>(context);
imageView = new WeakReference<>(mimageView);
}
@Override
protected Bitmap doInBackground(String... strings) {
String str = strings[0];
if (TextUtils.isEmpty(str)) {
return null;
}
return QRCodeEncoder.syncEncodeQRCode(str, 300);
}
@Override
protected void onPostExecute(Bitmap bitmap) {
super.onPostExecute(bitmap);
if (bitmap != null) {
imageView.get().setImageBitmap(bitmap);
} else {
Toast.makeText(mContext.get(), "生成失败", Toast.LENGTH_SHORT).show();
}
}
}
}
xml
<?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"
tools:context=".AnimatorOrCodeActivity"
android:orientation="vertical">
<ImageView
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_gravity="center_horizontal"
android:id="@+id/animator_icon"
android:layout_marginTop="20dp"
android:src="@mipmap/ic_launcher"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal"
android:layout_margin="30dp">
<EditText
android:layout_width="0dp"
android:layout_weight="7"
android:layout_height="50dp"
android:layout_gravity="center_vertical"
android:hint="请输入内容"
android:textSize="20sp"
android:id="@+id/text_qrcode"/>
<Button
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="3"
android:layout_marginLeft="10dp"
android:text="生成二维码"
android:background="@android:color/holo_orange_light"
android:textColor="#fff"
android:id="@+id/btn_qrcode"/>
</LinearLayout>
<ImageView
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_margin="5dp"
android:id="@+id/icon_qrcode"
android:layout_gravity="center_horizontal"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal"
android:layout_margin="30dp">
<Button
android:layout_width="100dp"
android:layout_height="30dp"
android:layout_marginLeft="10dp"
android:text="扫一扫"
android:background="@android:color/holo_orange_light"
android:textColor="#fff"
android:layout_gravity="center_horizontal"
android:id="@+id/btn_qrcode_sweep"/>
<Button
android:layout_width="100dp"
android:layout_height="30dp"
android:layout_marginLeft="10dp"
android:text="第三方登录"
android:background="@android:color/holo_orange_light"
android:textColor="#fff"
android:layout_gravity="center_horizontal"
android:id="@+id/btn_QQLogin"/>
</LinearLayout>
</LinearLayout>
相机
public class SweepActivity extends AppCompatActivity implements QRCodeView.Delegate{
private ZXingView xingView;
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sweep);
//获取资源ID
xingView=findViewById(R.id.zxingView);
xingView.setDelegate(this);
}
@Override
protected void onResume(){
super.onResume();
xingView.startCamera();
xingView.startSpotAndShowRect();
/*xingView.openFlashlight();*/
}
@Override
protected void onStop(){
super.onStop();
xingView.stopCamera();
}
@Override
protected void onDestroy(){
super.onDestroy();
xingView.onDestroy();
}
@Override
public void onScanQRCodeSuccess(String result){
Toast.makeText(this,result.toString(),Toast.LENGTH_SHORT).show();
}
@Override
public void onCameraAmbientBrightnessChanged(boolean isDark){
//环境改变,是否变暗,变暗isDark为true
}
@Override
public void onScanQRCodeOpenCameraError(){
//打开相机失败
}
}
xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".SweepActivity">
<cn.bingoogolapple.qrcode.zxing.ZXingView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/zxingView"></cn.bingoogolapple.qrcode.zxing.ZXingView>
</android.support.constraint.ConstraintLayout>