问题
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?
回答1:
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
回答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.
来源:https://stackoverflow.com/questions/23672520/how-make-qr-code-from-object-of-a-class-or-several-data