Tuesday, April 24, 2012

Encoding ZXing with Intent


Android : http://stackoverflow.com/questions/2489048/qr-code-encoding-and-decoding-using-zxing

(for iOS) http://dev4mac.blogspot.co.uk/2011/10/qr-using-zxing-zebra-crossing.html

My code: package com.thetmonaye.ucl;

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


public class EncoderActivity extends Activity {


@Override
   public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.encoder);
   
     
       //Pointers to Text fields
final EditText receive_amount = (EditText) findViewById(R.id.receive_amount);
final EditText emailAddressField = (EditText) findViewById(R.id.emailAddress);
final EditText memoField = (EditText) findViewById(R.id.memo);
final EditText barcodeField = (EditText) findViewById(R.id.barcode);


Button qrButton = (Button) this.findViewById(R.id.create_QR_button);
qrButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {

// getting the values from the EditText Fields
String amount = receive_amount.getText().toString();
String email = emailAddressField.getText().toString();
String memo = memoField.getText().toString();
String barcode = barcodeField.getText().toString();

if (amount != null && email != null && memo != null && barcode != null)
{
// URI to be encoded as a QR code
String uri = "thetUcl:"+"amount="+amount+"&"+"email="+email+"&"+"memo="+memo+"&"+"barcode="+barcode;

generateQRCode(uri);

}
}
});    
}


private void generateQRCode(String data) {
// call it simply by Intent and you don't need to add library or any codes
Intent intent = new Intent("com.google.zxing.client.android.ENCODE");
intent.putExtra("ENCODE_TYPE", "TEXT_TYPE");
intent.putExtra("ENCODE_DATA", data);
intent.putExtra("ENCODE_FORMAT", "QR_CODE");
startActivity(intent);
}

}

7 comments:

  1. Getting ActivityNotFoundEXceprion..

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete
  3. Hi, I'm getting ActivityNotFoundException too. I think we should declare an activity handling this intent in our Manifest, but I don't understand WHICH one :(
    Which is the activity in zxing package that handles this intent? Any suggestions?

    ReplyDelete
  4. is there any way out to save that QRcode image

    ReplyDelete
  5. @mayank , you can directly store your images to sqlite database or on dropbox

    ReplyDelete
  6. I learn a lot from your post,thanks.I am new for android eclipse,and i want to know how to configure or use to generat the QR code from zxing library ,a open source from zxing team.,If any alternative is there pls also inform me.So can anyone suggest me how should proceed ?

    ReplyDelete