การทำ Android เพื่อใช้สแกน QR Code และ Bar Code Reader

Mobile Application Developing- Android, iOS, Window Phone สอนเขียนโปรแกรมบนมือถือ ระบบปฏิบัติการต่าง แอนดรอยด์ ไอโอเอส วินโดโฟน สอนเขียนโปรแกรมบนมือถือ

Moderator: mindphp, ผู้ดูแลกระดาน

rangsan
PHP Hero Member
PHP Hero Member
โพสต์: 199
ลงทะเบียนเมื่อ: 30/04/2018 9:44 am

การทำ Android เพื่อใช้สแกน QR Code และ Bar Code Reader

โพสต์ที่ยังไม่ได้อ่าน โดย rangsan »

การทำ Android เพื่อใช้สแกน QR Code และ Bar Code Reader

ปัจจุบันนี้เรื่องของ Bar Code และ QR Code จะให้ว่ามีอยู่มากมายในการใช้ชีวิตของเรา ไม่ว่าจะเป็นการแอดเพื่อนใน Application Line หรือ การจ่ายเงินด้วย การแสกน QR Code ทุกอย่างเหล่านี้ สามารถทำได้อย่างง่ายดายด้วยการสแกน QR Code เพื่ออำนวยความสะดวกให้กับผู้ใช้งานให้ได้มากที่สุด

ทีนี้เราลองมาดูวิธีการเขียนแอพ Android เพื่อใช้ในการสแกน QR Code และ Bar Code Reader กันดูบ้างครับ

การที่เราจะเขียนแอพที่ใช้งานเกี่ยวกับการสแกน QR Code บน Android นั้นทาง Google ได้มี com.google.zxing เพื่อให้เราเรียกใช้งาน Feature ในการสแกน QR Code ขึ้นมาให้แล้วครับ

1. อันดับแรก เปิด Android Studio ขึ้นมาแล้วทำการสร้าง Blank Activity ให้เรียบร้อยครับ
Create_Qrcode1.jpg
Create_Qrcode1.jpg (32.38 KiB) Viewed 7883 times
2. เปิด activity_main.xml ขึ้นมาแล้วทำการกำหนดโค้ดตามนี้ครับจะเห็นได้ว่า มีในส่วนของ Text และ Button ขึ้นมาครับ

โค้ด: เลือกทั้งหมด

<RealtiveLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://chemas.android.com/tools" 
   	android:layout_width="match_parent"
	android:layout_height="match_parent"
	android:paddingBottom="16dp"
	tools:context=".Mainactivity">
	
	<TextView
		android:layout_width="300dp"
		android:layout_height="wrap_content"
		android:text=@"Result : "
		android:textAppearance="?android:attr/textAppearanceLarge"
		anroid:layout_centerHorizontal="true"
		android:id="@+id/textResult" />
	<Button
		android:layout_width="match_parent"
		android:layout_height="wrap_parent"
		android:id="@+id/btnScan"
		android:text=@"Scan"
		android:layout_below:"@+id/txtResult"
		android:layout_alignParentStart="true"
		android:layout_marginTop="98dp" />
</RealtiveLayout>
3. ไปที่ไฟล์ MainActivity ครับ ให้ import header ตามนี้

โค้ด: เลือกทั้งหมด

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import android.content.Intent;
4. ต่อไปเราจะเรียก Intent ส่วนของ Scan QR Code และ Toast มาใช้ ต่อมาให้ทำการเชื่อม Button และ TextView ให้เรียบร้อยครับ

โค้ด: เลือกทั้งหมด

public class ReadQRCodeActivity extends Activity {
 
    private Button btnScan;
    private TextView txtResult;
    
}
5. ให้ทำการ findViewById ให้กับ Text และ Button นั้นด้วยครับ

โค้ด: เลือกทั้งหมด

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
 
        btnScan = (Button)findViewById(R.id.btnScan);
        txtResult = (TextView)findViewById(R.id.txtResult);

}
6. เขียนคำสั่งอ่านค่า และรับค่า QR Code และเรียกใช้งาน Intent ไป com.google.zxing

โค้ด: เลือกทั้งหมด

btnScan.setOnClickListener(new View.OnClickListener(){
   public void onClick(View v){
      	//TODO Auto-generated method stub	
	try{
                 Intent intentOption = new Intent("com.google.zxing.client.android.SCAN");
	   intentOption.putExtra("SCAN_MODE", "QR_CODE_MODE");
	   startActivityForResult(intent,0);
	} catch(Exception e){
	  //TODO handle exception
	  Toast.makeText(getBaseContext(),"Please Install Barcode Scanner",Toast.LENGTH_SHORT).show();
	}
   }

});
7. เพิ่ม Method สำหรับรับข้อมูลจาก Barcode, QRCode Scanner ที่ได้จากการสแกนหน้ากล้อง ว่าเป็น BarCode แบบไหน แล้วแสดงผลที่ txtResult หรือตัว Text ที่เราสร้างขึ้นนั่นเอง

โค้ด: เลือกทั้งหมด

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
        // TODO Auto-generated method stub
        if (requestCode == 0)
        {
            if (resultCode == RESULT_OK)
            {
                    String contents = intent.getStringExtra("SCAN_RESULT");
                    String format = intent.getStringExtra("SCAN_RESULT_FORMAT");
                    txtResult.setText("Result : " + contents);
            }
        }
    }
ผลที่ได้จะออกมาประมาณนี้ครับ
Android-QRcode.jpg
Android-QRcode.jpg (47.67 KiB) Viewed 7883 times
อ้างอิง : daydev.com
It’s never too late to start again.
  • Similar Topics
    ตอบกลับ
    แสดง
    โพสต์ล่าสุด

ผู้ใช้งานขณะนี้

สมาชิกกำลังดูบอร์ดนี้: ไม่มีสมาชิกใหม่ และบุคลทั่วไป 26