1. 程式人生 > >二維碼掃描和生成二維碼

二維碼掃描和生成二維碼

1.佈局

<?xml version="1.0" encoding="utf-8"?>
<cn.bingoogolapple.qrcode.zxing.ZXingView
    android:id="@+id/zxing"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:qrcv_animTime="1000"
    app:qrcv_borderColor="@android:color/white"
    app:qrcv_borderSize="1dp"
    app:qrcv_cornerColor="@color/colorPrimaryDark"
    app:qrcv_cornerLength="20dp"
    app:qrcv_cornerSize="3dp"
    app:qrcv_maskColor="#33FFFFFF"
    app:qrcv_rectWidth="200dp"
    app:qrcv_scanLineColor="@color/colorPrimaryDark"
    app:qrcv_scanLineSize="1dp"
    app:qrcv_topOffset="90dp"
    android:layout_weight="1"
    />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="生成二維碼"
    android:id="@+id/shengcheng"
    android:layout_gravity="center_horizontal"
    />
2.掃描Activity package com.example.zhou2;

import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.Toast;

import cn.bingoogolapple.qrcode.core.QRCodeView; import cn.bingoogolapple.qrcode.zxing.ZXingView;

public class Main2Activity extends AppCompatActivity implements QRCodeView.Delegate {

private ZXingView zXingView;
private Button sheng;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main2);
    initView();
    initZView();
    initListener();
}

private void initListener() {
    sheng.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            startActivity(new Intent(Main2Activity.this,Shengcheng.class));
        }
    });
}

private void initView() {
    zXingView = findViewById(R.id.zxing);
    sheng = findViewById(R.id.shengcheng);
}

private void initZView() {
    zXingView.startCamera();
    zXingView.startSpot();
    zXingView.showScanRect();
    zXingView.setDelegate(this);
}

@Override
public void onScanQRCodeSuccess(String result) {
    Toast.makeText(this, result, Toast.LENGTH_SHORT).show();
}

@Override
public void onCameraAmbientBrightnessChanged(boolean isDark) {

}

@Override
public void onScanQRCodeOpenCameraError() {

}

@Override
protected void onStop() {
    super.onStop();
    zXingView.stopSpot();
    zXingView.stopCamera();
}

} 3.生成二維碼佈局

<?xml version="1.0" encoding="utf-8"?>
<SearchView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/sh"
    ></SearchView>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="飛灑地方撒旦法師的法師打發第三方三房  颯颯大V阿薩德個人gas  發的發撒的發生分 是的反腐發生大范德薩撒地方撒旦 撒旦飛灑發生的發生   法法師是負擔是"
    android:ellipsize="marquee"
    android:marqueeRepeatLimit="marquee_forever"
    android:focusable="true"
    android:focusableInTouchMode="true"
    android:singleLine="true"
    />

<EditText
    android:layout_width="150dp"
    android:layout_height="wrap_content"
    android:id="@+id/shu"
    android:hint="請輸入"
    />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/shengcheng"
    android:text="生成二維碼"
    />
<ImageView
    android:id="@+id/image"
    android:layout_width="200dp"
    android:layout_height="200dp"
    android:src="@mipmap/ic_launcher"
    />
4.生成二維碼Activity package com.example.zhou2;

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.zxing.QRCodeEncoder;

public class Shengcheng extends AppCompatActivity {

private ImageView image;
private String s;
private EditText shuru;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_shengcheng);

    image = findViewById(R.id.image);
    shuru = findViewById(R.id.shu);
    Button sh = findViewById(R.id.shengcheng);

    sh.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            s = shuru.getText().toString().trim();
            createQRcode();
        }
    });
}

private void createQRcode() {
    new AsyncTask<Void, Void, Bitmap>() {
        @Override
        protected Bitmap doInBackground(Void... strings) {
            Bitmap logo = BitmapFactory.decodeResource(Shengcheng.this.getResources(),R.drawable.h1);
            return QRCodeEncoder.syncEncodeQRCode(s,BGAQRCodeUtil.dp2px(Shengcheng.this,200),Color.RED,logo);
        }

        @Override
        protected void onPostExecute(Bitmap bitmap) {
            super.onPostExecute(bitmap);
            if (bitmap!=null){
                image.setImageBitmap(bitmap);
            }else {
                Toast.makeText(Shengcheng.this,"生成失敗",Toast.LENGTH_SHORT).show();
            }
        }
    }.execute();
}

}