3

I was looking for QR code example. And I found the below link for creating QR code from a string. This solution is working fine but if I have a class lets say Student and have fields: id,name,phone. Then create an object of Student class and want to make QR code out of all the info of an object(Which should be information of 1 student).

How am I going to do it? And after making the QR code, how am I going to retrieve it back to the original object?

Community
  • 1
  • 1
shaon007
  • 163
  • 3
  • 16

2 Answers2

4

A QR code can contains arbitrary text, so you can implement a toString/fromString serializer/desserializer in your Student class.

A QRCode can contains up to 4296 alpha-numeric chars (be careful with this limit)

A suggestion : you can use Json format since there a lot of libraries available to help you in serialization/desserialization process.


If you decide to use Json : you can use the gson library.

Serialization is as simple as calling

public String serialize(){
    return new Gson().toJson(this);
}

And for deserialization,

public static Student deserialize(String jsonString){
    new Gson().fromJson(jsonStr, Student.class);
}

more about Json on Android here

Community
  • 1
  • 1
ben75
  • 29,217
  • 10
  • 88
  • 134
  • thanks. I got the idea from your previous answer. So, I went for xmlSerializer. That worked :-). But in this code, while serializing, how is the object or data being passed? I can see in deserialization, the class name has been included. But while serialization? Plus, I should write this code in activity , right? – shaon007 May 15 '14 at 15:22
  • Frameworks like Jackson or Gson will perform introspection on the object to find fields that need to be serialized. You can configure this automatic serialization with annotations on fields (for instance to ignore collections and other irrelevant fields). I guess that your xmlSerializer do something similar. (the sample code provided here is expected to be in Student class - since `this` denote a Student) – ben75 May 15 '14 at 15:38
2

You have to find some way to serialise your class (http://en.wikipedia.org/wiki/Serialization)

You can consider JSON but using some binary format like protobuffers (https://developers.google.com/protocol-buffers/) you will be able to fit more data into qrcodes of the same size.

Maciek Sawicki
  • 6,717
  • 9
  • 34
  • 48