Android Open Source - qrcode-android Q R Code Reader Test Activity

From Project

Back to project page qrcode-android.


The source code is released under:

MIT License

If you think the Android project qrcode-android listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package tw.soleil.qrcodereadertest;
//from   w w w  .  j  a v  a2  s.c o m
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import retrofit.RestAdapter;
import tw.soleil.constant.Constants;
import tw.soleil.service.InvoiceService;
import tw.soleil.util.DateUtil;
import tw.soleil.util.Utils;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
import android.widget.Toast;
import eu.livotov.zxscan.ZXScanHelper;
import eu.livotov.zxscan.ZXUserCallback;

public class QRCodeReaderTestActivity extends Activity {
  private InvoiceService invoiceService;

  protected void onCreate(Bundle savedInstanceState) {
    invoiceService = provideInvoiceService(); // JTODO use Dagger to manage
    TextView openText = (TextView) findViewById(;
    openText.setOnClickListener(new OnClickListener() {
      public void onClick(View v) {
        ZXScanHelper.setUserCallback(new ZXUserCallback() { // JTODO ?????abstract?Adapter?implement???????????onCodeRead()??????????????new??Adapter
          public void onScannerActivityResumed(Activity captureActivity) {}
          public void onScannerActivityDestroyed(Activity activity) {}
          public void onScannerActivityCreated(Activity activity) {}
           * ?QR code???????????
          public boolean onCodeRead(String code) {
            return code.matches(Constants.PATTERN_INVOICE_MAIN_QR_CODE_CONTENT);
        ZXScanHelper.scan(QRCodeReaderTestActivity.this, Constants.SCAN_FOR_INVOICE_DETAIL);
  private InvoiceService provideInvoiceService() {
    return new RestAdapter.Builder()
  protected void onActivityResult(final int requestCode, final int resultCode, final Intent data)
        if (resultCode == RESULT_OK && requestCode == Constants.SCAN_FOR_INVOICE_DETAIL)
            String scannedCode = ZXScanHelper.getScannedCode(data);
            new GetInvoiceTask().execute(scannedCode);
  private class GetInvoiceTask extends AsyncTask<String, Void, Invoice> {
    private ProgressDialog progressDialog;

    protected void onPreExecute() {
      progressDialog = new ProgressDialog(QRCodeReaderTestActivity.this);
      progressDialog.setMessage("Loading..."); // JTODO ?string xml;

    protected Invoice doInBackground(String... params) {
      return invoiceService.getInvoice(composeQueryMap(params[0]));
    private Map<String, String> composeQueryMap(String rawQRCodeStr) {
      Map<String, String> queryMap = new HashMap<String, String>();
      // REFINE value?Constants
      queryMap.put("version", "0.2");
      queryMap.put("type", "QRCode");
      queryMap.put("action", "qryInvDetail");
      queryMap.put("generation", "V2");
      // end
      Matcher matcher = Pattern.compile(Constants.PATTERN_INVOICE_MAIN_QR_CODE_CONTENT).matcher(rawQRCodeStr);
      if (matcher.find()) {
        queryMap.put("invDate", DateUtil.getEDateWithSep(, "/"));
        queryMap.put("UUID", Build.SERIAL); // API Level 9(???)?????????
        queryMap.put("appID", Constants.INVOICE_API_ID);
      queryMap.put("signature", Utils.createSignature(queryMap));
      return queryMap;
    protected void onPostExecute(Invoice invoice) {
      Toast.makeText(QRCodeReaderTestActivity.this, getDetailsMsg(invoice), Toast.LENGTH_LONG).show();
    private String getDetailsMsg(Invoice invoice) {
      StringBuffer sb = new StringBuffer();
      for (InvoiceDtl invoiceDtl : invoice.getDetails()) {
      sb.deleteCharAt(sb.length() - 1); // delete the last line delimiter
      return sb.toString();

  public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(, menu);
    return true;

  public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == {
      return true;
    return super.onOptionsItemSelected(item);

Java Source Code List