Friday, 12 June 2015

Android Bar/QR Code Scanner!

Hi!

Today i am going to show the example how to read Bar / QR Code form android device.


1) Function on Call Class to Scan the Sequence

// startActivityForResult for safety carry of sequence.
Intent int_brcd = new Intent(PO_Entry.this, InitiatBarCd.class);
startActivityForResult(int_brcd, 3);


2) Class to Scan the Bar/QR Code


package vgs.raj.tot.ivnt;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.widget.Toast;
import com.google.zxing.integration.android.IntentIntegrator;
import com.google.zxing.integration.android.IntentResult;


public class InitiatBarCd extends ActionBarActivity {

    @Override    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_po__entry);

        IntentIntegrator scanIntegrator = new IntentIntegrator(this);
        scanIntegrator.initiateScan();
    }

    public void onActivityResult(int requestCode, int resultCode, Intent intent) {
        IntentResult scanningResult = IntentIntegrator.parseActivityResult(
                requestCode, resultCode, intent);
        if (scanningResult != null) {
            playSound();
            String scanContent = scanningResult.getContents();
            Intent int_get_br_bk = new Intent();
            int_get_br_bk.putExtra("ip_br_code", scanContent);
            setResult(3, int_get_br_bk);
            finish();
        } else {
            Toast.makeText(getApplicationContext(), "No scan data received!",
                    Toast.LENGTH_SHORT).show();
        }
    }

    public void playSound() {
        MediaPlayer mp = MediaPlayer.create(InitiatBarCd.this,
                R.drawable.camerasound);
        mp.start();
    }

    @Override    public void onBackPressed() {
        // super.onBackPressed();        finish();
    }
}


3) Class To receive the sequence of Code


@Overrideprotected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (data != null) {
        if (resultCode == 3) {
            String brcode_val = data.getStringExtra("ip_br_code");
            et_po_enty_brcd.setText(brcode_val);
        }
    }
}

Thank You!

Please Leave Your Comment..,

Have A Happy Day..,

No comments:

Post a Comment