demo修改

This commit is contained in:
tsm 2024-04-30 11:33:03 +08:00
parent 6c8e873b80
commit 9c356112e2
2 changed files with 10 additions and 44 deletions

View File

@ -6,7 +6,7 @@ export class CenterInside{
getScaleFactor(sourceWidth:number, sourceHeight:number,requestWidth:number, requestHeight:number):number {
return Math.min(1,this.getScale(sourceWidth, sourceHeight, requestWidth, requestHeight))
}
getSampleSizeRounding(sourceWidth:number, sourceHeight:number, requestWidth:number, requestHeight:number):SampleSizeRounding {
getSampleSizeRounding(sourceWidth:number, sourceHeight:number, requestWidth:number, requestHeight:number):number{
return 1
}
getScale(sourceWidth: number, sourceHeight: number, requestWidth: number, requestHeight: number): number {
@ -80,9 +80,6 @@ export class Atleast implements BaseDownsampling{
@Sendable
export class CenterOutside implements BaseDownsampling{
static getSampleSizeRounding(orientedSourceWidth: number, orientedSourceHeight: number, targetWidth: number, targetHeight: number): SampleSizeRounding {
throw new Error('Method not implemented.')
}
getName(){
return "CenterOutside"
}

View File

@ -26,7 +26,7 @@ export class Downsampler {
targetHeight = request.size.height;
targetWidth = request?.size.width;
} else {
console.log('宽高都不存在')
throw new Error("Cannot found width or height ");
}
if (sourceWidth <= 0 || sourceHeight <= 0) return;
let orientedSourceWidth = sourceWidth;
@ -36,13 +36,14 @@ export class Downsampler {
orientedSourceHeight = sourceWidth;
}
/*安卓的模式*/
let exactScaleFactor: number =new FitCenter().getScaleFactor(orientedSourceWidth, orientedSourceHeight, targetWidth, targetHeight)
let exactScaleFactor: number =new FitCenter()
.getScaleFactor(orientedSourceWidth, orientedSourceHeight, targetWidth, targetHeight)
if (exactScaleFactor <= 0) {
throw new Error("Cannot round with exactScaleFactor");
}
console.log('exactScaleFactor', exactScaleFactor)
/*安卓的模式*/
let rounding: SampleSizeRounding =new FitCenter().getSampleSizeRounding(orientedSourceWidth, orientedSourceHeight, targetWidth, targetHeight)
let rounding: SampleSizeRounding =new FitCenter()
.getSampleSizeRounding(orientedSourceWidth, orientedSourceHeight, targetWidth, targetHeight)
if (rounding == null) {
throw new Error("Cannot round with null rounding");
}
@ -50,7 +51,9 @@ export class Downsampler {
let outHeight: number = this.round(exactScaleFactor * orientedSourceHeight);
let widthScaleFactor = Math.floor(orientedSourceWidth / outWidth);
let heightScaleFactor = Math.floor(orientedSourceHeight / outHeight);
let scaleFactor = rounding == SampleSizeRounding.MEMORY ? Math.max(widthScaleFactor, heightScaleFactor) : Math.min(widthScaleFactor, heightScaleFactor)
let scaleFactor = rounding == SampleSizeRounding.MEMORY ?
Math.max(widthScaleFactor, heightScaleFactor) :
Math.min(widthScaleFactor, heightScaleFactor)
// 将整型的缩放因子转换为2的次幂采样大小
let powerOfTwoSampleSize: number = 0;
powerOfTwoSampleSize = Math.max(1,this.highestOneBit(scaleFactor))
@ -70,52 +73,18 @@ export class Downsampler {
} else if (fileType == "PNG") {
powerOfTwoWidth = Math.floor(orientedSourceWidth / powerOfTwoSampleSize);
powerOfTwoHeight = Math.floor(orientedSourceHeight / powerOfTwoSampleSize);
console.log('执行了没', powerOfTwoHeight, powerOfTwoWidth)
} else if (fileType == "WEBP") {
powerOfTwoWidth = Math.round(orientedSourceWidth / powerOfTwoSampleSize);
powerOfTwoHeight = Math.round(orientedSourceHeight / powerOfTwoSampleSize);
} else if (
orientedSourceWidth % powerOfTwoSampleSize != 0 || orientedSourceHeight % powerOfTwoSampleSize != 0) {
} else if (orientedSourceWidth % powerOfTwoSampleSize != 0 || orientedSourceHeight % powerOfTwoSampleSize != 0) {
// let dimensions; number[] = this.getDimensions(imageReader, options, decodeCallbacks,bitmapPool);
// powerOfTwoWidth = dimensions[0];
// powerofTwoHeight = dimensions[1];
} else {
powerOfTwoWidth = orientedSourceWidth / powerOfTwoSampleSize;
powerOfTwoHeight = orientedSourceHeight / powerOfTwoSampleSize;
}
// Let adjustedScaleFactor = downsampleStrategy.getScaleFactor(powerOfTwoWidth, powerOfTwoHeight, targetWidth, targetHeight);
// Density scaling is only supported if inBitmap is null prior to KitKat. Avoid setting
// densities here so we calculate the final Bitmap size correctly.
// if (Build.VERSION,SDK_INT >=Build.VERSION_CODES.KITKAT) {
// options.inTargetDensity = this.adjustTargetDensityForError(adjustedScaleFactor);
// options,inDensity = this.getDensityMultiplier(adjustedScaleFactor);
//}
// if (this.isScaling(options)){
// options.inScaled = true;
// }else {
// options.inDensity = options.inTargetDensity =0;
// }
//}
let a: ESObject = { "targetWidth": powerOfTwoWidth, "targetHeight": powerOfTwoHeight }
return a
}
//decodeStream(imageReader:ImageReader, options: ESObject, callbacks: DecodeCallbacks, bitmapPool: ESObject) {
//if (!options.inJustDecodeBounds){
// callbacks.onObtainBounds();
// imageReader.stopGrowingBuffers();
// }
// }
// getDimensions(imageReader: ImageReader, options: ESObject, decodeCallbacks: DecodeCallbacks, bitmapPool: ESObject):number[] {
// options.inJustDecodeBounds = true;
// this.decodeStream(imageReader, options, decodeCallbacks, bitmapPool);
// options.inJustDecodeBounds =false;
// return new Array(options.outWidth, options,outHeight);
//
// }
highestOneBit(i: number): number {
i |= (i >> 1);
i |= (i >> 2);