Android之掃描二維碼和根據輸入資訊生成名片二維碼
阿新 • • 發佈:2019-02-12
開發中常常遇到二維碼掃碼操作,前段時間做專案要實現該功能,於是網上查詢資料實現了,現在把他做出來給各位分享一下,主要包含了二維碼掃描和生成二維碼名片,
先來看看效果圖:
生成的二維碼,開啟微信掃一掃即可看到生成的名片了,可以儲存在聯絡人中。
二維碼掃描方式如下圖:
生成名片程式碼
public class BusinessCardActivity extends Activity{ private EditText et_only_company; private EditText et_only_position; private EditText et_only_phone; private EditText et_only_email; private EditText et_only_web1; private EditText et_only_add; private EditText et_only_note; private EditText et_only_name; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.activity_generate); et_only_company= (EditText) findViewById(R.id.et_only_company); et_only_position= (EditText) findViewById(R.id.et_only_position); et_only_phone= (EditText) findViewById(R.id.et_only_phone); et_only_email= (EditText) findViewById(R.id.et_only_email); et_only_web1= (EditText) findViewById(R.id.et_only_web1); et_only_add= (EditText) findViewById(R.id.et_only_add); et_only_note= (EditText) findViewById(R.id.et_only_note); et_only_name= (EditText) findViewById(R.id.et_only_name); initView(); } private void initView(){ findViewById(R.id.but).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub String name = et_only_name.getText().toString().trim(); String company = et_only_company.getText().toString().trim(); String position = et_only_position.getText().toString().trim(); String phone = et_only_phone.getText().toString().trim(); String email = et_only_email.getText().toString().trim(); String web1 = et_only_web1.getText().toString().trim(); String add = et_only_add.getText().toString().trim(); String note = et_only_note.getText().toString().trim(); String contents = "BEGIN:VCARD\nVERSION:3.0\n" + "N:" + name + "\nORG:" + company + "\nTITLE:" + position + "\nNOTE:" + note + "\nTEL:" + phone + "\nADR:" + add + "\nURL:" + web1 + "\nEMAIL:" + email + "\nEND:VCARD"; try { Bitmap bm = qr_code(contents, BarcodeFormat.QR_CODE); ImageView img = (ImageView) findViewById(R.id.img_only); img.setImageBitmap(bm); } catch (WriterException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }); } public Bitmap qr_code(String string, BarcodeFormat format) throws WriterException { MultiFormatWriter writer = new MultiFormatWriter(); Hashtable<EncodeHintType, String> hst = new Hashtable<EncodeHintType, String>(); hst.put(EncodeHintType.CHARACTER_SET, "UTF-8"); BitMatrix matrix = writer.encode(string, format, 400, 400, hst); int width = matrix.getWidth(); int height = matrix.getHeight(); int[] pixels = new int[width * height]; for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { if (matrix.get(x, y)) { pixels[y * width + x] = 0xff000000; } } } Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); // 通過畫素陣列生成bitmap,具體參考api bitmap.setPixels(pixels, 0, width, 0, 0, width, height); return bitmap; } }
原始碼有點多就不一一貼出來了,直接下載原始碼即可。