1. 程式人生 > >Android實現生成二維碼

Android實現生成二維碼

public class EwmActivity extends AppCompatActivity {

    private EditText text;
    private Button button;
    private ImageView image;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ewm);
        text = (EditText) findViewById(R.id.text);
        button = (Button) findViewById(R.id.erwei);
        image = (ImageView) findViewById(R.id.image);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                createQRcode();
            }
        });

    }

    private void createQRcode() {
        @SuppressLint("StaticFieldLeak") AsyncTask<Void, Integer, Bitmap> asyncTask = new AsyncTask<Void, Integer, Bitmap>() {
            @Override
            protected Bitmap doInBackground(Void... strings) {
                String trim = text.getText().toString().trim();

                return QRCodeEncoder.syncEncodeQRCode(trim, 150);
            }

            @Override
            protected void onPostExecute(Bitmap bitmap) {
                super.onPostExecute(bitmap);
                image.setImageBitmap(bitmap);
            }
        }.execute();
    }
}