// 全局导入Image模块
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);
// 获取resourceManager资源管理
const resourceMgr: resourceManager.ResourceManager = context.resourceManager;
// 获取rawfile文件夹下test.jpg的ArrayBuffer
const fileData: Uint8Array = await resourceMgr.getRawFileContent('comment.png');
// 获取图片的ArrayBuffer
const buffer = fileData.buffer;
// 创建imageSource
const imageSource: image.ImageSource = image.createImageSource(buffer);
// 创建PixelMap
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%')
}
}
图片开发Demo
于 2024-08-02 16:01:42 首次发布