|
|
|
@ -15,7 +15,9 @@
|
|
|
|
|
import {
|
|
|
|
|
CacheStrategy,
|
|
|
|
|
ImageKnifeRequestSource,
|
|
|
|
|
ImageKnifeRequestWithSource, RequestJobRequest } from './model/ImageKnifeData';
|
|
|
|
|
ImageKnifeRequestWithSource,
|
|
|
|
|
RequestJobRequest
|
|
|
|
|
} from './model/ImageKnifeData';
|
|
|
|
|
import List from '@ohos.util.List'
|
|
|
|
|
import { FileCache } from './cache/FileCache';
|
|
|
|
|
import { LogUtil } from './utils/LogUtil';
|
|
|
|
@ -42,20 +44,20 @@ class RequestData {
|
|
|
|
|
export class ImageKnifeLoader {
|
|
|
|
|
static async parseImage(resBuf: ArrayBuffer, typeValue: string, fileKey: string,
|
|
|
|
|
request: RequestJobRequest): Promise<RequestJobResult> {
|
|
|
|
|
if(request.isAnimator) {
|
|
|
|
|
return ImageKnifeLoader.parseForAnimatorComponent(resBuf ,typeValue ,fileKey, request)
|
|
|
|
|
if (request.isAnimator) {
|
|
|
|
|
return ImageKnifeLoader.parseForAnimatorComponent(resBuf, typeValue, fileKey, request)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (typeValue === 'gif' || typeValue === 'webp') {
|
|
|
|
|
return ImageKnifeLoader.parseAnimatorImage(resBuf ,typeValue ,fileKey , request)
|
|
|
|
|
} else if(typeValue == "svg") {
|
|
|
|
|
return ImageKnifeLoader.parseSvgImage(resBuf ,typeValue ,fileKey , request)
|
|
|
|
|
return ImageKnifeLoader.parseAnimatorImage(resBuf, typeValue, fileKey, request)
|
|
|
|
|
} else if (typeValue == "svg") {
|
|
|
|
|
return ImageKnifeLoader.parseSvgImage(resBuf, typeValue, fileKey, request)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ImageKnifeLoader.parseNormalImage(resBuf, typeValue, fileKey, request)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static makeEmptyResult(error: string): RequestJobResult{
|
|
|
|
|
static makeEmptyResult(error: string): RequestJobResult {
|
|
|
|
|
return {
|
|
|
|
|
pixelMap: undefined,
|
|
|
|
|
bufferSize: 0,
|
|
|
|
@ -64,23 +66,25 @@ export class ImageKnifeLoader {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static async parseNormalImage(resBuf: ArrayBuffer, typeValue: string, fileKey: string, request: RequestJobRequest):Promise<RequestJobResult> {
|
|
|
|
|
static async parseNormalImage(resBuf: ArrayBuffer, typeValue: string, fileKey: string,
|
|
|
|
|
request: RequestJobRequest): Promise<RequestJobResult> {
|
|
|
|
|
let resPixelmap: PixelMap | undefined = undefined
|
|
|
|
|
let decodingOptions: image.DecodingOptions = {
|
|
|
|
|
editable: request.requestSource === ImageKnifeRequestSource.SRC && request.transformation !== undefined ? true : false,
|
|
|
|
|
editable: request.requestSource === ImageKnifeRequestSource.SRC && request.transformation !== undefined ? true :
|
|
|
|
|
false,
|
|
|
|
|
}
|
|
|
|
|
let imageSource: image.ImageSource = image.createImageSource(resBuf)
|
|
|
|
|
if (imageSource === undefined){
|
|
|
|
|
if (imageSource === undefined) {
|
|
|
|
|
return ImageKnifeLoader.makeEmptyResult("image.createImageSource failed")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let size = (await imageSource.getImageInfo()).size
|
|
|
|
|
try{
|
|
|
|
|
try {
|
|
|
|
|
if ((request.downsampType !== DownsampleStrategy.NONE) &&
|
|
|
|
|
request.requestSource == ImageKnifeRequestSource.SRC ) {
|
|
|
|
|
decodingOptions =await ImageKnifeLoader.downsamplerReqSize(typeValue,request,size,ImageKnifeRequestSource.SRC)
|
|
|
|
|
request.requestSource == ImageKnifeRequestSource.SRC) {
|
|
|
|
|
decodingOptions = ImageKnifeLoader.downsamplerReqSize(typeValue, request, size, ImageKnifeRequestSource.SRC)
|
|
|
|
|
}
|
|
|
|
|
}catch(err){
|
|
|
|
|
} catch (err) {
|
|
|
|
|
return ImageKnifeLoader.makeEmptyResult("image.createImageSource failed")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -97,15 +101,16 @@ export class ImageKnifeLoader {
|
|
|
|
|
pixelMap: resPixelmap,
|
|
|
|
|
bufferSize: resBuf.byteLength,
|
|
|
|
|
fileKey: fileKey,
|
|
|
|
|
size:size,
|
|
|
|
|
type:typeValue
|
|
|
|
|
size: size,
|
|
|
|
|
type: typeValue
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static async parseSvgImage(resBuf: ArrayBuffer, typeValue: string, fileKey: string,
|
|
|
|
|
request: RequestJobRequest): Promise<RequestJobResult> {
|
|
|
|
|
let resPixelmap: PixelMap | undefined = undefined
|
|
|
|
|
let imageSource: image.ImageSource = image.createImageSource(resBuf)
|
|
|
|
|
if (imageSource === undefined){
|
|
|
|
|
if (imageSource === undefined) {
|
|
|
|
|
return ImageKnifeLoader.makeEmptyResult("image.createImageSource failed")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -121,12 +126,12 @@ export class ImageKnifeLoader {
|
|
|
|
|
editable: true,
|
|
|
|
|
desiredSize: defaultSize
|
|
|
|
|
};
|
|
|
|
|
try{
|
|
|
|
|
try {
|
|
|
|
|
if ((request.downsampType !== DownsampleStrategy.NONE) &&
|
|
|
|
|
request.requestSource == ImageKnifeRequestSource.SRC ) {
|
|
|
|
|
opts =await ImageKnifeLoader.downsamplerReqSize(typeValue,request,size)
|
|
|
|
|
request.requestSource == ImageKnifeRequestSource.SRC) {
|
|
|
|
|
opts = ImageKnifeLoader.downsamplerReqSize(typeValue, request, size)
|
|
|
|
|
}
|
|
|
|
|
}catch(err){
|
|
|
|
|
} catch (err) {
|
|
|
|
|
return ImageKnifeLoader.makeEmptyResult("image.createImageSource failed")
|
|
|
|
|
}
|
|
|
|
|
await imageSource.createPixelMap(opts)
|
|
|
|
@ -142,13 +147,14 @@ export class ImageKnifeLoader {
|
|
|
|
|
pixelMap: resPixelmap,
|
|
|
|
|
bufferSize: resBuf.byteLength,
|
|
|
|
|
fileKey: fileKey,
|
|
|
|
|
type:typeValue
|
|
|
|
|
type: typeValue
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static async parseAnimatorImage(resBuf: ArrayBuffer, typeValue: string,
|
|
|
|
|
fileKey: string,request: RequestJobRequest): Promise<RequestJobResult> {
|
|
|
|
|
fileKey: string, request: RequestJobRequest): Promise<RequestJobResult> {
|
|
|
|
|
let imageSource: image.ImageSource = image.createImageSource(resBuf)
|
|
|
|
|
if (imageSource === undefined){
|
|
|
|
|
if (imageSource === undefined) {
|
|
|
|
|
return ImageKnifeLoader.makeEmptyResult("image.createImageSource failed")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -156,28 +162,32 @@ export class ImageKnifeLoader {
|
|
|
|
|
let size = (await imageSource.getImageInfo()).size
|
|
|
|
|
imageSource.release()
|
|
|
|
|
|
|
|
|
|
if(frameCount == undefined || frameCount == 1) {
|
|
|
|
|
if (frameCount == undefined || frameCount == 1) {
|
|
|
|
|
} else {
|
|
|
|
|
let base64str = "data:image/" + typeValue + ";base64," + new util.Base64Helper().encodeToStringSync(new Uint8Array(resBuf))
|
|
|
|
|
let base64str =
|
|
|
|
|
"data:image/" + typeValue + ";base64," + new util.Base64Helper().encodeToStringSync(new Uint8Array(resBuf))
|
|
|
|
|
return {
|
|
|
|
|
pixelMap: base64str,
|
|
|
|
|
bufferSize: resBuf.byteLength,
|
|
|
|
|
fileKey: fileKey,
|
|
|
|
|
size:size,
|
|
|
|
|
type:typeValue
|
|
|
|
|
size: size,
|
|
|
|
|
type: typeValue
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
return ImageKnifeLoader.parseNormalImage(resBuf, typeValue, fileKey, request)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 为AnimatorComponent解析动图
|
|
|
|
|
static async parseForAnimatorComponent(resBuf: ArrayBuffer, typeValue: string, fileKey: string,request: RequestJobRequest): Promise<RequestJobResult> {
|
|
|
|
|
static async parseForAnimatorComponent(resBuf: ArrayBuffer, typeValue: string, fileKey: string,
|
|
|
|
|
request: RequestJobRequest): Promise<RequestJobResult> {
|
|
|
|
|
if (typeValue === 'gif' || typeValue === 'webp') {
|
|
|
|
|
let imageSource: image.ImageSource = image.createImageSource(resBuf);
|
|
|
|
|
if (imageSource === undefined){
|
|
|
|
|
if (imageSource === undefined) {
|
|
|
|
|
return ImageKnifeLoader.makeEmptyResult("image.createImageSource failed")
|
|
|
|
|
}
|
|
|
|
|
let decodingOptions: image.DecodingOptions = {
|
|
|
|
|
editable: request.requestSource === ImageKnifeRequestSource.SRC && request.transformation !== undefined ? true : false,
|
|
|
|
|
editable: request.requestSource === ImageKnifeRequestSource.SRC && request.transformation !== undefined ? true :
|
|
|
|
|
false,
|
|
|
|
|
}
|
|
|
|
|
let pixelMapList: Array<PixelMap> = []
|
|
|
|
|
let delayList: Array<number> = []
|
|
|
|
@ -214,13 +224,15 @@ export class ImageKnifeLoader {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 获取图片资源
|
|
|
|
|
static async getImageArrayBuffer(request: RequestJobRequest, requestList: List<ImageKnifeRequestWithSource> | undefined,fileKey:string): Promise<ArrayBuffer> {
|
|
|
|
|
static async getImageArrayBuffer(request: RequestJobRequest,
|
|
|
|
|
requestList: List<ImageKnifeRequestWithSource> | undefined, fileKey: string): Promise<ArrayBuffer> {
|
|
|
|
|
let resBuf: ArrayBuffer | undefined
|
|
|
|
|
|
|
|
|
|
// 判断自定义下载
|
|
|
|
|
if (request.customGetImage !== undefined && request.requestSource == ImageKnifeRequestSource.SRC && typeof request.src == "string") {
|
|
|
|
|
if (request.customGetImage !== undefined && request.requestSource == ImageKnifeRequestSource.SRC &&
|
|
|
|
|
typeof request.src == "string") {
|
|
|
|
|
// 先从文件缓存获取
|
|
|
|
|
resBuf = FileCache.getFileCacheByFile(request.context, fileKey , request.fileCacheFolder)
|
|
|
|
|
resBuf = FileCache.getFileCacheByFile(request.context, fileKey, request.fileCacheFolder)
|
|
|
|
|
if (resBuf === undefined) {
|
|
|
|
|
LogUtil.log("start customGetImage src=" + request.src)
|
|
|
|
|
try {
|
|
|
|
@ -239,16 +251,14 @@ export class ImageKnifeLoader {
|
|
|
|
|
LogUtil.log("ImageKnife_DataTime_requestJob_saveFileCacheOnlyFile.end:" + request.src)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
} else {
|
|
|
|
|
if (typeof request.src === 'string') {
|
|
|
|
|
if (request.src.indexOf("http://") == 0 || request.src.indexOf("https://") == 0) { //从网络下载
|
|
|
|
|
// 先从文件缓存获取
|
|
|
|
|
resBuf = FileCache.getFileCacheByFile(request.context, fileKey , request.fileCacheFolder)
|
|
|
|
|
if (resBuf !== undefined){
|
|
|
|
|
resBuf = FileCache.getFileCacheByFile(request.context, fileKey, request.fileCacheFolder)
|
|
|
|
|
if (resBuf !== undefined) {
|
|
|
|
|
LogUtil.log("success get image from filecache for key = " + fileKey + " src = " + request.src)
|
|
|
|
|
}
|
|
|
|
|
else if (request.onlyRetrieveFromCache != true) {
|
|
|
|
|
} else if (request.onlyRetrieveFromCache != true) {
|
|
|
|
|
LogUtil.log("HttpDownloadClient.start:" + request.src)
|
|
|
|
|
let httpRequest = http.createHttp();
|
|
|
|
|
let progress: number = 0
|
|
|
|
@ -277,10 +287,11 @@ export class ImageKnifeLoader {
|
|
|
|
|
if (requestList === undefined) {
|
|
|
|
|
// 子线程
|
|
|
|
|
emitter.emit(Constants.PROGRESS_EMITTER + request.memoryKey, { data: { "value": progress } })
|
|
|
|
|
}else {
|
|
|
|
|
} else {
|
|
|
|
|
// 主线程请求
|
|
|
|
|
requestList!.forEach((requestWithSource: ImageKnifeRequestWithSource) => {
|
|
|
|
|
if (requestWithSource.request.imageKnifeOption.progressListener !== undefined && requestWithSource.source === ImageKnifeRequestSource.SRC) {
|
|
|
|
|
if (requestWithSource.request.imageKnifeOption.progressListener !== undefined &&
|
|
|
|
|
requestWithSource.source === ImageKnifeRequestSource.SRC) {
|
|
|
|
|
requestWithSource.request.imageKnifeOption.progressListener(progress)
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
@ -311,29 +322,31 @@ export class ImageKnifeLoader {
|
|
|
|
|
LogUtil.log("HttpDownloadClient.end:" + request.src)
|
|
|
|
|
// 保存文件缓存
|
|
|
|
|
if (resBuf !== undefined && request.writeCacheStrategy !== CacheStrategy.Memory) {
|
|
|
|
|
LogUtil.log("ImageKnife_DataTime_requestJob_saveFileCacheOnlyFile.start:"+request.src)
|
|
|
|
|
FileCache.saveFileCacheOnlyFile(request.context, fileKey, resBuf , request.fileCacheFolder)
|
|
|
|
|
LogUtil.log("ImageKnife_DataTime_requestJob_saveFileCacheOnlyFile.end:"+request.src)
|
|
|
|
|
LogUtil.log("ImageKnife_DataTime_requestJob_saveFileCacheOnlyFile.start:" + request.src)
|
|
|
|
|
FileCache.saveFileCacheOnlyFile(request.context, fileKey, resBuf, request.fileCacheFolder)
|
|
|
|
|
LogUtil.log("ImageKnife_DataTime_requestJob_saveFileCacheOnlyFile.end:" + request.src)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
} else {
|
|
|
|
|
throw new Error('onlyRetrieveFromCache,do not fetch image src = ' + request.src)
|
|
|
|
|
}
|
|
|
|
|
} else if (request.src.startsWith('datashare://') || request.src.startsWith('file://')) {
|
|
|
|
|
await fs.open(request.src, fs.OpenMode.READ_ONLY).then(async (file) => {
|
|
|
|
|
await fs.stat(file.fd).then(async (stat) =>{
|
|
|
|
|
await fs.stat(file.fd).then(async (stat) => {
|
|
|
|
|
let buf = new ArrayBuffer(stat.size);
|
|
|
|
|
await fs.read(file.fd, buf).then((readLen) => {
|
|
|
|
|
resBuf = buf;
|
|
|
|
|
fs.closeSync(file.fd);
|
|
|
|
|
}).catch((err:BusinessError) => {
|
|
|
|
|
throw new Error('LoadDataShareFileClient fs.read err happened uri=' + request.src + " err.msg=" + err?.message + " err.code=" + err?.code)
|
|
|
|
|
}).catch((err: BusinessError) => {
|
|
|
|
|
throw new Error('LoadDataShareFileClient fs.read err happened uri=' + request.src + " err.msg=" +
|
|
|
|
|
err?.message + " err.code=" + err?.code)
|
|
|
|
|
})
|
|
|
|
|
}).catch((err:BusinessError) => {
|
|
|
|
|
throw new Error('LoadDataShareFileClient fs.stat err happened uri=' + request.src + " err.msg=" + err?.message + " err.code=" + err?.code)
|
|
|
|
|
}).catch((err: BusinessError) => {
|
|
|
|
|
throw new Error('LoadDataShareFileClient fs.stat err happened uri=' + request.src + " err.msg=" +
|
|
|
|
|
err?.message + " err.code=" + err?.code)
|
|
|
|
|
})
|
|
|
|
|
}).catch((err:BusinessError) => {
|
|
|
|
|
throw new Error('LoadDataShareFileClient fs.open err happened uri=' + request.src + " err.msg=" + err?.message + " err.code=" + err?.code)
|
|
|
|
|
}).catch((err: BusinessError) => {
|
|
|
|
|
throw new Error('LoadDataShareFileClient fs.open err happened uri=' + request.src + " err.msg=" +
|
|
|
|
|
err?.message + " err.code=" + err?.code)
|
|
|
|
|
})
|
|
|
|
|
} else { //从本地文件获取
|
|
|
|
|
try {
|
|
|
|
@ -350,15 +363,16 @@ export class ImageKnifeLoader {
|
|
|
|
|
}
|
|
|
|
|
} else if (typeof request.src == "number") { //从资源文件获取
|
|
|
|
|
let manager = request.context.createModuleContext(request.moduleName).resourceManager
|
|
|
|
|
if (resBuf == undefined && request.onlyRetrieveFromCache != true && request.requestSource == ImageKnifeRequestSource.SRC) {
|
|
|
|
|
if(request.src == -1) {
|
|
|
|
|
if (resBuf == undefined && request.onlyRetrieveFromCache != true &&
|
|
|
|
|
request.requestSource == ImageKnifeRequestSource.SRC) {
|
|
|
|
|
if (request.src == -1) {
|
|
|
|
|
let resName = request.resName as string
|
|
|
|
|
resBuf = (await manager.getMediaByName(resName.substring(10))).buffer as ArrayBuffer
|
|
|
|
|
} else {
|
|
|
|
|
resBuf = manager.getMediaContentSync(request.src).buffer as ArrayBuffer
|
|
|
|
|
}
|
|
|
|
|
} else if (resBuf == undefined && request.requestSource != ImageKnifeRequestSource.SRC) {
|
|
|
|
|
if(request.src == -1) {
|
|
|
|
|
if (request.src == -1) {
|
|
|
|
|
let resName = request.resName as string
|
|
|
|
|
resBuf = (await manager.getMediaByName(resName.substring(10))).buffer as ArrayBuffer
|
|
|
|
|
} else {
|
|
|
|
@ -368,29 +382,34 @@ export class ImageKnifeLoader {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (resBuf === undefined){
|
|
|
|
|
if (resBuf === undefined) {
|
|
|
|
|
throw new Error('getImageArrayBuffer undefined')
|
|
|
|
|
}
|
|
|
|
|
return resBuf
|
|
|
|
|
}
|
|
|
|
|
static async downsamplerReqSize(typeValue:string,request:RequestJobRequest ,size:Size,SRC?:ImageKnifeRequestSource): Promise<image.DecodingOptions>{
|
|
|
|
|
let reqSize = new Downsampler().calculateScaling(typeValue, size.width, size.height, request.targetWidth, request.targetHeight, request.downsampType)
|
|
|
|
|
if(typeValue=="svg") {
|
|
|
|
|
|
|
|
|
|
static downsamplerReqSize(typeValue: string, request: RequestJobRequest, size: Size,
|
|
|
|
|
SRC?: ImageKnifeRequestSource) {
|
|
|
|
|
let reqSize =
|
|
|
|
|
new Downsampler().calculateScaling(typeValue, size.width, size.height, request.targetWidth, request.targetHeight,
|
|
|
|
|
request.downsampType)
|
|
|
|
|
if (typeValue == "svg") {
|
|
|
|
|
return ({
|
|
|
|
|
editable: true,
|
|
|
|
|
desiredSize: ({
|
|
|
|
|
height: vp2px(reqSize.targetHeight),
|
|
|
|
|
width: vp2px(reqSize.targetWidth)
|
|
|
|
|
} as Size)
|
|
|
|
|
} as image.DecodingOptions )
|
|
|
|
|
}else {
|
|
|
|
|
return( {
|
|
|
|
|
editable: request.requestSource ===SRC && request.transformation !== undefined ? true : false,
|
|
|
|
|
|
|
|
|
|
} as image.DecodingOptions)
|
|
|
|
|
} else {
|
|
|
|
|
return ({
|
|
|
|
|
editable: request.requestSource === SRC && request.transformation !== undefined ? true : false,
|
|
|
|
|
desiredSize: ({
|
|
|
|
|
width: reqSize.targetWidth,
|
|
|
|
|
height: reqSize.targetHeight
|
|
|
|
|
}as Size)
|
|
|
|
|
}as image.DecodingOptions)
|
|
|
|
|
} as Size)
|
|
|
|
|
} as image.DecodingOptions)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|