Android之掃描二維碼和根據輸入資訊產生名片二維碼

來源:互聯網
上載者:User

標籤:config   down   and   imageview   super   https   lock   getwidth   dac   

開發中常常遇到二維碼掃碼操作,前段時間做項目要實現該功能,於是網上尋找資料實現了,現在把他做出來給各位分享一下,主要包含了二維碼掃描和產生二維碼名片。

先來看看: 

   

 

產生的二維碼,開啟掃一掃即可看到產生的名片了,可以儲存在連絡人中。

二維碼掃描方式如:

產生名片代碼

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;    }}

源碼有點多就不一一貼出來了,直接下載源碼即可。

源碼點擊下載

Android之掃描二維碼和根據輸入資訊產生名片二維碼

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.