Skip to content

Commit ea8d9ee

Browse files
authored
Update node-thumb.md
1 parent 2fceb68 commit ea8d9ee

File tree

1 file changed

+15
-10
lines changed
  • articles/cognitive-services/Computer-vision/QuickStarts

1 file changed

+15
-10
lines changed

articles/cognitive-services/Computer-vision/QuickStarts/node-thumb.md

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -48,16 +48,15 @@ To create and run the sample, do the following steps:
4848
```javascript
4949
'use strict';
5050

51-
const request = require('request');
51+
const fs = require('fs');
52+
const request = require('request').defaults({ encoding: null });
5253

5354
let subscriptionKey = process.env['COMPUTER_VISION_SUBSCRIPTION_KEY'];
5455
let endpoint = process.env['COMPUTER_VISION_ENDPOINT']
55-
if (!subscriptionKey) { throw new Error('Set your environment variables for your subscription key and endpoint.'); }
5656

57-
var uriBase = endpoint + 'vision/v2.1/generateThumbnail';
57+
var uriBase = endpoint + 'vision/v3.0/generateThumbnail';
5858

59-
const imageUrl =
60-
'https://upload.wikimedia.org/wikipedia/commons/9/94/Bloodhound_Puppy.jpg';
59+
const imageUrl = 'https://upload.wikimedia.org/wikipedia/commons/9/94/Bloodhound_Puppy.jpg';
6160

6261
// Request parameters.
6362
const params = {
@@ -66,6 +65,7 @@ const params = {
6665
'smartCropping': 'true'
6766
};
6867

68+
// Construct the request
6969
const options = {
7070
uri: uriBase,
7171
qs: params,
@@ -74,18 +74,23 @@ const options = {
7474
'Content-Type': 'application/json',
7575
'Ocp-Apim-Subscription-Key' : subscriptionKey
7676
}
77-
};
77+
}
7878

79+
// Post the request and get the response (an image stream)
7980
request.post(options, (error, response, body) => {
80-
if (error) {
81-
console.log('Error: ', error);
82-
return;
83-
}
81+
// Write the stream to file
82+
var buf = Buffer.from(body, 'base64');
83+
fs.writeFile('thumbnail.png', buf, function (err) {
84+
if (err) throw err;
85+
});
86+
87+
console.log('Image saved')
8488
});
8589
```
8690

8791
## Examine the response
8892

93+
A popup of the thumbnail image will display.
8994
A successful response is returned as binary data, which represents the image data for the thumbnail. If the request fails, the response is displayed in the console window. The response for the failed request contains an error code and a message to help determine what went wrong.
9095

9196
## Next steps

0 commit comments

Comments
 (0)