Showing posts with label zxing. Show all posts
Showing posts with label zxing. Show all posts

Tuesday, April 24, 2012

Bitmap in ZXing (how to encode QR)

// this is from QREncoder.java from ZXing

  static Bitmap encodeAsBitmap(String contents,
                               BarcodeFormat format,
                               int desiredWidth,
                               int desiredHeight) throws WriterException {
    Hashtable<EncodeHintType,Object> hints = null;
    String encoding = guessAppropriateEncoding(contents);
    if (encoding != null) {
      hints = new Hashtable<EncodeHintType,Object>(2);
      hints.put(EncodeHintType.CHARACTER_SET, encoding);
    }
    MultiFormatWriter writer = new MultiFormatWriter();   
    BitMatrix result = writer.encode(contents, format, desiredWidth, desiredHeight, hints);
    int width = result.getWidth();
    int height = result.getHeight();
    int[] pixels = new int[width * height];
    // All are 0, or black, by default
    for (int y = 0; y < height; y++) {
      int offset = y * width;
      for (int x = 0; x < width; x++) {
        pixels[offset + x] = result.get(x, y) ? BLACK : WHITE;
      }
    }

    Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
    return bitmap;
  }

//This is how you can generate normally:
private Bitmap generateQRCode2(String data)
    {
        //Size of the image generated.
        int h = 100;
        int w = 100;
        Config conf = Bitmap.Config.RGB_565;
        Bitmap bmp = Bitmap.createBitmap(w, h, conf); // this creates a MUTABLE bitmap
       
        Charset charset = Charset.forName("UTF-8");
        CharsetEncoder encoder = charset.newEncoder();
        byte[] b = null;
        try {
            // Convert a string to UTF-8 bytes in a ByteBuffer
            ByteBuffer bbuf = encoder.encode(CharBuffer.wrap(data));
            b = bbuf.array();
        } catch (CharacterCodingException e) {
            System.out.println(e.getMessage());
        }

        String data1;
        try {
            data1 = new String(b, "UTF-8");
            // get a byte matrix for the data
            BitMatrix matrix = null;
            // Size of the QR code
           
            com.google.zxing.Writer writer = new QRCodeWriter();
            try {
                Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>(2);
                hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
                matrix = writer.encode(data1, com.google.zxing.BarcodeFormat.QR_CODE,w, h);
            } catch (Exception e) {
                System.out.println(e.getMessage());
            }
           
            //generate an image from the bit matrix
            int width = matrix.getWidth();
            int height = matrix.getHeight();
           
            try {     
                for (int x = 0; x < width; x++)
                {
                    for (int y = 0; y < height; y++) {
                        bmp.setPixel(x, y, matrix.get(x, y) ? BLACK : WHITE);
                      }
                    }
                           
            } catch (Exception e) {
                System.out.println(e.getMessage());
            } 
        } catch (UnsupportedEncodingException e) {
            System.out.println(e.getMessage());
        }
       
        return bmp;

            // change this path to match yours (this is my mac home folder, you can use: c:\\qr_png.png if you are on windows)
            //String filePath = "/Users/shaybc/Desktop/OutlookQR/qr_png.png";
            /*
            String filePath;
            File file = new File(filePath);
            try {
                MatrixToImageWriter.writeToFile(matrix, "PNG", file);
                System.out.println("printing to " + file.getAbsolutePath());
            } catch (IOException e) {
                System.out.println(e.getMessage());
            }
        } catch (UnsupportedEncodingException e) {
            System.out.println(e.getMessage());
        }*/
    //}




Data Storage in ZXing

I realized that ZXing didnt store the QR code created at all. But here is the internal memory of the android
http://developer.android.com/guide/topics/data/data-storage.html

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);
}

}

About Sharing function in ZXing


I found this :
 Bitmap bitmap = QRCodeEncoder.encodeAsBitmap(contents, format, pixelResolution, pixelResolution);
      Message message = Message.obtain(handler, R.id.encode_succeeded);
      message.obj = bitmap;
      message.sendToTarget();
in the Intent.java class.
So, I somewhat understand that it sends to the "Handler" by "sendToTarget" method in handler class.  Then the 
In that
  final void shareByEmail(String contents) {
    sendEmailFromUri("mailto:", null, activity.getString(R.string.msg_share_subject_line),

        contents);
  }

Friday, April 20, 2012

creating QR with zxing in iphone

https://github.com/joelind/zxing-iphone/tree/master/zxing.appspot.com

ZXing Encoding Method

Calling Encoder with Intent: http://code.google.com/p/zxing/issues/detail?id=1032

When you want to use libraries: http://www.vineetmanohar.com/2010/09/java-barcode-api/

Wednesday, April 18, 2012

QR Generating Journey


Similar one (research from Stanford)

Step By Step: http://www.thonky.com/qr-code-tutorial/introduction/#general-overview-of-creating-a-qr-code

To make QR, we have to know :
1) The choice of characters eg. UTF8 or ISO-8859-1
2) Byte array to UTF8(but now we have to use ISO-8859-1)because UTF 8 has some non-decodable characters by ZXing library (because we are using ZXing for this app)   WHY WE CHOOOSE ZXING?
3) Storage is small so, we need URL shortening technique.

===========================
QR code future is

Next QR Code Generations
1) Microsoft Tag (write details about it like detecting the areas that which QR code is scanned most, etc)
2) Dynamic QR Code (where information changes from time to time by scanning it)
3) Designer QR Codes

Monday, April 16, 2012

for conflicts between Barcode Scanner App and Zxing Scanner

users don't have to select between the barcode scanner app and my app for scanning if they have Zxing's apk installed
http://stackoverflow.com/questions/7945951/integrating-zxing-barcode-scanner-with-my-android-app-custom-action-name-issue