From 22f79c3588f8349d1d700528634b1076a600902e Mon Sep 17 00:00:00 2001 From: sijainguo <547537959@qq.com> Date: Fri, 26 Apr 2024 22:09:24 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8F=90=E4=BA=A4=20Downsampler=20=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../imageknife/Downsampling/Downsampler.ets | 138 +++++++++++++++++- 1 file changed, 137 insertions(+), 1 deletion(-) diff --git a/library/src/main/ets/components/imageknife/Downsampling/Downsampler.ets b/library/src/main/ets/components/imageknife/Downsampling/Downsampler.ets index 317587f..568ab4a 100644 --- a/library/src/main/ets/components/imageknife/Downsampling/Downsampler.ets +++ b/library/src/main/ets/components/imageknife/Downsampling/Downsampler.ets @@ -1,2 +1,138 @@ /* - * asdfasdfasdfasdfasdf*/ \ No newline at end of file + * asdfasdfasdfasdfasdf*/ +import { CenterOutside, FitCenter, SampleSizeRounding } from './Downsamplestrategy'; +import { FileTypeutil } from '../../utits/Fitetypeutit'; +import { Request0ption } from '../../Requestoption'; +let TAG = 'downsampling' +export class Downsampler { + calculateScaling( + imageType: ArrayBuffer, + sourceHeight:number, + sourceWidth:number, + request?: RequestOption + ):ESObject { + const fileType: string | null = new FileTypeUtil().getFileType(imageType) + let powerOfTwoWidth: number | null = null; + let powerOfTwoHeight: number | null = null; + let targetWidth: number = 0 + let targetHeight: number = 0 + if (request?.size.width && !request?.size.height) { + targetWidth = this.round((request?.size.height) * sourceWidth / sourceHeight) + } else if (request?.size.height && !request?.size.width) { + targetHeight = this.round((request?.size.width) * sourceHeight / sourceWidth) + } else if (request?.size.height && request?.size.width) { + targetHeight = request.size.height; + targetWidth = request?.size.width; + } else { + console.log('宽高都不存在') + } + if (sourceWidth <= 0 || sourceleight <= 0) return; + let orientedSourceWidth = sourceWidth; + let orientedSourceHeight = sourceHeight; + if (this.isRotationRequired(90)) { + orientedSourceWidth = sourceHeight; + orientedSourceHeight = sourceWidth; + } + /*安卓的模式*/ + let exactScaleFactor: number = FitCenter.getScaleFactor(orientedSourceWidth, orientedSourceHeight, targetWidth, targetHeight) + if (exactScaleFactor <= 0) { + throw new Error("Cannot round with exactScaleFactor"); + console.log('exactScaleFactor', exactScaleFactor) + /*安卓的模式*/ + let rounding: SampleSizeRounding = Center0utside.getSampleSizeRounding(orientedSourceWidth, orientedSourceHeight, targetWidth, targetHeight) + if (rounding == null) { + throw new Error("Cannot round with null rounding"); + } + let outWidth: number = this.round(exactScaleFactor * orientedSourceWidth); + 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 powerOfTwoSampleSize: number; + powerOfTwoSampleSize = Math.max(1, this.highestOneBit(scaleFactor)); + if (fileType == "JPEG") { + let nativeScaling = Hath.min(powerOfTwoSampleSize, 8); + powerOfTwoWidth = Math.ceil(orientedSourceWidth / nativeScaling); + powerOfTwoHeight = Math.ceil(orientedSourceHeight / nativeScaling); + let secondaryScaling = Math.floor(powerOfTwoSampleSize / 8); + if (secondaryScaling > 0) { + powerOfTwoWidth = powerOfTwoWidth / secondaryScaling; + powerOfTwoHeight = powerOfTwoHeight / secondaryScaling; + } + } 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) { + + + // 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": power0fTwoWidth, "targetHeight": powerOfTuoHeight } + 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); + // + // } + + highest0neBit(i: number): number{ + i |= (i >> 1); + i |= (i >> 2); + i |= (i >> 4); + i |= (i >> 8); + i |= (i >> 16); + return i - (i >>> 1); + } + round(value: number): number { + return Math.floor(value +0.5); + } + isRotationRequired(degreesToRotate: number): boolean{ + return degreesToRotate == 90 || degreesToRotate == 270; + // isScaling(options: ESObject): boolean { + // return options.inTargetDensity >0 + // && options.inDensity>0 + // && options.inTargetDensity != options.inDensity; + //} + getDensityMultiplier(adjustedScaleFactor: number):number{ + return Math.round(Number.MAX_VALUE * (adjustedScaleFactor <= 1 ? adjustedScaleFactor : 1 / adjustedScaleFactor)); + } + adjustTargetDensityForError(adjustedScaleFactor:number):number{ + let densityMultiplier = this.getDensityMultiplier(adjustedScaleFactor); + let targetDensity = this.round(densityMultiplier * adjustedScaleFactor); + let scaleFactorWithError = targetDensity / densityMultiplier; + let difference = adjustedScaleFactor / scaleFactorWithError; + return this.round(difference * targetDensity); + } + } \ No newline at end of file