zoukankan      html  css  js  c++  java
  • Android之扫描二维码和根据输入信息生成名片二维码

    开发中常常遇到二维码扫码操作,前段时间做项目要实现该功能,于是网上查找资料实现了,现在把他做出来给各位分享一下,主要包含了二维码扫描和生成二维码名片。

    先来看看效果图: 

       

    生成的二维码,打开微信扫一扫即可看到生成的名片了,可以保存在联系人中。

    二维码扫描方式如下图:

    生成名片代码

    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
    VERSION:3.0
    " + "N:" + name
                            + "
    ORG:" + company + "
    TITLE:" + position
                            + "
    NOTE:" + note + "
    TEL:" + phone + "
    ADR:" + add
                            + "
    URL:" + web1 + "
    EMAIL:" + email + "
    END: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;
        }
    }

    源码有点多就不一一贴出来了,直接下载源码即可。

    源码点击下载

  • 相关阅读:
    Tableau Sheet
    Tableau Dashboard
    jQuery基础
    Tableau学习
    SQL小操作
    C#文件压缩成.Zip
    划水日记之大哥带我走渗透I
    各种推导式
    生成器表达式 (重点)
    生成器 生成器函数
  • 原文地址:https://www.cnblogs.com/zhangqie/p/6634954.html
Copyright © 2011-2022 走看看