0

Sử dụng bộ thư viện ZXing để tạo và scan mã QR Code trong ứng dụng Android

QR Code: mã vạch thế hệ mới
QR Code, viết tắt của Quick response code (tạm dịch “Mã phản hồi nhanh”) hay còn gọi là mã vạch ma trận (matrix-barcode) là dạng mã vạch hai chiều (2D) có thể được đọc bởi một máy đọc mã vạch hay smartphone (điện thoại thông minh) có chức năng chụp ảnh (camera) với ứng dụng chuyên biệt để quét mã vạch.

 Image
Mã QR đi vào đời sống
Tuy còn xa lạ với mọi người nhưng Mã QR đã và đang đi vào đời sống hiện đại, hiện diện ở khắp nơi, từ những cao ốc lớn đến những namecard trao tay, từ những thiếp mời các sự kiện công nghệ, hội thảo hay chỉ đơn giản là để nhập thêm bạn trên BlackBerry Messenger (quét mã QR để nhận dạng số PIN).

Cách đọc mã QR code
Việc đọc các mã QR code khá đơn giản, dưới đây mình giới thiệu một vài phần mềm thông dụng cho các loại điện thoại.
Bài viết này sẽ giới thiệu các bạn cách sử dụng bộ thư viện mã nguồn mở ZXing 2.2
Bước 1. Download mã nguồn thư viện Zxing trên trang chủ cung cấp.
Link download: https://code.google.com/p/zxing/downloads/list
Bước 2. : Tạo zxing core dùng Apache Ant
Link download Apache Ant:  http://ant.apache.org/ivy/download.cgi
Dùng cmd trỏ đến bộ thư viện Zxing ở đường dẫn src/ và execute câu lệnh: ant -f core/build.xml, kết quả là chúng ta có được file core.jar để sử dụng cho bước tiếp theo
Bước 3: Build ZXing Android trên Eclipse
Tạo mới project:  (File –> New –> Android Project). Tiếp tục chọn  “Create project from existing source”, đặt tên project là ZXing Lib.
Click “Browse” trỏ đến đường dẫn chứa bộ thư viện ZXING  đã download trong thư mục android/ . Click OK-> Finish.
 Right-click vào ZXing
  project –> properties –> Java Build Path –> Add External Jars –> Trỏ đến đường dẫn và ccos chứa core.jar và chọn file này –> Open –> OK.
