Android開發掃描和生成二維碼功能

現在二維碼在各種app中都顯得很常見,很多的app都會加入一個“掃一掃”的功能。這裏說一下利用第三方類庫開發掃描和生成二維碼的功能。

第一步要先下載第三方類庫 Barcode ,在Eclipse中導入該工程。

第二步新建工程ScanCode,這裏需要引入第三方類庫Barcode:右鍵工程名稱,打開Properties,在Library那裏點擊“Add...”按鈕,選中BarCode,點擊OK,確定。至此,配置項目已經完成。

第三步配置佈局文件,在activity_main.xml中配置如下佈局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <EditText
        android:id="@+id/input"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:hint="@string/inputToCreateCode" />

    <Button
        android:id="@+id/createcode"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/createdemincode" />

    <Button
        android:id="@+id/scan"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/scanMe" />

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <ImageView
            android:id="@+id/twoDimensionCode"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center" />
    </LinearLayout>

</LinearLayout>

佈局文件比較簡單,EditText爲輸入生成二維碼的內容,第一個按鈕爲生成二維碼,第二個按鈕爲掃描二維碼

MainActivity中主要爲對三個組件的監控,生成二維碼:

	private void createCode() {
		String inputUrl = input.getText().toString().trim();
		//如果輸入框沒有內容,發出提示
		if("".equals(inputUrl)){
		Toast toast = Toast.makeText(MainActivity.this, "請輸入你要生成的鏈接!", Toast.LENGTH_SHORT);
		toast.setGravity(Gravity.CENTER_HORIZONTAL, 0, 0);
		ImageView imageView = new ImageView(getApplicationContext());
		imageView.setImageResource(R.drawable.ic_launcher);
		//獲得Toast佈局
		LinearLayout toastLayout = (LinearLayout) toast.getView();
		//設置佈局爲橫向的
		toastLayout.setOrientation(LinearLayout.HORIZONTAL);
		toastLayout.addView(imageView,0);
		toast.show();
			
		}
		else{
			try {
				/*
				 *  這裏調用類庫裏的EncodingHandler類中的createQRCode方法,
				 *  第一個參數爲要生成二維碼的內容,第二個參數爲返回二維碼的大小(正方形)
				 */
				Bitmap bitmap = EncodingHandler.createQRCode(inputUrl, 400);
				imgCode.setImageBitmap(bitmap);
			} catch (WriterException e) {
				Toast toast = Toast.makeText(getApplicationContext(), "抱歉,出現異常!已停止生成二維碼!", Toast.LENGTH_SHORT);
				toast.setGravity(Gravity.TOP, 0, 0);
				toast.show();
				e.printStackTrace();
			}
		}
		
	}
生成二維碼比較簡單,直接調用類庫的方法傳入內容和二維碼大小就可以。掃描二維碼主要是跳轉去BarCode庫中的CaptureActivity,然後在回調方法裏面獲得返回的內容。這裏對返回的內容進行了異常捕捉,當返回的是二維碼的時候,就讓它跳轉到瀏覽器中瀏覽二維碼上的網址,如果是條形碼或者出錯,則讓它用toast提示。


private void scanCode() {
		Intent intent = new Intent(MainActivity.this,CaptureActivity.class);
		startActivityForResult(intent, 0);
	}
	@Override
	/**
	 * @author wacelike
	 * @prams 
	 */
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		String resultText;
		if(resultCode == RESULT_OK){
			//獲得返回的字符串
			 resultText = (data.getExtras().getString("result")).trim();
			//跳轉到返回的地址
			try{
			Uri uri = Uri.parse(resultText);
			Intent intent = new Intent(Intent.ACTION_VIEW,uri);
			startActivity(intent);}catch(Exception e){
				Toast.makeText(getApplicationContext(), resultText, 1000).show();
			}
		}
		
	}

至此,掃描和生成二維碼的功能就算完成了!



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章