降采样

Signed-off-by: tsm <tianshuangming@h-partners.com>
This commit is contained in:
tsm
2024-09-02 11:40:36 +08:00
parent 55fcfe6021
commit f415bb7aa1
20 changed files with 792 additions and 27 deletions

View File

@@ -66,4 +66,6 @@ export { CropTransformation } from './src/main/ets/transform/CropTransformation'
export { MaskTransformation } from './src/main/ets/transform/MaskTransformation'
export { SepiaTransformation } from './src/main/ets/transform/SepiaTransformation'
export { SepiaTransformation } from './src/main/ets/transform/SepiaTransformation'
export { DownsampleStrategy } from './src/main/ets/downsampling/DownsampleStartegy'

View File

@@ -40,6 +40,8 @@ import {
} from './model/ImageKnifeData'
import { combineArrayBuffers } from './model/utils';
import { BusinessError } from '@kit.BasicServicesKit';
import { Downsampler } from './downsampling/Downsampler';
import { DownsampleStrategy } from './downsampling/DownsampleStartegy';
export class ImageKnifeDispatcher {
// 最大并发
@@ -189,7 +191,10 @@ export class ImageKnifeDispatcher {
fileCacheFolder: ImageKnife.getInstance().getFileCache()?.getCacheFolder(),
isAnimator:isAnimator,
moduleName: moduleName == "" ? undefined : moduleName,
resName: resName == "" ? undefined : resName
resName: resName == "" ? undefined : resName,
targetWidth: currentRequest.componentWidth,
targetHeight: currentRequest.componentHeight,
downsampType: currentRequest.imageKnifeOption.downsampleOf,
}
if(request.customGetImage == undefined) {
@@ -204,7 +209,7 @@ export class ImageKnifeDispatcher {
emitter.on(Constants.PROGRESS_EMITTER + memoryKey, (data) => {
this.progressCallBack(requestList! , data?.data?.value as number)
});
}
}
LogUtil.log("ImageKnife_DataTime_getAndShowImage_execute.start:" + currentRequest.imageKnifeOption.loadSrc)
taskpool.execute(task).then((res: Object) => {
@@ -340,8 +345,8 @@ export class ImageKnifeDispatcher {
}
} else {
if (requestWithSource.source == ImageKnifeRequestSource.SRC && requestWithSource.request.imageKnifeOption.onLoadListener?.onLoadCancel) {
// 回调请求成功
requestWithSource.request.imageKnifeOption.onLoadListener.onLoadCancel("component has destroyed")
// 回调请求成功
requestWithSource.request.imageKnifeOption.onLoadListener.onLoadCancel("component has destroyed")
}
}
});
@@ -484,7 +489,7 @@ async function requestJob(request: RequestJobRequest, requestList?: List<ImageKn
if (data == 200 || data == 206 || data == 204) {
resBuf = combineArrayBuffers(arrayBuffers)
} else {
loadError = "HttpDownloadClient has error, http code =" + JSON.stringify(data)
loadError = "HttpDownloadClient has error, http code =" + JSON.stringify(data)
}
}).catch((err: Error) => {
loadError = err.message;
@@ -505,21 +510,21 @@ async function requestJob(request: RequestJobRequest, requestList?: List<ImageKn
loadError = "success get image from filecache for key = " + fileKey;
}
} 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) =>{
let buf = new ArrayBuffer(stat.size);
await fs.read(file.fd, buf).then((readLen) => {
resBuf = buf;
fs.close(file.fd);
}).catch((err:BusinessError) => {
loadError = 'LoadDataShareFileClient fs.read err happened uri=' + request.src + " err.msg=" + err?.message + " err.code=" + err?.code;
})
await fs.open(request.src, fs.OpenMode.READ_ONLY).then(async (file) => {
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.close(file.fd);
}).catch((err:BusinessError) => {
loadError = 'LoadDataShareFileClient fs.stat err happened uri=' + request.src + " err.msg=" + err?.message + " err.code=" + err?.code;
loadError = 'LoadDataShareFileClient fs.read err happened uri=' + request.src + " err.msg=" + err?.message + " err.code=" + err?.code;
})
}).catch((err:BusinessError) => {
loadError ='LoadDataShareFileClient fs.open err happened uri=' + request.src + " err.msg=" + err?.message + " err.code=" + err?.code;
loadError = 'LoadDataShareFileClient fs.stat err happened uri=' + request.src + " err.msg=" + err?.message + " err.code=" + err?.code;
})
}).catch((err:BusinessError) => {
loadError ='LoadDataShareFileClient fs.open err happened uri=' + request.src + " err.msg=" + err?.message + " err.code=" + err?.code;
})
} else { //从本地文件获取
try {
let stat = fs.statSync(request.src);
@@ -651,6 +656,20 @@ async function requestJob(request: RequestJobRequest, requestList?: List<ImageKn
editable: true,
desiredSize: defaultSize
};
let imageInfo = await imageSource.getImageInfo()
if ((request.downsampType !== DownsampleStrategy.NONE && request.downsampType !== undefined) &&
request.requestSource == ImageKnifeRequestSource.SRC) {
let reqSize =
new Downsampler().calculateScaling(typeValue, imageInfo.size.width, imageInfo.size.height, request.targetWidth,
request.targetHeight, request.downsampType)
opts = {
editable: true,
desiredSize: {
height: vp2px(reqSize.targetHeight),
width: vp2px(reqSize.targetWidth)
}
}
}
await imageSource.createPixelMap(opts)
.then((pixelmap: PixelMap) => {
resPixelmap = pixelmap
@@ -663,6 +682,22 @@ async function requestJob(request: RequestJobRequest, requestList?: List<ImageKn
type:typeValue
};
}
let imageInfo = await imageSource.getImageInfo()
if ((request.downsampType !== undefined && request.downsampType !== DownsampleStrategy.NONE) &&
request.requestSource == ImageKnifeRequestSource.SRC) {
let reqSize =
new Downsampler().calculateScaling(typeValue, imageInfo.size.width, imageInfo.size.height, request.targetWidth,
request.targetHeight, request.downsampType)
decodingOptions = {
editable: true,
desiredSize: {
width: reqSize.targetWidth,
height: reqSize.targetHeight
}
}
}
let size = (await imageSource.getImageInfo()).size
await imageSource.createPixelMap(decodingOptions)
.then((pixelmap: PixelMap) => {

View File

@@ -16,6 +16,7 @@ import taskpool from '@ohos.taskpool';
import common from '@ohos.app.ability.common'
import { CacheStrategy, ImageKnifeData,EventImage } from './model/ImageKnifeData';
import { PixelMapTransformation } from './transform/PixelMapTransformation';
import { DownsampleStrategy } from './downsampling/DownsampleStartegy';
import { drawing } from '@kit.ArkGraphics2D';
export interface HeaderOptions {
@@ -90,6 +91,8 @@ interface ImageOption {
onLoadListener?: OnLoadCallBack | undefined;
onComplete?:(event:EventImage | undefined) => void
drawingColorFilter?: ColorFilter | drawing.ColorFilter
downsampleOf?: DownsampleStrategy
}
@ObservedV2
export class ImageKnifeOption {
@@ -121,6 +124,9 @@ export class ImageKnifeOption {
onLoadListener?: OnLoadCallBack | undefined;
onComplete?:(event:EventImage | undefined) => void
drawingColorFilter?: ColorFilter | drawing.ColorFilter
// 下采样
downsampleOf?: DownsampleStrategy = DownsampleStrategy.NONE
constructor(option?:ImageOption) {
this.loadSrc = option?.loadSrc == undefined ? "" : option?.loadSrc
this.placeholderSrc = option?.placeholderSrc
@@ -141,6 +147,7 @@ export class ImageKnifeOption {
this.onLoadListener = option?.onLoadListener
this.onComplete = option?.onComplete
this.drawingColorFilter = option?.drawingColorFilter
this.downsampleOf = option?.downsampleOf
}
}

View File

@@ -15,7 +15,7 @@
import { ImageKnifeOption } from './ImageKnifeOption';
import common from '@ohos.app.ability.common';
import { ImageKnifeRequestSource } from './model/ImageKnifeData';
import { DownsampleStrategy } from './downsampling/DownsampleStartegy';
export class ImageKnifeRequest {
requestState: ImageKnifeRequestState = ImageKnifeRequestState.PROGRESS
@@ -27,18 +27,22 @@ export class ImageKnifeRequest {
ImageKnifeRequestCallback: ImageKnifeRequestCallback
componentVersion: number = 0
headers: Map<string,Object> = new Map<string,Object>()
downsampType?: DownsampleStrategy
constructor(option: ImageKnifeOption,
uIAbilityContext: common.UIAbilityContext,
width: number,
height: number,
version: number,
ImageKnifeRequestCallback: ImageKnifeRequestCallback) {
uIAbilityContext: common.UIAbilityContext,
width: number,
height: number,
version: number,
ImageKnifeRequestCallback: ImageKnifeRequestCallback,
downsampType?: DownsampleStrategy,
) {
this.imageKnifeOption = option
this.context = uIAbilityContext
this.componentWidth = width
this.componentHeight = height
this.componentVersion = version
this.ImageKnifeRequestCallback = ImageKnifeRequestCallback
this.downsampType = downsampType
}
// RequestOption调用header对于的方法
addHeader(key: string, value: Object) {

View File

@@ -0,0 +1,26 @@
/*
* Copyright (C) 2024 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { DownsampleStrategy } from './DownsampleStartegy';
import { SampleSizeRounding } from './DownsampleUtils';
export interface BaseDownsampling {
getName(): string
getScaleFactor(sourceWidth: number, sourceHeight: number, requestWidth: number, requestHeight: number,downsampType?:DownsampleStrategy): number
getSampleSizeRounding(sourceWidth: number, sourceHeight: number, requestWidth: number,
requestHeight: number,downsampType?:DownsampleStrategy): SampleSizeRounding
}

View File

@@ -0,0 +1,184 @@
/*
* Copyright (C) 2024 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { BaseDownsampling } from './BaseDownsampling';
import { highestOneBit, SampleSizeRounding } from './DownsampleUtils';
export class FitCenter implements BaseDownsampling {
getName() {
return "FitCenter"
}
getScaleFactor(sourceWidth: number, sourceHeight: number, requestedWidth: number, requestedHeight: number,downsampType:DownsampleStrategy
): number {
if (downsampType===DownsampleStrategy.FIT_CENTER_QUALITY) {
const widthPercentage = requestedWidth / sourceWidth
const heightPercentage = requestedHeight / sourceHeight
return Math.min(widthPercentage, heightPercentage)
} else {
const maxIntegerFactor = Math.max(sourceHeight / requestedHeight, sourceWidth / requestedWidth);
const a = maxIntegerFactor === 0 ? 1 : 1 / highestOneBit(maxIntegerFactor)
return a;
}
}
//实现 getSampleSizeRounding 方法
getSampleSizeRounding(sourceWidth: number, sourceHeight: number, requestedWidth: number, requestedHeight: number,downsampType:DownsampleStrategy
): number {
if (downsampType===DownsampleStrategy.FIT_CENTER_QUALITY) {
return SampleSizeRounding.QUALITY;
} else {
return SampleSizeRounding.MEMORY;
}
}
}
export class None implements BaseDownsampling {
getName(): string {
return "DownsampleNone"
}
public getScaleFactor(sourceWidth: number, sourceHeight: number, requestedWidth: number,
requestedHeight: number): number {
//不进行任何下采样,缩放因子为 1
return 1;
}
//实现 getSampleSizeRounding 方法
public getSampleSizeRounding(sourceWidth: number, sourceHeight: number, requestedWidth: number,
requestedHeight: number): SampleSizeRounding {
return SampleSizeRounding.QUALITY
}
}
/*宽高进行等比缩放宽高里面最小的比例先放进去
然后再更据原图的缩放比去适配另一边*/
export class CenterOutside implements BaseDownsampling {
getName() {
return "CenterOutside"
}
getScaleFactor(sourceWidth: number, sourceHeight: number, requestedWidth: number, requestedHeight: number): number {
const widthPercentage = requestedWidth / sourceWidth;
const heightPercentage = requestedHeight / sourceHeight;
//返回宽度和高度比例中最大的值
return Math.max(widthPercentage, heightPercentage);
}
getSampleSizeRounding(sourceWidth: number, sourceHeight: number, requestedWidth: number,
requestedHeight: number): SampleSizeRounding {
//根据 CenterOutside 的逻辑,总是返回 QUALITY
return SampleSizeRounding.QUALITY;
}
}
/*请求尺寸大于实际尺寸不进行放大,按照原图展示*/
export class AtMost implements BaseDownsampling {
getName() {
return "AtMost"
}
//实现 getScaleFactor 方法
getScaleFactor(sourceWidth: number, sourceHeight: number, requestedWidth: number, requestedHeight: number): number {
const maxIntegerFactor = Math.ceil(Math.max(sourceHeight / requestedHeight, sourceWidth / requestedWidth));
let lesserOrEqualSampleSize = Math.max(1, highestOneBit(maxIntegerFactor));
let greaterOrEqualSampleSize = lesserOrEqualSampleSize
if (lesserOrEqualSampleSize < maxIntegerFactor) {
greaterOrEqualSampleSize = lesserOrEqualSampleSize <<= 1;
}
greaterOrEqualSampleSize = lesserOrEqualSampleSize << (lesserOrEqualSampleSize < maxIntegerFactor ? 1 : 0)
//返回缩放因子
return 1 / greaterOrEqualSampleSize
}
getSampleSizeRounding(sourceWidth: number, sourceHeight: number, requestedWidth: number,
requestHeight: number): SampleSizeRounding {
//根据 AtMost 的逻辑,总是返回 MEMORY
return SampleSizeRounding.MEMORY
}
}
/*宽高进行等比缩放宽高里面最大的比例先放进去
然后再更据原图的缩放比去适配另一边*/
export class CenterInside implements BaseDownsampling {
getName() {
return "CenterInside"
}
getScaleFactor(sourceWidth: number, sourceHeight: number, requestedWidth: number, requestedHeight: number,downsampType:DownsampleStrategy
): number {
//获取 FIT_CENTER 的缩放因子
const fitCenterScaleFactor: ESObject =
this.getScale(sourceWidth, sourceHeight, requestedWidth, requestedHeight, downsampType);
//返回不超过 1 的缩放因子,即尽量缩小图像以适应目标尺寸,但不会放大
return Math.min(1, fitCenterScaleFactor);
}
getScale(sourceWidth: number, sourceHeight: number, requestedWidth: number, requestedHeight: number,downsampType:DownsampleStrategy
): number {
if (downsampType===DownsampleStrategy.FIT_CENTER_QUALITY) {
const widthPercentage = requestedWidth / sourceWidth
const heightPercentage = requestedHeight / sourceHeight
return Math.min(widthPercentage, heightPercentage)
} else {
//类似 AT_LEAST,但只要求一个维度或另一个维度大于等于请求的尺寸
const maxIntegerFactor = Math.max(sourceHeight / requestedHeight, sourceWidth / requestedWidth);
return maxIntegerFactor === 0 ? 1 : 1 / highestOneBit(maxIntegerFactor);
}
}
getSampleSizeRounding(sourceWidth: number, sourceHeight: number, requestedWidth: number, requestedHeight: number,downsampType:DownsampleStrategy
): SampleSizeRounding {
//如果缩放因子为 1表示没有缩放优先选择质量
if (this.getScaleFactor(sourceWidth, sourceHeight, requestedWidth, requestedHeight, downsampType) === 1) {
return SampleSizeRounding.QUALITY
}
//否则,使用 FIL_CENTER 的 SampleSizeRounding 值
return this.getSampleSize(sourceWidth, sourceHeight, requestedWidth, requestedHeight,downsampType);
}
getSampleSize(sourceWidth: number, sourceHeight: number, requestedWidth: number, requestedHeight: number,downsampType:DownsampleStrategy
): SampleSizeRounding {
if (downsampType===DownsampleStrategy.CENTER_INSIDE_QUALITY) {
return SampleSizeRounding.QUALITY;
} else {
return SampleSizeRounding.MEMORY;
}
}
}
export enum DownsampleStrategy {
//请求尺寸大于实际尺寸不进行放大
AT_MOST,
//两边自适应内存优先
FIT_CENTER_MEMORY ,
//两边自适应质量优先
FIT_CENTER_QUALITY,
//按照宽高比的最大比进行适配内存优先
CENTER_INSIDE_MEMORY ,
//按照宽高比的最大比进行适配质量优先
CENTER_INSIDE_QUALITY,
//宽高进行等比缩放宽高里面最小的比例先放进去,然后再根据原图的缩放比去适配
CENTER_OUTSIDE,
//不进行降采样
NONE,
}

View File

@@ -0,0 +1,36 @@
/*
* Copyright (C) 2024 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the 'License');
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an 'AS IS' BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
export enum SampleSizeRounding {
/**
* Prefer to round the sample size up so that the image is downsampled to smaller than the
* requested size to use less memory.
*/
//(内存优先)
MEMORY,
/**
* Prefer to round the sample size down so that the image is downsampled to larger than the
* requested size to maintain quality at the expense of extra memory usage.
*/
//(质量优先)
QUALITY
}
export function highestOneBit(i: number): number {
i |= (i >> 1);
i |= (i >> 2);
i |= (i >> 4);
i |= (i >> 8);
i |= (i >> 16);
return i - (i >>> 1);
}

View File

@@ -0,0 +1,147 @@
/*
* Copyright (C) 2024 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the 'License');
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an 'AS IS' BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import {
AtMost,
CenterInside,
CenterOutside,
DownsampleStrategy,
FitCenter,
None,
} from './DownsampleStartegy';
import { highestOneBit, SampleSizeRounding } from './DownsampleUtils';
export interface calculateScaleType {
targetWidth: number,
targetHeight: number
}
export class Downsampler {
calculateScaling(
typeValue: string | null,
sourceHeight: number, //原始宽高
sourceWidth: number | undefined, //原始宽高
requestHeight: number, //请求宽高
requestWidth: number, //请求宽高
downsampType: DownsampleStrategy | undefined,
): calculateScaleType {
let degreesToRotate: ESObject = 90;
const fileType = typeValue //获取图片类型
let powerOfTwoWidth: number | null = null;
let powerOfTwoHeight: number | null = null;
let targetWidth: number = 0
let targetHeight: number = 0
if (sourceHeight <= 0 || sourceWidth == undefined || sourceWidth == null || sourceWidth <= 0) {
throw new Error("Cannot found width or height");
}
let orientedSourceWidth = sourceWidth;
let orientedSourceHeight = sourceHeight;
if (this.isRotationRequired(degreesToRotate)) {
orientedSourceWidth = sourceHeight;
orientedSourceHeight = sourceWidth;
}
if (requestWidth && !requestHeight) {
targetWidth = this.round((requestHeight) * orientedSourceWidth / orientedSourceHeight)
} else if (requestHeight && !requestWidth) {
targetHeight = this.round((requestWidth) * orientedSourceHeight / orientedSourceWidth)
} else if (requestHeight && requestWidth) {
targetWidth =
requestHeight == sourceWidth ? (this.isRotationRequired(degreesToRotate) ? sourceHeight : sourceWidth) :
requestWidth;
targetHeight =
requestHeight == sourceHeight ? (this.isRotationRequired(degreesToRotate) ? sourceWidth : sourceHeight) :
requestWidth;
} else {
throw new Error("Cannot found width or height");
}
let exactScaleFactor: number | undefined
let rounding: SampleSizeRounding | undefined
switch (downsampType) {
case DownsampleStrategy.FIT_CENTER_MEMORY || DownsampleStrategy.FIT_CENTER_QUALITY:
exactScaleFactor = new FitCenter()
.getScaleFactor(orientedSourceWidth, orientedSourceHeight, targetWidth, targetHeight,downsampType)
rounding = new FitCenter()
.getSampleSizeRounding(orientedSourceWidth, orientedSourceHeight, targetWidth, targetHeight,downsampType)
break;
case DownsampleStrategy.NONE:
exactScaleFactor = new None()
.getScaleFactor(orientedSourceWidth, orientedSourceHeight, targetWidth, targetHeight)
rounding = new None()
.getSampleSizeRounding(orientedSourceWidth, orientedSourceHeight, targetWidth, targetHeight)
break
case DownsampleStrategy.AT_MOST:
exactScaleFactor = new AtMost()
.getScaleFactor(orientedSourceWidth, orientedSourceHeight, targetWidth, targetHeight)
rounding = new AtMost()
.getSampleSizeRounding(orientedSourceWidth, orientedSourceHeight, targetWidth, targetHeight)
break
case DownsampleStrategy.CENTER_INSIDE_MEMORY || DownsampleStrategy.CENTER_INSIDE_QUALITY:
exactScaleFactor = new CenterInside()
.getScaleFactor(orientedSourceWidth, orientedSourceHeight, targetWidth, targetHeight,downsampType)
rounding = new CenterInside()
.getSampleSizeRounding(orientedSourceWidth, orientedSourceHeight, targetWidth, targetHeight,downsampType)
break
case DownsampleStrategy.CENTER_OUTSIDE:
exactScaleFactor = new CenterOutside()
.getScaleFactor(orientedSourceWidth, orientedSourceHeight, targetWidth, targetHeight)
rounding = new CenterOutside()
.getSampleSizeRounding(orientedSourceWidth, orientedSourceHeight, targetWidth, targetHeight)
break
}
if (exactScaleFactor==undefined|| exactScaleFactor <= 0 ) {
throw new Error("Cannot round with exactScaleFactor");
}
if (rounding == undefined) {
throw new Error("Cannot round with null rounding");
}
let outWidth: number = this.round(exactScaleFactor * orientedSourceWidth);
let outHeight: number = this.round(exactScaleFactor * orientedSourceHeight);
let widthScaleFactor = orientedSourceWidth / outWidth;
let heightScaleFactor = orientedSourceHeight / outHeight;
let scaleFactor = rounding ==SampleSizeRounding.QUALITY ? Math.max(widthScaleFactor, heightScaleFactor) :
Math.min(widthScaleFactor, heightScaleFactor) //将整型的缩放因子转换为2的次幂采样大小
let powerOfTwoSampleSize: number = scaleFactor;
powerOfTwoSampleSize = Math.max(1, highestOneBit(scaleFactor))
if (rounding == 1 && (powerOfTwoSampleSize < (1 / exactScaleFactor))) {
powerOfTwoSampleSize = powerOfTwoSampleSize << 1;
}
//基于上一步得出的采样大小,根据不同的图片类型,计算采样后的图片尺寸
if (fileType === "png") {
powerOfTwoWidth = Math.floor(orientedSourceWidth / powerOfTwoSampleSize);
powerOfTwoHeight = Math.floor(orientedSourceHeight / powerOfTwoSampleSize);
} else if (fileType === "webp") {
powerOfTwoWidth = Math.round(orientedSourceWidth / powerOfTwoSampleSize);
powerOfTwoHeight = Math.round(orientedSourceHeight / powerOfTwoSampleSize);
} else {
powerOfTwoWidth = orientedSourceWidth / powerOfTwoSampleSize;
powerOfTwoHeight = orientedSourceHeight / powerOfTwoSampleSize;
}
let a: calculateScaleType = {
"targetWidth": powerOfTwoWidth,
"targetHeight": powerOfTwoHeight
}
return a
}
round(value: number): number {
return Math.floor(value + 0.5);
}
isRotationRequired(degreesToRotate: number): boolean {
return degreesToRotate == 90 || degreesToRotate == 270;
}
}

View File

@@ -17,6 +17,7 @@ import { ImageKnifeOption } from '../ImageKnifeOption';
import { IEngineKey } from './IEngineKey';
import { PixelMapTransformation } from '../transform/PixelMapTransformation';
import { ImageKnifeRequestSource } from '../model/ImageKnifeData';
import { DownsampleStrategy } from '../downsampling/DownsampleStartegy';
@Sendable
export class DefaultEngineKey implements IEngineKey {
@@ -31,6 +32,9 @@ export class DefaultEngineKey implements IEngineKey {
if (imageKnifeOption.transformation) {
key += "transformation=" + this.getTransformation(imageKnifeOption.transformation) + ";"
}
if ((imageKnifeOption.downsampleOf !== DownsampleStrategy.NONE && imageKnifeOption.downsampleOf !== undefined)) {
key += "downsampleOf" + imageKnifeOption.downsampleOf +"width="+width+"height="+ height
}
}
return key
}

View File

@@ -18,7 +18,7 @@ import { IEngineKey } from '../key/IEngineKey'
import { PixelMapTransformation } from '../transform/PixelMapTransformation'
import common from '@ohos.app.ability.common';
import { Size } from '@kit.ArkUI'
import { DownsampleStrategy } from '../downsampling/DownsampleStartegy'
export interface ImageKnifeData {
source: PixelMap | string,
imageWidth: number,
@@ -103,6 +103,9 @@ export interface RequestJobRequest {
fileCacheFolder: string,
isAnimator?: boolean,
moduleName?:string,
resName?: string
resName?: string,
targetWidth: number
targetHeight: number
downsampType?: DownsampleStrategy
}