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






From Project

Back to project page qrcode-android.

License

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.to.Invoice;
import tw.soleil.to.InvoiceDtl;
import tw.soleil.util.DateUtil;
import tw.soleil.util.Utils;
import android.app.Activity;
import android.app.ProgressDialog;
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;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_qrcode_reader_test);
    invoiceService = provideInvoiceService(); // JTODO use Dagger to manage
    TextView openText = (TextView) findViewById(R.id.openText);
    openText.setOnClickListener(new OnClickListener() {
      
      @Override
      public void onClick(View v) {
        ZXScanHelper.setUserCallback(new ZXUserCallback() { // JTODO ?????abstract?Adapter?implement???????????onCodeRead()??????????????new??Adapter
          
          @Override
          public void onScannerActivityResumed(Activity captureActivity) {}
          @Override
          public void onScannerActivityDestroyed(Activity activity) {}
          @Override
          public void onScannerActivityCreated(Activity activity) {}
          /**
           * ?QR code???????????
           */
          @Override
          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()
      .setEndpoint(Constants.INVOICE_SERVICE_ENDPOINT)
      .build()
      .create(InvoiceService.class);
  }
  
  @Override
  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;

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

    @Override
    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("invNum", matcher.group(1));
        queryMap.put("invDate", DateUtil.getEDateWithSep(matcher.group(2), "/"));
        queryMap.put("encrypt", matcher.group(5));
        queryMap.put("sellerID", matcher.group(4));
        queryMap.put("UUID", Build.SERIAL); // API Level 9(???)?????????
        queryMap.put("randomNumber", matcher.group(3));
        queryMap.put("appID", Constants.INVOICE_API_ID);
      }
      queryMap.put("signature", Utils.createSignature(queryMap));
      
      return queryMap;
    }
    
    @Override
    protected void onPostExecute(Invoice invoice) {
      Toast.makeText(QRCodeReaderTestActivity.this, getDetailsMsg(invoice), Toast.LENGTH_LONG).show();
      progressDialog.cancel();
    }
    
    private String getDetailsMsg(Invoice invoice) {
      StringBuffer sb = new StringBuffer();
      for (InvoiceDtl invoiceDtl : invoice.getDetails()) {
        sb.append(invoiceDtl.getDescription())
          .append(Constants.LINE_DELIMITER);
      }
      sb.deleteCharAt(sb.length() - 1); // delete the last line delimiter
      return sb.toString();
    }
  }

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

  @Override
  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 == R.id.action_settings) {
      return true;
    }
    return super.onOptionsItemSelected(item);
  }
}




Java Source Code List

com.google.zxing.client.android.BeepManager.java
com.google.zxing.client.android.CaptureActivityHandler.java
com.google.zxing.client.android.CaptureActivity.java
com.google.zxing.client.android.Contents.java
com.google.zxing.client.android.DecodeFormatManager.java
com.google.zxing.client.android.DecodeHandler.java
com.google.zxing.client.android.DecodeHintManager.java
com.google.zxing.client.android.DecodeThread.java
com.google.zxing.client.android.FinishListener.java
com.google.zxing.client.android.InactivityTimer.java
com.google.zxing.client.android.IntentSource.java
com.google.zxing.client.android.Intents.java
com.google.zxing.client.android.LocaleManager.java
com.google.zxing.client.android.ViewfinderResultPointCallback.java
com.google.zxing.client.android.ViewfinderView.java
com.google.zxing.client.android.camera.AutoFocusManager.java
com.google.zxing.client.android.camera.CameraConfigurationManager.java
com.google.zxing.client.android.camera.CameraManager.java
com.google.zxing.client.android.camera.FrontLightMode.java
com.google.zxing.client.android.camera.PreviewCallback.java
com.google.zxing.client.android.camera.exposure.DefaultExposureInterface.java
com.google.zxing.client.android.camera.exposure.ExposureInterface.java
com.google.zxing.client.android.camera.exposure.ExposureManager.java
com.google.zxing.client.android.camera.exposure.FroyoExposureInterface.java
com.google.zxing.client.android.camera.open.DefaultOpenCameraInterface.java
com.google.zxing.client.android.camera.open.GingerbreadOpenCameraInterface.java
com.google.zxing.client.android.camera.open.OpenCameraInterface.java
com.google.zxing.client.android.camera.open.OpenCameraManager.java
com.google.zxing.client.android.common.PlatformSupportManager.java
com.google.zxing.client.android.common.executor.AsyncTaskExecInterface.java
com.google.zxing.client.android.common.executor.AsyncTaskExecManager.java
com.google.zxing.client.android.common.executor.DefaultAsyncTaskExecInterface.java
com.google.zxing.client.android.common.executor.HoneycombAsyncTaskExecInterface.java
com.google.zxing.extra.RGBLuminanceSource.java
eu.livotov.zxscan.AutofocusMode.java
eu.livotov.zxscan.ZXScanHelper.java
eu.livotov.zxscan.ZXUserCallback.java
tw.soleil.constant.Constants.java
tw.soleil.qrcodereadertest.QRCodeReaderTestActivity.java
tw.soleil.service.InvoiceService.java
tw.soleil.to.InvoiceDtl.java
tw.soleil.to.Invoice.java
tw.soleil.util.DateUtil.java
tw.soleil.util.Utils.java