Android DeveloperDeveloperFeatured

เขียนแอพ Android การสแกน QR Code และ Bar Code Reader

บทเรียนนี้เป็นบทเรียนง่ายๆ สั้นๆ เกี่ยวกับการพัฒนาแอพพลิเคชันบนระบบปฏิบัติการ Android สำหรับสแกน QR Code และ Bar Code Reader ครับ

หลายๆ คนอยากจะพัฒนาแอพพลิเคชันสำหรับสแกน QR Code สำหรับรับค่าต่างๆ อยู่ บนสมาร์ทโฟน Android ซึ่งทางทีม Google เองก็มี com.google.zxing ที่ให้เราเรียกใช้ Feature ในการแสกน QR Code ขึ้นมาให้ครับ

เปิด Android Studio ขึ้นมาสร้าง Blank Activity ให้เรียบร้อยครับ

qrcode-android-1

เปิด activity_main.xml ขึ้นมาแล้วแต่งหน้าจอนี้

<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>

ไปที่ไฟล์ MainActivity.java ครับ ให้ 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;

เราจะเรียก Intent ส่วนของ Scan QR Code และ Toast มาใช้ ต่อมาให้ทำการเชื่อม Button และ TextView ให้เรียบร้อยครับ

public class ReadQRCodeActivity extends Activity {
 
    private Button btnScan;
    private TextView txtResult;
    ...

}

และ

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

        ...
}

เขียนคำสั่งอ่านค่า และรับค่า 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();
	}
   }

});

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

@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

Asst. Prof. Banyapon Poolsawas

อาจารย์ประจำสาขาวิชาการออกแบบเชิงโต้ตอบ และการพัฒนาเกม วิทยาลัยครีเอทีฟดีไซน์ & เอ็นเตอร์เทนเมนต์เทคโนโลยี มหาวิทยาลัยธุรกิจบัณฑิตย์ ผู้ก่อตั้ง บริษัท Daydev Co., Ltd, (เดย์เดฟ จำกัด)

Related Articles

Back to top button
Game & Mobile Development AR VR XR
Privacy Overview

This website uses cookies so that we can provide you with the best user experience possible. Cookie information is stored in your browser and performs functions such as recognising you when you return to our website and helping our team to understand which sections of the website you find most interesting and useful.

Adblock Detected

เราตรวจพบว่าคุณใช้ Adblock บนบราวเซอร์ของคุณ,กรุณาปิดระบบ Adblock ก่อนเข้าอ่าน Content ของเรานะครับ, ถือว่าช่วยเหลือกัน