How make QR code from object of a class or several data

社会主义新天地 提交于 2020-07-09 05:09:56

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!