import image from '@ohos.multimedia.image';
import resourceManager from '@ohos.resourceManager';
import fs from '@ohos.file.fs';
@Entry
@Component
struct Index {
@State message: string = '图像编辑处理'
@State imagePixel: PixelMap = undefined;
@State image2: PixelMap = undefined;
aboutToAppear() {
this.getPicture((d) => {
this.imagePixel = d;
})
}
async getPicture(callback) {
const context: Context = getContext(this);
const resourceMgr: resourceManager.ResourceManager = context.resourceManager;
const fileData: Uint8Array = await resourceMgr.getRawFileContent('comment.png');
const buffer = fileData.buffer;
const imageSource: image.ImageSource = image.createImageSource(buffer);
const pixelMap: image.PixelMap = await imageSource.createPixelMap();
pixelMap.getImageInfo().then((info: image.ImageInfo) => {
console.info('info.width = ' + info.size.width);
console.info('info.height = ' + info.size.height);
}).catch((err) => {
console.error("Failed to obtain the image pixel map information.And the error is: " + err);
});
callback(pixelMap)
}
async Demo(imagePixel, callback) {
await imagePixel.scale(2, 1);
callback(imagePixel.getPixelMap());
}
@State isShow:boolean = false
build() {
Column({ space: 20 }) {
Text(this.message)
.fontSize(50)
.fontWeight(FontWeight.Bold)
Button("解码")
.fontSize(40)
.onClick(() => {
this.getPicture((data) => {
this.imagePixel = data;
})
})
Image(this.imagePixel)
.height(200)
Button("PixelMap")
.fontSize(40)
.onClick(() => {
this.Demo(this.imagePixel,(data)=>{
this.image2 = data
})
})
Button("编码")
.fontSize(40)
.onClick(() => {
this.imagePixel.scale(2,1).then(
(data)=>{
console.info("成功")
this.isShow = true
}
).catch()
})
Image(this.imagePixel)
.visibility(this.isShow?Visibility.Visible:Visibility.None)
.height(230)
.objectFit(ImageFit.Contain)
}
.width('100%')
.height('100%')
}
}