• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Image Development
2
3## When to Use
4
5You can use image development APIs to decode images into pixel maps and encode the pixel maps into a supported format.
6
7## Available APIs
8
9For details about the APIs, see [Image Processing](../reference/apis/js-apis-image.md).
10
11## How to Develop
12
13### Full-Process Scenario
14
15The full process includes creating an instance, reading image information, reading and writing pixel maps, updating data, packaging pixels, and releasing resources.
16
17```js
18const Color = new ArrayBuffer(96); // Create a buffer to store image pixel data.
19let opts = { alphaType: 0, editable: true, pixelFormat: 4, scaleMode: 1, size: { height: 2, width: 3 } } // Image pixel data.
20
21// Create a PixelMap object.
22const Color = new ArrayBuffer(96);
23let opts = { alphaType: 0, editable: true, pixelFormat: 4, scaleMode: 1, size: { height: 2, width: 3 } }
24        image.createPixelMap(Color, opts, pixelmap => {
25            expect(pixelmap !== null).assertTrue();
26            console.info('TC_001-1 success');
27            done();
28        })
29// Read pixels.
30 pixelmap.readPixels(area,(data) => {
31 	if(data !== null) {
32		var bufferArr = new Uint8Array(area.pixels);
33        var res = true;
34        for (var i = 0; i < bufferArr.length; i++) {
35			console.info('TC_021-1 buffer ' + bufferArr[i]);
36				if(res) {
37					if(bufferArr[i] == 0) {
38						res = false;
39                        console.info('TC_021-1 Success');
40                        expect(true).assertTrue();
41                        done();
42                        break;
43                        }
44                    }
45                }
46
47// Store pixels.
48const readBuffer = new ArrayBuffer(96);
49pixelmap.readPixelsToBuffer(readBuffer,() => {
50var bufferArr = new Uint8Array(readBuffer);
51var res = true;
52for (var i = 0; i < bufferArr.length; i++) {
53     if(res) {
54       if (bufferArr[i] !== 0) {
55          res = false;
56          console.info('TC_020-1 Success');
57          expect(true).assertTrue();
58          done();
59          break;
60          }
61       }
62    }
63
64// Write pixels.
65pixelmap.writePixels(area,() => {
66    const readArea = { pixels: new ArrayBuffer(20), offset: 0, stride: 8, region: { size: { height: 1, width: 2 }, x: 0, y: 0 }}
67     pixelmap.readPixels(readArea,() => {
68         var readArr = new Uint8Array(readArea.pixels);
69         var res = true;
70         for (var i = 0; i < readArr.length; i++) {
71              if(res) {
72                  if (readArr[i] !== 0) {
73                      res = false;
74                      console.info('TC_022-1 Success');
75                      expect(true).assertTrue();
76                      done();
77                      break;
78                    }
79                 }
80            }
81
82// Write pixels to the buffer.
83 pixelmap.writeBufferToPixels(writeColor).then(() => {
84     const readBuffer = new ArrayBuffer(96);
85     pixelmap.readPixelsToBuffer(readBuffer).then (() => {
86    	var bufferArr = new Uint8Array(readBuffer);
87     	var res = true;
88     	for (var i = 0; i < bufferArr.length; i++) {
89     		if(res) {
90     			if (bufferArr[i] !== i) {
91     				res = false;
92                    console.info('TC_023 Success');
93                    expect(true).assertTrue()
94                    done();
95                    break;
96                }
97            }
98       }
99
100// Obtain image information.
101pixelmap.getImageInfo( imageInfo => {
102     if (imageInfo !== null) {
103         console.info('TC_024-1 imageInfo is ready');
104         expect(imageInfo.size.height == 4).assertTrue();
105         expect(imageInfo.size.width == 6).assertTrue();
106         expect(imageInfo.pixelFormat == 4).assertTrue();
107         done();
108      }
109 })
110
111// Release the PixelMap object.
112pixelmap.release(()=>{
113    expect(true).assertTrue();
114    console.log('TC_027-1 suc');
115    done();
116})
117
118// Create an image source (uri).
119const imageSourceApi = image.createImageSource(path);//'/data/local/tmp/test.jpg'
120
121// Create an image source (fd).
122const imageSourceApi = image.createImageSource(29);
123
124// Create an image source (data).
125const data = new ArrayBuffer(96);
126const imageSourceApi = image.createImageSource(data);
127
128// Release the image source.
129imageSourceApi.release(() => {
130    console.info('TC_044-1 Success');
131    })
132
133// Encode the image.
134const imagePackerApi = image.createImagePacker();
135imagePackerApi.packing(imageSourceApi, packOpts, data => {
136    console.info('TC_062-1 finished');
137    expect(data !== null).assertTrue();
138    done();
139 })
140
141// Release the ImagePacker object.
142imagePackerApi.release();
143```
144
145### Decoding Scenario
146
147```js
148/data/local/tmp/test.jpg // Set the path for creating an image source.
149
150// Create an image source using a path.
151const imageSourceApi = image.createImageSource(path);//'/data/local/tmp/test.jpg'
152
153// Set parameters.
154let decodingOptions = {
155    sampleSize:1, // Sampling size of the thumbnail.
156    editable: true, // Whether the image can be edited.
157    desiredSize:{ width:1, height:2}, // Desired output size of the image.
158    rotateDegrees:10, // Rotation angle of the image.
159    desiredPixelFormat:2, // Decoded pixel format.
160    desiredRegion: { size: { height: 1, width: 2 }, x: 0, y: 0 }, // Region of the image to decode.
161    index:0// Image sequence number.
162    };
163
164// Create a pixel map in callback mode.
165imageSourceApi.createPixelMap(decodingOptions, pixelmap => {
166    console.info('TC_050 createPixelMap ');
167    expect(pixelmap !== null ).assertTrue();
168    done();
169    })
170}
171
172// Create a pixel map in promise mode.
173imageSourceApi.createPixelMap().then(pixelmap => {
174   console.info('TC_050-11 createPixelMap ');
175   expect(pixelmap !== null ).assertTrue();
176   done();
177})
178
179// Capture error information when an exception occurs during function invoking.
180catch(error => {
181    console.log('TC_050-11 error: ' + error);
182    expect().assertFail();
183    done();
184  })
185
186// Obtain the number of bytes in each line of pixels.
187pixelmap.getBytesNumberPerRow( num => {
188	console.info('TC_025-1 num is ' + num);
189	expect(num == expectNum).assertTrue();
190	done();
191})
192
193// Obtain the total number of pixel bytes.
194pixelmap.getPixelBytesNumber(num => {
195        console.info('TC_026-1 num is ' + num);
196        expect(num == expectNum).assertTrue();
197        done();
198    })
199
200// Obtain the pixel map information.
201 pixelmap.getImageInfo( imageInfo => {})
202
203// Print the failure information.
204console.info('TC_024-1 imageInfo is empty');
205expect(false).assertTrue()
206
207// Release the PixelMap object.
208pixelmap.release(()=>{
209            expect(true).assertTrue();
210            console.log('TC_027-1 suc');
211            done();
212        })
213
214// Capture release failure information.
215catch(error => {
216            console.log('TC_027-1 error: ' + error);
217            expect().assertFail();
218            done();
219        })
220```
221
222### Encoding Scenario
223
224```js
225/data/local/tmp/test.png // Set the path for creating an image source.
226
227// Set the image source.
228 const imageSourceApi = image.createImageSource(path);//'/data/local/tmp/test.png'
229
230// Print the error message if the image source fails to be created.
231if (imageSourceApi == null) {
232   console.info('TC_062 create image source failed');
233   expect(false).assertTrue();
234   done();
235   }
236
237// Create an image packer if the image source is successfully created.
238const imagePackerApi = image.createImagePacker();
239
240// Print the error information if the image packer fails to be created.
241if (imagePackerApi == null) {
242    console.info('TC_062 create image packer failed');
243    expect(false).assertTrue();
244    done();
245    }
246
247// Set encoding parameters if the image packer is successfully created.
248let packOpts = { format:["image/jpeg"], // The supported encoding format is jpg.
249                 quality:98 }// Image quality, which ranges from 0 to 100.
250
251// Encode the image.
252imagePackerApi.packing(imageSourceApi, packOpts)
253.then( data => {
254         console.info('TC_062 finished');
255         expect(data !== null).assertTrue();
256         done();
257        })
258
259// Release the image packer after the encoding is complete.
260 imagePackerApi.release();
261
262// Obtain the image source information.
263imageSourceApi.getImageInfo(imageInfo => {
264     console.info('TC_045 imageInfo');
265     expect(imageInfo !== null).assertTrue();
266     done();
267   })
268
269// Update incremental data.
270imageSourceIncrementalSApi.updateData(array, false, 0, 10,(error,data )=> {})
271
272```
273