Bar Code Reading
Reading Barcodes
User Story: As a parent, I want to quickly log the book that I’m about to read.
Continuing to work on the mobile app to inspire parents to read books with their children, I’ve decided a great feature would be the ability to track the books that were read. I think the fastest way to accomplish this would be to allow the user to scan a barcode on the book. Users can type in a text search, but it seems that the barcode would be quick.
The first task is to search a book database to find the book from the ISBN. A great API for this is provided by the Internet Archive. They have an Open Library Books API that supports exactly this use case. This API is compatible with Google Books API also, and a combination of the two APIs could be used in case one does not respond with a book from the ISBN used.
The api/books
endpoint supports searching for a book by ISBN and returning the results as a JSON blob. This is perfect for this use case. In addition to the title of the book, it returns other useful data.
An image of the book cover is returned as part of the data. The thumbnail_url is a URL to an image of the book’s cover. This thumbnail is actually quite small (as expected since it is a thumbnail) but it does load quickly. Using the right image for the purpose that you need is always a good idea. There is also a cover attribute that includes small, medium, and large covers of the book. In my testing, the small seems like it is enough of an image to show on the results page. An optional enhancement might be to allow the user to tap the book cover and see a larger version of it. But since this is primarily for adults, the key is the title of the book and the cover is a visual cue that they did find the right book.
The title is probably the most important result, the attributes title
and subtitle
, both together are displayed to the user to indicate that this is indeed the right book.
The authors
attributes are also very important and will be displayed. Of course, there can be more than one author for the book, so this is an array of author objects.
There are many other attributes, but I decided I would only use publish_date
, number_of_pages
and description
. The other fields are interesting, but this is enough to finish this feature.
Most internet APIs require some form of key, but this one does not, so you can start using it right away.
$ curl 'https://books.google.com/books?jscmd=viewapi&bibkeys=ISBN:9781484799673'
var _GBSBookInfo = {"ISBN:9781484799673":{"bib_key":"ISBN:9781484799673","info_url":"https://books.google.com/books?id=Y95evgAACAAJu0026source=gbs_ViewAPI","preview_url":"https://books.google.com/books?id=Y95evgAACAAJu0026source=gbs_ViewAPI","thumbnail_url":"https://books.google.com/books/content?id=Y95evgAACAAJu0026printsec=frontcoveru0026img=1u0026zoom=5","preview":"noview","embeddable":false,"can_download_pdf":false,"can_download_epub":false,"is_pdf_drm_enabled":false,"is_epub_drm_enabled":false}};
$ curl 'http://openlibrary.org/api/books?jscmd=details&bibkeys=ISBN:9781484799673'
var _OLBookInfo = {"ISBN:9781484799673": {"info_url": "http://openlibrary.org/books/OL26346341M/An_Elephant_and_Piggy_Biggie", "bib_key": "ISBN:9781484799673", "preview_url": "http://openlibrary.org/books/OL26346341M/An_Elephant_and_Piggy_Biggie", "thumbnail_url": "https://covers.openlibrary.org/b/id/8029092-S.jpg", "details": {"publishers": ["Hyperion"], "classifications": {}, "created": {"type": "/type/datetime", "value": "2017-06-29T20:28:43.063626"}, "identifiers": {}, "covers": [8029092], "isbn_13": ["9781484799673"], "last_modified": {"type": "/type/datetime", "value": "2017-06-29T20:29:23.755103"}, "publish_date": "2017", "key": "/books/OL26346341M", "title": "An Elephant and Piggy Biggie", "latest_revision": 3, "works": [{"key": "/works/OL17748459W"}], "type": {"key": "/type/edition"}, "revision": 3}, "preview": "noview"}};
As you can see, the results are not identical, but both are compatible. Swapping out viewapi
for details
returns the information we need:
$ curl 'http://openlibrary.org/api/books?jscmd=details&bibkeys=ISBN:9781484799673'
var _OLBookInfo = {"ISBN:9781484799673": {"info_url": "http://openlibrary.org/books/OL26346341M/An_Elephant_and_Piggy_Biggie", "bib_key": "ISBN:9781484799673", "preview_url": "http://openlibrary.org/books/OL26346341M/An_Elephant_and_Piggy_Biggie", "thumbnail_url": "https://covers.openlibrary.org/b/id/8029092-S.jpg", "details": {"publishers": ["Hyperion"], "classifications": {}, "created": {"type": "/type/datetime", "value": "2017-06-29T20:28:43.063626"}, "identifiers": {}, "covers": [8029092], "isbn_13": ["9781484799673"], "last_modified": {"type": "/type/datetime", "value": "2017-06-29T20:29:23.755103"}, "publish_date": "2017", "key": "/books/OL26346341M", "title": "An Elephant and Piggy Biggie", "latest_revision": 3, "works": [{"key": "/works/OL17748459W"}], "type": {"key": "/type/edition"}, "revision": 3}, "preview": "noview"}};
Now that I have an API for returning the book details, I can start working on configuring the camera to read the bar code in order to send it to this API. I’ll need to also set up the app to use this API once that is completed.