25 April 2013

การอ่าน QR Code และ Barcode ด้วย Intent

Updated on


        คราวนี้ก็ต่อกันด้วยการใช้ Intent เพื่ออ่าน QR Code หรือ Barcode ซึ่งจะยังคงคอนเซปท์เดิมเลยก็คือ ไม่เขียนโปรแกรมเองหรอก แต่จะใช้การอ่าน QR Code และ Barcode จากแอพภายนอกแทน

        สำหรับแอพที่รองรับการ Intent เพื่อขอใช้งานอ่าน Code ที่เจ้าของบล็อกรู้จักมีอยู่สามตัวคือ Barcode Scanner ของ XZing เจ้านี้ชื่อดังเรื่องการอ่านข้อมูล QR Code กับ Barcode เลยล่ะ และอีกสองตัวก็คือ Google Goggle กับ Google Search นั่นเอง ทั้งสองตัวนี้มีความสามารถในการอ่าน Code ในตัวอยู่แล้ว

        ถ้าเอาจริงๆแล้วการเขียนโปรแกรมเองเพื่อให้อ่าน Code เหล่านี้ได้ต้องใช้เวลาพอสมควร ยิ่งคนที่ไม่เคยทำ Image Processing ยิ่งแล้วใหญ่ สุดท้ายแล้วก็ได้แต่หาก๊อปโค๊ดตัวอย่างจากเว็ปมาใช้งานอยู่ดี 

        ดังนั้นไหนๆก็ไม่ได้เขียนเองแล้ว ก็ใช้ Intent ไปเลยละกันง่ายดี คำสั่งก็ไม่มีอะไรมาก แค่ Intent ไปยังแอพที่รองรับการอ่าน Code แล้วก็รอรับค่าที่เป็น String จากการถอดรหัสตัว Code เหล่านั้นได้เลย

Intent intent = new Intent("com.google.zxing.client.android.SCAN");
startActivityForResult(Intent.createChooser(intent, "Scan with"), 1);

        จากตัวอย่างจะเห้นว่ามีการกำหนด Intent ไปยังแอพที่รองรับ และคราวนี้เจ้าของบล็อกขอกำหนดค่า Request Code เป็น 1 เดี๋ยวจะเบื่อ เพราะปกติเจ้าของบล็อกพิมเป็นเลข 0 ตลอด

        สำหรับการรับข้อมูลที่ส่งมา ก็จะเป็นการรับค่าจาก getStringExtra แทน

public void onActivityResult(int requestCode, int resultCode, Intent intent) {
    if (requestCode == REQUEST_QR_SCAN && resultCode == RESULT_OK) {
        String contents = intent.getStringExtra("SCAN_RESULT");
        textContent.setText(contents);
    }
}

        ถ้าใครคุ้นๆคำสั่งนี้ล่ะก็ จริงๆมันก็อยู่ในเรื่องการส่งข้อมูลข้าม Activity แหละ โดยที่แอพที่รองรับการอ่าน Code จะส่งข้อมูลมาในรูปของ String แบบนี้ ดังนั้นก็สามารถสร้างตัวแปร String มาเก็บค่าที่ได้จากแอพดังกล่าวได้เลย จากนั้นนำค่าไปใช้ได้ตามต้องการ โดยในตัวอย่างนี้จะให้แสดงบน Text View

        ทีนี้ก็มาดูตัวอย่างการใช้งานจริงๆเลยดีกว่า เนื้อหามีน้อยมากมาย


main.xml
<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:orientation="vertical" >

    <TextView
        android:id="@+id/textContent"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/buttonIntent"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="71dp"
        android:text="Content"
        android:textSize="20sp" />

    <Button
        android:id="@+id/buttonIntent"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="42dp"
        android:text="Scan QR/Barcode" />

</RelativeLayout>


Main.java
package app.akexorcist.intentqrbarcode;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class Main extends Activity {
    public static final int REQUEST_QR_SCAN = 4;
    TextView textContent;
    
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        textContent = (TextView)findViewById(R.id.textContent);
        
        Button buttonIntent = (Button)findViewById(R.id.buttonIntent);
        buttonIntent.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {                
                Intent intent = 
                        new Intent("com.google.zxing.client.android.SCAN");
                startActivityForResult(Intent.createChooser(intent
                        , "Scan with"), REQUEST_QR_SCAN);
            }
        });
    }
    
    public void onActivityResult(int requestCode, int resultCode
                , Intent intent) {
        if (requestCode == REQUEST_QR_SCAN && resultCode == RESULT_OK) {
            String contents = intent.getStringExtra("SCAN_RESULT");
            textContent.setText(contents);
        }
    }
}

        สำหรับโค๊ดก็สั้นๆง่ายๆ กำหนด Request Code เป็น 4 กำหนดค่า Text Viewเมื่อกดปุ่มก็จะ Intent เพื่อไปยังแอพสำหรับอ่าน QR Code กับ Barcode เมื่อแอพอ่านข้อมูลได้แล้วส่งกลับมาที่ onActivityResult ก็จะดึงข้อมูลที่ส่งมา เก็บไว้ในตัวแปร String ก่อน แล้วนำไปแสดงบน Text View ที่ได้สร้างขึ้นไว้


AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="app.akexorcist.intentqrbarcode"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="8" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="app.akexorcist.intentqrbarcode.Main"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>


        สำหรับผู้ที่หลงเข้ามาอ่านคนใดต้องการไฟล์ตัวอย่างสามารถดาวน์โหลดได้ที่ Intent QR & Barcode [Google Drive]



บทความที่เกี่ยวข้อง

        การใช้ Intent สำหรับแชร์ข้อความ String [Send]
        การใช้ Intent สำหรับแชร์ข้อความสำหรับ Email [Send]
        การใช้ Intent เพื่อเปิด URL [View]
        การใช้ Intent เพื่อเปิดแผนที่ [View]
        การใช้ Intent เพื่อเปิดไฟล์ใดๆ [View]
        การเรียกเปิดแอพฯอื่นๆ ด้วย Intent
        การใช้ Intent สำหรับแชร์ไฟล์ใดๆ [Send]
        การเลือกไฟล์ภาพจาก Gallery ด้วย Intent [Result]
        การใช้งานกล้องเพื่อถ่ายภาพแบบง่ายๆด้วย Intent [Result]
        การใช้งานกล้องเพื่อบันทึกวีดีโอแบบง่ายๆด้วย Intent [Result]
        การอ่าน QR Code และ Barcode ด้วย Intent [Result]
        การรับข้อมูล Intent จากแอพฯอื่นๆ [Get Content]
        การรับข้อมูล Intent จากแอพฯอื่นแล้วส่งข้อมูลกลับไป [Result Content]