標籤:
由於的推出二維碼走進了我們的生活,並且越來越多的人們正在發揮著自己的想象力去使用它,來方便我們的生活,我曾經聽說過一個笑話,當我們死後,墓碑上不再有墓誌銘,而會出現一個記錄你一生資訊的二維碼,當人們走到你們的墓碑前,掏出手機掃一掃就可以看到你一生的豐功偉績。這是不是很有意思,我都認這會在不久的將來成為現實,哈哈,玩笑說完了,下面我們來一起學習一下如何在Android開發中讓二維碼為我們服務。
本篇我將會帶領朋友們實現一個記錄個人基本資料的二維碼設計思路,對於搞過演算法的大牛們,這裡要讓你們失望了,對於二維碼產生的演算法,本人才疏學淺尚且無法為大家分享,本篇案例的實現我們將藉助core.jar實現,對於這個jar包的下載,我為大家提供一個連結,方便大家學習使用:http://pan.baidu.com/s/1bnGZoF9
準備好我們的jar包後,我們開始今天的設計,第一步:建立工程,匯入jar包
在我們的整合式開發環境中,建立一個Android工程項目,為我們今天案例的設計做鋪墊。建立好工程後,將我們剛剛下載好的jar包匯入到我們的工程中,Ctrl+c我們的jar包,在我們的工程目錄下找到libs檔案夾Ctrl+v,然後呢?就是通過整合式開發環境將我們的jar包匯入到工程。
第二步:建立我們的布局檔案:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical" android:id="@+id/ll" > <!-- 用於展示我們建立的二維碼 --> <ImageView android:id="@+id/imgCode" android:layout_width="100dip" android:layout_height="100dip" android:layout_gravity="center_horizontal" /> <!-- 公司 --> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="公司" /> <EditText android:id="@+id/etCompany" android:layout_width="fill_parent" android:layout_height="wrap_content" android:hint="選填" /> </LinearLayout> <!-- 電話 --> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="電話" /> <EditText android:id="@+id/etPhone" android:layout_width="fill_parent" android:layout_height="wrap_content" android:hint="選填" /> </LinearLayout> <!-- 郵箱 --> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="郵箱" /> <EditText android:id="@+id/etEmail" android:layout_width="fill_parent" android:layout_height="wrap_content" android:hint="選填" /> </LinearLayout> <!-- 網址 --> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="網址" /> <EditText android:id="@+id/etWeb" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="http://" /> </LinearLayout> <Button android:id="@+id/but" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="產生二維碼"/> </LinearLayout></RelativeLayout>
第三步:編輯我們Activity:
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.ic_launcher); 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) { 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) { 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; } }
上面的代碼注釋已經非常詳細,這裡我再簡單說幾句,這部分代碼分為上下兩部分,上部分都是我們經常使用的,下部分則是我們二維碼建立的重點。ok到這裡我們的效果就實現了,最後上一張:
Android二維碼之建立