支持多种组合变换

Signed-off-by: zhang_hanyong <zhang_hanyong@h-partners.com>
This commit is contained in:
zhang_hanyong
2024-05-21 10:56:45 +08:00
parent 6d43923279
commit 1e94a03d5b
6 changed files with 533 additions and 70 deletions
@@ -33,7 +33,9 @@ import { GIFFrame } from '../utils/gif/GIFFrame'
import { LogUtil } from '../../imageknife/utils/LogUtil'
import { BusinessError } from '@ohos.base'
import { DataFetchResult } from '../networkmanage/DataFetchResult'
import { BaseTransform } from '../transform/BaseTransform'
const imagePackerApi = image.createImagePacker();
export enum Stage {
@@ -173,16 +175,7 @@ export class RequestManager {
this.svgProcess(request, onComplete, onError, arrayBuffer, typeValue)
} else {
if (request.transformations[0]) {
request.transformations[0].transform(arrayBuffer, request, {
asyncTransform: (error: BusinessError | string, pixelMap: PixelMap | null) => {
// 输出给Image
if (pixelMap) {
onComplete(pixelMap);
} else {
onError(error);
}
}
})
this.getTransformImage(request, arrayBuffer, request.transformations, 0, onComplete, onError);
}
else {
let success = (value: PixelMap) => {
@@ -201,6 +194,60 @@ export class RequestManager {
}
private getTransformImage(request: RequestOption, source: ArrayBuffer, transformations: Array<BaseTransform<PixelMap>>, index: number,
onComplete: (value: PixelMap | GIFFrame[]) => void | PromiseLike<ImageKnifeData>, onError: (reason?: BusinessError | string) => void) {
transformations[index].transform(source, request, {
asyncTransform: (error: BusinessError | string, pixelMap: PixelMap | null) => {
// 输出给Image
if (pixelMap) {
if (index == request.transformations.length - 1) {
onComplete(pixelMap)
} else {
let packOpts: image.PackingOption = { format: "image/png", quality: 98 };
imagePackerApi.packing(pixelMap, packOpts).then((data: ArrayBuffer) => {
// data 为打包获取到的文件流,写入文件保存即可得到一张图片
index++;
this.getTransformImage(request, data, transformations, index, onComplete, onError);
}).catch((error: BusinessError) => {
onError(error);
console.error('requestManager Failed to pack the image. And the error is: ' + JSON.stringify(error));
})
}
} else {
onError(error);
console.error('requestManager getTransformImage error! ' + JSON.stringify(error));
}
}
})
}
private saveTransformImage(request: RequestOption, source: ArrayBuffer, transformations: Array<BaseTransform<PixelMap>>, index: number, filetype: string,
onComplete: (value: PixelMap) => void | PromiseLike<ImageKnifeData>, onError: (reason?: BusinessError | string) => void) {
transformations[index].transform(source, request, {
asyncTransform: (error: BusinessError | string, pixelMap: PixelMap | null) => {
// 输出给Image
if (pixelMap) {
if (index == request.transformations.length - 1) {
this.saveCacheAndDisk(pixelMap, filetype, onComplete, source);
} else {
let packOpts: image.PackingOption = { format: "image/png", quality: 98 };
imagePackerApi.packing(pixelMap, packOpts).then((data: ArrayBuffer) => {
// data 为打包获取到的文件流,写入文件保存即可得到一张图片
index++;
this.saveTransformImage(request, data, transformations, index, filetype, onComplete, onError);
}).catch((error: BusinessError) => {
onError(error);
console.error('requestManager Failed to pack the image. And the error is: ' + JSON.stringify(error));
})
}
} else {
onError(error);
console.error('requestManager saveTransformImage error! ' + JSON.stringify(error));
}
}
})
}
// 加载磁盘缓存 原图
private loadDiskFromSource(request: RequestOption, onComplete: (value: PixelMap | GIFFrame[]) => void | PromiseLike<ImageKnifeData>, onError: (reason?: BusinessError | string) => void) {
LogUtil.log("ImageKnife RequestManager loadDiskFromSource")
@@ -286,37 +333,13 @@ export class RequestManager {
}
let thumbCallback = this.options.thumbholderOnComplete;
let thumbError = this.options.thumbholderOnError;
this.options.transformations[0].transform(source, thumbOption, {
asyncTransform: (error: BusinessError | string, pixelMap: PixelMap | null) => {
if (pixelMap) {
thumbCallback(pixelMap);
} else {
thumbError(error);
}
}
})
this.getTransformImage(thumbOption, source, this.options.transformations, 0, thumbCallback, thumbError);
setTimeout(() => {
this.options.transformations[0].transform(source, request, {
asyncTransform: (error: BusinessError | string, pixelMap: PixelMap | null) => {
if (pixelMap) {
onComplete(pixelMap);
} else {
onError(error);
}
}
})
this.getTransformImage(request, source, this.options.transformations, 0, onComplete, onError);
}, this.options.thumbDelayTime);
}
else {
this.options.transformations[0].transform(source, request, {
asyncTransform: (error: BusinessError | string, pixelMap: PixelMap | null) => {
if (pixelMap) {
onComplete(pixelMap);
} else {
onError(error);
}
}
})
this.getTransformImage(request, source, this.options.transformations, 0, onComplete, onError);
}
} else {
// thumbnail 缩略图部分
@@ -427,17 +450,7 @@ export class RequestManager {
this.thumbnailProcess(source, filetype, onComplete, onError);
}
} else {
this.options.transformations[0].transform(source, this.options, {
asyncTransform: (error: BusinessError | string, pixelMap: PixelMap | null) => {
if (pixelMap) {
if (filetype != null) {
this.saveCacheAndDisk(pixelMap, filetype, onComplete, source);
}
} else {
onError(error);
}
}
})
this.saveTransformImage(this.options, source, this.options.transformations, 0, filetype, onComplete, onError);
}
} else {
// thumbnail 缩略图部分
@@ -507,25 +520,9 @@ export class RequestManager {
})
let thumbCallback = this.options.thumbholderOnComplete
let thumbError = this.options.thumbholderOnError
this.options.transformations[0].transform(source, thumbOption, {
asyncTransform: (error: BusinessError | string, pixelMap: PixelMap | null) => {
if (pixelMap) {
thumbCallback(pixelMap);
} else {
thumbError(error);
}
}
})
this.getTransformImage(thumbOption, source, this.options.transformations, 0, thumbCallback, thumbError);
setTimeout(() => {
this.options.transformations[0].transform(source, this.options, {
asyncTransform: (error: BusinessError | string, pixelMap: PixelMap | null) => {
if (pixelMap) {
this.saveCacheAndDisk(pixelMap, filetype, onComplete, source);
} else {
onError(error);
}
}
})
this.saveTransformImage(this.options, source, this.options.transformations, 0, filetype, onComplete, onError);
}, this.options.thumbDelayTime)
}