1. 程式人生 > >Android 生成帶圖片的二維碼

Android 生成帶圖片的二維碼

public class MainActivity extends Activity {
    private EditText etCompany;
    private EditText etPhone;
    private EditText etEmail;
    private EditText etWeb;
    private Bitmap logo;
    private static final int IMAGE_HALFWIDTH = 40;//寬度值,影響中間圖片大小
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //獲得資源圖片,可改成獲取本地圖片或拍照獲取圖片
        logo=BitmapFactory.decodeResource(super.getResources(),R.drawable.y014);
        etCompany =(EditText) findViewById(R.id.etCompany);
        etPhone=(EditText) findViewById(R.id.etPhone);
        etEmail =(EditText) findViewById(R.id.etEmail);
        etWeb =(EditText) findViewById(R.id.etWeb); 
        findViewById(R.id.but).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                String company=etCompany.getText().toString().trim() ;
                String phone =etPhone .getText().toString().trim() ; 
                String email = etEmail.getText().toString().trim() ;
                String web = etWeb.getText().toString().trim() ; 
                //二維碼中包含的文字資訊
                String contents= "BEGIN:VCARD/nVERSION:3.0/nORG:"+company+"/nTEL:"+phone+"/nURL:"+web+"/nEMAIL:"+email+"/nEND:VCARD";
            try {
                //呼叫方法createCode生成二維碼
                Bitmap bm=createCode(contents,logo,BarcodeFormat.QR_CODE);
                ImageView img=(ImageView)findViewById(R.id.imgCode) ;
                //將二維碼在介面中顯示
                img.setImageBitmap(bm);
            } catch (WriterException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } 

            }
        });

    }

    /**
     * 生成二維碼
     * @param string 二維碼中包含的文字資訊
     * @param mBitmap logo圖片
     * @param format  編碼格式
     * @return Bitmap 點陣圖
     * @throws WriterException
     */
    public Bitmap createCode(String string,Bitmap mBitmap, BarcodeFormat format)
            throws WriterException {
        Matrix m = new Matrix();
        float sx = (float) 2 * IMAGE_HALFWIDTH / mBitmap.getWidth();
        float sy = (float) 2 * IMAGE_HALFWIDTH
                / mBitmap.getHeight();
        m.setScale(sx, sy);//設定縮放資訊
        //將logo圖片按martix設定的資訊縮放
        mBitmap = Bitmap.createBitmap(mBitmap, 0, 0,
                mBitmap.getWidth(), mBitmap.getHeight(), m, false);
        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 halfW = width / 2;
        int halfH = height / 2;
        int[] pixels = new int[width * height];//定義陣列長度為矩陣高度*矩陣寬度,用於記錄矩陣中畫素資訊
        for (int y = 0; y < height; y++) {//從行開始迭代矩陣
            for (int x = 0; x < width; x++) {//迭代列
                if (x > halfW - IMAGE_HALFWIDTH && x < halfW + IMAGE_HALFWIDTH
                        && y > halfH - IMAGE_HALFWIDTH
                        && y < halfH + IMAGE_HALFWIDTH) {//該位置用於存放圖片資訊
//記錄圖片每個畫素資訊
                    pixels[y * width + x] = mBitmap.getPixel(x - halfW
                            + IMAGE_HALFWIDTH, y - halfH + IMAGE_HALFWIDTH);                } else {
                    if (matrix.get(x, y)) {//如果有黑塊點,記錄資訊
                        pixels[y * width + x] = 0xff000000;//記錄黑塊資訊
                    }
                }

            }
        }
        Bitmap bitmap = Bitmap.createBitmap(width, height,
                Bitmap.Config.ARGB_8888);
        // 通過畫素陣列生成bitmap
        bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
        return bitmap;
    }

}