1/* 2 * Copyright (c) 2025-2025 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16import { image } from "./@ohos.multimedia.image.ets"; 17 18function main() { 19 // test createPixemap & getImageInfo 20 console.println("Test PixelMap START"); 21 const opts: image.InitializationOptions = { 22 size: { width: 480, height: 360 }, 23 srcPixelFormat: image.PixelMapFormat.RGBA_8888, 24 pixelFormat: image.PixelMapFormat.BGRA_8888, 25 editable: true, 26 alphaType: image.AlphaType.UNPREMUL, 27 scaleMode: image.ScaleMode.FIT_TARGET_SIZE 28 }; 29 let pixelMap:image.PixelMap = image.createPixelMapSync(opts); 30 31 if (pixelMap != undefined) { 32 console.println("Create PixelMap success"); 33 } 34 35 const retImageInfo: image.ImageInfo = pixelMap.getImageInfoSync(); 36 console.println(`Get image info: ${retImageInfo.size.width}, ${retImageInfo.size.height}, ${retImageInfo.pixelFormat}, ${retImageInfo.alphaType}`); 37 pixelMap.getImageInfo() 38 .then((imageInfo: image.ImageInfo) => { 39 console.println(`ASYNC Get image info: ${imageInfo.size.width}, ${imageInfo.size.height}, ${imageInfo.pixelFormat}, ${imageInfo.alphaType}`); 40 }); 41 42 const rowBytes = pixelMap.getBytesNumberPerRow(); 43 console.println("PixelMap bytes per row: " + rowBytes); 44 const totalBytes = pixelMap.getPixelBytesNumber(); 45 console.println("PixelMap total bytes: " + totalBytes); 46 47 if (retImageInfo.isHdr) { 48 console.println("Test PixelMap HDR"); 49 } else { 50 console.println("Test PixelMap not HDR"); 51 } 52 53 pixelMap.scaleSync(2, 2); 54 const scaledInfo = pixelMap.getImageInfoSync(); 55 console.println(`Scaled image info: ${scaledInfo.size.width}, ${scaledInfo.size.height}`); 56 const region: image.Region = { 57 size: { width: 512, height: 512 }, 58 x: 0, 59 y: 0 60 }; 61 pixelMap.cropSync(region); 62 const croppedInfo = pixelMap.getImageInfoSync(); 63 console.println(`Cropped image info: ${croppedInfo.size.width}, ${croppedInfo.size.height}`); 64 pixelMap.flipSync(true, true); 65 const flippedInfo = pixelMap.getImageInfoSync(); 66 console.println(`Flipped image info: ${flippedInfo.size.width}, ${flippedInfo.size.height}`); 67 68 const alphaPixelMap = pixelMap.createAlphaPixelmapSync(); 69 if (alphaPixelMap != undefined) { 70 console.println("Create alpha PixelMap success"); 71 } 72 const alphaImageInfo = alphaPixelMap.getImageInfoSync(); 73 console.println(`Alpha get image info: ${alphaImageInfo.size.width}, ${alphaImageInfo.size.height}, ${alphaImageInfo.pixelFormat}, ${alphaImageInfo.alphaType}`); 74 pixelMap.createAlphaPixelmap() 75 .then((alphaPixelMap: image.PixelMap) => { 76 const alphaImageInfo = alphaPixelMap.getImageInfoSync(); 77 console.println(`ASYNC Alpha get image info: ${alphaImageInfo.size.width}, ${alphaImageInfo.size.height}, ${alphaImageInfo.pixelFormat}, ${alphaImageInfo.alphaType}`); 78 }) 79 80 let imageSource: image.ImageSource = image.createImageSource("/data/local/tmp/test.png"); 81 if (imageSource != undefined) { 82 console.println("Create ImageSource by URI success"); 83 } 84 85 let imagesourceImageInfo: image.ImageInfo = imageSource.getImageInfoSync(0); 86 console.println(`Image source image info: ${imagesourceImageInfo.size.width}, ${imagesourceImageInfo.size.height}, ${imagesourceImageInfo.pixelFormat}, ${imagesourceImageInfo.density}, ${imagesourceImageInfo.mimeType}`); 87 imageSource.getImageInfo(0) 88 .then((imageInfo: image.ImageInfo) => { 89 console.println(`ASYNC Image source image info: ${imageInfo.size.width}, ${imageInfo.size.height}, ${imageInfo.pixelFormat}, ${imageInfo.density}, ${imageInfo.mimeType}`); 90 }); 91 92 const desiredSize: image.Size | undefined = { width: 60, height: 60 }; 93 const desiredRegion: image.Region | undefined = { 94 size: { width: 60, height: 60 }, 95 x: 0, 96 y: 0 97 }; 98 const decodeOpt: image.DecodingOptions = { 99 index: 0, 100 sampleSize: 1, 101 rotate: 0, 102 editable: true, 103 desiredSize, 104 desiredRegion, 105 desiredPixelFormat: image.PixelMapFormat.RGBA_8888, 106 fitDensity: 1 107 }; 108 let pixelmap2: image.PixelMap = imageSource.createPixelMapSync(decodeOpt); 109 if (pixelmap2 != undefined) { 110 console.println("Create imageSource.createPixelMapSync success"); 111 } 112 imageSource.createPixelMap(decodeOpt) 113 .then((pixelmap2: image.PixelMap) => { 114 if (pixelmap2 != undefined) { 115 console.println("ASYNC Create imageSource PixelMap success") 116 } 117 }); 118 pixelmap2.release() 119 .then((): void => console.println("ASYNC Release PixelMap success")); 120 121 imageSource.modifyImageProperty("Orientation", "Right-top"); 122 const map: Record<string, string | null> = { 123 "Artist": "CQY", 124 "Make": "Huawei", 125 "Gamma": "0.9", 126 }; 127 imageSource.modifyImageProperties(map) 128 .then((): void => { 129 let array: Array<string> = new Array<string>(4); 130 array[0] = "Orientation"; 131 array[1] = "Artist"; 132 array[2] = "Make"; 133 array[3] = "Gamma"; 134 imageSource.getImageProperties(array) 135 .then((properties: Record<string, string | null>): void => { 136 console.println("ASYNC Get image properties: " + properties); 137 }); 138 }) 139 140 console.println("TEST ImagePacker begin"); 141 const imagePacker = image.createImagePacker(); 142 console.println("ImagePacker supported formats: " + imagePacker.supportedFormats); 143 let packOpts: image.PackingOption = new image.PackingOption(); 144 packOpts.format = "image/jpeg"; 145 packOpts.quality = 90; 146 let packBuffer: ArrayBuffer = imagePacker.packing(pixelMap, packOpts); 147 console.println("TEST ImagePacker end, bufferSize: " + packBuffer.byteLength); 148 imagePacker.release(); 149 150 console.println("TEST pixelMap readPixelsToBuffer begin"); 151 let arrayBuffer: ArrayBuffer = new ArrayBuffer(opts.size.width * opts.size.height * 4); 152 pixelMap.readPixelsToBufferSync(arrayBuffer); 153 console.println("Read pixels to buffer success, size: " + arrayBuffer.byteLength); 154 pixelMap.readPixelsToBuffer(arrayBuffer) 155 .then((): void => console.println("ASYNC Read pixels to buffer success, size: " + arrayBuffer.byteLength)); 156 157 console.println("TEST Picture begin"); 158 const picture = image.createPicture(pixelMap); 159 if (picture != undefined) { 160 console.println("Create picture success"); 161 } 162 const picturePixelMap = picture.getMainPixelmap(); 163 const pictureInfo = picturePixelMap.getImageInfoSync(); 164 console.println(`Picture PixelMap image info: ${pictureInfo.size.width}, ${pictureInfo.size.height}, ${pictureInfo.pixelFormat}, ${pictureInfo.alphaType}`); 165 166 const regionAsync: image.Region = { 167 size: { width: 1, height: 1 }, 168 x: 256, 169 y: 256 170 } 171 pixelMap.crop(regionAsync) 172 .then((): void => { 173 const croppedInfo = pixelMap.getImageInfoSync(); 174 console.println(`ASYNC Cropped image info: ${croppedInfo.size.width}, ${croppedInfo.size.height}`); 175 }); 176 177 console.println("====== Sync methods completed ======") 178}