update library/src/main/ets/downsampling/Downsampler.ets.
简化代码 Signed-off-by: 田双明 <tianshuangming@h-partners.com>
This commit is contained in:
parent
b2af4e1958
commit
fe9235d8b1
|
@ -29,53 +29,50 @@ export interface calculateScaleType {
|
|||
|
||||
export class Downsampler {
|
||||
calculateScaling(
|
||||
typeValue: string | null,
|
||||
sourceWidth: number | undefined, //原始宽高
|
||||
typeValue: string,
|
||||
sourceWidth: number, //原始宽高
|
||||
sourceHeight: number, //原始宽高
|
||||
requestWidth: number, //请求宽高
|
||||
requestHeight: number, //请求宽高
|
||||
downsampType: DownsampleStrategy | undefined,
|
||||
downsampType: DownsampleStrategy,
|
||||
): calculateScaleType {
|
||||
const fileType = typeValue //获取图片类型
|
||||
let targetWidth: number | null = null;//降采样之后的宽
|
||||
let targetHeight: number | null = null;//降采样之后的高
|
||||
if (sourceHeight <= 0 || sourceWidth == undefined || sourceWidth == null || sourceWidth <= 0) {
|
||||
throw new Error("Cannot found width or height")
|
||||
}
|
||||
if(downsampType===undefined){
|
||||
throw new Error("Cannot found downsampType");
|
||||
|
||||
if (sourceHeight <= 0 || sourceWidth <= 0) {
|
||||
throw new Error("非法宽高 sourceHeight sourceWidth")
|
||||
}
|
||||
let downsampler = this.getDownsampler(downsampType);
|
||||
let exactScaleFactor : number = downsampler.getScaleFactor(sourceWidth, sourceHeight, requestWidth, requestHeight, downsampType);
|
||||
let rounding: SampleSizeRounding= downsampler.getSampleSizeRounding(sourceWidth, sourceHeight, requestWidth, requestHeight, downsampType);//采样类型
|
||||
if (exactScaleFactor==undefined|| exactScaleFactor <= 0 ) {
|
||||
throw new Error("Cannot round with exactScaleFactor");
|
||||
let exactScaleFactor: number =
|
||||
downsampler.getScaleFactor(sourceWidth, sourceHeight, requestWidth, requestHeight, downsampType);
|
||||
let rounding: SampleSizeRounding =
|
||||
downsampler.getSampleSizeRounding(sourceWidth, sourceHeight, requestWidth, requestHeight, downsampType); //采样类型
|
||||
//原始宽高和缩放系数的乘积
|
||||
let outSize = {
|
||||
width: this.round(exactScaleFactor * sourceWidth),
|
||||
height: this.round(exactScaleFactor * sourceHeight)
|
||||
}
|
||||
let outWidth: number = this.round(exactScaleFactor * sourceWidth);//原始宽和缩放系数的乘积
|
||||
let outHeight: number = this.round(exactScaleFactor * sourceHeight);//原始高和缩放系数的乘积
|
||||
let widthScaleFactor = sourceWidth / outWidth;//计算的缩放因子
|
||||
let heightScaleFactor = sourceHeight / 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 == 0 && (powerOfTwoSampleSize < (1 / exactScaleFactor))) {
|
||||
powerOfTwoSampleSize = powerOfTwoSampleSize << 1;
|
||||
let scaleFactor =
|
||||
rounding == SampleSizeRounding.QUALITY ? Math.max(sourceWidth / outSize.width, sourceHeight / outSize.height) :
|
||||
Math.min(sourceWidth / outSize.width, sourceHeight / outSize.height) //将整型的缩放因子转换为2的次幂采样大小
|
||||
scaleFactor = Math.max(1, highestOneBit(scaleFactor))
|
||||
if (rounding == 0 && (scaleFactor < (1 / exactScaleFactor))) {
|
||||
scaleFactor = scaleFactor << 1;
|
||||
}
|
||||
//基于上一步得出的采样大小,根据不同的图片类型,计算采样后的图片尺寸
|
||||
if (fileType === "png") {
|
||||
targetWidth = Math.floor(sourceWidth / powerOfTwoSampleSize);
|
||||
targetHeight = Math.floor(sourceHeight / powerOfTwoSampleSize);
|
||||
} else if (fileType === "webp") {
|
||||
targetWidth = Math.round(sourceWidth / powerOfTwoSampleSize);
|
||||
targetHeight = Math.round(sourceHeight / powerOfTwoSampleSize);
|
||||
if (typeValue === "png") {
|
||||
return {
|
||||
targetWidth: Math.floor(sourceWidth / scaleFactor),
|
||||
targetHeight: Math.floor(sourceHeight / scaleFactor)
|
||||
}
|
||||
} else if (typeValue === "webp") {
|
||||
targetWidth = Math.round(sourceWidth / scaleFactor);
|
||||
targetHeight = Math.round(sourceHeight / scaleFactor);
|
||||
} else {
|
||||
targetWidth = sourceWidth / powerOfTwoSampleSize;
|
||||
targetHeight = sourceHeight / powerOfTwoSampleSize;
|
||||
targetWidth = sourceWidth / scaleFactor;
|
||||
targetHeight = sourceHeight / scaleFactor;
|
||||
}
|
||||
return { targetWidth, targetHeight }
|
||||
}
|
||||
getDownsampler(downsampType:DownsampleStrategy) {
|
||||
|
||||
getDownsampler(downsampType: DownsampleStrategy) {
|
||||
switch (downsampType) {
|
||||
case DownsampleStrategy.FIT_CENTER_MEMORY:
|
||||
case DownsampleStrategy.FIT_CENTER_QUALITY:
|
||||
|
@ -93,6 +90,7 @@ export class Downsampler {
|
|||
throw new Error('Unsupported downsampling strategy');
|
||||
}
|
||||
}
|
||||
|
||||
round(value: number): number {
|
||||
return Math.floor(value + 0.5);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue