image.md 8.1 KB
Newer Older
W
wusongqing 已提交
1 2 3 4 5 6 7 8
# Image Development

## When to Use

You can use image development APIs to decode images into pixel maps and encode the pixel maps into a supported format.

## Available APIs

W
wusongqing 已提交
9
For details about the APIs, see [Image Processing](../reference/apis/js-apis-image.md).
W
wusongqing 已提交
10 11 12 13 14 15 16 17

## How to Develop

### Full-Process Scenario

The full process includes creating an instance, reading image information, reading and writing pixel maps, updating data, packaging pixels, and releasing resources.

```js
W
wusongqing 已提交
18
const color = new ArrayBuffer(96); // Create a buffer to store image pixel data.
W
wusongqing 已提交
19 20 21
let opts = { alphaType: 0, editable: true, pixelFormat: 4, scaleMode: 1, size: { height: 2, width: 3 } } // Image pixel data.

// Create a PixelMap object.
W
wusongqing 已提交
22
const color = new ArrayBuffer(96);
W
wusongqing 已提交
23
let opts = { alphaType: 0, editable: true, pixelFormat: 4, scaleMode: 1, size: { height: 2, width: 3 } }
G
Gloria 已提交
24 25 26 27
image.createPixelMap(color, opts, pixelmap => {
    console.log('Succeeded in creating pixelmap.');
})

W
wusongqing 已提交
28
// Read pixels.
W
wusongqing 已提交
29
pixelmap.readPixels(area,(data) => {
G
Gloria 已提交
30 31
    if(data !== null) {
        var bufferArr = new Uint8Array(area.pixels);
W
wusongqing 已提交
32 33
        var res = true;
        for (var i = 0; i < bufferArr.length; i++) {
G
Gloria 已提交
34 35 36 37 38 39
            console.info(' buffer ' + bufferArr[i]);
            if(res) {
                if(bufferArr[i] == 0) {
                    res = false;
                    console.log('readPixels end.');
                    break;
W
wusongqing 已提交
40
                }
G
Gloria 已提交
41 42 43 44
            }
        }
    }
})
W
wusongqing 已提交
45 46 47 48
 
// Store pixels.
const readBuffer = new ArrayBuffer(96);
pixelmap.readPixelsToBuffer(readBuffer,() => {
G
Gloria 已提交
49 50 51 52 53 54 55 56 57
    var bufferArr = new Uint8Array(readBuffer);
    var res = true;
    for (var i = 0; i < bufferArr.length; i++) {
        if(res) {
            if (bufferArr[i] !== 0) {
                res = false;
                console.log('readPixelsToBuffer end.');
                break;
            }
W
wusongqing 已提交
58
        }
W
wusongqing 已提交
59
    }
G
Gloria 已提交
60
})
W
wusongqing 已提交
61 62 63 64
    
// Write pixels.
pixelmap.writePixels(area,() => {
    const readArea = { pixels: new ArrayBuffer(20), offset: 0, stride: 8, region: { size: { height: 1, width: 2 }, x: 0, y: 0 }}
W
wusongqing 已提交
65 66 67 68 69 70 71
    pixelmap.readPixels(readArea,() => {
        var readArr = new Uint8Array(readArea.pixels);
        var res = true;
        for (var i = 0; i < readArr.length; i++) {
            if(res) {
                if (readArr[i] !== 0) {
                    res = false;
G
Gloria 已提交
72
                    console.log('readPixels end.please check buffer');
W
wusongqing 已提交
73 74
                    break;
                }
W
wusongqing 已提交
75
            }
W
wusongqing 已提交
76
        }
G
Gloria 已提交
77 78
    })
})
W
wusongqing 已提交
79 80
  
// Write pixels to the buffer.
W
wusongqing 已提交
81 82 83
pixelmap.writeBufferToPixels(writeColor).then(() => {
    const readBuffer = new ArrayBuffer(96);
    pixelmap.readPixelsToBuffer(readBuffer).then (() => {
G
Gloria 已提交
84 85 86 87 88 89 90
        var bufferArr = new Uint8Array(readBuffer);
        var res = true;
        for (var i = 0; i < bufferArr.length; i++) {
            if(res) {
                if (bufferArr[i] !== i) {
                    res = false;
                    console.log('readPixels end.please check buffer');
W
wusongqing 已提交
91 92 93
                    break;
                }
            }
W
wusongqing 已提交
94
        }
G
Gloria 已提交
95 96
    })
})
W
wusongqing 已提交
97 98 99

// Obtain image information.
pixelmap.getImageInfo( imageInfo => {
W
wusongqing 已提交
100
    if (imageInfo !== null) {
G
Gloria 已提交
101
	    console.log('Succeeded in getting imageInfo');
W
wusongqing 已提交
102 103
    } 
})
W
wusongqing 已提交
104 105 106

// Release the PixelMap object.
pixelmap.release(()=>{
G
Gloria 已提交
107
    console.log('Succeeded in releasing pixelmap');
W
wusongqing 已提交
108 109 110
})

// Create an image source (uri).
G
Gloria 已提交
111 112
let path = '/data/local/tmp/test.jpg';
const imageSourceApi = image.createImageSource(path);
W
wusongqing 已提交
113 114

// Create an image source (fd).
G
Gloria 已提交
115 116
let fd = 29;
const imageSourceApi = image.createImageSource(fd);
W
wusongqing 已提交
117 118 119 120 121 122 123

// Create an image source (data).
const data = new ArrayBuffer(96);
const imageSourceApi = image.createImageSource(data);

// Release the image source.
imageSourceApi.release(() => {
G
Gloria 已提交
124 125
    console.log('Succeeded in releasing imagesource');
})
W
wusongqing 已提交
126 127 128
    
// Encode the image.
const imagePackerApi = image.createImagePacker();
G
Gloria 已提交
129 130
const imageSourceApi = image.createImageSource(0);
let packOpts = { format:"image/jpeg", quality:98 };
W
wusongqing 已提交
131
imagePackerApi.packing(imageSourceApi, packOpts, data => {
G
Gloria 已提交
132
    console.log('Succeeded in packing');
W
wusongqing 已提交
133
})
W
wusongqing 已提交
134 135 136 137 138 139 140 141
 
// Release the ImagePacker object.
imagePackerApi.release();
```

### Decoding Scenario

```js
W
wusongqing 已提交
142
let path = '/data/local/tmp/test.jpg'; // Set the path for creating an image source.
W
wusongqing 已提交
143 144

// Create an image source using a path.
W
wusongqing 已提交
145
const imageSourceApi = image.createImageSource(path); // '/data/local/tmp/test.jpg'
W
wusongqing 已提交
146 147 148 149 150 151 152 153 154 155 156 157 158 159

// Set parameters.
let decodingOptions = {
    sampleSize:1, // Sampling size of the thumbnail.
    editable: true, // Whether the image can be edited.
    desiredSize:{ width:1, height:2}, // Desired output size of the image.
    rotateDegrees:10, // Rotation angle of the image.
    desiredPixelFormat:2, // Decoded pixel format.
    desiredRegion: { size: { height: 1, width: 2 }, x: 0, y: 0 }, // Region of the image to decode.
    index:0// Image sequence number.
    };
    
// Create a pixel map in callback mode.
imageSourceApi.createPixelMap(decodingOptions, pixelmap => {
G
Gloria 已提交
160 161
    console.log('Succeeded in creating pixelmap.');
})
W
wusongqing 已提交
162 163 164

// Create a pixel map in promise mode.
imageSourceApi.createPixelMap().then(pixelmap => {
G
Gloria 已提交
165
    console.log('Succeeded in creating pixelmap.');
W
wusongqing 已提交
166 167 168 169
})

// Capture error information when an exception occurs during function invoking.
catch(error => {
G
Gloria 已提交
170
    console.log('Failed in creating pixelmap.' + error);
W
wusongqing 已提交
171
})
W
wusongqing 已提交
172 173 174

// Obtain the number of bytes in each line of pixels.
pixelmap.getBytesNumberPerRow( num => {
G
Gloria 已提交
175
    console.log('Succeeded in getting BytesNumber PerRow.');
W
wusongqing 已提交
176 177 178 179
})

// Obtain the total number of pixel bytes.
pixelmap.getPixelBytesNumber(num => {
G
Gloria 已提交
180
    console.log('Succeeded in getting PixelBytesNumber.');
W
wusongqing 已提交
181
})
W
wusongqing 已提交
182 183

// Obtain the pixel map information.
W
wusongqing 已提交
184
pixelmap.getImageInfo( imageInfo => {})
W
wusongqing 已提交
185 186 187

// Release the PixelMap object.
pixelmap.release(()=>{
G
Gloria 已提交
188
    console.log('Succeeded in releasing pixelmap');
W
wusongqing 已提交
189
})    
W
wusongqing 已提交
190 191 192

// Capture release failure information.
catch(error => {
G
Gloria 已提交
193
    console.log('Failed in releasing pixelmap.' + error);
W
wusongqing 已提交
194
})
W
wusongqing 已提交
195 196 197 198 199
```

### Encoding Scenario

```js
W
wusongqing 已提交
200
let path = '/data/local/tmp/test.png' // Set the path for creating an image source.
W
wusongqing 已提交
201 202

// Set the image source.
W
wusongqing 已提交
203
const imageSourceApi = image.createImageSource(path); // '/data/local/tmp/test.png'
W
wusongqing 已提交
204 205 206
 
// Print the error message if the image source fails to be created.
if (imageSourceApi == null) {
G
Gloria 已提交
207
    console.log('Failed in creating imageSource.');
W
wusongqing 已提交
208
}
W
wusongqing 已提交
209 210 211 212 213 214
   
// Create an image packer if the image source is successfully created.
const imagePackerApi = image.createImagePacker();

// Print the error information if the image packer fails to be created.
if (imagePackerApi == null) {
G
Gloria 已提交
215
    console.log('Failed in creating imagePacker.');
W
wusongqing 已提交
216
}
W
wusongqing 已提交
217 218 219

// Set encoding parameters if the image packer is successfully created.
let packOpts = { format:["image/jpeg"], // The supported encoding format is jpg.
W
wusongqing 已提交
220
                 quality:98 } // Image quality, which ranges from 0 to 100.
W
wusongqing 已提交
221 222 223 224

// Encode the image.
imagePackerApi.packing(imageSourceApi, packOpts)
.then( data => {
G
Gloria 已提交
225
    console.log('Succeeded in packing');
W
wusongqing 已提交
226
})
G
Gloria 已提交
227
         
W
wusongqing 已提交
228
// Release the image packer after the encoding is complete.
W
wusongqing 已提交
229 230
imagePackerApi.release();

W
wusongqing 已提交
231 232
// Obtain the image source information.
imageSourceApi.getImageInfo(imageInfo => {
G
Gloria 已提交
233
    console.log('Succeeded in getting imageInfo');
W
wusongqing 已提交
234 235
})

W
wusongqing 已提交
236
// Update incremental data.
W
wusongqing 已提交
237
imageSourceIncrementalSApi.updateData(array, false, 0, 10,(error, data)=> {})
W
wusongqing 已提交
238 239

```
W
wusongqing 已提交
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266

### Using ImageReceiver

Example scenario: The camera functions as the client to transmit image data to the server.

```js
public async init(surfaceId: any) {

    // (Server code) Create an ImageReceiver object.
    var receiver = image.createImageReceiver(8 * 1024, 8, image.ImageFormat.JPEG, 1);

    // Obtain the surface ID.
    var surfaceId = await receiver.getReceivingSurfaceId();

    // Register a surface listener, which is triggered after the buffer of the surface is ready.
    receiver.on('imageArrival', () => {
        // Obtain the latest buffer of the surface.
        receiver.readNextImage((err, img) => {
            img.getComponent(4, (err, component) => {
                // Consume component.byteBuffer. For example, save the content in the buffer as an image.
		    })
	    })
    })

    // Call a Camera API to transfer the surface ID to the camera, which then obtains the surface based on the surface ID and generates a surface buffer.
}
```