Right-click vào ZXing project –> properties –> Android –> Tick “Is Library” checkbox –>OK
Bước 4Tạo mới project của bạn và thêm bộ thư viện ZXing Android đã tạo ở bước trên.
Thêm bộ thư viện vào project
Image
Cấu hình buid path đến file core.jar cho project của bạn đã tạo
Image
Image
AndroidManifest.xml
<?xml version=”1.0″ encoding=”utf-8″?>
<manifest xmlns:android=”http://schemas.android.com/apk/res/android&#8221;
package=”com.example.qrdelux”
android:versionCode=”1″
android:versionName=”1.0″ >
<uses-sdk
android:minSdkVersion=”8″
android:targetSdkVersion=”15″ />
<uses-permission android:name=”android.permission.CAMERA” />
<uses-permission android:name=”android.permission.FLASHLIGHT” />
<application
android:allowBackup=”true”
android:icon=”@drawable/qrcode_oranges”
android:label=”@string/app_name”
android:theme=”@style/AppTheme” >
<activity
android:name=”com.example.qrdelux.StartAppActivity”
android:label=”@string/app_name” >
<intent-filter>
<action android:name=”android.intent.action.MAIN” />
<category android:name=”android.intent.category.LAUNCHER” />
</intent-filter>
</activity>
<activity
android:name=”com.google.zxing.client.android.CaptureActivity”
android:configChanges=”orientation|keyboardHidden”
android:screenOrientation=”landscape”
android:theme=”@android:style/Theme.NoTitleBar.Fullscreen”
android:windowSoftInputMode=”stateAlwaysHidden” >
<intent-filter>
<action android:name=”android.intent.action.MAIN” />
<category android:name=”android.intent.category.DEFAULT” />
</intent-filter>
<intent-filter>
<action android:name=”com.google.zxing.client.android.SCAN” />
<category android:name=”android.intent.category.DEFAULT” />
</intent-filter>
</activity>
<activity
android:name=”com.example.qrdelux.QRMainActivity”
android:label=”@string/app_name” >
</activity>
</application>
</manifest>
Code activity chính:
package com.example.qrdelux;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class StartAppActivity extends Activity {
TextView tvStatus;
TextView tvResult;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_start_app);
Button btnShowQRCode=(Button)findViewById(R.id.btnGenerateQR);
btnShowQRCode.setOnClickListener(showQRCode);
Button btnScanQRCode=(Button)findViewById(R.id.btnScanQR);
btnScanQRCode.setOnClickListener(scanQRCodeListener);
tvStatus = (TextView) findViewById(R.id.tvStatus);
tvResult = (TextView) findViewById(R.id.tvResult);
}
private OnClickListener showQRCode= new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
EditText qrInput=(EditText)findViewById(R.id.qrInput);
String qrInputText=qrInput.getText().toString();
Intent intent=new Intent(StartAppActivity.this,QRMainActivity.class);
intent.putExtra(“QR_INPUT”, qrInputText);
startActivity(intent);
}
};
private OnClickListener scanQRCodeListener=new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
/*Intent intent = new Intent(getApplicationContext(),
com.google.zxing.client.android.CaptureActivity.class);
startActivity(intent);*/
try {
Intent intent = new Intent(“com.google.zxing.client.android.SCAN”);
intent.putExtra(“SCAN_MODE”, “QR_CODE_MODE,PRODUCT_MODE”);
startActivityForResult(intent, 0);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
Toast.makeText(getApplicationContext(), “ERROR:” + e, 1).show();
}
}
};
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
System.out.print(“O dau vay???”);
if (requestCode == 0) {
if (resultCode == RESULT_OK) {
System.out.print(“Where???”);
String contents = data.getStringExtra(“SCAN_RESULT”);
String format = data.getStringExtra(“SCAN_RESULT_FORMAT”);
tvStatus.setText(format);
tvResult.setText(contents);
// Handle successful scan
} else if (resultCode == RESULT_CANCELED) {
tvStatus.setText(“Press a button to start a scan.”);
tvResult.setText(“Scan cancelled.”);
}
}
}
}
Layout: (activity_start_app.xml)
<LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android&#8221;
android:layout_width=”fill_parent”
android:layout_height=”fill_parent”
android:orientation=”vertical” >
<TextView
android:id=”@+id/textView1″
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:text=”@string/required_input”
android:textAppearance=”?android:attr/textAppearanceMedium” />
<EditText
android:id=”@+id/qrInput”
android:layout_width=”match_parent”
android:layout_height=”wrap_content”
android:ems=”10″ >
<requestFocus />
</EditText>
<Button
android:id=”@+id/btnGenerateQR”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:text=”@string/generateQRCode” />
<Button
android:id=”@+id/btnScanQR”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:text=”@string/scanQRCode” />
<TextView
android:id=”@+id/tvStatuslbl”
android:layout_width=”match_parent”
android:layout_height=”wrap_content”
android:layout_weight=”0.03″
android:text=”Status”
android:textAppearance=”?android:attr/textAppearanceLarge”
android:textColor=”@android:color/darker_gray” />
<TextView
android:id=”@+id/tvStatus”
android:layout_width=”match_parent”
android:layout_height=”wrap_content”
android:layout_weight=”1″
android:textColor=”@android:color/black”
android:textAppearance=”?android:attr/textAppearanceLarge” />
<TextView
android:id=”@+id/tvResultlbl”
android:layout_width=”match_parent”
android:layout_height=”wrap_content”
android:layout_weight=”0.03″
android:text=”Result”
android:textColor=”@android:color/darker_gray” />
<TextView
android:id=”@+id/tvResult”
android:layout_width=”match_parent”
android:layout_height=”wrap_content”
android:layout_weight=”2.95″
android:textColor=”@android:color/black”
/>
</LinearLayout>

Post a Comment

 
Top