gzip Compression for Cloud API
To improve performance using the Anyline Cloud API, we have introduced an optional gzip compression method to enable faster file transfers (Post Request).
This Article will guide you on how to add this feature yourself to reduce the size of your images and make the most of our Cloud API.
PLEASE NOTE:
This feature is available for Cloud API V1 (Energy, ID, MRZ) and Cloud API V2 (Tire Sidewall and Barcode).
The implementation of this feature is not strictly limited to the following programming languages and code examples below. These examples are simply to demonstrate a variety of ways you could use this feature.
Option 1 - JavaScript
For the JavaScript Implementation, you will need to first install a 3rd party library to manage the compression of the images - our recommendation is Pako along with the gzip compression function included in the library.
Depending on your choice of package manager, use either one of the following commands to install Pako.
npm install pako
yarn add pako
Once you have the latest version of Pako included in your
package.json
file, import it and use it’s gzip function:
const pako = require('pako');
Inside your Post Request function, create a second variable to compress the payload using gzip like so:
Finally, include your selected content encoder (gzip) as a header inside the Post Request function like so:
Below, you will see JavaScript Examples of a Post Request function with no image compression (LEFT) and the other including image compression (RIGHT). There are both examples a compressed and non-compressed function for each Cloud API Version (Cloud API V1 and Cloud API V2):
Option 2 - Python
For the Python Implementation, you do not need to install any 3rd party library to manage the compression of the images.
Import gzip into your project using the following import statement:
Include your selected content encoder (gzip) as a header inside the Post Request function like so:
Finally, inside your Post Request function, create a variable to compress the payload using gzip like so:
Below, you will see Python Examples of a Post Request function with no image compression (LEFT) and the other including image compression (RIGHT). There are both examples a compressed and non-compressed function for each Cloud API Version (Cloud API V1 and Cloud API V2):
Option 3 - Java
For the Java Implementation, you do not need to install any 3rd party library to manage the compression of the images. You can use the
GZIPOutputStream
class included in thejava.util
library.Import gzip into your project using the following import statement:
Create a new instance of the
GZIPOutputStream
class that wraps around theByteArrayOutputStream
instance. Then, write the Post Request Data to the newGZIPOutputStream
instance and convert it to a byte array like so:
Finally, inside your
HttpRequest
, include your selected content encoder (gzip) as a header and be sure to post the compressed data like so:
Below, you will see Java Examples of a Post Request function with no image compression (LEFT) and the other including image compression (RIGHT). There are both examples a compressed and non-compressed function for each Cloud API Version (Cloud API V1 and Cloud API V2):
Final Notes
Please be sure to add the following to any of the code examples:
Replace
“base64String"
with your actual base 64 Image String inside theIMG
variable.Replace
“yourAccessToken"
with your actual Access Token inside theACCESS_TOKEN
variable.