去除typevalue 的null
Signed-off-by: tsm <tianshuangming@h-partners.com>
This commit is contained in:
parent
066c370b20
commit
f80dd101d0
|
@ -13,7 +13,7 @@
|
|||
* limitations under the License.
|
||||
*/
|
||||
import { DownsampleStrategy, ImageKnifeOption, } from '@ohos/imageknife';
|
||||
import { ImageKnifeComponent, BlurTransformation, } from '@ohos/libraryimageknife';
|
||||
import { ImageKnifeComponent } from '@ohos/libraryimageknife';
|
||||
import { image } from '@kit.ImageKit';
|
||||
import { BusinessError } from '@kit.BasicServicesKit';
|
||||
import { Downsampler } from '@ohos/imageknife/src/main/ets/downsampling/Downsampler';
|
||||
|
@ -73,8 +73,7 @@ struct DownSamplePage {
|
|||
})
|
||||
this.originalPixMap($r('app.media.pngSample'))
|
||||
this.afterSamplingFunc($r('app.media.pngSample'))
|
||||
}
|
||||
else if (value ==='FIT_CENTER_QUALITY') {
|
||||
} else if (value === 'FIT_CENTER_QUALITY') {
|
||||
this.imageKnifeOption = new ImageKnifeOption({
|
||||
loadSrc: $r('app.media.pngSample'),
|
||||
placeholderSrc: $r("app.media.loading"),
|
||||
|
@ -84,8 +83,7 @@ struct DownSamplePage {
|
|||
})
|
||||
this.originalPixMap($r('app.media.pngSample'))
|
||||
this.afterSamplingFunc($r('app.media.pngSample'))
|
||||
}
|
||||
else if (value === 'CENTER_INSIDE_MEMORY') {
|
||||
} else if (value === 'CENTER_INSIDE_MEMORY') {
|
||||
this.imageKnifeOption = new ImageKnifeOption({
|
||||
loadSrc: $r('app.media.pngSample'),
|
||||
placeholderSrc: $r("app.media.loading"),
|
||||
|
@ -105,8 +103,7 @@ struct DownSamplePage {
|
|||
})
|
||||
this.originalPixMap($r('app.media.pngSample'))
|
||||
this.afterSamplingFunc($r('app.media.pngSample'))
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
this.imageKnifeOption = new ImageKnifeOption({
|
||||
loadSrc: $r('app.media.pngSample'),
|
||||
placeholderSrc: $r("app.media.loading"),
|
||||
|
@ -123,7 +120,7 @@ struct DownSamplePage {
|
|||
let img: Uint8Array = await getContext(this).resourceManager.getMediaContent(imgs);
|
||||
let imageSource: image.ImageSource = image.createImageSource(img.buffer.slice(0));
|
||||
let fileTypeUtil = new FileTypeUtil();
|
||||
let typeValue = fileTypeUtil.getFileType(img.buffer.slice(0));
|
||||
let typeValue = fileTypeUtil.getFileType(img.buffer.slice(0)) as string;
|
||||
let decodingOptions: image.DecodingOptions = {
|
||||
editable: true,
|
||||
desiredPixelFormat: 3,
|
||||
|
|
|
@ -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';
|
||||
|
@ -64,10 +66,12 @@ 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) {
|
||||
|
@ -78,7 +82,7 @@ export class ImageKnifeLoader {
|
|||
try {
|
||||
if ((request.downsampType !== DownsampleStrategy.NONE) &&
|
||||
request.requestSource == ImageKnifeRequestSource.SRC) {
|
||||
decodingOptions =await ImageKnifeLoader.downsamplerReqSize(typeValue,request,size,ImageKnifeRequestSource.SRC)
|
||||
decodingOptions = ImageKnifeLoader.downsamplerReqSize(typeValue, request, size, ImageKnifeRequestSource.SRC)
|
||||
}
|
||||
} catch (err) {
|
||||
return ImageKnifeLoader.makeEmptyResult("image.createImageSource failed")
|
||||
|
@ -101,6 +105,7 @@ export class ImageKnifeLoader {
|
|||
type: typeValue
|
||||
};
|
||||
}
|
||||
|
||||
static async parseSvgImage(resBuf: ArrayBuffer, typeValue: string, fileKey: string,
|
||||
request: RequestJobRequest): Promise<RequestJobResult> {
|
||||
let resPixelmap: PixelMap | undefined = undefined
|
||||
|
@ -124,7 +129,7 @@ export class ImageKnifeLoader {
|
|||
try {
|
||||
if ((request.downsampType !== DownsampleStrategy.NONE) &&
|
||||
request.requestSource == ImageKnifeRequestSource.SRC) {
|
||||
opts =await ImageKnifeLoader.downsamplerReqSize(typeValue,request,size)
|
||||
opts = ImageKnifeLoader.downsamplerReqSize(typeValue, request, size)
|
||||
}
|
||||
} catch (err) {
|
||||
return ImageKnifeLoader.makeEmptyResult("image.createImageSource failed")
|
||||
|
@ -145,6 +150,7 @@ export class ImageKnifeLoader {
|
|||
type: typeValue
|
||||
};
|
||||
}
|
||||
|
||||
static async parseAnimatorImage(resBuf: ArrayBuffer, typeValue: string,
|
||||
fileKey: string, request: RequestJobRequest): Promise<RequestJobResult> {
|
||||
let imageSource: image.ImageSource = image.createImageSource(resBuf)
|
||||
|
@ -158,7 +164,8 @@ export class ImageKnifeLoader {
|
|||
|
||||
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,
|
||||
|
@ -169,15 +176,18 @@ export class ImageKnifeLoader {
|
|||
}
|
||||
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) {
|
||||
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,11 +224,13 @@ 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)
|
||||
if (resBuf === undefined) {
|
||||
|
@ -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) {
|
||||
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
|
||||
|
@ -280,7 +290,8 @@ export class ImageKnifeLoader {
|
|||
} 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)
|
||||
}
|
||||
})
|
||||
|
@ -315,8 +326,7 @@ export class ImageKnifeLoader {
|
|||
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://')) {
|
||||
|
@ -327,13 +337,16 @@ export class ImageKnifeLoader {
|
|||
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)
|
||||
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)
|
||||
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)
|
||||
throw new Error('LoadDataShareFileClient fs.open err happened uri=' + request.src + " err.msg=" +
|
||||
err?.message + " err.code=" + err?.code)
|
||||
})
|
||||
} else { //从本地文件获取
|
||||
try {
|
||||
|
@ -350,7 +363,8 @@ 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 (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
|
||||
|
@ -373,8 +387,12 @@ export class ImageKnifeLoader {
|
|||
}
|
||||
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)
|
||||
|
||||
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,
|
||||
|
@ -382,6 +400,7 @@ export class ImageKnifeLoader {
|
|||
height: vp2px(reqSize.targetHeight),
|
||||
width: vp2px(reqSize.targetWidth)
|
||||
} as Size)
|
||||
|
||||
} as image.DecodingOptions)
|
||||
} else {
|
||||
return ({
|
||||
|
|
|
@ -30,7 +30,7 @@ export interface calculateScaleType {
|
|||
|
||||
export class Downsampler {
|
||||
calculateScaling(
|
||||
typeValue: string |null,
|
||||
typeValue: string,
|
||||
sourceWidth: number, //原始宽高
|
||||
sourceHeight: number, //原始宽高
|
||||
requestWidth: number, //请求宽高
|
||||
|
|
Loading…
Reference in New Issue